Commit de33f72d authored by titanium007's avatar titanium007

Fix issues identified from review issue #27

parent 4ac7568b
......@@ -44,7 +44,7 @@ namespace Titanium.Web.Proxy.Test
//Read browser URL send back to proxy by the injection script in OnResponse event
public void OnRequest(object sender, SessionEventArgs e)
{
Console.WriteLine(e.ProxySession.Request.RequestUrl);
Console.WriteLine(e.ProxySession.Request.Url);
////read request headers
//var requestHeaders = e.ProxySession.Request.RequestHeaders;
......@@ -74,29 +74,23 @@ namespace Titanium.Web.Proxy.Test
//Insert script to read the Browser URL and send it back to proxy
public void OnResponse(object sender, SessionEventArgs e)
{
////read response headers
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);
// //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);
// //Set modifed response Html Body
// e.SetResponseBodyString(modified);
// }
//}
//if (!e.ProxySession.Request.Hostname.Equals("medeczane.sgk.gov.tr")) return;
if (e.RequestMethod == "GET" || e.RequestMethod == "POST")
{
if (e.ProxySession.Response.ResponseStatusCode == "200")
{
if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
{
string body = e.GetResponseBodyAsString(); //This line crashes
}
}
}
}
}
}
\ No newline at end of file
......@@ -35,24 +35,18 @@ namespace Titanium.Web.Proxy.EventArguments
ProxySession = new HttpWebSession();
}
public Client Client { get; set; }
internal Client Client { get; set; }
public bool IsHttps { get; internal set; }
public HttpWebSession ProxySession { get; set; }
public int RequestContentLength
{
get
{
if (ProxySession.Request.RequestHeaders.All(x => x.Name.ToLower() != "content-length")) return -1;
int contentLen;
int.TryParse(ProxySession.Request.RequestHeaders.First(x => x.Name.ToLower() == "content-length").Value, out contentLen);
if (contentLen != 0)
return contentLen;
return -1;
return ProxySession.Request.ContentLength;
}
}
......@@ -71,9 +65,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
get
{
return ProxySession.Response.ResponseHeaders.Any(x => x.Name.ToLower() == "content-type")
? ProxySession.Response.ResponseHeaders.First(x => x.Name.ToLower() == "content-type").Value
: null;
return ProxySession.Response.ContentType;
}
}
......@@ -201,7 +193,7 @@ namespace Titanium.Web.Proxy.EventArguments
responseBodyStream.Write(buffer, 0, buffer.Length);
}
switch (ProxySession.Response.ResponseContentEncoding)
switch (ProxySession.Response.ContentEncoding)
{
case "gzip":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(responseBodyStream.ToArray());
......@@ -223,12 +215,11 @@ namespace Titanium.Web.Proxy.EventArguments
}
public Encoding GetRequestBodyEncoding()
{
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
return ProxySession.Request.RequestEncoding;
return ProxySession.Request.Encoding;
}
public byte[] GetRequestBody()
......@@ -246,7 +237,7 @@ namespace Titanium.Web.Proxy.EventArguments
ReadRequestBody();
return ProxySession.Request.RequestBodyString ?? (ProxySession.Request.RequestBodyString = ProxySession.Request.RequestEncoding.GetString(ProxySession.Request.RequestBody));
return ProxySession.Request.RequestBodyString ?? (ProxySession.Request.RequestBodyString = ProxySession.Request.Encoding.GetString(ProxySession.Request.RequestBody));
}
public void SetRequestBody(byte[] body)
......@@ -271,7 +262,7 @@ namespace Titanium.Web.Proxy.EventArguments
ReadRequestBody();
}
ProxySession.Request.RequestBody = ProxySession.Request.RequestEncoding.GetBytes(body);
ProxySession.Request.RequestBody = ProxySession.Request.Encoding.GetBytes(body);
ProxySession.Request.RequestBodyRead = true;
}
......@@ -279,7 +270,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
return ProxySession.Response.ResponseEncoding;
return ProxySession.Response.Encoding;
}
public byte[] GetResponseBody()
......@@ -296,7 +287,7 @@ namespace Titanium.Web.Proxy.EventArguments
GetResponseBody();
return ProxySession.Response.ResponseBodyString ?? (ProxySession.Response.ResponseBodyString = ProxySession.Response.ResponseEncoding.GetString(ProxySession.Response.ResponseBody));
return ProxySession.Response.ResponseBodyString ?? (ProxySession.Response.ResponseBodyString = ProxySession.Response.Encoding.GetString(ProxySession.Response.ResponseBody));
}
public void SetResponseBody(byte[] body)
......@@ -320,7 +311,7 @@ namespace Titanium.Web.Proxy.EventArguments
GetResponseBody();
}
var bodyBytes = ProxySession.Response.ResponseEncoding.GetBytes(body);
var bodyBytes = ProxySession.Response.Encoding.GetBytes(body);
SetResponseBody(bodyBytes);
}
......@@ -335,22 +326,20 @@ namespace Titanium.Web.Proxy.EventArguments
var result = Encoding.Default.GetBytes(html);
var connectStreamWriter = new StreamWriter(this.Client.ClientStream);
var s = string.Format("HTTP/{0}.{1} {2} {3}", ProxySession.Request.RequestHttpVersion.Major, ProxySession.Request.RequestHttpVersion.Minor, 200, "Ok");
connectStreamWriter.WriteLine(s);
connectStreamWriter.WriteLine(string.Format("{0} {2} {3}", ProxySession.Request.HttpVersion, 200, "Ok"));
connectStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now);
connectStreamWriter.WriteLine("content-length: " + result.Length);
connectStreamWriter.WriteLine("Cache-Control: no-cache, no-store, must-revalidate");
connectStreamWriter.WriteLine("Pragma: no-cache");
connectStreamWriter.WriteLine("Expires: 0");
connectStreamWriter.WriteLine(ProxySession.Request.RequestIsAlive ? "Connection: Keep-Alive" : "Connection: close");
connectStreamWriter.WriteLine(ProxySession.Request.IsAlive ? "Connection: Keep-Alive" : "Connection: close");
connectStreamWriter.WriteLine();
connectStreamWriter.Flush();
this.Client.ClientStream.Write(result, 0, result.Length);
ProxySession.Request.CancelRequest = true;
}
}
......
......@@ -10,9 +10,9 @@ namespace Titanium.Web.Proxy.Extensions
{
try
{
if (request.Request.RequestContentType == null) return Encoding.GetEncoding("ISO-8859-1");
if (request.Request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");
var contentTypes = request.Request.RequestContentType.Split(';');
var contentTypes = request.Request.ContentType.Split(';');
foreach (var contentType in contentTypes)
{
var encodingSplit = contentType.Split('=');
......
......@@ -8,8 +8,8 @@ namespace Titanium.Web.Proxy.Extensions
{
public static Encoding GetResponseEncoding(this HttpWebSession response)
{
if (string.IsNullOrEmpty(response.Response.ResponseCharacterSet)) return Encoding.GetEncoding("ISO-8859-1");
return Encoding.GetEncoding(response.Response.ResponseCharacterSet.Replace(@"""", string.Empty));
if (string.IsNullOrEmpty(response.Response.CharacterSet)) return Encoding.GetEncoding("ISO-8859-1");
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
}
}
}
\ No newline at end of file
......@@ -16,20 +16,19 @@ namespace Titanium.Web.Proxy.Network
{
public string Method { get; internal set; }
public Uri RequestUri { get; internal set; }
public string Version { get; internal set; }
public string HttpVersion { get; internal 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 Status { get; internal set; }
public int ContentLength { get; internal set; }
public bool SendChunked { get; internal set; }
public string ContentType { get; internal set; }
public bool KeepAlive { get; internal set; }
public string Hostname { get; internal set; }
public string RequestUrl { get; internal set; }
public string Url { get; internal set; }
internal Encoding RequestEncoding { get; set; }
internal Version RequestHttpVersion { get; set; }
internal bool RequestIsAlive { get; set; }
internal Encoding Encoding { get; set; }
internal bool IsAlive { get; set; }
internal bool CancelRequest { get; set; }
internal byte[] RequestBody { get; set; }
internal string RequestBodyString { get; set; }
......@@ -48,20 +47,20 @@ namespace Titanium.Web.Proxy.Network
public class Response
{
internal Encoding ResponseEncoding { get; set; }
internal Encoding Encoding { get; set; }
internal Stream ResponseStream { get; set; }
internal byte[] ResponseBody { get; set; }
internal string ResponseBodyString { get; set; }
internal bool ResponseBodyRead { get; set; }
internal bool ResponseLocked { get; set; }
public List<HttpHeader> ResponseHeaders { get; internal 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 string CharacterSet { get; set; }
internal string ContentEncoding { get; set; }
internal string HttpVersion { get; set; }
public string ResponseStatusCode { get; internal set; }
public string ResponseStatusDescription { get; internal set; }
internal bool ResponseKeepAlive { get; set; }
internal string ResponseContentType { get; set; }
public string ContentType { get; internal set; }
internal int ContentLength { get; set; }
internal bool IsChunked { get; set; }
......@@ -110,7 +109,7 @@ namespace Titanium.Web.Proxy.Network
{
this.Request.Method,
this.Request.RequestUri.PathAndQuery,
this.Request.Version
this.Request.HttpVersion
}));
foreach (HttpHeader httpHeader in this.Request.RequestHeaders)
......@@ -134,19 +133,8 @@ namespace Titanium.Web.Proxy.Network
{
var s = ProxyClient.ServerStreamReader.ReadLine();
}
var httpVersion = httpResult[0];
Version version;
if (httpVersion == "HTTP/1.1")
{
version = new Version(1, 1);
}
else
{
version = new Version(1, 0);
}
this.Response.ResponseProtocolVersion = version;
this.Response.HttpVersion = httpResult[0];
this.Response.ResponseStatusCode = httpResult[1];
string status = httpResult[2];
......
......@@ -177,21 +177,20 @@ namespace Titanium.Web.Proxy
args.ProxySession.Request.RequestUri = httpRemoteUri;
args.ProxySession.Request.Method = httpMethod;
args.ProxySession.Request.Version = httpVersion;
args.ProxySession.Request.HttpVersion = httpVersion;
args.Client.ClientStream = clientStream;
args.Client.ClientStreamReader = clientStreamReader;
args.Client.ClientStreamWriter = clientStreamWriter;
args.ProxySession.Request.RequestHost = args.ProxySession.Request.RequestUri.Host;
args.ProxySession.Request.RequestUrl = args.ProxySession.Request.RequestUri.OriginalString;
args.ProxySession.Request.Hostname = args.ProxySession.Request.RequestUri.Host;
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;
args.ProxySession.Request.RequestHttpVersion = version;
//If requested interception
if (BeforeRequest != null)
{
args.ProxySession.Request.RequestEncoding = args.ProxySession.GetEncoding();
args.ProxySession.Request.Encoding = args.ProxySession.GetEncoding();
BeforeRequest(null, args);
}
......@@ -207,10 +206,10 @@ namespace Titanium.Web.Proxy
//construct the web request that we are going to issue on behalf of the client.
connection = connection == null ?
TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps)
: lastRequestHostName != args.ProxySession.Request.RequestHost ? TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps)
: lastRequestHostName != args.ProxySession.Request.Hostname ? TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps)
: connection;
lastRequestHostName = args.ProxySession.Request.RequestHost;
lastRequestHostName = args.ProxySession.Request.Hostname;
args.ProxySession.SetConnection(connection);
args.ProxySession.SendRequest();
......@@ -218,7 +217,7 @@ namespace Titanium.Web.Proxy
//If request was modified by user
if (args.ProxySession.Request.RequestBodyRead)
{
args.ProxySession.Request.RequestContentLength = args.ProxySession.Request.RequestBody.Length;
args.ProxySession.Request.ContentLength = args.ProxySession.Request.RequestBody.Length;
var newStream = args.ProxySession.ProxyClient.ServerStreamReader.BaseStream;
newStream.Write(args.ProxySession.Request.RequestBody, 0, args.ProxySession.Request.RequestBody.Length);
}
......@@ -276,25 +275,25 @@ namespace Titanium.Web.Proxy
break;
case "connection":
if (requestHeaders[i].Value.ToLower() == "keep-alive")
webRequest.Request.RequestKeepAlive = true;
webRequest.Request.KeepAlive = true;
break;
case "content-length":
int contentLen;
int.TryParse(requestHeaders[i].Value, out contentLen);
if (contentLen != 0)
webRequest.Request.RequestContentLength = contentLen;
webRequest.Request.ContentLength = contentLen;
break;
case "content-type":
webRequest.Request.RequestContentType = requestHeaders[i].Value;
webRequest.Request.ContentType = requestHeaders[i].Value;
break;
case "host":
webRequest.Request.RequestHost = requestHeaders[i].Value;
webRequest.Request.Hostname = requestHeaders[i].Value;
break;
case "proxy-connection":
if (requestHeaders[i].Value.ToLower() == "keep-alive")
webRequest.Request.RequestKeepAlive = true;
webRequest.Request.KeepAlive = true;
else if (requestHeaders[i].Value.ToLower() == "close")
webRequest.Request.RequestKeepAlive = false;
webRequest.Request.KeepAlive = false;
break;
case "upgrade":
......@@ -306,9 +305,9 @@ namespace Titanium.Web.Proxy
//But how to identify if client is sending chunked body for PUT/POST?
case "transfer-encoding":
if (requestHeaders[i].Value.ToLower().Contains("chunked"))
webRequest.Request.RequestSendChunked = true;
webRequest.Request.SendChunked = true;
else
webRequest.Request.RequestSendChunked = false;
webRequest.Request.SendChunked = false;
break;
default:
......@@ -343,7 +342,7 @@ namespace Titanium.Web.Proxy
var postStream = args.ProxySession.ProxyClient.Stream;
if (args.ProxySession.Request.RequestContentLength > 0)
if (args.ProxySession.Request.ContentLength > 0)
{
//args.ProxyRequest.AllowWriteStreamBuffering = true;
try
......@@ -351,20 +350,20 @@ namespace Titanium.Web.Proxy
var totalbytesRead = 0;
int bytesToRead;
if (args.ProxySession.Request.RequestContentLength < BUFFER_SIZE)
if (args.ProxySession.Request.ContentLength < BUFFER_SIZE)
{
bytesToRead = (int)args.ProxySession.Request.RequestContentLength;
bytesToRead = (int)args.ProxySession.Request.ContentLength;
}
else
bytesToRead = BUFFER_SIZE;
while (totalbytesRead < (int)args.ProxySession.Request.RequestContentLength)
while (totalbytesRead < (int)args.ProxySession.Request.ContentLength)
{
var buffer = args.Client.ClientStreamReader.ReadBytes(bytesToRead);
totalbytesRead += buffer.Length;
var remainingBytes = (int)args.ProxySession.Request.RequestContentLength - totalbytesRead;
var remainingBytes = (int)args.ProxySession.Request.ContentLength - totalbytesRead;
if (remainingBytes < bytesToRead)
{
bytesToRead = remainingBytes;
......@@ -378,7 +377,7 @@ namespace Titanium.Web.Proxy
}
}
//Need to revist, find any potential bugs
else if (args.ProxySession.Request.RequestSendChunked)
else if (args.ProxySession.Request.SendChunked)
{
try
{
......
......@@ -31,7 +31,7 @@ namespace Titanium.Web.Proxy
if (BeforeResponse != null)
{
args.ProxySession.Response.ResponseEncoding = args.ProxySession.GetResponseEncoding();
args.ProxySession.Response.Encoding = args.ProxySession.GetResponseEncoding();
BeforeResponse(null, args);
}
......@@ -40,7 +40,7 @@ namespace Titanium.Web.Proxy
if (args.ProxySession.Response.ResponseBodyRead)
{
var isChunked = args.ProxySession.Response.IsChunked;
var contentEncoding = args.ProxySession.Response.ResponseContentEncoding;
var contentEncoding = args.ProxySession.Response.ContentEncoding;
if(contentEncoding!=null)
switch (contentEncoding)
......@@ -56,7 +56,7 @@ namespace Titanium.Web.Proxy
break;
}
WriteResponseStatus(args.ProxySession.Response.ResponseProtocolVersion, args.ProxySession.Response.ResponseStatusCode,
WriteResponseStatus(args.ProxySession.Response.HttpVersion, args.ProxySession.Response.ResponseStatusCode,
args.ProxySession.Response.ResponseStatusDescription, args.Client.ClientStreamWriter);
WriteResponseHeaders(args.Client.ClientStreamWriter, args.ProxySession.Response.ResponseHeaders, args.ProxySession.Response.ResponseBody.Length,
isChunked);
......@@ -64,7 +64,7 @@ namespace Titanium.Web.Proxy
}
else
{
WriteResponseStatus(args.ProxySession.Response.ResponseProtocolVersion, args.ProxySession.Response.ResponseStatusCode,
WriteResponseStatus(args.ProxySession.Response.HttpVersion, args.ProxySession.Response.ResponseStatusCode,
args.ProxySession.Response.ResponseStatusDescription, args.Client.ClientStreamWriter);
WriteResponseHeaders(args.Client.ClientStreamWriter, args.ProxySession.Response.ResponseHeaders);
......@@ -96,17 +96,17 @@ namespace Titanium.Web.Proxy
break;
case "content-encoding":
response.Response.ResponseContentEncoding = response.Response.ResponseHeaders[i].Value.Trim().ToLower();
response.Response.ContentEncoding = response.Response.ResponseHeaders[i].Value.Trim().ToLower();
break;
case "content-type":
if (response.Response.ResponseHeaders[i].Value.Contains(";"))
{
response.Response.ResponseContentType = response.Response.ResponseHeaders[i].Value.Split(';')[0].Trim();
response.Response.ResponseCharacterSet = response.Response.ResponseHeaders[i].Value.Split(';')[1].ToLower().Replace("charset=", string.Empty).Trim();
response.Response.ContentType = response.Response.ResponseHeaders[i].Value.Split(';')[0].Trim();
response.Response.CharacterSet = response.Response.ResponseHeaders[i].Value.Split(';')[1].ToLower().Replace("charset=", string.Empty).Trim();
}
else
response.Response.ResponseContentType = response.Response.ResponseHeaders[i].Value.ToLower().Trim();
response.Response.ContentType = response.Response.ResponseHeaders[i].Value.ToLower().Trim();
break;
case "transfer-encoding":
......@@ -128,11 +128,10 @@ namespace Titanium.Web.Proxy
}
private static void WriteResponseStatus(Version version, string code, string description,
private static void WriteResponseStatus(string version, string code, string description,
StreamWriter responseWriter)
{
var s = string.Format("HTTP/{0}.{1} {2} {3}", version.Major, version.Minor, code, description);
responseWriter.WriteLine(s);
responseWriter.WriteLine(string.Format("{0} {1} {2}", version, code, description));
}
private static void WriteResponseHeaders(StreamWriter responseWriter, List<HttpHeader> headers)
......
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