You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.4 KiB
66 lines
2.4 KiB
"use strict"; |
|
var __importDefault = (this && this.__importDefault) || function (mod) { |
|
return (mod && mod.__esModule) ? mod : { "default": mod }; |
|
}; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
const debug_1 = __importDefault(require("debug")); |
|
const debug = debug_1.default('https-proxy-agent:parse-proxy-response'); |
|
function parseProxyResponse(socket) { |
|
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 = []; |
|
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) { |
|
debug('onclose had error %o', err); |
|
} |
|
function onend() { |
|
debug('onend'); |
|
} |
|
function onerror(err) { |
|
cleanup(); |
|
debug('onerror %o', err); |
|
reject(err); |
|
} |
|
function ondata(b) { |
|
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(); |
|
}); |
|
} |
|
exports.default = parseProxyResponse; |
|
//# sourceMappingURL=parse-proxy-response.js.map
|