Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ public Task OnCertificateValidation(object sender, CertificateValidationEventArg
{
//set IsValid to true/false based on Certificate Errors
if (e.SslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
e.IsValid = true;
}

return Task.FromResult(0);
}
Expand Down
7 changes: 6 additions & 1 deletion Titanium.Web.Proxy/CertificateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ internal bool ValidateServerCertificate(
}

if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}

//By default
//do not allow this client to communicate with unauthenticated servers.
Expand Down Expand Up @@ -82,13 +84,17 @@ internal X509Certificate SelectClientCertificate(
{
string issuer = certificate.Issuer;
if (Array.IndexOf(acceptableIssuers, issuer) != -1)
{
clientCertificate = certificate;
}
}
}

if (localCertificates != null &&
localCertificates.Count > 0)
{
clientCertificate = localCertificates[0];
}

//If user call back is registered
if (ClientCertificateSelectionCallback != null)
Expand All @@ -115,7 +121,6 @@ internal X509Certificate SelectClientCertificate(
}

return clientCertificate;

}
}
}
53 changes: 45 additions & 8 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ private async Task ReadRequestBody()
if ((WebSession.Request.Method.ToUpper() != "POST" && WebSession.Request.Method.ToUpper() != "PUT"))
{
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 (WebSession.Request.RequestBody == null)
{

//If chunked then its easy just read the whole body with the content length mentioned in the request header

using (var requestBodyStream = new MemoryStream())
{
//For chunked request we need to read data as they arrive, until we reach a chunk end symbol
Expand All @@ -94,13 +94,17 @@ private async Task ReadRequestBody()
if (WebSession.Request.ContentLength > 0)
{
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream, WebSession.Request.ContentLength);
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream,
WebSession.Request.ContentLength);

}
else if (WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0)
{
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, requestBodyStream, long.MaxValue);
}
}
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray());
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding,
requestBodyStream.ToArray());
}

//Now set the flag to true
Expand Down Expand Up @@ -130,14 +134,18 @@ private async Task ReadResponseBody()
if (WebSession.Response.ContentLength > 0)
{
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, WebSession.Response.ContentLength);
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream,
WebSession.Response.ContentLength);

}
else if (WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0)
{
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, long.MaxValue);
}
}

