simplify code
This commit is contained in:
parent
1d12889ac1
commit
50e6658e71
26
tests/third_party/proxy/index.ts
vendored
26
tests/third_party/proxy/index.ts
vendored
|
|
@ -468,34 +468,18 @@ function requestAuthorization(
|
||||||
res.end('Proxy authorization required');
|
res.end('Proxy authorization required');
|
||||||
}
|
}
|
||||||
|
|
||||||
function createHttpHeader(line: string, headers: http.IncomingHttpHeaders) {
|
|
||||||
return Object.keys(headers).reduce(function (head, key) {
|
|
||||||
var value = headers[key];
|
|
||||||
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
head.push(key + ': ' + value);
|
|
||||||
return head;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < value.length; i++) {
|
|
||||||
head.push(key + ': ' + value[i]);
|
|
||||||
}
|
|
||||||
return head;
|
|
||||||
}, [line])
|
|
||||||
.join('\r\n') + '\r\n\r\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
function onupgrade(req: http.IncomingMessage, socket: net.Socket, head: Buffer) {
|
function onupgrade(req: http.IncomingMessage, socket: net.Socket, head: Buffer) {
|
||||||
const parsed = url.parse(req.url || '/');
|
const proxyReq = http.request(req.url, {
|
||||||
const proxyReq = http.request({
|
|
||||||
...parsed,
|
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: req.headers,
|
headers: req.headers,
|
||||||
localAddress: this.localAddress,
|
localAddress: this.localAddress,
|
||||||
});
|
});
|
||||||
|
|
||||||
proxyReq.on('upgrade', function (proxyRes, proxySocket, proxyHead) {
|
proxyReq.on('upgrade', function (proxyRes, proxySocket, proxyHead) {
|
||||||
socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
|
const header = ['HTTP/1.1 101 Switching Protocols'];
|
||||||
|
for (const [key, value] of Object.entries(proxyRes.headersDistinct))
|
||||||
|
header.push(`${key}: ${value}`);
|
||||||
|
socket.write(header.join('\r\n') + '\r\n\r\n');
|
||||||
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
|
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
|
||||||
proxySocket.pipe(socket).pipe(proxySocket);
|
proxySocket.pipe(socket).pipe(proxySocket);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue