@@ -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,54 +175,56 @@ 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 ( ) ) ;
187- bufferDup . Seek ( 0 , SeekOrigin . Begin ) ;
186+ if ( retries > MAX_REDIRECTS )
187+ {
188+ throw new Exception ( "too many redirects or authentication replays" ) ;
189+ }
188190
189- request . Content = new StreamContent ( bufferDup ) ;
190- request . Content . Headers . Add ( "Content-Type" , ContentType ) ;
191- }
191+ if ( IsPost && postBuffer . Length > 0 )
192+ {
193+ var bufferDup = new MemoryStream ( postBuffer . GetBuffer ( ) ) ;
194+ bufferDup . Seek ( 0 , SeekOrigin . Begin ) ;
192195
193- var response = httpClient . SendAsync ( request , HttpCompletionOption . ResponseContentRead ) . GetAwaiter ( ) . GetResult ( ) ;
196+ request . Content = new StreamContent ( bufferDup ) ;
197+ request . Content . Headers . Add ( "Content-Type" , ContentType ) ;
198+ }
194199
195- if ( response . StatusCode == HttpStatusCode . OK )
196- {
197- return response ;
198- }
199- else if ( response . StatusCode == HttpStatusCode . Unauthorized )
200- {
201- Credentials cred ;
202- int ret = SmartTransport . AcquireCredentials ( out cred , null , typeof ( UsernamePasswordCredentials ) ) ;
200+ var response = httpClient . SendAsync ( request , HttpCompletionOption . ResponseContentRead ) . GetAwaiter ( ) . GetResult ( ) ;
203201
204- if ( ret != 0 )
202+ if ( response . StatusCode == HttpStatusCode . OK )
205203 {
206- throw new InvalidOperationException ( "authentication cancelled" ) ;
204+ return response ;
207205 }
206+ else if ( response . StatusCode == HttpStatusCode . Unauthorized )
207+ {
208+ Credentials cred ;
209+ int ret = SmartTransport . AcquireCredentials ( out cred , null , typeof ( UsernamePasswordCredentials ) ) ;
208210
209- UsernamePasswordCredentials userpass = ( UsernamePasswordCredentials ) cred ;
210- credentials = new NetworkCredential ( userpass . Username , userpass . Password ) ;
211- continue ;
212- }
213- else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
214- {
215- url = new Uri ( response . Headers . GetValues ( "Location" ) . First ( ) ) ;
216- continue ;
217- }
211+ if ( ret != 0 )
212+ {
213+ throw new InvalidOperationException ( "authentication cancelled" ) ;
214+ }
215+
216+ UsernamePasswordCredentials userpass = ( UsernamePasswordCredentials ) cred ;
217+ credentials = new NetworkCredential ( userpass . Username , userpass . Password ) ;
218+ continue ;
219+ }
220+ else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
221+ {
222+ url = new Uri ( response . Headers . GetValues ( "Location" ) . First ( ) ) ;
223+ continue ;
224+ }
218225
219- throw new Exception ( string . Format ( "unexpected HTTP response: {0}" , response . StatusCode ) ) ;
226+ throw new Exception ( string . Format ( "unexpected HTTP response: {0}" , response . StatusCode ) ) ;
227+ }
220228 }
221229 }
222230
@@ -265,12 +273,6 @@ protected override void Free()
265273 response = null ;
266274 }
267275
268- if ( httpClient != null )
269- {
270- httpClient . Dispose ( ) ;
271- httpClient = null ;
272- }
273-
274276 base . Free ( ) ;
275277 }
276278 }
0 commit comments