Skip to content

Commit aa9b800

Browse files
The cookie header was not parsed correctly; it failed when the value had a blank space before the cookie name.
1 parent 07675de commit aa9b800

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,12 @@ void setHeaders(HttpRequestMessage request, CookieContainer cookies)
584584
{
585585
if (cookie.Contains("="))
586586
{
587-
cookies.Add(new Uri(request.RequestUri.Host), new Cookie(cookie.Split('=')[0], cookie.Split('=')[1]) { Domain = request.RequestUri.Host });
587+
UriBuilder uriBuilder = new UriBuilder(request.RequestUri.Scheme, request.RequestUri.Host);
588+
Cookie pCookie = ParseCookie(cookie, request.RequestUri.Host);
589+
if (pCookie != null)
590+
{
591+
cookies.Add(uriBuilder.Uri, pCookie);
592+
}
588593
}
589594
}
590595
break;
@@ -608,6 +613,16 @@ void setHeaders(HttpRequestMessage request, CookieContainer cookies)
608613
}
609614
InferContentType(contentType, request);
610615
}
616+
Cookie ParseCookie(string cookie, string domain)
617+
{
618+
string[] values = cookie.TrimEnd(';').Split('=');
619+
if (values.Length >= 2) {
620+
string cookieName = values[0].Trim();
621+
string cookieValue = values[1];
622+
return new Cookie(cookieName, cookieValue) { Domain = domain };
623+
}
624+
return null;
625+
}
611626
void AddHeader(HttpRequestHeaders headers, string headerName, string headerValue)
612627
{
613628
headers.TryAddWithoutValidation(headerName, headerValue);

dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ public void HttpClientInvalidURLWithCustomPort()
4545
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode);
4646
}
4747
}
48+
49+
[Fact]
50+
public void HttpClientCookieHeader()
51+
{
52+
string headerValue = "CognitoIdentityServiceProvider.3tgmin25m9bkg6vgi7vpavu7a9.M00000936.refreshToken=eyJjdHkiOiJKV1QiLCJlbmMiSkRCAmMpYqndvORnWLTfHw; CognitoIdentityServiceProvider.3tgmin25m9bkg6vgi7vpavu7a9.LastAuthUser=M00000936";
53+
string headerName = "Cookie";
54+
using (GxHttpClient httpclient = new GxHttpClient())
55+
{
56+
httpclient.AddHeader(headerName, headerValue);
57+
httpclient.Host = "localhost";
58+
httpclient.Port = 80;
59+
httpclient.BaseURL = @"NotFound/NotFound.php";
60+
httpclient.HttpClientExecute("GET", string.Empty);
61+
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode);
62+
}
63+
using (GxHttpClient oldHttpclient = new GxHttpClient())
64+
{
65+
oldHttpclient.AddHeader(headerName, headerValue);
66+
oldHttpclient.Host = "localhost";
67+
oldHttpclient.Port = 80;
68+
oldHttpclient.BaseURL = @"NotFound/NotFound.php";
69+
oldHttpclient.Execute("GET", string.Empty);
70+
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), oldHttpclient.StatusCode);
71+
}
72+
73+
}
4874
#if !NETCORE
4975
[Fact]
5076
public void NoStoreHeader()

0 commit comments

Comments
 (0)