Skip to content

Is there a way to get the CONNECT response headers? #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
surlyhacker opened this issue May 5, 2023 · 4 comments · Fixed by #156
Closed

Is there a way to get the CONNECT response headers? #153

surlyhacker opened this issue May 5, 2023 · 4 comments · Fixed by #156

Comments

@surlyhacker
Copy link

surlyhacker commented May 5, 2023

This is a re-open of issue #119. I've looked at the newest code and this is still not possible.

Hi,

Is there a way to get the response headers that are sent back to the client by the proxy in response to the CONNECT method?
Typically these headers return metadata associated with the proxy itself and how it may be acting.
These are distinct of course from response headers to the actual proxied request issued subsequent to the CONNECT.

If you debug into the source, it appears to capture these headers here:
https://github.com/TooTallNate/proxy-agents/blob/main/packages/https-proxy-agent/src/parse-proxy-response.ts#L53
And then returns them internally in a Promise here:
https://github.com/TooTallNate/proxy-agents/blob/main/packages/https-proxy-agent/src/index.ts#L133
So that buffered variable holds the headers returned by the proxy server after CONNECT is issued.

Would be really nice if this was exposed via an API and/or callback event.

if the proxy itself adds headers (to responses from the target host), then those would show up and be accessible like any other response headers and pass right through this agent.
But in my case I am using a proxy that does not do that, and passed some important headers only in its own CONNECT response.

Thanks

@TooTallNate
Copy link
Owner

TooTallNate commented May 5, 2023

Ok, sounds good. I'm thinking there could be two different APIs for this. We could do one, or both:

Option 1

Event on the req object:

const req = https.get(url, { agent });
req.on('proxyConnect', (connect) => {
  console.log(connect.statusCode);
  // 200

  console.log(connect.headers);
  // {
  //   connection: 'keep-alive'
  // }
});

This API feels more intuitive to me, but might be problematic for wrappers around the http/https modules (i.e. node-fetch) since they don't provide access to the underlying req instance.

Option 2

Event on the Agent instance:

const agent = new HttpsProxyAgent(proxyUrl);
agent.on('proxyConnect', (connect, req) => {

});

Same connect object as above. Thinking it could be useful to also pass in the req instance, though not sure what it would be useful for off the top of my head.

@surlyhacker
Copy link
Author

Thanks for the quick response!
Yes I happen to use node-fetch quite a bit with the proxy agent, so Option 2 would be my vote, I think. Not opposed to also doing Option 1.

TooTallNate added a commit that referenced this issue May 5, 2023
Part of #153.

I'd like to also emit this event on the `agent` instance itself, but
this is currently blocked by
DefinitelyTyped/DefinitelyTyped#65408.
TooTallNate added a commit that referenced this issue May 5, 2023
Part of #153.

I'd like to also emit this event on the `agent` instance itself, but
this is currently blocked by
DefinitelyTyped/DefinitelyTyped#65408.
@TooTallNate
Copy link
Owner

@surlyhacker
Copy link
Author

Thanks! You're awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants