Repository navigation
SocketsHttpHandler: Fix some issues with cookie handling, and add cookie tests - #26989
Conversation
|
@stephentoub @davidsh @dotnet/ncl |
| public static void ProcessReceivedCookies(HttpResponseMessage response, CookieContainer cookieContainer) | ||
| { | ||
| IEnumerable<string> setCookies; | ||
| if (response.Headers.TryGetValues(HttpKnownHeaderNames.SetCookie, out setCookies)) |
There was a problem hiding this comment.
Nit: based on internal knowledge, do we know anything about what the actual type of setCookies will be? I'm wondering if, for example, it's always an array, we could cast to the array and avoid the enumerator allocation.
There was a problem hiding this comment.
Yes, it's always a string[]. I'm a little queasy about doing the cast; seems like it would be better to have an internal API that exposes it as string[]. That said, we are doing this in other places already.
| await WriteStringAsync(cookies, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| cookies = null; | ||
| } |
There was a problem hiding this comment.
Will a Cookie header never have multiple values? I'm wondering about the loop below this... seems like if it did have multiple values, they'll end up getting appended with commas.
There was a problem hiding this comment.
A 'Cookie' request header can have multiple cookies on the single 'Cookie' request header.
There has been confusion on this over the years. The latest RFC 6265 states:
https://tools.ietf.org/html/rfc6265#section-5.4
"5.4 When the user agent generates an HTTP request, the user agent MUST NOT attach more than one Cookie header field."
There was a problem hiding this comment.
The various HTTP APIs we have allow for multiple Cookie request headers to be set. But the end result of the HTTP stack is supposed to only send a single 'Cookie' header on the wire for the request.
There was a problem hiding this comment.
Yeah, basically the Cookie header is weird because Netscape designed it poorly.
If the user sets multiple Cookie headers, that's arguably an error on their part. We could handle this by appending with ";" instead of "," -- this should be easy to do since we're already doing special handling here. Or we could just leave as-is and tell them not to do this.
I'm not sure what the other handlers do here; I'll investigate.
Note there's no strongly-typed Cookie property on the HttpRequestHeaders. Presumably this is intentional...
There was a problem hiding this comment.
To be clear, though, I wasn't meaning to suggest multiple cookie headers, but rather multiple values for the single header, e.g.
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static void Main()
{
var m = new HttpRequestMessage();
m.Headers.Add("Cookie", new[] { "CustomCookie=456", "AnotherCustomCookie=789" });
Console.WriteLine(m.Headers.ToString());
foreach (var c in m.Headers.GetValues("Cookie"))
{
Console.WriteLine(c);
}
}
}outputs:
Cookie: CustomCookie=456, AnotherCustomCookie=789
CustomCookie=456
AnotherCustomCookie=789
Am I misunderstanding?
There was a problem hiding this comment.
Yes, I meant multiple values.
If the user sets multiple values for a Cookie header, that's arguably an error on their part. We could handle this by appending with ";" instead of "," -- this should be easy to do since we're already doing special handling here. Or we could just leave as-is and tell them not to do this.
I'll investigate what other handlers do here.
There was a problem hiding this comment.
Note there's no strongly-typed Cookie property on the HttpRequestHeaders. Presumably this is intentional...
Yes. It is intentional. The HttpWebRequest and HttpClient/HttpClientHandler APIs encouraged the use of the CookieContainer. Overall having devs manually add 'Cookie' request headers is discouraged.
There was a problem hiding this comment.
WinHttpHandler behaves essentially the same as current SocketsHttpHandler here. That is, if you specify multiple Cookie header values, they get appended using a comma, e.g. "A=1, B=2". If you also have a cookie from the container, you get something like "ContainerCookie=Foo; A=1, B=2" -- that is, the container cookie is added to one of the cookie header values.
I'm fine with this behavior, as it matches header handling generally. I suppose we could special-case manual cookie header handling, but I don't think there's any real value here. Thoughts?
(edited for clarity to be consistent about "header values")
| if (cookies != null) | ||
| { | ||
| await WriteAsciiStringAsync(HttpKnownHeaderNames.Cookie, cancellationToken).ConfigureAwait(false); | ||
| await WriteTwoBytesAsync((byte)':', (byte)' ', cancellationToken).ConfigureAwait(false); |
There was a problem hiding this comment.
Can we use a cached byte[] representation of "Cookie: " and just blit that out with WriteBytesAsync?
There was a problem hiding this comment.
At some point I'd like to do this for all headers. We have already resolved them to well-known header values internally, it's just a matter of exposing an enumerator of KVP<HeaderDescriptor, IEnumerable> instead of KVP<string, IEnumerable>. Given this I don't think we should special case Cookie now.
| public DateTimeOffset CreationTime { get; } = DateTimeOffset.UtcNow; | ||
|
|
||
| private async Task WriteHeadersAsync(HttpHeaders headers, CancellationToken cancellationToken) | ||
| private async Task WriteHeadersAsync(HttpHeaders headers, string cookies, CancellationToken cancellationToken) |
There was a problem hiding this comment.
Nit: could we name the parameter something like "cookiesFromContainer"? Before reading the rest of the code, I was left wondering whether this was meant to be instead of or in addition to anything in the headers.
|
|
||
| namespace System.Net.Http.Functional.Tests | ||
| { | ||
| public class HttpCookieProtocolTests : HttpClientTestBase |
There was a problem hiding this comment.
There are already some cookie tests here:
corefx/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.cs
Lines 981 to 1039 in ef0aca5
and here:
corefx/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.cs
Lines 1077 to 1174 in ef0aca5
If we're creating a new test class for cookies, can we consolidate them to the same place?
Pre-existing your PR, but exacerbated every time we add tests to new places, our scheme for where we put tests in this suite is unwieldy.
There was a problem hiding this comment.
I will consolidate them into this file.
| Uri requestUri = response.RequestMessage.RequestUri; | ||
| foreach (string setCookie in setCookies) | ||
| { | ||
| cookieContainer.SetCookies(requestUri, setCookie); |
There was a problem hiding this comment.
I must be missing something, but... how is this thread-safe? Does SetCookies internally lock? Same for getting the cookie values.
There was a problem hiding this comment.
Yes, CookieContainer is thread-safe and locks internally.
| using (HttpClient client = new HttpClient(handler)) | ||
| { | ||
| HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url); | ||
| requestMessage.Headers.Add("Cookie", s_customCookieHeaderValue); |
There was a problem hiding this comment.
Can you add a variation for multiple values/cookies set?
|
@dotnet-bot test outerloop Windows x64 Debug Build |
|
@dotnet-bot test outerloop Windows x64 Debug Build |
SocketsHttpHandler: Fix some issues with cookie handling, and add cookie tests Commit migrated from dotnet/corefx@8cee267
Remove CookieHandler and integrate the cookie handling logic into HttpConnection, which fixes some issues with how cookies were handled.
Add a bunch of protocol tests for cookie handling. Issues filed for failures in CurlHandler and WinHttpHandler.
Fixes #26984
Fixes #26985