@@ -53,18 +53,24 @@ private class ManagedHttpSmartSubtransportStream : SmartSubtransportStream
5353 private HttpResponseMessage response ;
5454 private Stream responseStream ;
5555
56- private HttpClientHandler httpClientHandler ;
57- private HttpClient httpClient ;
58-
5956 public ManagedHttpSmartSubtransportStream ( ManagedHttpSmartSubtransport parent , string endpointUrl , bool isPost , string contentType )
6057 : base ( parent )
6158 {
6259 EndpointUrl = new Uri ( endpointUrl ) ;
6360 IsPost = isPost ;
6461 ContentType = contentType ;
62+ }
6563
66- httpClientHandler = CreateClientHandler ( ) ;
67- httpClient = new HttpClient ( httpClientHandler ) ;
64+ private HttpClient CreateHttpClient ( HttpMessageHandler handler )
65+ {
66+ return new HttpClient ( handler )
67+ {
68+ DefaultRequestHeaders =
69+ {
70+ // This worked fine when it was on, but git.exe doesn't specify this header, so we don't either.
71+ ExpectContinue = false ,
72+ } ,
73+ } ;
6874 }
6975
7076 private HttpClientHandler CreateClientHandler ( )
@@ -132,7 +138,7 @@ private bool CertificateValidationProxy(object sender, X509Certificate cert, X50
132138
133139 return true ;
134140 }
135- catch ( Exception e )
141+ catch ( Exception e )
136142 {
137143 SetError ( e ) ;
138144 return false ;
@@ -169,53 +175,55 @@ private HttpResponseMessage GetResponseWithRedirects()
169175
170176 for ( retries = 0 ; ; retries ++ )
171177 {
172- var httpClientHandler = CreateClientHandler ( ) ;
173- httpClientHandler . Credentials = credentials ;
174-
175- using ( var httpClient = new HttpClient ( httpClientHandler ) )
178+ using ( var httpClientHandler = CreateClientHandler ( ) )
176179 {
177- var request = CreateRequest ( url , IsPost , ContentType ) ;
180+ httpClientHandler . Credentials = credentials ;
178181
179- if ( retries > MAX_REDIRECTS )
182+ using ( var httpClient = this . CreateHttpClient ( httpClientHandler ) )
180183 {
181- throw new Exception ( "too many redirects or authentication replays" ) ;
182- }
184+ var request = CreateRequest ( url , IsPost , ContentType ) ;
183185
184- if ( IsPost && postBuffer . Length > 0 )
185- {
186- var bufferDup = new MemoryStream ( postBuffer . GetBuffer ( ) , 0 , ( int ) postBuffer . Length ) ;
186+ if ( retries > MAX_REDIRECTS )
187+ {
188+ throw new Exception ( "too many redirects or authentication replays" ) ;
189+ }
187190
188- request . Content = new StreamContent ( bufferDup ) ;
189- request . Content . Headers . Add ( "Content-Type" , ContentType ) ;
190- }
191+ if ( IsPost && postBuffer . Length > 0 )
192+ {
193+ var bufferDup = new MemoryStream ( postBuffer . GetBuffer ( ) , 0 , ( int ) postBuffer . Length ) ;
191194
192- var response = httpClient . SendAsync ( request , HttpCompletionOption . ResponseContentRead ) . GetAwaiter ( ) . GetResult ( ) ;
195+ request . Content = new StreamContent ( bufferDup ) ;
196+ request . Content . Headers . Add ( "Content-Type" , ContentType ) ;
197+ }
193198
194- if ( response . StatusCode == HttpStatusCode . OK )
195- {
196- return response ;
197- }
198- else if ( response . StatusCode == HttpStatusCode . Unauthorized )
199- {
200- Credentials cred ;
201- int ret = SmartTransport . AcquireCredentials ( out cred , null , typeof ( UsernamePasswordCredentials ) ) ;
199+ var response = httpClient . SendAsync ( request , HttpCompletionOption . ResponseContentRead ) . GetAwaiter ( ) . GetResult ( ) ;
202200
203- if ( ret != 0 )
201+ if ( response . StatusCode == HttpStatusCode . OK )
204202 {
205- throw new InvalidOperationException ( "authentication cancelled" ) ;
203+ return response ;
206204 }
205+ else if ( response . StatusCode == HttpStatusCode . Unauthorized )
206+ {
207+ Credentials cred ;
208+ int ret = SmartTransport . AcquireCredentials ( out cred , null , typeof ( UsernamePasswordCredentials ) ) ;
207209
208- UsernamePasswordCredentials userpass = ( UsernamePasswordCredentials ) cred ;
209- credentials = new NetworkCredential ( userpass . Username , userpass . Password ) ;
210- continue ;
211- }
212- else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
213- {
214- url = new Uri ( response . Headers . GetValues ( "Location" ) . First ( ) ) ;
215- continue ;
216- }
210+ if ( ret != 0 )
211+ {
212+ throw new InvalidOperationException ( "authentication cancelled" ) ;
213+ }
214+
215+ UsernamePasswordCredentials userpass = ( UsernamePasswordCredentials ) cred ;
216+ credentials = new NetworkCredential ( userpass . Username , userpass . Password ) ;
217+ continue ;
218+ }
219+ else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
220+ {
221+ url = new Uri ( response . Headers . GetValues ( "Location" ) . First ( ) ) ;
222+ continue ;
223+ }
217224
218- throw new Exception ( string . Format ( "unexpected HTTP response: {0}" , response . StatusCode ) ) ;
225+ throw new Exception ( string . Format ( "unexpected HTTP response: {0}" , response . StatusCode ) ) ;
226+ }
219227 }
220228 }
221229
@@ -264,12 +272,6 @@ protected override void Free()
264272 response = null ;
265273 }
266274
267- if ( httpClient != null )
268- {
269- httpClient . Dispose ( ) ;
270- httpClient = null ;
271- }
272-
273275 base . Free ( ) ;
274276 }
275277 }
0 commit comments