Hi,
I use the proxy a lot to inject a javascript into the head of a page, which mostly works fine, but there are some troublesome scenarios, one of which is possibly a bug or my misunderstanding of how best to use the proxy.
1. When the response Transfer-Encoding is set to 'chunked' and the Content property is retrived from the response, the hex chunk sizes are found in the the Content - is this a bug?
An example can be seen when you Intercept the response from this URL: http://www.bbc.co.uk/search/?q=Music
Here's the code I use to read and write the content (for a non compressed response):
public class ProxyContentHandlerPlain : IProxyContentHandler
{
public String readContent(byte[] content)
{
using (var stream = new System.IO.MemoryStream(content))
{
using (var reader = new StreamReader(stream,Encoding.Default))
{
return reader.ReadToEnd();
}
}
}
public byte[] writeContent(string content)
{
return Encoding.Default.GetBytes(content);
}
}
When writing the Content back into the response, you will see the hex chunk sizes in the page.
I have been able overcome this when the encoding is not compressed, by a creative regex to find the hex chunk sizes and parse the source chunk by chunk - this should really be accomodated for within the proxy and/or respose class?
2. Related to abvove, when the Transfer-Encoding is 'chunked' and the Content-Encoding is 'gzip' or 'deflate' I have not bee able to parse the content from the response class. An example URL which has this scenario is: http://www.bing.com/search?q=Music&go=&form=QBLH&filt=all&qs=n&sk=
Any advice on how to overcome this?
Here's how I currently read and write gzip compressed content:
class ProxyContentHandlerGzip : IProxyContentHandler
{
public string readContent(byte[] content)
{
using (var stream = new System.IO.MemoryStream(content))
{
using (var zs = new GZipStream(stream, CompressionMode.Decompress))
{
using (var reader = new StreamReader(zs,Encoding.Default))
{
return reader.ReadToEnd();
}
}
}
}
public byte[] writeContent(string content)
{
byte[] data = Encoding.Default.GetBytes(content);
using (var stream = new MemoryStream())
{
using (var zs = new GZipStream(stream, CompressionMode.Compress))
{
zs.Write(data, 0, data.Length);
return stream.ToArray();
}
}
}
}
In general, I'd love to see these sort of things handled better within the proxy and response class - I think it should be smarter and allow me to simply read and write the content from the response class - would you agree?
Any help or advice would be appreciated.
Thanks,
Dan
I use the proxy a lot to inject a javascript into the head of a page, which mostly works fine, but there are some troublesome scenarios, one of which is possibly a bug or my misunderstanding of how best to use the proxy.
1. When the response Transfer-Encoding is set to 'chunked' and the Content property is retrived from the response, the hex chunk sizes are found in the the Content - is this a bug?
An example can be seen when you Intercept the response from this URL: http://www.bbc.co.uk/search/?q=Music
Here's the code I use to read and write the content (for a non compressed response):
public class ProxyContentHandlerPlain : IProxyContentHandler
{
public String readContent(byte[] content)
{
using (var stream = new System.IO.MemoryStream(content))
{
using (var reader = new StreamReader(stream,Encoding.Default))
{
return reader.ReadToEnd();
}
}
}
public byte[] writeContent(string content)
{
return Encoding.Default.GetBytes(content);
}
}
When writing the Content back into the response, you will see the hex chunk sizes in the page.
I have been able overcome this when the encoding is not compressed, by a creative regex to find the hex chunk sizes and parse the source chunk by chunk - this should really be accomodated for within the proxy and/or respose class?
2. Related to abvove, when the Transfer-Encoding is 'chunked' and the Content-Encoding is 'gzip' or 'deflate' I have not bee able to parse the content from the response class. An example URL which has this scenario is: http://www.bing.com/search?q=Music&go=&form=QBLH&filt=all&qs=n&sk=
Any advice on how to overcome this?
Here's how I currently read and write gzip compressed content:
class ProxyContentHandlerGzip : IProxyContentHandler
{
public string readContent(byte[] content)
{
using (var stream = new System.IO.MemoryStream(content))
{
using (var zs = new GZipStream(stream, CompressionMode.Decompress))
{
using (var reader = new StreamReader(zs,Encoding.Default))
{
return reader.ReadToEnd();
}
}
}
}
public byte[] writeContent(string content)
{
byte[] data = Encoding.Default.GetBytes(content);
using (var stream = new MemoryStream())
{
using (var zs = new GZipStream(stream, CompressionMode.Compress))
{
zs.Write(data, 0, data.Length);
return stream.ToArray();
}
}
}
}
In general, I'd love to see these sort of things handled better within the proxy and response class - I think it should be smarter and allow me to simply read and write the content from the response class - would you agree?
Any help or advice would be appreciated.
Thanks,
Dan