feature: requestReplace和responseReplace一起完成下载请求的处理。

pull/301/head
王良 2024-04-19 14:59:16 +08:00
parent 162d9d92f3
commit 8c3f47e1c9
2 changed files with 17 additions and 3 deletions

View File

@ -35,6 +35,13 @@ module.exports = {
actions += 'headers'
}
// 替换下载文件请求的请求地址(此功能主要是为了方便拦截配置)
if (requestReplaceConfig.doDownload && rOptions.path.indexOf('?DS_DOWNLOAD') >= 0) {
rOptions.doDownload = true
rOptions.path = rOptions.path.replace('?DS_DOWNLOAD', '')
actions += (actions ? ',' : '') + 'path:remove-DS_DOWNLOAD'
}
res.setHeader('DS-RequestReplace-Interceptor', actions)
},
is (interceptOpt) {

View File

@ -57,18 +57,25 @@ module.exports = {
priority: 201,
replaceResponseHeaders,
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
const { log } = context
const { rOptions, log } = context
if (proxyRes.statusCode !== 200) {
return
}
const responseConfig = interceptOpt.responseReplace
const responseReplaceConfig = interceptOpt.responseReplace
let actions = ''
// 处理文件下载请求
if (responseReplaceConfig.doDownload && rOptions.doDownload) {
const filename = (rOptions.path.match('^.*/([^/?]+)/?(\\?.*)?$') || [])[1] || 'UNKNOWN_FILENAME'
res.setHeader('Content-Disposition', 'attachment; filename=' + filename)
actions += 'download:' + filename
}
// 替换响应头
if (replaceResponseHeaders(responseConfig.headers, res, proxyRes)) {
if (replaceResponseHeaders(responseReplaceConfig.headers, res, proxyRes)) {
actions += 'headers'
}