Skip to content

[http-proxy-agent] Raw header support #382

@smckee-r7

Description

@smckee-r7

While using mockttp(https://github.com/httptoolkit/mockttp) an issue was found with http-proxy-agent and by extension pac-proxy-agent. It fails to connect when you make a request using raw headers. Below is an example showing this:

const mockttp = require("mockttp");
const { HttpProxyAgent } = require('http-proxy-agent');
const http = require('http');

(async () => {
  const https = await mockttp.generateCACertificate();

  // Create proxy
  const proxy = mockttp.getLocal({ https });
  await proxy.start(8095);
  await proxy.forAnyRequest().thenPassThrough({
    beforeRequest: req => {
      console.log('Request reached the Proxy:', req.url)
      return req;
    }
  });

  // Standard headers request
  const req1 = http.request('http://example.com', {
    headers: {
      host: 'example.com',
      connection: 'close'
    },
    agent: new HttpProxyAgent('http://localhost:8095')
  }, (res) => {
    res.on('data', () => console.log('Standard headers request worked'));
  });
  req1.on('error', () => console.log('Standard headers request failed'));
  req1.end();

  // Raw headers request
  const req2 = http.request('http://example.com', {
    headers: [
      'host',
      'example.com',
      'connection',
      'close'
    ],
    agent: new HttpProxyAgent('http://localhost:8095')
  }, (res) => {
    res.on('data', () => console.log('Raw headers request worked'));
  });
  req2.on('error', () => console.log('Raw headers request failed'));
  req2.end();
})();

The issues seems to be that http-proxy-agent uses req.getHeader('host') to get the host, however, that method returns undefined if raw headers are used (which is what mockttp uses). It also looks like this isn't an issue with https-proxy-agent as it doesn't make use of req.getHeader('host') like http-proxy-agent does. So the fix would be to get the host in a similar or same way that https-proxy-agent does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions