forked from http-party/node-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-outgoing.ts
164 lines (150 loc) · 4.65 KB
/
web-outgoing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { IncomingMessage, ServerResponse } from 'node:http';
import url from 'node:url';
import { rewriteCookieProperty } from '../common';
import { ProxyServer, ResolvedProxyServerOptions } from '../proxyServer';
const redirectRegex = /^201|30(1|2|7|8)$/;
/*
* Array of passes.
*
* A `pass` is just a function that is executed on `req, res, options`
* so that you can easily add new checks while still keeping the base
* flexible.
*/
export type WebOutgoingPass = (
this: ProxyServer,
req: IncomingMessage,
res: ServerResponse,
proxyRes: IncomingMessage,
options: ResolvedProxyServerOptions,
server: ProxyServer,
) => boolean | unknown;
/**
* If is an HTTP 1.0 request, remove chunk headers
*
* @internal
*/
export const removeChunked: WebOutgoingPass = (req, res, proxyRes) => {
if (req.httpVersion === '1.0') {
delete proxyRes.headers['transfer-encoding'];
}
};
/**
* If is a HTTP 1.0 request, set the correct connection header
* or if connection header not present, then use `keep-alive`
*
* @internal
*/
export const setConnection: WebOutgoingPass = (req, res, proxyRes) => {
if (req.httpVersion === '1.0') {
proxyRes.headers.connection = req.headers.connection || 'close';
} else if (req.httpVersion !== '2.0' && !proxyRes.headers.connection) {
proxyRes.headers.connection = req.headers.connection || 'keep-alive';
}
};
/**
* @internal
*/
export const setRedirectHostRewrite: WebOutgoingPass = (
req,
res,
proxyRes,
options,
) => {
if (
(options.hostRewrite || options.autoRewrite || options.protocolRewrite) &&
options.target &&
proxyRes.headers['location'] &&
proxyRes.statusCode &&
redirectRegex.test(String(proxyRes.statusCode))
) {
const target =
// @ts-ignore -- url.Url is the legacy Url instance
options.target instanceof url.Url
? options.target
: // @ts-ignore
url.parse(options.target);
const u = url.parse(proxyRes.headers['location']);
// make sure the redirected host matches the target host before rewriting
if (target.host != u.host) {
return;
}
if (options.hostRewrite) {
u.host = options.hostRewrite;
} else if (options.autoRewrite) {
u.host = req.headers['host'] || null;
}
if (options.protocolRewrite) {
u.protocol = options.protocolRewrite;
}
proxyRes.headers['location'] = url.format(u);
}
};
/**
* Copy headers from proxyResponse to response
* set each header in response object.
*
* @internal
*/
export const writeHeaders: WebOutgoingPass = (req, res, proxyRes, options) => {
let rawHeaderKeyMap: Record<string, string> | undefined;
const preserveHeaderKeyCase = options.preserveHeaderKeyCase,
setHeader = function (key: string, header: string | string[] | undefined) {
if (header == undefined) return;
if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
header = rewriteCookieProperty(
header,
rewriteCookieDomainConfig,
'domain',
);
}
if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
header = rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
}
res.setHeader(String(key).trim(), header!);
};
const rewriteCookieDomainConfig =
typeof options.cookieDomainRewrite === 'string'
? { '*': options.cookieDomainRewrite }
: options.cookieDomainRewrite;
const rewriteCookiePathConfig =
typeof options.cookiePathRewrite === 'string'
? { '*': options.cookiePathRewrite }
: options.cookiePathRewrite;
// message.rawHeaders is added in: v0.11.6
// https://nodejs.org/api/http.html#http_message_rawheaders
if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) {
rawHeaderKeyMap = {};
for (let i = 0; i < proxyRes.rawHeaders.length; i += 2) {
const key = proxyRes.rawHeaders[i];
rawHeaderKeyMap[key.toLowerCase()] = key;
}
}
Object.keys(proxyRes.headers).forEach(function (key) {
const header = proxyRes.headers[key];
if (preserveHeaderKeyCase && rawHeaderKeyMap) {
key = rawHeaderKeyMap[key] || key;
}
setHeader(key, header);
});
};
/**
* Set the statusCode from the proxyResponse
*
* @internal
*/
export const writeStatusCode: WebOutgoingPass = (req, res, proxyRes) => {
// From Node.js docs: response.writeHead(statusCode[, statusMessage][, headers])
if (proxyRes.statusMessage) {
res.statusCode = proxyRes.statusCode!;
res.statusMessage = proxyRes.statusMessage;
} else {
res.statusCode = proxyRes.statusCode!;
}
};
export const webOutgoingPasses = [
removeChunked,
setConnection,
setRedirectHostRewrite,
writeHeaders,
writeStatusCode,
];