fix: header set

This commit is contained in:
2025-12-19 18:36:40 +08:00
parent fb2b2d4d6f
commit 2ca71aea5d

View File

@@ -81,16 +81,23 @@ export const httpProxy = (req: http.IncomingMessage, res: http.ServerResponse, p
// @ts-ignore // @ts-ignore
options.rejectUnauthorized = false; // 忽略证书错误 options.rejectUnauthorized = false; // 忽略证书错误
} }
if (res.headersSent) {
return;
}
// 创建代理请求 // 创建代理请求
const proxyReq = httpProxy.request(options, (proxyRes) => { const proxyReq = httpProxy.request(options, (proxyRes) => {
// Modify the 'set-cookie' headers using rewriteCookieDomain // 检查响应头是否已经发送
if (proxyRes.headers['set-cookie']) { if (res.headersSent) {
proxyRes.headers['set-cookie'] = proxyRes.headers['set-cookie'].map((cookie) => rewriteCookieDomain(cookie, cookieHost)); return;
console.log('rewritten set-cookie:', proxyRes.headers['set-cookie']); }
// 复制响应头并修改 'set-cookie'
const responseHeaders = { ...proxyRes.headers };
if (responseHeaders['set-cookie']) {
responseHeaders['set-cookie'] = responseHeaders['set-cookie'].map((cookie) => rewriteCookieDomain(cookie, cookieHost));
console.log('rewritten set-cookie:', responseHeaders['set-cookie']);
} }
// 将代理服务器的响应头和状态码返回给客户端 // 将代理服务器的响应头和状态码返回给客户端
res.writeHead(proxyRes.statusCode, proxyRes.headers); res.writeHead(proxyRes.statusCode, responseHeaders);
// 将代理响应流写入客户端响应 // 将代理响应流写入客户端响应
proxyRes.pipe(res, { end: true }); proxyRes.pipe(res, { end: true });
}); });