@@ -61,7 +61,7 @@ public TooManyRedirectsException(int redirect, Uri uri)
61
61
this . uri = uri ;
62
62
}
63
63
64
- public TooManyRedirectsException ( ) : base ( ) { }
64
+ public TooManyRedirectsException ( ) { }
65
65
66
66
public TooManyRedirectsException ( string message ) : base ( message ) { }
67
67
@@ -91,7 +91,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
91
91
[ Serializable ]
92
92
public class BadServerResponseException : Exception
93
93
{
94
- public BadServerResponseException ( ) : base ( ) { }
94
+ public BadServerResponseException ( ) { }
95
95
96
96
public BadServerResponseException ( string message ) : base ( message ) { }
97
97
@@ -105,7 +105,7 @@ protected BadServerResponseException(SerializationInfo info, StreamingContext co
105
105
[ Serializable ]
106
106
public class CancelledException : Exception
107
107
{
108
- public CancelledException ( ) : base ( ) { }
108
+ public CancelledException ( ) { }
109
109
110
110
public CancelledException ( string message ) : base ( message ) { }
111
111
@@ -118,7 +118,7 @@ protected CancelledException(SerializationInfo info, StreamingContext context) :
118
118
[ Serializable ]
119
119
public class ProxyServerAuthenticationException : Exception
120
120
{
121
- public ProxyServerAuthenticationException ( ) : base ( ) { }
121
+ public ProxyServerAuthenticationException ( ) { }
122
122
123
123
public ProxyServerAuthenticationException ( string message ) : base ( message ) { }
124
124
@@ -142,6 +142,9 @@ protected ProxyServerAuthenticationException(SerializationInfo info, StreamingCo
142
142
public const int DEFAULT_HTTPS_PORT = 443 ;
143
143
private const int NONCE_LENGTH = 16 ;
144
144
145
+ private const int FILE_MOVE_MAX_RETRIES = 5 ;
146
+ private const int FILE_MOVE_SLEEP_BETWEEN_RETRIES = 100 ;
147
+
145
148
public enum ProxyAuthenticationMethod
146
149
{
147
150
Basic = 0 ,
@@ -158,7 +161,7 @@ public enum ProxyAuthenticationMethod
158
161
159
162
private static void WriteLine ( String txt , Stream stream )
160
163
{
161
- byte [ ] bytes = System . Text . Encoding . ASCII . GetBytes ( String . Format ( "{0 }\r \n ", txt ) ) ;
164
+ byte [ ] bytes = Encoding . ASCII . GetBytes ( $ " { txt } \r \n ") ;
162
165
stream . Write ( bytes , 0 , bytes . Length ) ;
163
166
}
164
167
@@ -173,7 +176,7 @@ private static void WriteLine(Stream stream)
173
176
// done here.
174
177
private static string ReadLine ( Stream stream )
175
178
{
176
- System . Text . StringBuilder result = new StringBuilder ( ) ;
179
+ StringBuilder result = new StringBuilder ( ) ;
177
180
while ( true )
178
181
{
179
182
int b = stream . ReadByte ( ) ;
@@ -217,9 +220,8 @@ private static bool ReadHttpHeaders(ref Stream stream, IWebProxy proxy, bool nod
217
220
// read chunk size
218
221
string chunkSizeStr = ReadLine ( stream ) ;
219
222
chunkSizeStr = chunkSizeStr . TrimEnd ( '\r ' , '\n ' ) ;
220
- int chunkSize = 0 ;
221
223
int . TryParse ( chunkSizeStr , System . Globalization . NumberStyles . HexNumber ,
222
- System . Globalization . CultureInfo . InvariantCulture , out chunkSize ) ;
224
+ System . Globalization . CultureInfo . InvariantCulture , out var chunkSize ) ;
223
225
224
226
// read <chunkSize> number of bytes from the stream
225
227
int totalNumberOfBytesRead = 0 ;
@@ -231,8 +233,8 @@ private static bool ReadHttpHeaders(ref Stream stream, IWebProxy proxy, bool nod
231
233
totalNumberOfBytesRead += numberOfBytesRead ;
232
234
} while ( numberOfBytesRead > 0 && totalNumberOfBytesRead < chunkSize ) ;
233
235
234
- string str = System . Text . Encoding . ASCII . GetString ( bytes ) ;
235
- string [ ] split = str . Split ( new string [ ] { "\r \n " } , StringSplitOptions . RemoveEmptyEntries ) ;
236
+ string str = Encoding . ASCII . GetString ( bytes ) ;
237
+ string [ ] split = str . Split ( new [ ] { "\r \n " } , StringSplitOptions . RemoveEmptyEntries ) ;
236
238
headers . AddRange ( split ) ;
237
239
238
240
entityBody += str ;
@@ -276,7 +278,7 @@ private static bool ReadHttpHeaders(ref Stream stream, IWebProxy proxy, bool nod
276
278
277
279
private static int getResultCode ( string line )
278
280
{
279
- string [ ] bits = line . Split ( new char [ ] { ' ' } ) ;
281
+ string [ ] bits = line . Split ( ' ' ) ;
280
282
return ( bits . Length < 2 ? 0 : Int32 . Parse ( bits [ 1 ] ) ) ;
281
283
}
282
284
@@ -426,7 +428,7 @@ public static Uri BuildUri(string hostname, string path, params object[] args)
426
428
427
429
private static string GetPartOrNull ( string str , int partIndex )
428
430
{
429
- string [ ] parts = str . Split ( new char [ ] { ' ' } , partIndex + 2 , StringSplitOptions . RemoveEmptyEntries ) ;
431
+ string [ ] parts = str . Split ( new [ ] { ' ' } , partIndex + 2 , StringSplitOptions . RemoveEmptyEntries ) ;
430
432
return partIndex < parts . Length - 1 ? parts [ partIndex ] : null ;
431
433
}
432
434
@@ -457,8 +459,7 @@ private static NetworkStream ConnectSocket(Uri uri, bool nodelay, int timeoutMs)
457
459
/// <param name="timeoutMs">Timeout, in ms. 0 for no timeout.</param>
458
460
public static Stream ConnectStream ( Uri uri , IWebProxy proxy , bool nodelay , int timeoutMs )
459
461
{
460
- IMockWebProxy mockProxy = proxy as IMockWebProxy ;
461
- if ( mockProxy != null )
462
+ if ( proxy is IMockWebProxy mockProxy )
462
463
return mockProxy . GetStream ( uri ) ;
463
464
464
465
Stream stream ;
@@ -478,7 +479,7 @@ public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int t
478
479
{
479
480
if ( useProxy )
480
481
{
481
- string line = string . Format ( "CONNECT {0 }:{1 } HTTP/1.0" , uri . Host , uri . Port ) ;
482
+ string line = $ "CONNECT { uri . Host } :{ uri . Port } HTTP/1.0";
482
483
WriteLine ( line , stream ) ;
483
484
WriteLine ( stream ) ;
484
485
@@ -490,8 +491,7 @@ public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int t
490
491
491
492
if ( UseSSL ( uri ) )
492
493
{
493
- SslStream sslStream = new SslStream ( stream , false ,
494
- new RemoteCertificateValidationCallback ( ValidateServerCertificate ) , null ) ;
494
+ SslStream sslStream = new SslStream ( stream , false , ValidateServerCertificate , null ) ;
495
495
sslStream . AuthenticateAsClient ( "" , null , SslProtocols . Tls | SslProtocols . Tls11 | SslProtocols . Tls12 , true ) ;
496
496
497
497
stream = sslStream ;
@@ -523,7 +523,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
523
523
}
524
524
525
525
if ( proxy . Credentials == null )
526
- throw new BadServerResponseException ( string . Format ( "Received error code {0 } from the server" , initialResponse [ 0 ] ) ) ;
526
+ throw new BadServerResponseException ( $ "Received error code { initialResponse [ 0 ] } from the server") ;
527
527
528
528
NetworkCredential credentials = proxy . Credentials . GetCredential ( uri , null ) ;
529
529
@@ -535,10 +535,9 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
535
535
if ( string . IsNullOrEmpty ( basicField ) )
536
536
throw new ProxyServerAuthenticationException ( "Basic authentication scheme is not supported/enabled by the proxy server." ) ;
537
537
538
- string authenticationFieldReply = string . Format ( "Proxy-Authorization: Basic {0}" ,
539
- Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( credentials . UserName + ":" + credentials . Password ) ) ) ;
538
+ var creds = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( credentials . UserName + ":" + credentials . Password ) ) ;
540
539
WriteLine ( header , stream ) ;
541
- WriteLine ( authenticationFieldReply , stream ) ;
540
+ WriteLine ( $ "Proxy-Authorization: Basic { creds } " , stream ) ;
542
541
WriteLine ( stream ) ;
543
542
}
544
543
else if ( CurrentProxyAuthenticationMethod == ProxyAuthenticationMethod . Digest )
@@ -548,9 +547,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
548
547
if ( string . IsNullOrEmpty ( digestField ) )
549
548
throw new ProxyServerAuthenticationException ( "Digest authentication scheme is not supported/enabled by the proxy server." ) ;
550
549
551
- string authenticationFieldReply = string . Format (
552
- "Proxy-Authorization: Digest username=\" {0}\" , uri=\" {1}:{2}\" " ,
553
- credentials . UserName , uri . Host , uri . Port ) ;
550
+ string authenticationFieldReply = $ "Proxy-Authorization: Digest username=\" { credentials . UserName } \" , uri=\" { uri . Host } :{ uri . Port } \" ";
554
551
555
552
int len = "Proxy-Authorization: Digest" . Length ;
556
553
string directiveString = digestField . Substring ( len , digestField . Length - len ) ;
@@ -571,19 +568,19 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
571
568
throw new ProxyServerAuthenticationException ( "Stale nonce in Digest authentication attempt." ) ;
572
569
break ;
573
570
case "realm=" :
574
- authenticationFieldReply += string . Format ( ", realm=\" {0} \" " , directives [ ++ i ] ) ;
571
+ authenticationFieldReply += $ ", realm=\" { directives [ ++ i ] } \" " ;
575
572
realm = directives [ i ] ;
576
573
break ;
577
574
case "nonce=" :
578
- authenticationFieldReply += string . Format ( ", nonce=\" {0} \" " , directives [ ++ i ] ) ;
575
+ authenticationFieldReply += $ ", nonce=\" { directives [ ++ i ] } \" " ;
579
576
nonce = directives [ i ] ;
580
577
break ;
581
578
case "opaque=" :
582
- authenticationFieldReply += string . Format ( ", opaque=\" {0} \" " , directives [ ++ i ] ) ;
579
+ authenticationFieldReply += $ ", opaque=\" { directives [ ++ i ] } \" " ;
583
580
opaque = directives [ i ] ;
584
581
break ;
585
582
case "algorithm=" :
586
- authenticationFieldReply += string . Format ( ", algorithm={0}" , directives [ ++ i ] ) ; //unquoted; see RFC7616-3.4
583
+ authenticationFieldReply += $ ", algorithm={ directives [ ++ i ] } " ; //unquoted; see RFC7616-3.4
587
584
algorithm = directives [ i ] ;
588
585
break ;
589
586
case "qop=" :
@@ -593,21 +590,20 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
593
590
qop = qops . FirstOrDefault ( q => q . ToLowerInvariant ( ) == "auth" ) ??
594
591
qops . FirstOrDefault ( q => q . ToLowerInvariant ( ) == "auth-int" ) ;
595
592
if ( qop == null )
596
- throw new ProxyServerAuthenticationException (
597
- "Digest authentication's quality-of-protection directive is not supported." ) ;
598
- authenticationFieldReply += string . Format ( ", qop={0}" , qop ) ; //unquoted; see RFC7616-3.4
593
+ throw new ProxyServerAuthenticationException ( "Digest authentication's quality-of-protection directive is not supported." ) ;
594
+ authenticationFieldReply += $ ", qop={ qop } "; //unquoted; see RFC7616-3.4
599
595
}
600
596
break ;
601
597
}
602
598
}
603
599
604
600
string clientNonce = GenerateNonce ( ) ;
605
601
if ( qop != null )
606
- authenticationFieldReply += string . Format ( ", cnonce=\" {0 }\" " , clientNonce ) ;
602
+ authenticationFieldReply += $ ", cnonce=\" { clientNonce } \" ";
607
603
608
604
string nonceCount = "00000001" ; // todo: track nonces and their corresponding nonce counts
609
605
if ( qop != null )
610
- authenticationFieldReply += string . Format ( ", nc={0}" , nonceCount ) ; //unquoted; see RFC7616-3.4
606
+ authenticationFieldReply += $ ", nc={ nonceCount } " ; //unquoted; see RFC7616-3.4
611
607
612
608
Func < string , string > algFunc ;
613
609
var scratch1 = string . Join ( ":" , credentials . UserName , realm , credentials . Password ) ;
@@ -645,7 +641,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
645
641
: new [ ] { HA1 , nonce , nonceCount , clientNonce , qop , HA2 } ;
646
642
var response = algFunc ( string . Join ( ":" , array3 ) ) ;
647
643
648
- authenticationFieldReply += string . Format ( ", response=\" {0 }\" " , response ) ;
644
+ authenticationFieldReply += $ ", response=\" { response } \" ";
649
645
650
646
WriteLine ( header , stream ) ;
651
647
WriteLine ( authenticationFieldReply , stream ) ;
@@ -654,8 +650,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
654
650
else
655
651
{
656
652
string authType = GetPartOrNull ( fields [ 0 ] , 1 ) ;
657
- throw new ProxyServerAuthenticationException (
658
- string . Format ( "Proxy server's {0} authentication method is not supported." , authType ?? "chosen" ) ) ;
653
+ throw new ProxyServerAuthenticationException ( $ "Proxy server's { authType ?? "chosen" } authentication method is not supported.") ;
659
654
}
660
655
661
656
// handle authentication attempt response
@@ -671,12 +666,10 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
671
666
case 407 :
672
667
throw new ProxyServerAuthenticationException ( "Proxy server denied access due to wrong credentials." ) ;
673
668
default :
674
- throw new BadServerResponseException ( string . Format (
675
- "Received error code {0} from the server" , authenticatedResponse [ 0 ] ) ) ;
669
+ throw new BadServerResponseException ( $ "Received error code { authenticatedResponse [ 0 ] } from the server") ;
676
670
}
677
671
}
678
672
679
-
680
673
private static Stream DoHttp ( Uri uri , IWebProxy proxy , bool noDelay , int timeoutMs , params string [ ] headers )
681
674
{
682
675
Stream stream = ConnectStream ( uri , proxy , noDelay , timeoutMs ) ;
@@ -838,9 +831,6 @@ public static void Get(DataCopiedDelegate dataCopiedDelegate, FuncBool cancellin
838
831
}
839
832
}
840
833
841
- private const int FILE_MOVE_MAX_RETRIES = 5 ;
842
- private const int FILE_MOVE_SLEEP_BETWEEN_RETRIES = 100 ;
843
-
844
834
/// <summary>
845
835
/// Move a file, retrying a few times with a short sleep between retries.
846
836
/// If it still fails after these retries, then throw the error.
0 commit comments