| 26097 | var debug_1 = __importDefault(require_src2()); |
| 26098 | var debug5 = (0, debug_1.default)("https-proxy-agent:parse-proxy-response"); |
| 26099 | function parseProxyResponse2(socket) { |
| 26100 | return new Promise((resolve, reject) => { |
| 26101 | let buffersLength = 0; |
| 26102 | const buffers = []; |
| 26103 | function read() { |
| 26104 | const b7 = socket.read(); |
| 26105 | if (b7) |
| 26106 | ondata(b7); |
| 26107 | else |
| 26108 | socket.once("readable", read); |
| 26109 | } |
| 26110 | function cleanup() { |
| 26111 | socket.removeListener("end", onend); |
| 26112 | socket.removeListener("error", onerror); |
| 26113 | socket.removeListener("readable", read); |
| 26114 | } |
| 26115 | function onend() { |
| 26116 | cleanup(); |
| 26117 | debug5("onend"); |
| 26118 | reject(new Error("Proxy connection ended before receiving CONNECT response")); |
| 26119 | } |
| 26120 | function onerror(err) { |
| 26121 | cleanup(); |
| 26122 | debug5("onerror %o", err); |
| 26123 | reject(err); |
| 26124 | } |
| 26125 | function ondata(b7) { |
| 26126 | buffers.push(b7); |
| 26127 | buffersLength += b7.length; |
| 26128 | const buffered = Buffer.concat(buffers, buffersLength); |
| 26129 | const endOfHeaders = buffered.indexOf("\r\n\r\n"); |
| 26130 | if (endOfHeaders === -1) { |
| 26131 | debug5("have not received end of HTTP headers yet..."); |
| 26132 | read(); |
| 26133 | return; |
| 26134 | } |
| 26135 | const headerParts = buffered.slice(0, endOfHeaders).toString("ascii").split("\r\n"); |
| 26136 | const firstLine = headerParts.shift(); |
| 26137 | if (!firstLine) { |
| 26138 | socket.destroy(); |
| 26139 | return reject(new Error("No header received from proxy CONNECT response")); |
| 26140 | } |
| 26141 | const firstLineParts = firstLine.split(" "); |
| 26142 | const statusCode = +firstLineParts[1]; |
| 26143 | const statusText = firstLineParts.slice(2).join(" "); |
| 26144 | const headers = {}; |
| 26145 | for (const header of headerParts) { |
| 26146 | if (!header) |
| 26147 | continue; |
| 26148 | const firstColon = header.indexOf(":"); |
| 26149 | if (firstColon === -1) { |
| 26150 | socket.destroy(); |
| 26151 | return reject(new Error(`Invalid header from proxy CONNECT response: "${header}"`)); |
| 26152 | } |
| 26153 | const key = header.slice(0, firstColon).toLowerCase(); |
| 26154 | const value = header.slice(firstColon + 1).trimStart(); |
| 26155 | const current = headers[key]; |
| 26156 | if (typeof current === "string") { |