Skip to content

Commit b74f9ab

Browse files
miya789ldenningtonmjcheetham
committed
http: fix implementation of setting cookie
based on the review Co-authored-by: Lessley Dennington <[email protected]> Co-authored-by: Matthew John Cheetham <[email protected]>
1 parent b841181 commit b74f9ab

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

src/shared/Core/HttpClientFactory.cs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -197,32 +197,25 @@ public HttpClient CreateClient()
197197
#endif
198198
}
199199

200-
_trace.WriteLine($"Custom cookie file has been enabled with {_settings.CustomCookieFilePath}");
201200
// If IsUsingCookieEnabled is enabled, set Cookie header from cookie file, which is written by libcurl
202-
if (!string.IsNullOrWhiteSpace(_settings.CustomCookieFilePath))
201+
if (!string.IsNullOrWhiteSpace(_settings.CustomCookieFilePath) && _fileSystem.FileExists(_settings.CustomCookieFilePath))
203202
{
204203
// get the filename from gitconfig
205204
string cookieFilePath = _settings.CustomCookieFilePath;
206205
_trace.WriteLine($"Custom cookie file has been enabled with {cookieFilePath}");
207-
208-
// Throw exception if cookie file not found
209-
if (!_fileSystem.FileExists(cookieFilePath) || string.IsNullOrEmpty(_fileSystem.ReadAllText(cookieFilePath)))
210-
{
211-
var format = "Custom cookie file not found at path: {0}";
212-
var message = string.Format(format, cookieFilePath);
213-
throw new Trace2FileNotFoundException(_trace2, message, format, cookieFilePath);
214-
}
215206

216207
// get cookie from cookie file
208+
string cookieFileContents = _fileSystem.ReadAllText(cookieFilePath);
209+
217210
var cookieParser = new CurlCookieParser(_trace);
218-
var cookies = cookieParser.Parse(_fileSystem.ReadAllText(cookieFilePath));
211+
var cookies = cookieParser.Parse(cookieFileContents);
219212

220213
// Set the cookie
221214
var cookieContainer = new CookieContainer();
222215
foreach (var cookie in cookies)
223216
{
224217
var schema = cookie.Secure ? "https" : "http";
225-
var uri = new UriBuilder(schema, cookie.Domain).Uri;
218+
var uri = new UriBuilder(schema, cookie.Domain.TrimStart('.')).Uri;
226219
cookieContainer.Add(uri, new Cookie(cookie.Name, cookie.Value));
227220
}
228221
handler.CookieContainer = cookieContainer;
@@ -347,35 +340,48 @@ public CurlCookieParser(ITrace trace)
347340

348341
public IList<Cookie> Parse(string content)
349342
{
343+
if (string.IsNullOrWhiteSpace(content))
344+
{
345+
return Array.Empty<Cookie>();
346+
}
347+
348+
const string HttpOnlyPrefix = "#HttpOnly_";
349+
350350
var cookies = new List<Cookie>();
351351

352352
// Parse the cookie file content
353353
var lines = content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
354-
for (int i = 3; i < lines.Length; i++)
354+
foreach (var line in lines)
355355
{
356-
var parts = lines[i].Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
357-
if (parts.Length >= 7)
356+
var parts = line.Split(new[] { '\t' }, StringSplitOptions.None);
357+
if (parts.Length >= 7 && (!parts[0].StartsWith("#") || parts[0].StartsWith(HttpOnlyPrefix)))
358358
{
359-
var domain = parts[0];
360-
domain = domain.StartsWith("#HttpOnly_") ? parts[0].Substring("#HttpOnly_".Length) : parts[0];
361-
domain = domain.TrimStart('.');
359+
var domain = parts[0].StartsWith(HttpOnlyPrefix) ? parts[0].Substring(HttpOnlyPrefix.Length) : parts[0];
362360
var includeSubdomains = parts[1] == "TRUE";
363-
var path = parts[2];
364-
var secureOnly = parts[3] == "TRUE";
365-
var expires = parts[4];
361+
if (!includeSubdomains)
362+
{
363+
domain = domain.TrimStart('.');
364+
}
365+
var path = parts[2] == "" ? "/" : parts[2];
366+
var secureOnly = parts[3].Equals("TRUE", StringComparison.OrdinalIgnoreCase);
367+
var expires = ParseExpires(parts[4]);
366368
var name = parts[5];
367369
var value = parts[6];
368370

369-
cookies.Add(new Cookie(name, value, path, domain)
371+
cookies.Add(new Cookie()
370372
{
371-
Expires = ParseExpires(expires),
373+
Domain = domain,
374+
Path = path,
375+
Expires = expires,
376+
HttpOnly = true,
372377
Secure = secureOnly,
373-
HttpOnly = true
378+
Name = name,
379+
Value = value,
374380
});
375381
}
376382
else
377383
{
378-
_trace.WriteLine($"Invalid cookie line: {lines[i]}");
384+
_trace.WriteLine($"Invalid cookie line: {line}");
379385
}
380386
}
381387

@@ -384,18 +390,18 @@ public IList<Cookie> Parse(string content)
384390

385391
private static DateTime ParseExpires(string expires)
386392
{
387-
if (expires.Equals("0", StringComparison.OrdinalIgnoreCase))
388-
{
389-
return DateTime.MinValue;
390-
}
391-
else if (DateTime.TryParse(expires, out var result))
392-
{
393-
return result;
394-
}
395-
else
393+
#if NETFRAMEWORK
394+
DateTime epoch = new DateTime(1970, 01, 01, 0, 0, 0, DateTimeKind.Utc);
395+
#else
396+
DateTime epoch = DateTime.UnixEpoch;
397+
#endif
398+
399+
if (long.TryParse(expires, out long i))
396400
{
397-
return DateTime.MaxValue;
401+
return epoch.AddSeconds(i);
398402
}
403+
404+
return epoch;
399405
}
400406
}
401407
}

0 commit comments

Comments
 (0)