Skip to content
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

Return unclosed body to fix localhost redirects #12

Merged

Conversation

mikesep
Copy link
Contributor

@mikesep mikesep commented Aug 11, 2020

Use case scenario:
I'm testing a server locally that serves a 302 redirect. If I curl localhost:<port> through pacproxy, curl gives me a warning that the Content-Length header doesn't match the actual length of content in the response body.

Note: Redirects from remote servers seem to work fine. I think curl has special logic to slightly shortcut the proxy when you make a localhost request -- it sends an absolute URL rather than a CONNECT request.

From the documentation for net/http.Client:

If CheckRedirect returns an error, the Client's Get method returns both the previous Response (with its Body closed) and CheckRedirect's error (wrapped in a url.Error) instead of issuing the Request req. As a special case, if CheckRedirect returns ErrUseLastResponse, then the most recent response is returned with its body unclosed, along with a nil error.

Since the response's Body is closed, no content is relayed, but the Content-Length header still makes it through, leading to the mismatch.

ErrUseLastResponse didn't exist when pacproxy's CheckRedirect function was originally written. :)

Use case scenario:
I'm testing a server locally that serves a 302 redirect. If I curl
localhost:<port> through pacproxy, curl gives me a warning that the
Content-Length header doesn't match the actual length of content in the
response body.

Note: Redirects from remote servers seem to work fine. I think curl has
special logic to slightly shortcut the proxy when you make a localhost
request -- it sends an absolute URL rather than a CONNECT request.

From the documentation for net/http.Client:

> If CheckRedirect returns an error, the Client's Get method returns
> both the previous Response (with its Body closed) and CheckRedirect's
> error (wrapped in a url.Error) instead of issuing the Request req. As
> a special case, if CheckRedirect returns ErrUseLastResponse, then the
> most recent response is returned with its body unclosed, along with
> a nil error.

Since the response's Body is closed, no content is relayed, but the
Content-Length header still makes it through, leading to the mismatch.

ErrUseLastResponse didn't exist when pacproxy's CheckRedirect function
was originally written. :)
@williambailey
Copy link
Owner

Thanks for the PR. I'll try to get this in a new release over the next few days.

@williambailey williambailey merged commit 0566880 into williambailey:master Sep 4, 2020
@mikesep mikesep deleted the localhost-redirect-body branch September 4, 2020 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants