(req, reqOptions)
| 148 | // and modifies the headers as well as req.path if necessary. |
| 149 | // The handling of the proxy server connection is done in createConnection. |
| 150 | function rewriteForProxiedHttp(req, reqOptions) { |
| 151 | if (req._header) { |
| 152 | debug('request._header is already sent, skipping rewriteForProxiedHttp', reqOptions); |
| 153 | return false; |
| 154 | } |
| 155 | const agent = req.agent; |
| 156 | if (!agent || !agent[kProxyConfig]) { |
| 157 | return false; |
| 158 | } |
| 159 | if ((reqOptions.protocol || agent.protocol) !== 'http:') { |
| 160 | return false; |
| 161 | } |
| 162 | // TODO(joyeecheung): cache this on the req or on the agent. |
| 163 | const shouldUseProxy = checkShouldUseProxy(agent[kProxyConfig], reqOptions); |
| 164 | debug(`rewriteForProxiedHttp should use proxy for ${reqOptions.host}:${reqOptions.port}:`, shouldUseProxy); |
| 165 | if (!shouldUseProxy) { |
| 166 | return false; |
| 167 | } |
| 168 | // Add proxy headers. |
| 169 | const { auth, href } = agent[kProxyConfig]; |
| 170 | if (auth) { |
| 171 | req.setHeader('proxy-authorization', auth); |
| 172 | } |
| 173 | if (req.shouldKeepAlive) { |
| 174 | req.setHeader('proxy-connection', 'keep-alive'); |
| 175 | } else { |
| 176 | req.setHeader('proxy-connection', 'close'); |
| 177 | } |
| 178 | |
| 179 | // Convert the path to absolute-form. |
| 180 | // https://datatracker.ietf.org/doc/html/rfc7230#section-5.3.2 |
| 181 | const requestHost = req.getHeader('host') || 'localhost'; |
| 182 | const requestBase = `http://${requestHost}`; |
| 183 | const requestURL = new URL(req.path, requestBase); |
| 184 | if (reqOptions.port) { |
| 185 | requestURL.port = reqOptions.port; |
| 186 | } |
| 187 | req.path = requestURL.href; |
| 188 | debug(`updated request for HTTP proxy ${href} with ${req.path} `, req[kOutHeaders]); |
| 189 | return true; |
| 190 | }; |
| 191 | |
| 192 | function ClientRequest(input, options, cb) { |
| 193 | OutgoingMessage.call(this); |
no test coverage detected
searching dependent graphs…