Skip to content

Commit 8c5e4dc

Browse files
Cookies with different domain to the requested URL were not added to the container.
1 parent e427380 commit 8c5e4dc

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,64 @@ internal static void ExtractCookies(this HttpResponseMessage response, CookieCon
105105
{
106106
cookieContainer.SetCookies(uri, cookieValue);
107107
}
108-
catch (CookieException ex)
108+
catch (CookieException)
109109
{
110-
GXLogging.Warn(log, $"Ignored cookie for container: {cookieValue} url:{uri}", ex);
110+
try
111+
{
112+
cookieContainer.Add(ParseCookieHeader(cookieValue));
113+
}
114+
catch (Exception ex2)
115+
{
116+
GXLogging.Warn(log, $"Ignored cookie for container: {cookieValue} url:{uri}", ex2.Message);
117+
}
111118
}
112119
}
113120

114121
}
115122
}
123+
static Cookie ParseCookieHeader(string cookieHeader)
124+
{
125+
string[] parts = cookieHeader.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
126+
if (parts.Length == 0)
127+
{
128+
throw new ArgumentException("Invalid cookie header.");
129+
}
130+
string[] nameValuePair = parts[0].Split('=', 2);
131+
if (nameValuePair.Length != 2)
132+
{
133+
throw new ArgumentException("Invalid cookie header: missing name or value.");
134+
}
135+
136+
string name = nameValuePair[0];
137+
string value = nameValuePair[1];
138+
139+
var cookie = new Cookie(name, value);
140+
141+
foreach (string part in parts[1..])
142+
{
143+
string[] attribute = part.Split('=', 2);
144+
string attributeName = attribute[0].ToLowerInvariant();
145+
146+
if (attributeName == "path" && attribute.Length == 2)
147+
{
148+
cookie.Path = attribute[1];
149+
}
150+
else if (attributeName == "domain" && attribute.Length == 2)
151+
{
152+
cookie.Domain = attribute[1];
153+
}
154+
else if (attributeName == "secure")
155+
{
156+
cookie.Secure = true;
157+
}
158+
else if (attributeName == "httponly")
159+
{
160+
cookie.HttpOnly = true;
161+
}
162+
}
163+
164+
return cookie;
165+
}
116166
}
117167
#endif
118168
public class HttpHelper

0 commit comments

Comments
 (0)