Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #85 from afili/DotNet-pass-gzip-html-error
Browse files Browse the repository at this point in the history
Pass gzip html error without unzipping
  • Loading branch information
jgravois committed Apr 9, 2014
2 parents 3cdc9cd + 02c5c8e commit b6f790c
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions DotNet/proxy.ashx
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,33 @@ public class proxy : IHttpHandler {
try {
serverResponse = forwardToServer(context, addTokenToUri(uri, token, tokenParamName), postBody);
} catch (System.Net.WebException webExc) {

string errorMsg = webExc.Message + " " + uri;
log(TraceLevel.Error, errorMsg);

//if there is a response then set the response's statusCode else set HttpStatusCode.InternalServerError
var statusCode = webExc.Response != null ? (webExc.Response as System.Net.HttpWebResponse).StatusCode
: System.Net.HttpStatusCode.InternalServerError;

sendErrorResponse(context.Response, null, errorMsg, statusCode);
if (webExc.Response != null)
{
string contentEncoding = (webExc.Response as System.Net.HttpWebResponse).ContentEncoding;
context.Response.AddHeader("Content-Encoding", contentEncoding);

using (Stream responseStream = webExc.Response.GetResponseStream())
{
byte[] bytes = new byte[32768];
int bytesRead = 0;

while ((bytesRead = responseStream.Read(bytes, 0, bytes.Length)) > 0)
{
responseStream.Write(bytes, 0, bytesRead);
}

context.Response.OutputStream.Write(bytes, 0, bytes.Length);
}
}
else
{
System.Net.HttpStatusCode statusCode = System.Net.HttpStatusCode.InternalServerError;
sendErrorResponse(context.Response, null, errorMsg, statusCode);
}
return;
}

Expand Down

0 comments on commit b6f790c

Please sign in to comment.