@@ -53,18 +53,24 @@ private class ManagedHttpSmartSubtransportStream : SmartSubtransportStream
53
53
private HttpResponseMessage response ;
54
54
private Stream responseStream ;
55
55
56
- private HttpClientHandler httpClientHandler ;
57
- private HttpClient httpClient ;
58
-
59
56
public ManagedHttpSmartSubtransportStream ( ManagedHttpSmartSubtransport parent , string endpointUrl , bool isPost , string contentType )
60
57
: base ( parent )
61
58
{
62
59
EndpointUrl = new Uri ( endpointUrl ) ;
63
60
IsPost = isPost ;
64
61
ContentType = contentType ;
62
+ }
65
63
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
+ } ;
68
74
}
69
75
70
76
private HttpClientHandler CreateClientHandler ( )
@@ -132,7 +138,7 @@ private bool CertificateValidationProxy(object sender, X509Certificate cert, X50
132
138
133
139
return true ;
134
140
}
135
- catch ( Exception e )
141
+ catch ( Exception e )
136
142
{
137
143
SetError ( e ) ;
138
144
return false ;
@@ -169,53 +175,55 @@ private HttpResponseMessage GetResponseWithRedirects()
169
175
170
176
for ( retries = 0 ; ; retries ++ )
171
177
{
172
- var httpClientHandler = CreateClientHandler ( ) ;
173
- httpClientHandler . Credentials = credentials ;
174
-
175
- using ( var httpClient = new HttpClient ( httpClientHandler ) )
178
+ using ( var httpClientHandler = CreateClientHandler ( ) )
176
179
{
177
- var request = CreateRequest ( url , IsPost , ContentType ) ;
180
+ httpClientHandler . Credentials = credentials ;
178
181
179
- if ( retries > MAX_REDIRECTS )
182
+ using ( var httpClient = this . CreateHttpClient ( httpClientHandler ) )
180
183
{
181
- throw new Exception ( "too many redirects or authentication replays" ) ;
182
- }
184
+ var request = CreateRequest ( url , IsPost , ContentType ) ;
183
185
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
+ }
187
190
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 ) ;
191
194
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
+ }
193
198
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 ( ) ;
202
200
203
- if ( ret != 0 )
201
+ if ( response . StatusCode == HttpStatusCode . OK )
204
202
{
205
- throw new InvalidOperationException ( "authentication cancelled" ) ;
203
+ return response ;
206
204
}
205
+ else if ( response . StatusCode == HttpStatusCode . Unauthorized )
206
+ {
207
+ Credentials cred ;
208
+ int ret = SmartTransport . AcquireCredentials ( out cred , null , typeof ( UsernamePasswordCredentials ) ) ;
207
209
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
+ }
217
224
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
+ }
219
227
}
220
228
}
221
229
@@ -264,12 +272,6 @@ protected override void Free()
264
272
response = null ;
265
273
}
266
274
267
- if ( httpClient != null )
268
- {
269
- httpClient . Dispose ( ) ;
270
- httpClient = null ;
271
- }
272
-
273
275
base . Free ( ) ;
274
276
}
275
277
}
0 commit comments