@@ -197,32 +197,25 @@ public HttpClient CreateClient()
197
197
#endif
198
198
}
199
199
200
- _trace . WriteLine ( $ "Custom cookie file has been enabled with { _settings . CustomCookieFilePath } ") ;
201
200
// 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 ) )
203
202
{
204
203
// get the filename from gitconfig
205
204
string cookieFilePath = _settings . CustomCookieFilePath ;
206
205
_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
- }
215
206
216
207
// get cookie from cookie file
208
+ string cookieFileContents = _fileSystem . ReadAllText ( cookieFilePath ) ;
209
+
217
210
var cookieParser = new CurlCookieParser ( _trace ) ;
218
- var cookies = cookieParser . Parse ( _fileSystem . ReadAllText ( cookieFilePath ) ) ;
211
+ var cookies = cookieParser . Parse ( cookieFileContents ) ;
219
212
220
213
// Set the cookie
221
214
var cookieContainer = new CookieContainer ( ) ;
222
215
foreach ( var cookie in cookies )
223
216
{
224
217
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 ;
226
219
cookieContainer . Add ( uri , new Cookie ( cookie . Name , cookie . Value ) ) ;
227
220
}
228
221
handler . CookieContainer = cookieContainer ;
@@ -347,35 +340,48 @@ public CurlCookieParser(ITrace trace)
347
340
348
341
public IList < Cookie > Parse ( string content )
349
342
{
343
+ if ( string . IsNullOrWhiteSpace ( content ) )
344
+ {
345
+ return Array . Empty < Cookie > ( ) ;
346
+ }
347
+
348
+ const string HttpOnlyPrefix = "#HttpOnly_" ;
349
+
350
350
var cookies = new List < Cookie > ( ) ;
351
351
352
352
// Parse the cookie file content
353
353
var lines = content . Split ( new [ ] { '\r ' , '\n ' } , StringSplitOptions . RemoveEmptyEntries ) ;
354
- for ( int i = 3 ; i < lines . Length ; i ++ )
354
+ foreach ( var line in lines )
355
355
{
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 ) ) )
358
358
{
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 ] ;
362
360
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 ] ) ;
366
368
var name = parts [ 5 ] ;
367
369
var value = parts [ 6 ] ;
368
370
369
- cookies . Add ( new Cookie ( name , value , path , domain )
371
+ cookies . Add ( new Cookie ( )
370
372
{
371
- Expires = ParseExpires ( expires ) ,
373
+ Domain = domain ,
374
+ Path = path ,
375
+ Expires = expires ,
376
+ HttpOnly = true ,
372
377
Secure = secureOnly ,
373
- HttpOnly = true
378
+ Name = name ,
379
+ Value = value ,
374
380
} ) ;
375
381
}
376
382
else
377
383
{
378
- _trace . WriteLine ( $ "Invalid cookie line: { lines [ i ] } ") ;
384
+ _trace . WriteLine ( $ "Invalid cookie line: { line } ") ;
379
385
}
380
386
}
381
387
@@ -384,18 +390,18 @@ public IList<Cookie> Parse(string content)
384
390
385
391
private static DateTime ParseExpires ( string expires )
386
392
{
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 ) )
396
400
{
397
- return DateTime . MaxValue ;
401
+ return epoch . AddSeconds ( i ) ;
398
402
}
403
+
404
+ return epoch ;
399
405
}
400
406
}
401
407
}
0 commit comments