Commit de516799 authored by titanium007's avatar titanium007

add comments

parent c244b2b6
...@@ -13,35 +13,48 @@ using Titanium.Web.Proxy.Models; ...@@ -13,35 +13,48 @@ using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.EventArguments namespace Titanium.Web.Proxy.EventArguments
{ {
public class Client
{
internal TcpClient TcpClient { get; set; }
internal Stream ClientStream { get; set; }
internal CustomBinaryReader ClientStreamReader { get; set; }
internal StreamWriter ClientStreamWriter { get; set; }
public int ClientPort { get; internal set; }
public IPAddress ClientIpAddress { get; internal set; }
} /// <summary>
/// Holds info related to a single proxy session (single request/response sequence)
/// A proxy session is bounded to a single connection from client
/// A proxy session ends when client terminates connection to proxy
/// or when server terminates connection from proxy
/// </summary>
public class SessionEventArgs : EventArgs, IDisposable public class SessionEventArgs : EventArgs, IDisposable
{ {
readonly int _bufferSize; readonly int _bufferSize;
/// <summary>
/// Constructor to initialize the proxy
/// </summary>
internal SessionEventArgs(int bufferSize) internal SessionEventArgs(int bufferSize)
{ {
_bufferSize = bufferSize; _bufferSize = bufferSize;
Client = new Client(); Client = new ProxyClient();
ProxySession = new HttpWebSession(); ProxySession = new HttpWebSession();
} }
internal Client Client { get; set; } /// <summary>
/// Holds a reference to server connection
/// </summary>
internal ProxyClient Client { get; set; }
/// <summary>
/// Does this session uses SSL
/// </summary>
public bool IsHttps { get; internal set; } public bool IsHttps { get; internal set; }
/// <summary>
/// A web session corresponding to a single request/response sequence
/// within a proxy connection
/// </summary>
public HttpWebSession ProxySession { get; set; } public HttpWebSession ProxySession { get; set; }
/// <summary>
/// A shortcut to get the request content length
/// </summary>
public int RequestContentLength public int RequestContentLength
{ {
get get
...@@ -50,17 +63,25 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -50,17 +63,25 @@ namespace Titanium.Web.Proxy.EventArguments
} }
} }
/// <summary>
/// A shortcut to get the request Method (GET/POST/PUT etc)
/// </summary>
public string RequestMethod public string RequestMethod
{ {
get { return ProxySession.Request.Method; } get { return ProxySession.Request.Method; }
} }
/// <summary>
/// A shortcut to get the response status code (200 OK, 404 etc)
/// </summary>
public string ResponseStatusCode public string ResponseStatusCode
{ {
get { return ProxySession.Response.ResponseStatusCode; } get { return ProxySession.Response.ResponseStatusCode; }
} }
/// <summary>
/// A shortcut to get the response content type
/// </summary>
public string ResponseContentType public string ResponseContentType
{ {
get get
...@@ -69,30 +90,39 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -69,30 +90,39 @@ namespace Titanium.Web.Proxy.EventArguments
} }
} }
/// <summary>
/// implement any cleanup here
/// </summary>
public void Dispose() public void Dispose()
{ {
} }
/// <summary>
/// Read request body content as bytes[] for current session
/// </summary>
private void ReadRequestBody() private void ReadRequestBody()
{ {
//GET request don't have a request body to read
if ((ProxySession.Request.Method.ToUpper() != "POST" && ProxySession.Request.Method.ToUpper() != "PUT")) if ((ProxySession.Request.Method.ToUpper() != "POST" && ProxySession.Request.Method.ToUpper() != "PUT"))
{ {
throw new BodyNotFoundException("Request don't have a body." + throw new BodyNotFoundException("Request don't have a body." +
"Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body."); "Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body.");
} }
//Caching check
if (ProxySession.Request.RequestBody == null) if (ProxySession.Request.RequestBody == null)
{ {
var isChunked = false; var isChunked = false;
string requestContentEncoding = null; string requestContentEncoding = null;
//get compression method (gzip, zlib etc)
if (ProxySession.Request.RequestHeaders.Any(x => x.Name.ToLower() == "content-encoding")) if (ProxySession.Request.RequestHeaders.Any(x => x.Name.ToLower() == "content-encoding"))
{ {
requestContentEncoding = ProxySession.Request.RequestHeaders.First(x => x.Name.ToLower() == "content-encoding").Value; requestContentEncoding = ProxySession.Request.RequestHeaders.First(x => x.Name.ToLower() == "content-encoding").Value;
} }
//check if the request have chunked body (body send chunck by chunck without a fixed length)
if (ProxySession.Request.RequestHeaders.Any(x => x.Name.ToLower() == "transfer-encoding")) if (ProxySession.Request.RequestHeaders.Any(x => x.Name.ToLower() == "transfer-encoding"))
{ {
var transferEncoding = var transferEncoding =
...@@ -103,13 +133,14 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -103,13 +133,14 @@ namespace Titanium.Web.Proxy.EventArguments
} }
} }
//If not chunked then its easy just read the whole body with the content length mentioned in the request header
if (requestContentEncoding == null && !isChunked) if (requestContentEncoding == null && !isChunked)
ProxySession.Request.RequestBody = this.Client.ClientStreamReader.ReadBytes(RequestContentLength); ProxySession.Request.RequestBody = this.Client.ClientStreamReader.ReadBytes(RequestContentLength);
else else
{ {
using (var requestBodyStream = new MemoryStream()) using (var requestBodyStream = new MemoryStream())
{ {
//For chunked request we need to read data as they arrive, until we reach a chunk end symbol
if (isChunked) if (isChunked)
{ {
while (true) while (true)
...@@ -126,6 +157,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -126,6 +157,7 @@ namespace Titanium.Web.Proxy.EventArguments
} }
else else
{ {
//chunk end
this.Client.ClientStreamReader.ReadLine(); this.Client.ClientStreamReader.ReadLine();
break; break;
} }
...@@ -134,6 +166,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -134,6 +166,7 @@ namespace Titanium.Web.Proxy.EventArguments
try try
{ {
//decompress
switch (requestContentEncoding) switch (requestContentEncoding)
{ {
case "gzip": case "gzip":
...@@ -152,20 +185,29 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -152,20 +185,29 @@ namespace Titanium.Web.Proxy.EventArguments
} }
catch catch
{ {
//if decompression fails, just assign the body stream as it it
//Not a safe option
ProxySession.Request.RequestBody = requestBodyStream.ToArray(); ProxySession.Request.RequestBody = requestBodyStream.ToArray();
} }
} }
} }
} }
//Now set the flag to true
//So that next time we can deliver body from cache
ProxySession.Request.RequestBodyRead = true; ProxySession.Request.RequestBodyRead = true;
} }
/// <summary>
/// Read response body as byte[] for current response
/// </summary>
private void ReadResponseBody() private void ReadResponseBody()
{ {
//If not already read (not cached yet)
if (ProxySession.Response.ResponseBody == null) if (ProxySession.Response.ResponseBody == null)
{ {
using (var responseBodyStream = new MemoryStream()) using (var responseBodyStream = new MemoryStream())
{ {
//If chuncked the read chunk by chunk until we hit chunk end symbol
if (ProxySession.Response.IsChunked) if (ProxySession.Response.IsChunked)
{ {
while (true) while (true)
...@@ -182,6 +224,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -182,6 +224,7 @@ namespace Titanium.Web.Proxy.EventArguments
} }
else else
{ {
//chuck end
ProxySession.ProxyClient.ServerStreamReader.ReadLine(); ProxySession.ProxyClient.ServerStreamReader.ReadLine();
break; break;
} }
...@@ -189,10 +232,11 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -189,10 +232,11 @@ namespace Titanium.Web.Proxy.EventArguments
} }
else else
{ {
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(ProxySession.Response.ContentLength); var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(ProxySession.Response.ContentLength);
responseBodyStream.Write(buffer, 0, buffer.Length); responseBodyStream.Write(buffer, 0, buffer.Length);
} }
//decompress
switch (ProxySession.Response.ContentEncoding) switch (ProxySession.Response.ContentEncoding)
{ {
case "gzip": case "gzip":
...@@ -209,19 +253,15 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -209,19 +253,15 @@ namespace Titanium.Web.Proxy.EventArguments
break; break;
} }
} }
//set this to true for caching
ProxySession.Response.ResponseBodyRead = true; ProxySession.Response.ResponseBodyRead = true;
} }
} }
/// <summary>
public Encoding GetRequestBodyEncoding() /// Gets the request body as bytes
{ /// </summary>
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server."); /// <returns></returns>
return ProxySession.Request.Encoding;
}
public byte[] GetRequestBody() public byte[] GetRequestBody()
{ {
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server."); if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
...@@ -229,7 +269,10 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -229,7 +269,10 @@ namespace Titanium.Web.Proxy.EventArguments
ReadRequestBody(); ReadRequestBody();
return ProxySession.Request.RequestBody; return ProxySession.Request.RequestBody;
} }
/// <summary>
/// Gets the request body as string
/// </summary>
/// <returns></returns>
public string GetRequestBodyAsString() public string GetRequestBodyAsString()
{ {
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server."); if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
...@@ -237,13 +280,19 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -237,13 +280,19 @@ namespace Titanium.Web.Proxy.EventArguments
ReadRequestBody(); ReadRequestBody();
//Use the encoding specified in request to decode the byte[] data to string
return ProxySession.Request.RequestBodyString ?? (ProxySession.Request.RequestBodyString = ProxySession.Request.Encoding.GetString(ProxySession.Request.RequestBody)); return ProxySession.Request.RequestBodyString ?? (ProxySession.Request.RequestBodyString = ProxySession.Request.Encoding.GetString(ProxySession.Request.RequestBody));
} }
/// <summary>
/// Sets the request body
/// </summary>
/// <param name="body"></param>
public void SetRequestBody(byte[] body) public void SetRequestBody(byte[] body)
{ {
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server."); if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
//syphon out the request body from client before setting the new body
if (!ProxySession.Request.RequestBodyRead) if (!ProxySession.Request.RequestBodyRead)
{ {
ReadRequestBody(); ReadRequestBody();
...@@ -253,10 +302,15 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -253,10 +302,15 @@ namespace Titanium.Web.Proxy.EventArguments
ProxySession.Request.RequestBodyRead = true; ProxySession.Request.RequestBodyRead = true;
} }
/// <summary>
/// Sets the body with the specified string
/// </summary>
/// <param name="body"></param>
public void SetRequestBodyString(string body) public void SetRequestBodyString(string body)
{ {
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server."); if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
//syphon out the request body from client before setting the new body
if (!ProxySession.Request.RequestBodyRead) if (!ProxySession.Request.RequestBodyRead)
{ {
ReadRequestBody(); ReadRequestBody();
...@@ -266,13 +320,10 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -266,13 +320,10 @@ namespace Titanium.Web.Proxy.EventArguments
ProxySession.Request.RequestBodyRead = true; ProxySession.Request.RequestBodyRead = true;
} }
public Encoding GetResponseBodyEncoding() /// <summary>
{ /// Gets the response body as byte array
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server."); /// </summary>
/// <returns></returns>
return ProxySession.Response.Encoding;
}
public byte[] GetResponseBody() public byte[] GetResponseBody()
{ {
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server."); if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
...@@ -281,6 +332,10 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -281,6 +332,10 @@ namespace Titanium.Web.Proxy.EventArguments
return ProxySession.Response.ResponseBody; return ProxySession.Response.ResponseBody;
} }
/// <summary>
/// Gets the response body as string
/// </summary>
/// <returns></returns>
public string GetResponseBodyAsString() public string GetResponseBodyAsString()
{ {
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server."); if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
...@@ -290,10 +345,15 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -290,10 +345,15 @@ namespace Titanium.Web.Proxy.EventArguments
return ProxySession.Response.ResponseBodyString ?? (ProxySession.Response.ResponseBodyString = ProxySession.Response.Encoding.GetString(ProxySession.Response.ResponseBody)); return ProxySession.Response.ResponseBodyString ?? (ProxySession.Response.ResponseBodyString = ProxySession.Response.Encoding.GetString(ProxySession.Response.ResponseBody));
} }
/// <summary>
/// Set the response body bytes
/// </summary>
/// <param name="body"></param>
public void SetResponseBody(byte[] body) public void SetResponseBody(byte[] body)
{ {
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server."); if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
//syphon out the response body from server before setting the new body
if (ProxySession.Response.ResponseBody == null) if (ProxySession.Response.ResponseBody == null)
{ {
GetResponseBody(); GetResponseBody();
...@@ -302,10 +362,15 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -302,10 +362,15 @@ namespace Titanium.Web.Proxy.EventArguments
ProxySession.Response.ResponseBody = body; ProxySession.Response.ResponseBody = body;
} }
/// <summary>
/// Replace the response body with the specified string
/// </summary>
/// <param name="body"></param>
public void SetResponseBodyString(string body) public void SetResponseBodyString(string body)
{ {
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server."); if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
//syphon out the response body from server before setting the new body
if (ProxySession.Response.ResponseBody == null) if (ProxySession.Response.ResponseBody == null)
{ {
GetResponseBody(); GetResponseBody();
...@@ -316,6 +381,14 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -316,6 +381,14 @@ namespace Titanium.Web.Proxy.EventArguments
} }
/// <summary>
/// Before request is made to server
/// Respond with the specified HTML string to client
/// and ignore the request
/// Marking as obsolete, need to comeup with a generic responder method in future
/// </summary>
/// <param name="html"></param>
[Obsolete]
public void Ok(string html) public void Ok(string html)
{ {
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server."); if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
......
...@@ -239,8 +239,6 @@ namespace Titanium.Web.Proxy ...@@ -239,8 +239,6 @@ namespace Titanium.Web.Proxy
args.Client.ClientStreamWriter = clientStreamWriter; args.Client.ClientStreamWriter = clientStreamWriter;
args.ProxySession.Request.Hostname = args.ProxySession.Request.RequestUri.Host; args.ProxySession.Request.Hostname = args.ProxySession.Request.RequestUri.Host;
args.ProxySession.Request.Url = args.ProxySession.Request.RequestUri.OriginalString; args.ProxySession.Request.Url = args.ProxySession.Request.RequestUri.OriginalString;
args.Client.ClientPort = ((IPEndPoint)client.Client.RemoteEndPoint).Port;
args.Client.ClientIpAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
//If requested interception //If requested interception
......
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