Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Fix WinHttpHandler response handling for manual cookie parsing - #5001

Merged
davidsh merged 1 commit into
dotnet:masterfrom
davidsh:cookie_bug
Dec 17, 2015
Merged

davidsh merged 1 commit into
dotnet:masterfrom
davidsh:cookie_bug

Conversation

@davidsh

@davidsh davidsh commented Dec 16, 2015

Copy link
Copy Markdown
Contributor

WinHttpHandler was losing 'Set-Cookie' response headers during auto redirection handling when the handler was in 'manual cookie' mode. I.e. CookieUsePolicy.UseSpecifiedCookieContainer. This is the only mode that HttpClientHandler uses.

The 'Set-Cookie' headers were currently only parsed out when a final HTTP response happened. This meant that 'Set-Cookie' headers present in interim 3xx redirection responses as well as interim 401, 407 auth responses would not be stored into the CookieContainer. Hence, those same cookies could not be sent back to the server when needed.

The fix is to make sure we parse out the 'Set-Cookie' headers whenever we get an HTTP response back from WinHTTP. As part of this fix, I re-factored out the cookie handling code into a separate class. I also added specific WinHttpHandler tests for the two different cookie modes as well as a HttpClientHandler test to validate cross-platform (i.e. CurlHandler).

Fixes #4960

@davidsh

davidsh commented Dec 16, 2015

Copy link
Copy Markdown
Contributor Author

@CIPop @stephentoub PTAL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cookieHeader = HttpKnownHeaderNames.Cookie + ": " + cookieValues; should be more efficient than string.Format and avoids having to redefine the Cookie constant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed. I had forgotten about the Common/src/System/Net/HttpKnownHeaderNames.cs class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

private const string CookieHeaderNameWithColon = HttpKnownHeaderNames.Cookie + ":";?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@davidsh

davidsh commented Dec 16, 2015

Copy link
Copy Markdown
Contributor Author

Test Innerloop OpenSUSE13.2 Debug Build and Test

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This adds a lot of unnecessary strings. We should at least have a TODO item saying that we need to split it into partial classes to ensure that only the needed strings are added to the binary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

FWIW, the System.Net.Http.dll binary already drags in this class HttpKnownHeaderNames.cs. Now this change will drag in another copy for the System.Net.Http.WinHttpHandler.dll as well.

But won't the compiler assist in this in some way by figuring out what is really needed and trim out the rest?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FYI, in #3199 I extended HttpKnownHeaderNames to allow looking up and reusing the header string instances to avoid having to always allocate new strings for known response headers. Not sure how easy it will be to split apart the strings with that optimization -- maybe splitting it up between request vs. response headers at some point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Although we're using a little more static storage in the library, doing too much micro-optimization on these allocations might end up making this code much harder to maintain and extend in the future. So, we should balance that against all of these optimizations (which we don't really understand the true quantitative benefits of).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

So, we should balance that against all of these optimizations (which we don't really understand the true quantitative benefits of).

What is the DLL size before and after this change?

My proposal was to simply create HttpKnownHeaderNames.Cookie.cs and HttpKnownHeaderNames.cs as partial and import within each project only what is needed. The split will be made whenever it's necessary to reduce the DLL size.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think for now, I'm going to undo the inclusion of HttpKnownHeaderNames.cs from this PR and simply use "Cookie" string directly.

I'll let the next PR such as #3199 look into further optimizations.

@davidsh

davidsh commented Dec 17, 2015

Copy link
Copy Markdown
Contributor Author

Test Innerloop Windows_NT Release Build and Test

@CIPop

CIPop commented Dec 17, 2015

Copy link
Copy Markdown

LGTM

The object model needs improvement.
We should assert that the WinHttp callback flags are such that the redirect callback is always called. Otherwise we run the risk of exposing cookies to unwanted websites during a redirect.

@davidsh

davidsh commented Dec 17, 2015

Copy link
Copy Markdown
Contributor Author

Added comments and issue #5036 to track the WinHttp callback flags improvements.

WinHttpHandler was losing 'Set-Cookie' response headers during auto redirection handling when the handler was in 'manual cookie' mode. I.e. `CookieUsePolicy.UseSpecifiedCookieContainer`. This is the only mode that HttpClientHandler uses.

The 'Set-Cookie' headers were currently only parsed out when a final HTTP response happened. This meant that 'Set-Cookie' headers present in interim 3xx redirection responses as well as interim 401, 407 auth responses would not be stored into the CookieContainer. Hence, those same cookies could not be sent back to the server when needed.

The fix is to make sure we parse out the 'Set-Cookie' headers whenever we get an HTTP response back from WinHTTP. As part of this fix, I re-factored out the cookie handling code into a separate class. I also added specific WinHttpHandler tests for the two different cookie modes as well as a HttpClientHandler test to validate cross-platform (i.e. CurlHandler).

Address #4960 but further fixes are needed for *NIX as well.
@davidsh

davidsh commented Dec 17, 2015

Copy link
Copy Markdown
Contributor Author

Test Innerloop Ubuntu Release Build and Test

davidsh added a commit that referenced this pull request Dec 17, 2015
Fix WinHttpHandler response handling for manual cookie parsing
@davidsh
davidsh merged commit a9e2c63 into dotnet:master Dec 17, 2015
@davidsh
davidsh deleted the cookie_bug branch December 17, 2015 21:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HttpClient not sending cookies which were part of earlier redirect response in Core Clr

5 participants