Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress Expect: 100 Continue header #1772

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 49 additions & 47 deletions LibGit2Sharp/Core/ManagedHttpSmartSubtransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ private class ManagedHttpSmartSubtransportStream : SmartSubtransportStream
private HttpResponseMessage response;
private Stream responseStream;

private HttpClientHandler httpClientHandler;
private HttpClient httpClient;

public ManagedHttpSmartSubtransportStream(ManagedHttpSmartSubtransport parent, string endpointUrl, bool isPost, string contentType)
: base(parent)
{
EndpointUrl = new Uri(endpointUrl);
IsPost = isPost;
ContentType = contentType;
}

httpClientHandler = CreateClientHandler();
httpClient = new HttpClient(httpClientHandler);
private HttpClient CreateHttpClient(HttpMessageHandler handler)
{
return new HttpClient(handler)
{
DefaultRequestHeaders =
{
// This worked fine when it was on, but git.exe doesn't specify this header, so we don't either.
ExpectContinue = false,
},
};
}

private HttpClientHandler CreateClientHandler()
Expand Down Expand Up @@ -132,7 +138,7 @@ private bool CertificateValidationProxy(object sender, X509Certificate cert, X50

return true;
}
catch(Exception e)
catch (Exception e)
{
SetError(e);
return false;
Expand Down Expand Up @@ -169,53 +175,55 @@ private HttpResponseMessage GetResponseWithRedirects()

for (retries = 0; ; retries++)
{
var httpClientHandler = CreateClientHandler();
httpClientHandler.Credentials = credentials;

using (var httpClient = new HttpClient(httpClientHandler))
using (var httpClientHandler = CreateClientHandler())
{
var request = CreateRequest(url, IsPost, ContentType);
httpClientHandler.Credentials = credentials;

if (retries > MAX_REDIRECTS)
using (var httpClient = this.CreateHttpClient(httpClientHandler))
{
throw new Exception("too many redirects or authentication replays");
}
var request = CreateRequest(url, IsPost, ContentType);

if (IsPost && postBuffer.Length > 0)
{
var bufferDup = new MemoryStream(postBuffer.GetBuffer(), 0, (int) postBuffer.Length);
if (retries > MAX_REDIRECTS)
{
throw new Exception("too many redirects or authentication replays");
}

request.Content = new StreamContent(bufferDup);
request.Content.Headers.Add("Content-Type", ContentType);
}
if (IsPost && postBuffer.Length > 0)
{
var bufferDup = new MemoryStream(postBuffer.GetBuffer(), 0, (int)postBuffer.Length);

var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).GetAwaiter().GetResult();
request.Content = new StreamContent(bufferDup);
request.Content.Headers.Add("Content-Type", ContentType);
}

if (response.StatusCode == HttpStatusCode.OK)
{
return response;
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Credentials cred;
int ret = SmartTransport.AcquireCredentials(out cred, null, typeof(UsernamePasswordCredentials));
var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).GetAwaiter().GetResult();

if (ret != 0)
if (response.StatusCode == HttpStatusCode.OK)
{
throw new InvalidOperationException("authentication cancelled");
return response;
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Credentials cred;
int ret = SmartTransport.AcquireCredentials(out cred, null, typeof(UsernamePasswordCredentials));

UsernamePasswordCredentials userpass = (UsernamePasswordCredentials)cred;
credentials = new NetworkCredential(userpass.Username, userpass.Password);
continue;
}
else if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
{
url = new Uri(response.Headers.GetValues("Location").First());
continue;
}
if (ret != 0)
{
throw new InvalidOperationException("authentication cancelled");
}

UsernamePasswordCredentials userpass = (UsernamePasswordCredentials)cred;
credentials = new NetworkCredential(userpass.Username, userpass.Password);
continue;
}
else if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
{
url = new Uri(response.Headers.GetValues("Location").First());
continue;
}

throw new Exception(string.Format("unexpected HTTP response: {0}", response.StatusCode));
throw new Exception(string.Format("unexpected HTTP response: {0}", response.StatusCode));
}
}
}

Expand Down Expand Up @@ -264,12 +272,6 @@ protected override void Free()
response = null;
}

if (httpClient != null)
{
httpClient.Dispose();
httpClient = null;
}

base.Free();
}
}
Expand Down