diff --git a/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js b/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js index 7b5a1e4..27ad856 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js @@ -32,7 +32,7 @@ module.exports = { // 替换请求头 if (requestReplaceConfig.headers) { replaceRequestHeaders(rOptions, requestReplaceConfig.headers, log) - actions += 'headers' + actions += (actions ? ',' : '') + 'headers' } // 替换下载文件请求的请求地址(此功能主要是为了方便拦截配置) diff --git a/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js b/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js index c75bb57..f848e2e 100644 --- a/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js +++ b/packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js @@ -1,4 +1,5 @@ const lodash = require('lodash') +const cacheReq = require('../req/cacheReq') const REMOVE = '[remove]' // 替换响应头 @@ -71,16 +72,31 @@ module.exports = { let actions = '' + const replaceHeaders = responseReplaceConfig.headers || {} + // 处理文件下载请求 if (responseReplaceConfig.doDownload || rOptions.doDownload) { const filename = (rOptions.path.match('^.*/([^/?]+)/?(\\?.*)?$') || [])[1] || 'UNKNOWN_FILENAME' - res.setHeader('Content-Disposition', 'attachment; filename=' + filename) - actions += 'download:' + filename + // 设置文件下载响应头 + replaceHeaders['content-disposition'] = `attachment; filename="${encodeURIComponent(filename)}"` + // 设置文件类型 + if (replaceHeaders['content-type'] == null) { + replaceHeaders['content-type'] = 'application/octet-stream' + } + // 如果未手动配置需要缓存,则不允许使用缓存 + const maxAge = cacheReq.getMaxAge(interceptOpt) + if (maxAge == null || maxAge <= 0) { + replaceHeaders['cache-control'] = '[remove]' + replaceHeaders['last-modified'] = '[remove]' + replaceHeaders.expires = '[remove]' + } + + actions += (actions ? ',' : '') + 'download:' + filename } // 替换响应头 - if (replaceResponseHeaders(responseReplaceConfig.headers, res, proxyRes)) { - actions += 'headers' + if (replaceResponseHeaders(replaceHeaders, res, proxyRes)) { + actions += (actions ? ',' : '') + 'headers' } if (actions) {