forked from TooTallNate/proxy-agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-proxy-response.ts
More file actions
84 lines (70 loc) · 1.94 KB
/
Copy pathparse-proxy-response.ts
File metadata and controls
84 lines (70 loc) · 1.94 KB
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
import createDebug from 'debug';
import { Readable } from 'stream';
const debug = createDebug('https-proxy-agent:parse-proxy-response');
export interface ProxyResponse {
statusCode: number;
buffered: Buffer;
}
export default function parseProxyResponse(
socket: Readable
): Promise<ProxyResponse> {
return new Promise((resolve, reject) => {
// we need to buffer any HTTP traffic that happens with the proxy before we get
// the CONNECT response, so that if the response is anything other than an "200"
// response code, then we can re-play the "data" events on the socket once the
// HTTP parser is hooked up...
let buffersLength = 0;
const buffers: Buffer[] = [];
function read() {
const b = socket.read();
if (b) ondata(b);
else socket.once('readable', read);
}
function cleanup() {
socket.removeListener('end', onend);
socket.removeListener('error', onerror);
socket.removeListener('close', onclose);
socket.removeListener('readable', read);
}
function onclose(err?: Error) {
debug('onclose had error %o', err);
reject(err);
}
function onend() {
debug('onend');
reject(new Error('Socket was closed by other party!'));
}
function onerror(err: Error) {
cleanup();
debug('onerror %o', err);
reject(err);
}
function ondata(b: Buffer) {
buffers.push(b);
buffersLength += b.length;
const buffered = Buffer.concat(buffers, buffersLength);
const endOfHeaders = buffered.indexOf('\r\n\r\n');
if (endOfHeaders === -1) {
// keep buffering
debug('have not received end of HTTP headers yet...');
read();
return;
}
const firstLine = buffered.toString(
'ascii',
0,
buffered.indexOf('\r\n')
);
const statusCode = +firstLine.split(' ')[1];
debug('got proxy server response: %o', firstLine);
resolve({
statusCode,
buffered
});
}
socket.on('error', onerror);
socket.on('close', onclose);
socket.on('end', onend);
read();
});
}