I have been building my own HTTP/1.0 proxy server using HTTPWebRequest/HTTPWebResponse. Part of the original code streamed the response from the remote server directly to the client like so:
if (response != null)
{
List<Tuple<String, String>> responseHeaders = ProcessResponse(response);
StreamWriter myResponseWriter = new StreamWriter(outStream);
Stream responseStream = response.GetResponseStream();
HttpStatusCode statusCode = response.StatusCode;
string statusDesc = response.StatusDescription;
Byte[] buffer;
if (response.ContentLength > 0)
{
buffer = new Byte[response.ContentLength];
}
else
{
buffer = new Byte[BUFFER_SIZE];
}
int bytesRead;
//send the response status and response headers
WriteResponseStatus(statusCode, statusDesc, myResponseWriter);
WriteResponseHeaders(myResponseWriter, responseHeaders);
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
// this is the response body being streamed directly to the client browser
outStream.Write(buffer, 0, bytesRead);
}
}
I have been trying to intercept the response body so that I can modify the contents, but before I can do this I need to decompress the contents if it has been gzip/br/deflate by the remote server. This is what I have come up with so far, but as you can see from my comments I just can't work out how to store the byte stream into one var so that I can then send it for decompression:
Byte[] buffer;
if (response.ContentLength > 0)
buffer = new Byte[response.ContentLength];
else
buffer = new Byte[BUFFER_SIZE];
int bytesRead;
var res = "";
// if the url and content type matches the criteria, then we want to edit it
if (hostPathMatch.Count > 0 && contentTypeMatch.Count > 0)
{
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
// how to we send this response stream to a var so that the entire contents can be sent to decompress
//res += UTF8Encoding.UTF8.GetString(buffer, 0, bytesRead); // this doesnt work as it mangles gzipped contents
}
//was the page compressed? check the content-encoding header.
if (responseHeaders.Any(p => p.Item1.ToLower() == "content-encoding" && p.Item2.ToLower() == "gzip"))
{
Output._Log.Output_Log("CONTENT IS GZIPPED");
res = Tools.Unzip(res); // expects byte[], returns UTF8
}
// THIS IS WHERE WE WILL MODIFY THE BODY CONTENTS
res = res.Replace("Foo", "Bar Bar");
// then we will re-compress
// update the response headers with the correct content length after modification
responseHeaders.RemoveAll(p => p.Item1 == "Content-Length");
responseHeaders.Add(new Tuple<string, string>("Content-Length", res.Length));
//send the response status and response headers
WriteResponseStatus(statusCode, statusDesc, myResponseWriter);
WriteResponseHeaders(myResponseWriter, responseHeaders);
}
else // we didnt want to modify this file, so just stream it out directly to the browser
{
//send the response status and response headers
WriteResponseStatus(statusCode, statusDesc, myResponseWriter);
WriteResponseHeaders(myResponseWriter, responseHeaders);
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
outStream.Write(buffer, 0, bytesRead);
}
}
For the case where you don't know the total size in advance, you could do something like following
[...]
List<byte> data = new List<byte>();
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
for(int i = 0; i < bytesRead; ++i)
data.Add(buffer[i]);
}
var bytes = data.ToArray();
[...]
Should you know it in advance you could use something like
[...]
int offset = 0;
byte[] data = new byte[TOTAL_SIZE];
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
buffer.CopyTo(data, offset);
offset += bytesRead;
}
[...]
Note that nothing of the snippets above have been tested and some checks would have to be added (e.g. out-of-range, etc.). Furthermore, depending on the use cases it might not fit your needs (e.g. for huge data).
PS: DO NOT FORGET to dispose/cleanup all of the IDisposable
s.