Skip to content

Commit

Permalink
feat: 缓存拦截器,新增 cacheExcludeStatusCodeList 用于排除某个响应码不使用缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Nov 6, 2024
1 parent 0840831 commit 35faea4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/mitmproxy/src/lib/interceptor/impl/res/cacheRes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ module.exports = {
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
const { rOptions, log } = context

// 只有GET请求,且响应码为 2xx 或 3xx 时才进行缓存
const minStatusCode = interceptOpt.cacheMinStatusCode || 200
const maxStatusCode = interceptOpt.cacheMaxStatusCode || 303
if (rOptions.method !== 'GET' || proxyRes.statusCode < minStatusCode || proxyRes.statusCode > maxStatusCode) {
// 只有GET请求
if (rOptions.method !== 'GET') {
return
}

// 判断当前响应码是否不使用缓存
if (interceptOpt.cacheExcludeStatusCodeList && interceptOpt.cacheExcludeStatusCodeList[proxyRes.statusCode + '']) {
return
}

// 响应码为 200~303 时才进行缓存(可通过以下两个参数调整范围)
let minStatusCode = interceptOpt.cacheMinStatusCode || 200
let maxStatusCode = interceptOpt.cacheMaxStatusCode || 303
if (minStatusCode > maxStatusCode) {
const temp = minStatusCode
minStatusCode = maxStatusCode
maxStatusCode = temp
}
if (proxyRes.statusCode < minStatusCode || proxyRes.statusCode > maxStatusCode) {
// res.setHeader('DS-Cache-Response-Interceptor', `skip: 'method' or 'status' not match`)
return
}
Expand Down

0 comments on commit 35faea4

Please sign in to comment.