Commit a7545c90 authored by titanium007's avatar titanium007

Update readme; new api; fix gzip decompression

parent ecfc7ff5
...@@ -58,12 +58,12 @@ Sample request and response event handlers ...@@ -58,12 +58,12 @@ Sample request and response event handlers
//Test On Request, intercept requests //Test On Request, intercept requests
public void OnRequest(object sender, SessionEventArgs e) public void OnRequest(object sender, SessionEventArgs e)
{ {
Console.WriteLine(e.RequestURL); Console.WriteLine(e.ProxySession.Request.RequestUrl);
//read request headers //read request headers
var requestHeaders = e.RequestHeaders; var requestHeaders = e.ProxySession.Request.RequestHeaders;
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT") && e.RequestContentLength > 0) if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
{ {
//Get/Set request body bytes //Get/Set request body bytes
byte[] bodyBytes = e.GetRequestBody(); byte[] bodyBytes = e.GetRequestBody();
...@@ -78,7 +78,7 @@ Sample request and response event handlers ...@@ -78,7 +78,7 @@ Sample request and response event handlers
//To cancel a request with a custom HTML content //To cancel a request with a custom HTML content
//Filter URL //Filter URL
if (e.RequestURL.Contains("google.com")) if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
{ {
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>"); e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
} }
...@@ -86,10 +86,11 @@ Sample request and response event handlers ...@@ -86,10 +86,11 @@ Sample request and response event handlers
public void OnResponse(object sender, SessionEventArgs e) public void OnResponse(object sender, SessionEventArgs e)
{ {
//read response headers ////read response headers
var responseHeaders = e.ResponseHeaders; var responseHeaders = e.ProxySession.Response.ResponseHeaders;
if (e.ResponseStatusCode == HttpStatusCode.OK)
if (e.ResponseStatusCode == "200")
{ {
if (e.ResponseContentType.Trim().ToLower().Contains("text/html")) if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
{ {
......
...@@ -46,28 +46,28 @@ namespace Titanium.Web.Proxy.Test ...@@ -46,28 +46,28 @@ namespace Titanium.Web.Proxy.Test
{ {
Console.WriteLine(e.ProxySession.Request.RequestUrl); Console.WriteLine(e.ProxySession.Request.RequestUrl);
////read request headers //read request headers
//var requestHeaders = e.ProxySession.Request.RequestHeaders; var requestHeaders = e.ProxySession.Request.RequestHeaders;
//if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT")) if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
//{ {
// //Get/Set request body bytes //Get/Set request body bytes
// byte[] bodyBytes = e.GetRequestBody(); byte[] bodyBytes = e.GetRequestBody();
// e.SetRequestBody(bodyBytes); e.SetRequestBody(bodyBytes);
// //Get/Set request body as string //Get/Set request body as string
// string bodyString = e.GetRequestBodyAsString(); string bodyString = e.GetRequestBodyAsString();
// e.SetRequestBodyString(bodyString); e.SetRequestBodyString(bodyString);
//} }
////To cancel a request with a custom HTML content //To cancel a request with a custom HTML content
////Filter URL //Filter URL
//if (e.ProxySession.Request.RequestUrl.Contains("google.com")) if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
//{ {
// e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>"); e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
//} }
} }
//Test script injection //Test script injection
...@@ -78,25 +78,25 @@ namespace Titanium.Web.Proxy.Test ...@@ -78,25 +78,25 @@ namespace Titanium.Web.Proxy.Test
var responseHeaders = e.ProxySession.Response.ResponseHeaders; var responseHeaders = e.ProxySession.Response.ResponseHeaders;
//if (e.ResponseStatusCode == "200") if (e.ResponseStatusCode == "200")
//{ {
// if (e.ResponseContentType.Trim().ToLower().Contains("text/html")) if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
// { {
// //Get/Set response body bytes //Get/Set response body bytes
// byte[] responseBodyBytes = e.GetResponseBody(); byte[] responseBodyBytes = e.GetResponseBody();
// e.SetResponseBody(responseBodyBytes); e.SetResponseBody(responseBodyBytes);
// //Get response body as string //Get response body as string
// string responseBody = e.GetResponseBodyAsString(); string responseBody = e.GetResponseBodyAsString();
// //Modify e.ServerResponse //Modify e.ServerResponse
// Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline); Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
// string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1); string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);
// //Set modifed response Html Body //Set modifed response Html Body
// e.SetResponseBodyString(modified); e.SetResponseBodyString(modified);
// } }
//} }
} }
} }
} }
\ No newline at end of file
...@@ -139,12 +139,13 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -139,12 +139,13 @@ namespace Titanium.Web.Proxy.EventArguments
} }
} }
} }
try try
{ {
switch (requestContentEncoding) switch (requestContentEncoding)
{ {
case "gzip": case "gzip":
ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream); ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream.ToArray());
break; break;
case "deflate": case "deflate":
ProxySession.Request.RequestBody = CompressionHelper.DecompressDeflate(requestBodyStream); ProxySession.Request.RequestBody = CompressionHelper.DecompressDeflate(requestBodyStream);
...@@ -171,41 +172,57 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -171,41 +172,57 @@ namespace Titanium.Web.Proxy.EventArguments
{ {
if (ProxySession.Response.ResponseBody == null) if (ProxySession.Response.ResponseBody == null)
{ {
using (var responseBodyStream = new MemoryStream())
{
if (ProxySession.Response.IsChunked)
{
while (true)
{
var chuchkHead = ProxySession.ProxyClient.ServerStreamReader.ReadLine();
var chunkSize = int.Parse(chuchkHead, NumberStyles.HexNumber);
if (chunkSize != 0)
{
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(chunkSize);
responseBodyStream.Write(buffer, 0, buffer.Length);
//chunk trail
ProxySession.ProxyClient.ServerStreamReader.ReadLine();
}
else
{
ProxySession.ProxyClient.ServerStreamReader.ReadLine();
break;
}
}
}
else
{
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(ProxySession.Response.ContentLength);
responseBodyStream.Write(buffer, 0, buffer.Length);
}
switch (ProxySession.Response.ResponseContentEncoding) switch (ProxySession.Response.ResponseContentEncoding)
{ {
case "gzip": case "gzip":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(ProxySession.Response.ResponseStream); ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(responseBodyStream.ToArray());
break; break;
case "deflate": case "deflate":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(ProxySession.Response.ResponseStream); ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(responseBodyStream);
break; break;
case "zlib": case "zlib":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(ProxySession.Response.ResponseStream); ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(responseBodyStream);
break; break;
default: default:
ProxySession.Response.ResponseBody = DecodeData(ProxySession.Response.ResponseStream); ProxySession.Response.ResponseBody = responseBodyStream.ToArray();
break; break;
} }
}
ProxySession.Response.ResponseBodyRead = true; ProxySession.Response.ResponseBodyRead = true;
} }
} }
//stream reader not recomended for images
private byte[] DecodeData(Stream responseStream)
{
var buffer = new byte[_bufferSize];
using (var ms = new MemoryStream())
{
int read;
while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public Encoding GetRequestBodyEncoding() public Encoding GetRequestBodyEncoding()
{ {
......
...@@ -50,11 +50,10 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -50,11 +50,10 @@ namespace Titanium.Web.Proxy.Helpers
} }
} }
public static byte[] DecompressGzip(Stream input) //identify why passing stream instead of bytes returns empty result
public static byte[] DecompressGzip(byte[] gzip)
{ {
using ( using (var decompressor = new System.IO.Compression.GZipStream(new MemoryStream(gzip), System.IO.Compression.CompressionMode.Decompress))
var decompressor = new System.IO.Compression.GZipStream(input,
System.IO.Compression.CompressionMode.Decompress))
{ {
var buffer = new byte[BufferSize]; var buffer = new byte[BufferSize];
......
...@@ -14,16 +14,16 @@ namespace Titanium.Web.Proxy.Network ...@@ -14,16 +14,16 @@ namespace Titanium.Web.Proxy.Network
{ {
public class Request public class Request
{ {
public string Method { get; set; } public string Method { get; internal set; }
public Uri RequestUri { get; set; } public Uri RequestUri { get; internal set; }
public string Version { get; set; } public string Version { get; internal set; }
public string RequestStatus { get; set; } public string RequestStatus { get; internal set; }
public int RequestContentLength { get; set; } public int RequestContentLength { get; internal set; }
public bool RequestSendChunked { get; set; } public bool RequestSendChunked { get; internal set; }
public string RequestContentType { get; set; } public string RequestContentType { get; internal set; }
public bool RequestKeepAlive { get; set; } public bool RequestKeepAlive { get; internal set; }
public string RequestHost { get; set; } public string RequestHost { get; internal set; }
public string RequestUrl { get; internal set; } public string RequestUrl { get; internal set; }
...@@ -34,7 +34,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -34,7 +34,7 @@ namespace Titanium.Web.Proxy.Network
internal byte[] RequestBody { get; set; } internal byte[] RequestBody { get; set; }
internal string RequestBodyString { get; set; } internal string RequestBodyString { get; set; }
internal bool RequestBodyRead { get; set; } internal bool RequestBodyRead { get; set; }
public bool UpgradeToWebSocket { get; set; } internal bool UpgradeToWebSocket { get; set; }
public List<HttpHeader> RequestHeaders { get; internal set; } public List<HttpHeader> RequestHeaders { get; internal set; }
internal bool RequestLocked { get; set; } internal bool RequestLocked { get; set; }
...@@ -55,25 +55,21 @@ namespace Titanium.Web.Proxy.Network ...@@ -55,25 +55,21 @@ namespace Titanium.Web.Proxy.Network
internal bool ResponseBodyRead { get; set; } internal bool ResponseBodyRead { get; set; }
internal bool ResponseLocked { get; set; } internal bool ResponseLocked { get; set; }
public List<HttpHeader> ResponseHeaders { get; internal set; } public List<HttpHeader> ResponseHeaders { get; internal set; }
public string ResponseCharacterSet { get; set; } internal string ResponseCharacterSet { get; set; }
public string ResponseContentEncoding { get; set; } internal string ResponseContentEncoding { get; set; }
public System.Version ResponseProtocolVersion { get; set; } internal System.Version ResponseProtocolVersion { get; set; }
public string ResponseStatusCode { get; set; } internal string ResponseStatusCode { get; set; }
public string ResponseStatusDescription { get; set; } internal string ResponseStatusDescription { get; set; }
public bool ResponseKeepAlive { get; set; } internal bool ResponseKeepAlive { get; set; }
public string ResponseContentType { get; set; } internal string ResponseContentType { get; set; }
public int ContentLength { get; set; } internal int ContentLength { get; set; }
public bool IsChunked { get; set; } internal bool IsChunked { get; set; }
public Response() public Response()
{ {
this.ResponseHeaders = new List<HttpHeader>(); this.ResponseHeaders = new List<HttpHeader>();
this.ResponseKeepAlive = true; this.ResponseKeepAlive = true;
} }
} }
public class HttpWebSession public class HttpWebSession
...@@ -90,7 +86,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -90,7 +86,7 @@ namespace Titanium.Web.Proxy.Network
public Request Request { get; set; } public Request Request { get; set; }
public Response Response { get; set; } public Response Response { get; set; }
public TcpConnection ProxyClient { get; set; } internal TcpConnection ProxyClient { get; set; }
public void SetConnection(TcpConnection Connection) public void SetConnection(TcpConnection Connection)
{ {
...@@ -102,12 +98,8 @@ namespace Titanium.Web.Proxy.Network ...@@ -102,12 +98,8 @@ namespace Titanium.Web.Proxy.Network
{ {
this.Request = new Request(); this.Request = new Request();
this.Response = new Response(); this.Response = new Response();
} }
public void SendRequest() public void SendRequest()
{ {
Stream stream = ProxyClient.Stream; Stream stream = ProxyClient.Stream;
...@@ -169,8 +161,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -169,8 +161,6 @@ namespace Titanium.Web.Proxy.Network
} }
} }
} }
......
...@@ -39,10 +39,11 @@ namespace Titanium.Web.Proxy ...@@ -39,10 +39,11 @@ namespace Titanium.Web.Proxy
if (args.ProxySession.Response.ResponseBodyRead) if (args.ProxySession.Response.ResponseBodyRead)
{ {
var isChunked =args.ProxySession.Response.IsChunked; var isChunked = args.ProxySession.Response.IsChunked;
var contentEncoding = args.ProxySession.Response.ResponseContentEncoding; var contentEncoding = args.ProxySession.Response.ResponseContentEncoding;
switch (contentEncoding.ToLower()) if(contentEncoding!=null)
switch (contentEncoding)
{ {
case "gzip": case "gzip":
args.ProxySession.Response.ResponseBody = CompressionHelper.CompressGzip(args.ProxySession.Response.ResponseBody); args.ProxySession.Response.ResponseBody = CompressionHelper.CompressGzip(args.ProxySession.Response.ResponseBody);
...@@ -91,11 +92,11 @@ namespace Titanium.Web.Proxy ...@@ -91,11 +92,11 @@ namespace Titanium.Web.Proxy
switch (response.Response.ResponseHeaders[i].Name.ToLower()) switch (response.Response.ResponseHeaders[i].Name.ToLower())
{ {
case "content-length": case "content-length":
response.Response.ContentLength = int.Parse(response.Response.ResponseHeaders[i].Value); response.Response.ContentLength = int.Parse(response.Response.ResponseHeaders[i].Value.Trim());
break; break;
case "content-encoding": case "content-encoding":
response.Response.ResponseContentEncoding = response.Response.ResponseHeaders[i].Value; response.Response.ResponseContentEncoding = response.Response.ResponseHeaders[i].Value.Trim().ToLower();
break; break;
case "content-type": case "content-type":
...@@ -112,10 +113,12 @@ namespace Titanium.Web.Proxy ...@@ -112,10 +113,12 @@ namespace Titanium.Web.Proxy
if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("chunked")) if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("chunked"))
response.Response.IsChunked = true; response.Response.IsChunked = true;
break; break;
case "connection": case "connection":
if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("close")) if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("close"))
response.Response.ResponseKeepAlive = false; response.Response.ResponseKeepAlive = false;
break; break;
default: default:
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment