I have this proxy code taken from this article and created as HttpHandler
public void ProcessRequest(HttpContext context) { string url = context.Request["url"]; string contentType = context.Request["type"]; // no buffering as we want to save memory context.Response.Buffer = false; // beging getting content using (WebClient client = new WebClient()) { // set content type if specified if (!string.IsNullOrEmpty(contentType)) { client.Headers.Add(HttpRequestHeader.ContentType, contentType); } client.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); client.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US"); client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows; U; Windows NT 6.0; " + "en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); client.Headers.Add(HttpRequestHeader.Accept, "*/*"); // get that data byte[] data = client.DownloadData(url); if (!context.Response.IsClientConnected) return; // deliver content type, encoding and length as it // is received from the external url context.Response.ContentType = client.ResponseHeaders["Content-Type"]; string contentEncoding = client.ResponseHeaders["Content-Encoding"]; string contentLength = client.ResponseHeaders["Content-Length"]; if (!string.IsNullOrEmpty(contentEncoding)) context.Response.AppendHeader(HttpRequestHeader.ContentEncoding.ToString(), contentEncoding); if (!string.IsNullOrEmpty(contentLength)) context.Response.AppendHeader(HttpRequestHeader.ContentLength.ToString(), contentLength); // transmit the exact bytes downloaded context.Response.BinaryWrite(data); } }
Ive mapped this Http module in IIS7 as a managed hanlder and on my simple Html Im page, using jQuery to call a proxy server and post the results in an iframe.
$(document).ready(function() { $.ajax({ type: "GET", url: "a.RegularProxy", data: { url: 'http://example.org/test.html', type: "text/html" }, dataType: "html", success: function(data) { $("iframe").contents().find('html body').html(data.toString()); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); } }); });
Everything works fine when the page is pleasant and simple, however, if the page is compressed (gzip, deflate) I need to find a way to unpack it on the client side , and not in the proxy server - the proxy function should be as fast as possible.
source share