Commit a013e311 authored by justcoding121's avatar justcoding121

#428 Fix incorrect compression relay

parent b06dd785
......@@ -18,21 +18,8 @@ namespace Titanium.Web.Proxy.Examples.Basic
private readonly ProxyServer proxyServer;
// keep track of request headers
private readonly IDictionary<Guid, HeaderCollection> requestHeaderHistory =
new ConcurrentDictionary<Guid, HeaderCollection>();
// keep track of response headers
private readonly IDictionary<Guid, HeaderCollection> responseHeaderHistory =
new ConcurrentDictionary<Guid, HeaderCollection>();
private ExplicitProxyEndPoint explicitEndPoint;
// share requestBody outside handlers
// Using a dictionary is not a good idea since it can cause memory overflow
// ideally the data should be moved out of memory
// private readonly IDictionary<Guid, string> requestBodyHistory = new ConcurrentDictionary<Guid, string>();
public ProxyTestController()
{
proxyServer = new ProxyServer();
......@@ -164,12 +151,14 @@ namespace Titanium.Web.Proxy.Examples.Basic
WriteToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
WriteToConsole(e.WebSession.Request.Url);
// create custom id for the request and store it in the UserData property
// store it in the UserData property
// It can be a simple integer, Guid, or any type
e.UserData = Guid.NewGuid();
// read request headers
requestHeaderHistory[(Guid)e.UserData] = e.WebSession.Request.Headers;
//e.UserData = new CustomUserData()
//{
// RequestHeaders = e.WebSession.Request.Headers,
// RequestBody = e.WebSession.Request.HasBody ? e.WebSession.Request.Body:null,
// RequestBodyString = e.WebSession.Request.HasBody? e.WebSession.Request.BodyString:null
//};
////This sample shows how to get the multipart form data headers
//if (e.WebSession.Request.Host == "mail.yahoo.com" && e.WebSession.Request.IsMultipartFormData)
......@@ -177,19 +166,6 @@ namespace Titanium.Web.Proxy.Examples.Basic
// e.MultipartRequestPartSent += MultipartRequestPartSent;
//}
//if (e.WebSession.Request.HasBody)
//{
// // Get/Set request body bytes
// var bodyBytes = await e.GetRequestBody();
// await e.SetRequestBody(bodyBytes);
// // Get/Set request body as string
// string bodyString = await e.GetRequestBodyAsString();
// await e.SetRequestBodyString(bodyString);
// //requestBodyHistory[e.Id] = bodyString;
//}
// To cancel a request with a custom HTML content
// Filter URL
//if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("yahoo.com"))
......@@ -227,6 +203,9 @@ namespace Titanium.Web.Proxy.Examples.Basic
string ext = System.IO.Path.GetExtension(e.WebSession.Request.RequestUri.AbsolutePath);
//access user data set in request to do something with it
//var userData = e.WebSession.UserData as CustomUserData;
//if (ext == ".gif" || ext == ".png" || ext == ".jpg")
//{
// byte[] btBody = Encoding.UTF8.GetBytes("<!DOCTYPE html>" +
......@@ -243,15 +222,6 @@ namespace Titanium.Web.Proxy.Examples.Basic
// e.Respond(response);
// e.TerminateServerConnection();
//}
//if (requestBodyHistory.ContainsKey(e.Id))
//{
// // access request body by looking up the shared dictionary using requestId
// var requestBody = requestBodyHistory[e.Id];
//}
////read response headers
//responseHeaderHistory[e.Id] = e.WebSession.Response.Headers;
//// print out process id of current session
////WriteToConsole($"PID: {e.WebSession.ProcessId.Value}");
......@@ -308,5 +278,16 @@ namespace Titanium.Web.Proxy.Examples.Basic
Console.WriteLine(message);
}
}
///// <summary>
///// User data object as defined by user.
///// User data can be set to each SessionEventArgs.WebSession.UserData property
///// </summary>
//public class CustomUserData
//{
// public HeaderCollection RequestHeaders { get; set; }
// public byte[] RequestBody { get; set; }
// public string RequestBodyString { get; set; }
//}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
......@@ -24,9 +25,15 @@ namespace Titanium.Web.Proxy
private static readonly Regex uriSchemeRegex =
new Regex("^[a-z]*://", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly HashSet<string> proxySupportedCompressions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"gzip",
"deflate"
};
private bool isWindowsAuthenticationEnabledAndSupported =>
EnableWinAuth && RunTime.IsWindows && !RunTime.IsRunningOnMono;
/// <summary>
/// This is the core request handler method for a particular connection from client.
/// Will create new session (request/response) sequence until
......@@ -404,12 +411,23 @@ namespace Titanium.Web.Proxy
/// <summary>
/// Prepare the request headers so that we can avoid encodings not parsable by this proxy
/// </summary>
/// <param name="requestHeaders"></param>
private void PrepareRequestHeaders(HeaderCollection requestHeaders)
{
if (requestHeaders.HeaderExists(KnownHeaders.AcceptEncoding))
var acceptEncoding = requestHeaders.GetHeaderValueOrNull(KnownHeaders.AcceptEncoding);
if (acceptEncoding != null)
{
requestHeaders.SetOrAddHeaderValue(KnownHeaders.AcceptEncoding, "gzip,deflate");
var supporedAcceptEncoding = new List<string>();
//only allow proxy supported compressions
supporedAcceptEncoding.AddRange(acceptEncoding.Split(',')
.Select(x => x.Trim())
.Where(x => proxySupportedCompressions.Contains(x)));
//uncompressed is always supported by proxy
supporedAcceptEncoding.Add("identity");
requestHeaders.SetOrAddHeaderValue(KnownHeaders.AcceptEncoding, string.Join(",", supporedAcceptEncoding));
}
requestHeaders.FixProxyHeaders();
......
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