Skip to content

Commit b173c07

Browse files
perf: 正则变为常量,可提交预编译,避免每次都创建
1 parent 5c058f0 commit b173c07

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

packages/mitmproxy/src/lib/interceptor/impl/req/requestReplace.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const REMOVE = '[remove]'
22

3+
const DS_DOWNLOAD_CHECK_RE = /DS_DOWNLOAD/i
4+
const DS_DOWNLOAD_STRIP_RE = /[?&/]?DS_DOWNLOAD(=[^?&/]+)?$/i
5+
36
function replaceRequestHeaders (rOptions, headers, log) {
47
for (const key in headers) {
58
let value = headers[key]
@@ -37,9 +40,9 @@ module.exports = {
3740

3841
// 替换下载文件请求的请求地址(此功能主要是为了方便拦截配置)
3942
// 注:要转换为下载请求,需要 responseReplace 拦截器的配合使用。
40-
if (requestReplaceConfig.doDownload && rOptions.path.match(/DS_DOWNLOAD/i)) {
43+
if (requestReplaceConfig.doDownload && DS_DOWNLOAD_CHECK_RE.test(rOptions.path)) {
4144
rOptions.doDownload = true
42-
rOptions.path = rOptions.path.replace(/[?&/]?DS_DOWNLOAD(=[^?&/]+)?$/gi, '')
45+
rOptions.path = rOptions.path.replace(DS_DOWNLOAD_STRIP_RE, '')
4346
actions += `${actions ? ',' : ''}path:remove-DS_DOWNLOAD`
4447
}
4548

packages/mitmproxy/src/lib/interceptor/impl/res/responseReplace.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const cacheReq = require('../req/cacheRequest')
33

44
const REMOVE = '[remove]'
55

6+
const FILENAME_RE = /^.*\/([^/?]+)\/?(\?.*)?$/
7+
68
// 替换响应头
79
function replaceResponseHeaders (newHeaders, res, proxyRes) {
810
if (!newHeaders || lodash.isEmpty(newHeaders)) {
@@ -82,7 +84,7 @@ module.exports = {
8284

8385
// 处理文件下载请求
8486
if (responseReplaceConfig.doDownload || rOptions.doDownload) {
85-
const filename = (rOptions.path.match('^.*/([^/?]+)/?(\\?.*)?$') || [])[1] || 'UNKNOWN_FILENAME'
87+
const filename = (FILENAME_RE.exec(rOptions.path) || [])[1] || 'UNKNOWN_FILENAME'
8688
// 设置文件下载响应头
8789
replaceHeaders['content-disposition'] = `attachment; filename="${encodeURIComponent(filename)}"`
8890
// 设置文件类型

packages/mitmproxy/src/lib/proxy/middleware/InsertScriptMiddleware.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const zlib = require('node:zlib')
22
const through = require('through2')
33
const log = require('../../../utils/util.log.server')
44

5+
const HTML_CONTENT_TYPE_RE = /text\/html|application\/xhtml\+xml/
6+
const CSP_SCRIPT_SRC_RE = /script-src ([^:]*);/i
7+
58
// 编解码器
69
const codecMap = {
710
gzip: {
@@ -40,7 +43,7 @@ const httpUtil = {
4043
// 是否HTML代码
4144
isHtml (res) {
4245
const contentType = res.headers['content-type']
43-
return (typeof contentType !== 'undefined') && /text\/html|application\/xhtml\+xml/.test(contentType)
46+
return (typeof contentType !== 'undefined') && HTML_CONTENT_TYPE_RE.test(contentType)
4447
},
4548
}
4649
const HEAD = Buffer.from('</head>')
@@ -97,8 +100,7 @@ function handleResponseHeaders (res, proxyRes) {
97100
if (key === 'content-security-policy') {
98101
// content-security-policy
99102
let policy = proxyRes.headers[key]
100-
const reg = /script-src ([^:]*);/i
101-
const matched = policy.match(reg)
103+
const matched = policy.match(CSP_SCRIPT_SRC_RE)
102104
if (matched) {
103105
if (!matched[1].includes('self')) {
104106
policy = policy.replace('script-src', 'script-src \'self\' ')
@@ -143,7 +145,7 @@ module.exports = {
143145
'Cache-Control': 'public, max-age=86401, immutable', // 缓存1天
144146
'Last-Modified': now.toUTCString(),
145147
'Expires': new Date(now.getTime() + 86400000).toUTCString(), // 缓存1天
146-
'Date': new Date().toUTCString(),
148+
'Date': now.toUTCString(),
147149
})
148150
res.write(script.script)
149151
res.end()

packages/mitmproxy/src/lib/proxy/mitmproxy/createRequestHandler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const InsertScriptMiddleware = require('../middleware/InsertScriptMiddleware')
1111
const dnsLookup = require('./dnsLookup')
1212

1313
const MAX_SLOW_TIME = 8000 // 超过此时间 则认为太慢了
14+
const WWW_AUTH_HEADER_RE = /^www-authenticate$/i
1415

1516
// create requestHandler function
1617
module.exports = function createRequestHandler (createIntercepts, middlewares, externalProxy, dnsConfig, setting, compatibleConfig) {
@@ -313,7 +314,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
313314
Object.keys(proxyRes.headers).forEach((key) => {
314315
if (proxyRes.headers[key] !== undefined) {
315316
// https://github.com/nodejitsu/node-http-proxy/issues/362
316-
if (/^www-authenticate$/i.test(key)) {
317+
if (WWW_AUTH_HEADER_RE.test(key)) {
317318
if (proxyRes.headers[key]) {
318319
proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',')
319320
}

0 commit comments

Comments
 (0)