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
//Test On Request, intercept requests
public void OnRequest(object sender, SessionEventArgs e)
{
Console.WriteLine(e.RequestURL);
Console.WriteLine(e.ProxySession.Request.RequestUrl);
//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
byte[] bodyBytes = e.GetRequestBody();
......@@ -78,7 +78,7 @@ Sample request and response event handlers
//To cancel a request with a custom HTML content
//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>");
}
......@@ -86,10 +86,11 @@ Sample request and response event handlers
public void OnResponse(object sender, SessionEventArgs e)
{
//read response headers
var responseHeaders = e.ResponseHeaders;
////read response headers
var responseHeaders = e.ProxySession.Response.ResponseHeaders;
if (e.ResponseStatusCode == HttpStatusCode.OK)
if (e.ResponseStatusCode == "200")
{
if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
{
......
......@@ -46,28 +46,28 @@ namespace Titanium.Web.Proxy.Test
{
Console.WriteLine(e.ProxySession.Request.RequestUrl);
////read request headers
//var requestHeaders = e.ProxySession.Request.RequestHeaders;
//read request headers
var requestHeaders = e.ProxySession.Request.RequestHeaders;
//if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
//{
// //Get/Set request body bytes
// byte[] bodyBytes = e.GetRequestBody();
// e.SetRequestBody(bodyBytes);
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
{
//Get/Set request body bytes
byte[] bodyBytes = e.GetRequestBody();
e.SetRequestBody(bodyBytes);
// //Get/Set request body as string
// string bodyString = e.GetRequestBodyAsString();
// e.SetRequestBodyString(bodyString);
//Get/Set request body as string
string bodyString = e.GetRequestBodyAsString();
e.SetRequestBodyString(bodyString);
//}
}
////To cancel a request with a custom HTML content
////Filter URL
//To cancel a request with a custom HTML content
//Filter URL
//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>");
//}
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>");
}
}
//Test script injection
......@@ -78,25 +78,25 @@ namespace Titanium.Web.Proxy.Test
var responseHeaders = e.ProxySession.Response.ResponseHeaders;
//if (e.ResponseStatusCode == "200")
//{
// if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
// {
// //Get/Set response body bytes
// byte[] responseBodyBytes = e.GetResponseBody();
// e.SetResponseBody(responseBodyBytes);
if (e.ResponseStatusCode == "200")
{
if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
{
//Get/Set response body bytes
byte[] responseBodyBytes = e.GetResponseBody();
e.SetResponseBody(responseBodyBytes);
// //Get response body as string
// string responseBody = e.GetResponseBodyAsString();
//Get response body as string
string responseBody = e.GetResponseBodyAsString();
// //Modify e.ServerResponse
// 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);
//Modify e.ServerResponse
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);
// //Set modifed response Html Body
// e.SetResponseBodyString(modified);
// }
//}
//Set modifed response Html Body
e.SetResponseBodyString(modified);
}
}
}
}
}
\ No newline at end of file
......@@ -32,7 +32,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
_bufferSize = bufferSize;
Client = new Client();
ProxySession = new HttpWebSession();
ProxySession = new HttpWebSession();
}
public Client Client { get; set; }
......@@ -79,7 +79,7 @@ namespace Titanium.Web.Proxy.EventArguments
public void Dispose()
{
}
private void ReadRequestBody()
......@@ -139,12 +139,13 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
}
try
{
switch (requestContentEncoding)
{
case "gzip":
ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream);
ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream.ToArray());
break;
case "deflate":
ProxySession.Request.RequestBody = CompressionHelper.DecompressDeflate(requestBodyStream);
......@@ -171,20 +172,50 @@ namespace Titanium.Web.Proxy.EventArguments
{
if (ProxySession.Response.ResponseBody == null)
{
switch (ProxySession.Response.ResponseContentEncoding)
using (var responseBodyStream = new MemoryStream())
{
case "gzip":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(ProxySession.Response.ResponseStream);
break;
case "deflate":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(ProxySession.Response.ResponseStream);
break;
case "zlib":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(ProxySession.Response.ResponseStream);
break;
default:
ProxySession.Response.ResponseBody = DecodeData(ProxySession.Response.ResponseStream);
break;
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)
{
case "gzip":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(responseBodyStream.ToArray());
break;
case "deflate":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(responseBodyStream);
break;
case "zlib":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(responseBodyStream);
break;
default:
ProxySession.Response.ResponseBody = responseBodyStream.ToArray();
break;
}
}
ProxySession.Response.ResponseBodyRead = true;
......@@ -192,20 +223,6 @@ namespace Titanium.Web.Proxy.EventArguments
}
//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()
{
......
......@@ -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 (
var decompressor = new System.IO.Compression.GZipStream(input,
System.IO.Compression.CompressionMode.Decompress))
using (var decompressor = new System.IO.Compression.GZipStream(new MemoryStream(gzip), System.IO.Compression.CompressionMode.Decompress))
{
var buffer = new byte[BufferSize];
......
......@@ -14,16 +14,16 @@ namespace Titanium.Web.Proxy.Network
{
public class Request
{
public string Method { get; set; }
public Uri RequestUri { get; set; }
public string Version { get; set; }
public string Method { get; internal set; }
public Uri RequestUri { get; internal set; }
public string Version { get; internal set; }
public string RequestStatus { get; set; }
public int RequestContentLength { get; set; }
public bool RequestSendChunked { get; set; }
public string RequestContentType { get; set; }
public bool RequestKeepAlive { get; set; }
public string RequestHost { get; set; }
public string RequestStatus { get; internal set; }
public int RequestContentLength { get; internal set; }
public bool RequestSendChunked { get; internal set; }
public string RequestContentType { get; internal set; }
public bool RequestKeepAlive { get; internal set; }
public string RequestHost { get; internal set; }
public string RequestUrl { get; internal set; }
......@@ -34,7 +34,7 @@ namespace Titanium.Web.Proxy.Network
internal byte[] RequestBody { get; set; }
internal string RequestBodyString { get; set; }
internal bool RequestBodyRead { get; set; }
public bool UpgradeToWebSocket { get; set; }
internal bool UpgradeToWebSocket { get; set; }
public List<HttpHeader> RequestHeaders { get; internal set; }
internal bool RequestLocked { get; set; }
......@@ -55,25 +55,21 @@ namespace Titanium.Web.Proxy.Network
internal bool ResponseBodyRead { get; set; }
internal bool ResponseLocked { get; set; }
public List<HttpHeader> ResponseHeaders { get; internal set; }
public string ResponseCharacterSet { get; set; }
public string ResponseContentEncoding { get; set; }
public System.Version ResponseProtocolVersion { get; set; }
public string ResponseStatusCode { get; set; }
public string ResponseStatusDescription { get; set; }
public bool ResponseKeepAlive { get; set; }
public string ResponseContentType { get; set; }
public int ContentLength { get; set; }
public bool IsChunked { get; set; }
internal string ResponseCharacterSet { get; set; }
internal string ResponseContentEncoding { get; set; }
internal System.Version ResponseProtocolVersion { get; set; }
internal string ResponseStatusCode { get; set; }
internal string ResponseStatusDescription { get; set; }
internal bool ResponseKeepAlive { get; set; }
internal string ResponseContentType { get; set; }
internal int ContentLength { get; set; }
internal bool IsChunked { get; set; }
public Response()
{
this.ResponseHeaders = new List<HttpHeader>();
this.ResponseKeepAlive = true;
}
}
}
public class HttpWebSession
......@@ -90,7 +86,7 @@ namespace Titanium.Web.Proxy.Network
public Request Request { get; set; }
public Response Response { get; set; }
public TcpConnection ProxyClient { get; set; }
internal TcpConnection ProxyClient { get; set; }
public void SetConnection(TcpConnection Connection)
{
......@@ -102,12 +98,8 @@ namespace Titanium.Web.Proxy.Network
{
this.Request = new Request();
this.Response = new Response();
}
public void SendRequest()
{
Stream stream = ProxyClient.Stream;
......@@ -169,8 +161,6 @@ namespace Titanium.Web.Proxy.Network
}
}
}
......
......@@ -39,10 +39,11 @@ namespace Titanium.Web.Proxy
if (args.ProxySession.Response.ResponseBodyRead)
{
var isChunked =args.ProxySession.Response.IsChunked;
var isChunked = args.ProxySession.Response.IsChunked;
var contentEncoding = args.ProxySession.Response.ResponseContentEncoding;
switch (contentEncoding.ToLower())
if(contentEncoding!=null)
switch (contentEncoding)
{
case "gzip":
args.ProxySession.Response.ResponseBody = CompressionHelper.CompressGzip(args.ProxySession.Response.ResponseBody);
......@@ -91,11 +92,11 @@ namespace Titanium.Web.Proxy
switch (response.Response.ResponseHeaders[i].Name.ToLower())
{
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;
case "content-encoding":
response.Response.ResponseContentEncoding = response.Response.ResponseHeaders[i].Value;
response.Response.ResponseContentEncoding = response.Response.ResponseHeaders[i].Value.Trim().ToLower();
break;
case "content-type":
......@@ -112,10 +113,12 @@ namespace Titanium.Web.Proxy
if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("chunked"))
response.Response.IsChunked = true;
break;
case "connection":
if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("close"))
response.Response.ResponseKeepAlive = false;
break;
default:
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