WebSession.Response.ResponseBody = await GetDecompressedResponseBody(WebSession.Response.ContentEncoding, responseBodyStream.ToArray());
WebSession.Response.ResponseBody = await GetDecompressedResponseBody(WebSession.Response.ContentEncoding,
responseBodyStream.ToArray());

}
//set this to true for caching
Expand All @@ -154,7 +162,9 @@ public async Task<byte[]> GetRequestBody()
if (!WebSession.Request.RequestBodyRead)
{
if (WebSession.Request.RequestLocked)
{
throw new Exception("You cannot call this function after request is made to server.");
}

await ReadRequestBody();
}
Expand All @@ -169,7 +179,9 @@ public async Task<string> GetRequestBodyAsString()
if (!WebSession.Request.RequestBodyRead)
{
if (WebSession.Request.RequestLocked)
{
throw new Exception("You cannot call this function after request is made to server.");
}

await ReadRequestBody();
}
Expand All @@ -184,7 +196,9 @@ public async Task<string> GetRequestBodyAsString()
public async Task SetRequestBody(byte[] body)
{
if (WebSession.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 (!WebSession.Request.RequestBodyRead)
Expand All @@ -195,9 +209,13 @@ public async Task SetRequestBody(byte[] body)
WebSession.Request.RequestBody = body;

if (WebSession.Request.IsChunked == false)
{
WebSession.Request.ContentLength = body.Length;
}
else
{
WebSession.Request.ContentLength = -1;
}
}

/// <summary>
Expand All @@ -207,7 +225,9 @@ public async Task SetRequestBody(byte[] body)
public async Task SetRequestBodyString(string body)
{
if (WebSession.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 (!WebSession.Request.RequestBodyRead)
Expand All @@ -226,7 +246,9 @@ public async Task SetRequestBodyString(string body)
public async Task<byte[]> GetResponseBody()
{
if (!WebSession.Request.RequestLocked)
{
throw new Exception("You cannot call this function before request is made to server.");
}

await ReadResponseBody();
return WebSession.Response.ResponseBody;
Expand All @@ -239,11 +261,14 @@ public async Task<byte[]> GetResponseBody()
public async Task<string> GetResponseBodyAsString()
{
if (!WebSession.Request.RequestLocked)
{
throw new Exception("You cannot call this function before request is made to server.");
}

await GetResponseBody();

return WebSession.Response.ResponseBodyString ?? (WebSession.Response.ResponseBodyString = WebSession.Response.Encoding.GetString(WebSession.Response.ResponseBody));
return WebSession.Response.ResponseBodyString ??
(WebSession.Response.ResponseBodyString = WebSession.Response.Encoding.GetString(WebSession.Response.ResponseBody));
}

/// <summary>
Expand All @@ -253,7 +278,9 @@ public async Task<string> GetResponseBodyAsString()
public async Task SetResponseBody(byte[] body)
{
if (!WebSession.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 (WebSession.Response.ResponseBody == null)
Expand All @@ -265,9 +292,13 @@ public async Task SetResponseBody(byte[] body)

//If there is a content length header update it
if (WebSession.Response.IsChunked == false)
{
WebSession.Response.ContentLength = body.Length;
}
else
{
WebSession.Response.ContentLength = -1;
}
}

/// <summary>
Expand All @@ -277,7 +308,9 @@ public async Task SetResponseBody(byte[] body)
public async Task SetResponseBodyString(string body)
{
if (!WebSession.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 (WebSession.Response.ResponseBody == null)
Expand Down Expand Up @@ -308,10 +341,14 @@ private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte
public async Task Ok(string html)
{
if (WebSession.Request.RequestLocked)
{
throw new Exception("You cannot call this function after request is made to server.");
}

if (html == null)
{
html = string.Empty;
}

var result = Encoding.Default.GetBytes(html);

Expand Down Expand Up @@ -341,7 +378,7 @@ public async Task Redirect(string url)
var response = new RedirectResponse();

response.HttpVersion = WebSession.Request.HttpVersion;
response.ResponseHeaders.Add(new Models.HttpHeader("Location", url));
response.ResponseHeaders.Add("Location", new Models.HttpHeader("Location", url));
response.ResponseBody = Encoding.ASCII.GetBytes(string.Empty);

await Respond(response);
Expand Down
2 changes: 2 additions & 0 deletions Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ internal static Encoding GetEncoding(this Request request)
{
//return default if not specified
if (request.ContentType == null)
{
return Encoding.GetEncoding("ISO-8859-1");
}

//extract the encoding by finding the charset
var contentTypes = request.ContentType.Split(ProxyConstants.SemiColonSplit);
Expand Down
2 changes: 2 additions & 0 deletions Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ internal static Encoding GetResponseCharacterEncoding(this Response response)
{
//return default if not specified
if (response.ContentType == null)
{
return Encoding.GetEncoding("ISO-8859-1");
}

//extract the encoding by finding the charset
var contentTypes = response.ContentType.Split(ProxyConstants.SemiColonSplit);
Expand Down
14 changes: 14 additions & 0 deletions Titanium.Web.Proxy/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal static async Task CopyToAsync(this Stream input, string initialData, St
var bytes = Encoding.ASCII.GetBytes(initialData);
await output.WriteAsync(bytes, 0, bytes.Length);
}

await input.CopyToAsync(output);
}
/// <summary>
Expand All @@ -45,15 +46,19 @@ internal static async Task CopyBytesToStream(this CustomBinaryReader streamReade
bytesToRead = totalBytesToRead;
}
else
{
bytesToRead = bufferSize;
}


while (totalbytesRead < totalBytesToRead)
{
var buffer = await streamReader.ReadBytesAsync(bufferSize, bytesToRead);

if (buffer.Length == 0)
{
break;
}

totalbytesRead += buffer.Length;

Expand All @@ -62,6 +67,7 @@ internal static async Task CopyBytesToStream(this CustomBinaryReader streamReade
{
bytesToRead = remainingBytes;
}

await stream.WriteAsync(buffer, 0, buffer.Length);
}
}
Expand Down Expand Up @@ -107,7 +113,9 @@ internal static async Task WriteResponseBody(this Stream clientStream, byte[] da
await clientStream.WriteAsync(data, 0, data.Length);
}
else
{
await WriteResponseBodyChunked(data, clientStream);
}
}
/// <summary>
/// Copies the specified content length number of bytes to the output stream from the given inputs stream
Expand All @@ -124,12 +132,16 @@ internal static async Task WriteResponseBody(this CustomBinaryReader inStreamRea
{
//http 1.0
if (ContentLength == -1)
{
ContentLength = long.MaxValue;
}

int bytesToRead = bufferSize;

if (ContentLength < bufferSize)
{
bytesToRead = (int)ContentLength;
}

var buffer = new byte[bufferSize];

Expand All @@ -150,7 +162,9 @@ internal static async Task WriteResponseBody(this CustomBinaryReader inStreamRea
}
}
else
{
await WriteResponseBodyChunked(inStreamReader, bufferSize, outStream);
}
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions Titanium.Web.Proxy/Extensions/TcpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal static bool IsConnected(this Socket client)
{
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;

try
{
byte[] tmp = new byte[1];
Expand All @@ -25,7 +26,9 @@ internal static bool IsConnected(this Socket client)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
{
return true;
}
else
{
return false;
Expand Down
5 changes: 5 additions & 0 deletions Titanium.Web.Proxy/Helpers/SystemProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ internal class HttpSystemProxyValue
public override string ToString()
{
if (!IsHttps)
{
return "http=" + HostName + ":" + Port;
}
else
{
return "https=" + HostName + ":" + Port;
}
}
}
/// <summary>
Expand All @@ -44,6 +48,7 @@ internal void SetHttpProxy(string hostname, int port)
{
var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);

if (reg != null)
{
prepareRegistry(reg);
Expand Down
Loading