-
Notifications
You must be signed in to change notification settings - Fork 564
Reading Error response properly in AndroidClientHandler #180
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
Merged
jonpryor
merged 2 commits into
dotnet:master
from
smstuebe:client-handler-error-response
Aug 26, 2016
+39
−21
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -268,10 +268,10 @@ protected void AssertSelf () | |
| // idea. We'll return the response message with all the information required by the | ||
| // application to fill in the blanks and provide the requested credentials instead. | ||
| // | ||
| // We should return the body of the response too but, alas, the Java client will throw | ||
| // a, wait for it, FileNotFound exception if we attempt to access the input stream. So | ||
| // no body, just a dummy. Java FTW! | ||
| ret.Content = new StringContent ("Unauthorized", Encoding.ASCII); | ||
| // We return the body of the response too, but the Java client will throw | ||
| // a FileNotFound exception if we attempt to access the input stream. | ||
| // Instead we try to read the error stream and return an default message if the error stream isn't readable. | ||
| ret.Content = GetErrorContent (httpConnection, new StringContent ("Unauthorized", Encoding.ASCII)); | ||
| CopyHeaders (httpConnection, ret); | ||
|
|
||
| if (ret.Headers.WwwAuthenticate != null) { | ||
|
|
@@ -285,28 +285,20 @@ protected void AssertSelf () | |
| ret.RequestedAuthentication = RequestedAuthentication; | ||
| return ret; | ||
| } | ||
|
|
||
|
Contributor
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. Please remove whitespace-only changes |
||
| if (!IsErrorStatusCode (statusCode)) { | ||
| if (Logger.LogNet) | ||
| Logger.Log (LogLevel.Info, LOG_APP, $"Reading..."); | ||
| Stream inputStream = new BufferedStream (httpConnection.InputStream); | ||
| if (decompress_here) { | ||
| string[] encodings = httpConnection.ContentEncoding?.Split (','); | ||
| if (encodings != null) { | ||
| if (encodings.Contains (GZIP_ENCODING, StringComparer.OrdinalIgnoreCase)) | ||
| inputStream = new GZipStream (inputStream, CompressionMode.Decompress); | ||
| else if (encodings.Contains (DEFLATE_ENCODING, StringComparer.OrdinalIgnoreCase)) | ||
| inputStream = new DeflateStream (inputStream, CompressionMode.Decompress); | ||
| } | ||
| } | ||
| ret.Content = new StreamContent (inputStream); | ||
| } else { | ||
| ret.Content = GetContent (httpConnection, httpConnection.InputStream); | ||
| } | ||
| else { | ||
| if (Logger.LogNet) | ||
| Logger.Log (LogLevel.Info, LOG_APP, $"Status code is {statusCode}, returning empty content"); | ||
| // For 400 >= response code <= 599 the Java client throws the FileNotFound exeption when attempting to read from the connection | ||
| // Client tests require we return no content here | ||
| ret.Content = new StringContent (String.Empty, Encoding.ASCII); | ||
| Logger.Log (LogLevel.Info, LOG_APP, $"Status code is {statusCode}, reading..."); | ||
| // For 400 >= response code <= 599 the Java client throws the FileNotFound exception when attempting to read from the input stream. | ||
| // Instead we try to read the error stream and return an empty string if the error stream isn't readable. | ||
| ret.Content = GetErrorContent (httpConnection, new StringContent (String.Empty, Encoding.ASCII)); | ||
| } | ||
|
|
||
| CopyHeaders (httpConnection, ret); | ||
|
|
||
| IEnumerable <string> cookieHeaderValue; | ||
|
|
@@ -334,6 +326,32 @@ protected void AssertSelf () | |
| return ret; | ||
| } | ||
|
|
||
| HttpContent GetErrorContent (HttpURLConnection httpConnection, HttpContent fallbackContent) | ||
| { | ||
| var contentStream = httpConnection.ErrorStream; | ||
|
|
||
| if (contentStream != null) { | ||
| return GetContent (httpConnection, contentStream); | ||
| } | ||
|
|
||
| return fallbackContent; | ||
| } | ||
|
|
||
| HttpContent GetContent (URLConnection httpConnection, Stream contentStream) | ||
| { | ||
| Stream inputStream = new BufferedStream (contentStream); | ||
| if (decompress_here) { | ||
| string[] encodings = httpConnection.ContentEncoding?.Split (','); | ||
| if (encodings != null) { | ||
| if (encodings.Contains (GZIP_ENCODING, StringComparer.OrdinalIgnoreCase)) | ||
| inputStream = new GZipStream (inputStream, CompressionMode.Decompress); | ||
| else if (encodings.Contains (DEFLATE_ENCODING, StringComparer.OrdinalIgnoreCase)) | ||
| inputStream = new DeflateStream (inputStream, CompressionMode.Decompress); | ||
| } | ||
| } | ||
| return new StreamContent (inputStream); | ||
| } | ||
|
|
||
| bool HandleRedirect (HttpStatusCode redirectCode, HttpURLConnection httpConnection, RequestRedirectionState redirectState, out bool disposeRet) | ||
| { | ||
| if (!AllowAutoRedirect) { | ||
|
|
||
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.
There's no reason to remove the entire section of the comment. If you think that wording here is indeed inflammatory, please rephrase as there's actual information here.