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

Fix content length in CONNECT requests and responses #866

Merged
merged 5 commits into from
Jul 25, 2024
Merged

Conversation

mmatczuk
Copy link
Contributor

@mmatczuk mmatczuk commented Jul 24, 2024

Make sure that we correctly read and send Content-Length header value for CONNECT requests.

@mmatczuk mmatczuk requested a review from Choraden as a code owner July 24, 2024 14:45
@mmatczuk mmatczuk changed the title Connect content length fixes Fix content length in CONNECT requests and responses Jul 24, 2024
This is for compatibility between conn and handler versions.

Also, handler version sets content length for http/2 to indicate that there will be DATA frames.
@mmatczuk mmatczuk force-pushed the mmt/var_fix branch 2 times, most recently from 83ece87 to 5659193 Compare July 25, 2024 10:13

func newConnectResponse(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set Status too. The response log lacks it, you can see it in commit message.

sc-1         | HTTP/2.0

vs

sc-server-1  | HTTP/1.1 200 OK

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and donner

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong 🤔. That's how it's done in proxyutil

Status:     fmt.Sprintf("%d %s", code, http.StatusText(code)),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the best way of doing it, fixed.

@mmatczuk mmatczuk force-pushed the mmt/var_fix branch 2 times, most recently from 50184fd to 0e79abf Compare July 25, 2024 10:37
@Choraden
Copy link
Contributor

res.ContentLength = -1 // Do we still need that?
 if err := p.tunnel("CONNECT", res, crw); err != nil {
 	log.Errorf(ctx, "CONNECT tunnel: %v", err)
 }

I wonder if we still need that, since we set the ContentLength in read. I understand this is just a sanity check if the ContentLength was modified in Martian req/res modifiers?

@mmatczuk
Copy link
Contributor Author

I wonder if we still need that, since we set the ContentLength in read. I understand this is just a sanity check if the ContentLength was modified in Martian req/res modifiers?

I'd leave that to be on the safe side, we can add some logging.

@Choraden
Copy link
Contributor

Sure, let's log it.

Without this patch we (the net/http package) assume any request content length without Content-Length header is 0.
This is not the case for CONNECT requests.
Make CONNECT response use http.NoBody and ConetentLenth = -1

Logs from Sauce Connect showing clean CONNECT requests and responses when using this patch.

sc-1         | CONNECT //cdn.cookielaw.org:443 HTTP/2.0
sc-1         | Host: cdn.cookielaw.org:443
sc-1         | Sl-Forwarded-Host: cdn.cookielaw.org:443
sc-1         | Sl-Forwarded-Proto:
sc-1         | User-Agent: curl/8.6.0
sc-1         | Via: 1.1 sc_server-159f7f75742d33630cfc, 2.0 sauce_connect-e31d884000ef4e50a1a0
sc-1         |
sc-1         | HTTP/2.0 OK
sc-1         |
sc-1         |
...
sc-server-1  | CONNECT https://cdn.cookielaw.org:443 HTTP/1.1
sc-server-1  | Host: cdn.cookielaw.org:443
sc-server-1  | Sl-Forwarded-Host: cdn.cookielaw.org:443
sc-server-1  | Sl-Forwarded-Proto:
sc-server-1  | User-Agent: curl/8.6.0
sc-server-1  | Via: 1.1 sc_server-159f7f75742d33630cfc
sc-server-1  |
sc-server-1  | HTTP/1.1 200 OK
sc-server-1  |
sc-server-1  |
Copy link
Contributor

@Choraden Choraden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mmatczuk mmatczuk merged commit 12421cb into main Jul 25, 2024
6 checks passed
@mmatczuk mmatczuk deleted the mmt/var_fix branch July 25, 2024 11:53
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