bugfix: 解决success、abort、redirect拦截器跨域问题。

release-2.0.0.2
王良 2025-05-09 17:06:25 +08:00
parent 785e7be4d1
commit b582ac63ad
3 changed files with 30 additions and 6 deletions

View File

@ -5,10 +5,18 @@ module.exports = {
const { rOptions, log } = context const { rOptions, log } = context
if (interceptOpt.abort === true || interceptOpt.abort === 'true') { if (interceptOpt.abort === true || interceptOpt.abort === 'true') {
res.writeHead(403, { const headers = {
'Content-Type': 'text/plain; charset=utf-8', 'Content-Type': 'text/plain; charset=utf-8',
'DS-Interceptor': 'abort', 'DS-Interceptor': 'abort',
}) }
// headers.Access-Control-Allow-*:避免跨域问题
if (rOptions.headers.origin) {
headers['Access-Control-Allow-Credentials'] = 'true'
headers['Access-Control-Allow-Origin'] = rOptions.headers.origin
}
res.writeHead(403, headers)
res.write( res.write(
'DevSidecar 403: Request abort.\n\n' 'DevSidecar 403: Request abort.\n\n'
+ ' This request is matched by abort intercept.\n\n' + ' This request is matched by abort intercept.\n\n'

View File

@ -9,10 +9,18 @@ module.exports = {
// 获取重定向目标地址 // 获取重定向目标地址
const redirect = proxyApi.buildTargetUrl(rOptions, interceptOpt.redirect, interceptOpt, matched) const redirect = proxyApi.buildTargetUrl(rOptions, interceptOpt.redirect, interceptOpt, matched)
res.writeHead(302, { const headers = {
'Location': redirect, 'Location': redirect,
'DS-Interceptor': 'redirect', 'DS-Interceptor': 'redirect',
}) }
// headers.Access-Control-Allow-*:避免跨域问题
if (rOptions.headers.origin) {
headers['Access-Control-Allow-Credentials'] = 'true'
headers['Access-Control-Allow-Origin'] = rOptions.headers.origin
}
res.writeHead(302, headers)
res.end() res.end()
const url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${req.url}` const url = `${rOptions.method}${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${req.url}`

View File

@ -5,10 +5,18 @@ module.exports = {
const { rOptions, log } = context const { rOptions, log } = context
if (interceptOpt.success === true || interceptOpt.success === 'true') { if (interceptOpt.success === true || interceptOpt.success === 'true') {
res.writeHead(200, { const headers = {
'Content-Type': 'text/plain; charset=utf-8', 'Content-Type': 'text/plain; charset=utf-8',
'DS-Interceptor': 'success', 'DS-Interceptor': 'success',
}) }
// headers.Access-Control-Allow-*:避免跨域问题
if (rOptions.headers.origin) {
headers['Access-Control-Allow-Credentials'] = 'true'
headers['Access-Control-Allow-Origin'] = rOptions.headers.origin
}
res.writeHead(200, headers)
res.write( res.write(
'DevSidecar 200: Request success.\n\n' 'DevSidecar 200: Request success.\n\n'
+ ' This request is matched by success intercept.\n\n' + ' This request is matched by success intercept.\n\n'