@@ -44,32 +44,36 @@ public void Abort()
4444
4545 public async Task ConnectAsync ( Uri uri , HttpMessageInvoker ? invoker , CancellationToken cancellationToken , ClientWebSocketOptions options )
4646 {
47- invoker ??= new HttpMessageInvoker ( SetupHandler ( options ) ) ;
47+ bool disposeHandler = false ;
48+ invoker ??= new HttpMessageInvoker ( SetupHandler ( options , out disposeHandler ) ) ;
4849 HttpResponseMessage ? response = null ;
4950
50- // TODO setup to false
51- bool disposeHandler = true ;
51+ bool tryDowngrade = false ;
5252 try
5353 {
54- if ( options . HttpVersion . Major >= 3 && options . HttpVersionPolicy != HttpVersionPolicy . RequestVersionOrLower )
55- {
56- throw new Exception ( ) ;
57- }
58-
59- var request = new HttpRequestMessage ( HttpMethod . Get , uri ) ;
60- if ( options . HttpVersion . Major >= 2 || ( options . HttpVersionPolicy == HttpVersionPolicy . RequestVersionOrHigher ) )
61- {
62- request . Version = new Version ( 2 , 0 ) ;
63- }
64- else
65- {
66- request . Version = new Version ( 1 , 1 ) ;
67- }
6854
6955 while ( true )
7056 {
7157 try
7258 {
59+ HttpRequestMessage request ;
60+ if ( ! tryDowngrade && options . HttpVersion == HttpVersion . Version20
61+ || ( options . HttpVersion == HttpVersion . Version11 && options . HttpVersionPolicy == HttpVersionPolicy . RequestVersionOrHigher ) )
62+ {
63+ request = new HttpRequestMessage ( HttpMethod . Connect , uri ) ;
64+ request . Version = new Version ( 2 , 0 ) ;
65+ tryDowngrade = true ;
66+ }
67+ else if ( tryDowngrade || options . HttpVersion == HttpVersion . Version11 )
68+ {
69+ request = new HttpRequestMessage ( HttpMethod . Get , uri ) ;
70+ request . Version = new Version ( 1 , 1 ) ;
71+ tryDowngrade = false ;
72+ }
73+ else
74+ {
75+ throw new WebSocketException ( WebSocketError . UnsupportedProtocol ) ;
76+ }
7377 if ( options . _requestHeaders ? . Count > 0 ) // use field to avoid lazily initializing the collection
7478 {
7579 foreach ( string key in options . RequestHeaders )
@@ -107,13 +111,11 @@ public async Task ConnectAsync(Uri uri, HttpMessageInvoker? invoker, Cancellatio
107111 }
108112 catch ( HttpRequestException ex )
109113 {
110- if ( ex . Data . Contains ( "SETTINGS_ENABLE_CONNECT_PROTOCOL" ) && request . Version . Major == 2
111- && ( options . HttpVersion . Major == 2 && options . HttpVersionPolicy == HttpVersionPolicy . RequestVersionOrLower
112- || options . HttpVersion . Major == 1 && options . HttpVersionPolicy == HttpVersionPolicy . RequestVersionOrHigher ) )
114+ if ( ! ex . Data . Contains ( "SETTINGS_ENABLE_CONNECT_PROTOCOL" ) || ! tryDowngrade
115+ || ( options . HttpVersion != HttpVersion . Version11 && options . HttpVersionPolicy != HttpVersionPolicy . RequestVersionOrLower ) )
113116 {
114- request . Version = new Version ( 1 , 1 ) ;
117+ throw ex ;
115118 }
116- else { throw ex ; }
117119 }
118120
119121 }
@@ -205,7 +207,7 @@ public async Task ConnectAsync(Uri uri, HttpMessageInvoker? invoker, Cancellatio
205207 }
206208 }
207209
208- private static SocketsHttpHandler SetupHandler ( ClientWebSocketOptions options )
210+ private static SocketsHttpHandler SetupHandler ( ClientWebSocketOptions options , out bool disposeHandler )
209211 {
210212 SocketsHttpHandler ? handler ;
211213 // Create the handler for this request and populate it with all of the options.
@@ -218,6 +220,7 @@ private static SocketsHttpHandler SetupHandler(ClientWebSocketOptions options)
218220 options . RemoteCertificateValidationCallback == null &&
219221 options . _clientCertificates ? . Count == 0 )
220222 {
223+ disposeHandler = false ;
221224 handler = s_defaultHandler ;
222225 if ( handler == null )
223226 {
@@ -236,6 +239,7 @@ private static SocketsHttpHandler SetupHandler(ClientWebSocketOptions options)
236239 }
237240 else
238241 {
242+ disposeHandler = true ;
239243 handler = new SocketsHttpHandler ( ) ;
240244 handler . PooledConnectionLifetime = TimeSpan . Zero ;
241245 handler . CookieContainer = options . Cookies ;
@@ -424,7 +428,7 @@ private static void ValidateResponse(HttpResponseMessage response, string secVal
424428 {
425429 if ( response . StatusCode != HttpStatusCode . OK )
426430 {
427- throw new WebSocketException ( WebSocketError . NotAWebSocket , SR . Format ( SR . net_WebSockets_Connect101Expected , ( int ) response . StatusCode ) ) ;
431+ throw new WebSocketException ( WebSocketError . NotAWebSocket , SR . Format ( SR . net_WebSockets_Connect200Expected , ( int ) response . StatusCode ) ) ;
428432 }
429433 }
430434
0 commit comments