-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix redirect timeout on renamed repositories #1411
Merged
ryangribble
merged 20 commits into
octokit:master
from
martinscholz83:fix-timeout-getting-multiple-repositories
Aug 14, 2016
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dcf1cf0
Merge remote-tracking branch 'refs/remotes/octokit/master'
martinscholz83 bce9696
add test
martinscholz83 61dfff8
Merge remote-tracking branch 'refs/remotes/octokit/master'
martinscholz83 dd38e4c
Merge remote-tracking branch 'refs/remotes/octokit/master' into fix-t…
martinscholz83 dc65dab
Merge remote-tracking branch 'refs/remotes/octokit/master'
martinscholz83 f6191d2
Merge branch 'master' of https://github.com/maddin2016/octokit.net
martinscholz83 7f8cfba
Merge remote-tracking branch 'refs/remotes/octokit/master' into fix-t…
martinscholz83 84f0e7b
[WIP]
martinscholz83 b4d0ea9
put logic for redirects outside of delegating handler
martinscholz83 d2748c3
change send method
martinscholz83 f3efff1
format code
martinscholz83 b42240b
reorganized http client adapter
martinscholz83 c85508e
Merge remote-tracking branch 'refs/remotes/octokit/master' into fix-t…
martinscholz83 e6309cb
change HttpClientAdapter
martinscholz83 54225c9
rework http redirect tests - still an issue with accessing the respon…
ryangribble efe587b
Merge remote-tracking branch 'refs/remotes/octokit/master' into fix-t…
martinscholz83 2f4e03e
Merge branch 'fix-timeout-getting-multiple-repositories' of https://g…
martinscholz83 ff58f05
Merge remote-tracking branch 'refs/remotes/octokit/master' into fix-t…
martinscholz83 802091c
remove some unused lines in httpclientadapter
martinscholz83 815f0d1
Reworked redirect implementation to fully clone http request and re-u…
ryangribble File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ public class HttpClientAdapter : IHttpClient | |
{ | ||
readonly HttpClient _http; | ||
|
||
public const string RedirectCountKey = "RedirectCount"; | ||
|
||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] | ||
public HttpClientAdapter(Func<HttpMessageHandler> getHandler) | ||
{ | ||
|
@@ -42,8 +44,8 @@ public async Task<IResponse> Send(IRequest request, CancellationToken cancellati | |
|
||
using (var requestMessage = BuildRequestMessage(request)) | ||
{ | ||
// Make the request | ||
var responseMessage = await _http.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, cancellationTokenForRequest).ConfigureAwait(false); | ||
var responseMessage = await SendAsync(requestMessage, cancellationTokenForRequest).ConfigureAwait(false); | ||
|
||
return await BuildResponse(responseMessage).ConfigureAwait(false); | ||
} | ||
} | ||
|
@@ -168,18 +170,30 @@ protected virtual void Dispose(bool disposing) | |
if (_http != null) _http.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
internal class RedirectHandler : DelegatingHandler | ||
{ | ||
public const string RedirectCountKey = "RedirectCount"; | ||
public bool Enabled { get; set; } | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||
StreamContent savedContent = null; | ||
bool unbuffered = false; | ||
if (request.Content != null && request.Content.Headers.ContentLength != 0) | ||
{ | ||
var stream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false); | ||
if (stream.CanSeek) | ||
{ | ||
stream.Position = 0; | ||
savedContent = new StreamContent(stream); | ||
} | ||
else | ||
{ | ||
unbuffered = true; | ||
//throw new Exception("Cannot redirect a request with an unbuffered body"); | ||
//savedContent = null; | ||
} | ||
} | ||
|
||
var response = await _http.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false); | ||
|
||
// Can't redirect without somewhere to redirect too. Throw? | ||
// Can't redirect without somewhere to redirect to. Throw? | ||
if (response.Headers.Location == null) return response; | ||
|
||
// Don't redirect if we exceed max number of redirects | ||
|
@@ -210,25 +224,18 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage | |
} | ||
else | ||
{ | ||
if (request.Content != null && request.Content.Headers.ContentLength != 0) | ||
if (unbuffered) | ||
{ | ||
var stream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false); | ||
if (stream.CanSeek) | ||
{ | ||
stream.Position = 0; | ||
} | ||
else | ||
{ | ||
throw new Exception("Cannot redirect a request with an unbuffered body"); | ||
} | ||
newRequest.Content = new StreamContent(stream); | ||
throw new Exception("Cannot redirect a request with an unbuffered body"); | ||
} | ||
newRequest.Content = savedContent; | ||
} | ||
newRequest.RequestUri = response.Headers.Location; | ||
if (string.Compare(newRequest.RequestUri.Host, request.RequestUri.Host, StringComparison.OrdinalIgnoreCase) != 0) | ||
{ | ||
newRequest.Headers.Authorization = null; | ||
} | ||
|
||
response = await SendAsync(newRequest, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
|
@@ -238,18 +245,28 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage | |
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] | ||
private static HttpRequestMessage CopyRequest(HttpRequestMessage oldRequest) | ||
{ | ||
var newrequest = new HttpRequestMessage(oldRequest.Method, oldRequest.RequestUri); | ||
var newRequest = new HttpRequestMessage(oldRequest.Method, oldRequest.RequestUri); | ||
|
||
foreach (var header in oldRequest.Headers) | ||
{ | ||
newrequest.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
newRequest.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
} | ||
foreach (var property in oldRequest.Properties) | ||
{ | ||
newrequest.Properties.Add(property); | ||
newRequest.Properties.Add(property); | ||
} | ||
|
||
return newrequest; | ||
return newRequest; | ||
} | ||
} | ||
|
||
internal class RedirectHandler : DelegatingHandler | ||
{ | ||
public bool Enabled { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this value being used anywhere (I suspect it also wasn't used before). |
||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
return base.SendAsync(request, cancellationToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This override also doesn't seem necessary now. |
||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💄 let's 🔥 these commented lines