Commit 6f57a319 authored by Honfika's avatar Honfika

fixes

parent 27791e02
......@@ -103,7 +103,7 @@ namespace Titanium.Web.Proxy
}
// write back successful CONNECT response
var response = ConnectResponse.CreateSuccessfulConnectResponse(requestLine.Version);
var response = ConnectResponse.CreateSuccessfulConnectResponse(connectRequest.HttpVersion);
// Set ContentLength explicitly to properly handle HTTP 1.0
response.ContentLength = 0;
......
using System;
using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Extensions
{
internal static class UriExtensions
{
internal static string GetOriginalPathAndQuery(this Uri uri)
public static string GetOriginalPathAndQuery(this Uri uri)
{
string leftPart = uri.GetLeftPart(UriPartial.Authority);
if (uri.OriginalString.StartsWith(leftPart))
......@@ -12,5 +13,47 @@ namespace Titanium.Web.Proxy.Extensions
return uri.IsWellFormedOriginalString() ? uri.PathAndQuery : uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
}
public static ByteString GetScheme(ByteString str)
{
if (str.Length < 3)
{
return ByteString.Empty;
}
// regex: "^[a-z]*://"
int i;
for (i = 0; i < str.Length - 3; i++)
{
byte ch = str[i];
if (ch == ':')
{
break;
}
if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
{
return ByteString.Empty;
}
}
if (str[i++] != ':')
{
return ByteString.Empty;
}
if (str[i++] != '/')
{
return ByteString.Empty;
}
if (str[i] != '/')
{
return ByteString.Empty;
}
return new ByteString(str.Data.Slice(0, i - 2));
}
}
}
......@@ -2,6 +2,7 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network.Tcp;
......@@ -113,10 +114,21 @@ namespace Titanium.Web.Proxy.Http
string? upstreamProxyPassword = null;
string url;
if (!useUpstreamProxy || isTransparent)
if (isTransparent)
{
url = Request.RequestUriString;
}
else if (!useUpstreamProxy)
{
if (UriExtensions.GetScheme(Request.RequestUriString8).Length == 0)
{
url = Request.RequestUriString;
}
else
{
url = Request.RequestUri.GetOriginalPathAndQuery();
}
}
else
{
url = Request.RequestUri.ToString();
......@@ -129,6 +141,11 @@ namespace Titanium.Web.Proxy.Http
}
}
if (url == string.Empty)
{
url = "/";
}
// prepare the request & headers
var headerBuilder = new HeaderBuilder();
headerBuilder.WriteRequestLine(Request.Method, url, Request.HttpVersion);
......
......@@ -30,7 +30,7 @@ namespace Titanium.Web.Proxy.Http
set
{
requestUriString8 = value;
var scheme = getUriScheme(value);
var scheme = UriExtensions.GetScheme(value);
if (scheme.Length > 0)
{
IsHttps = scheme.Equals(ProxyServer.UriSchemeHttps8);
......@@ -71,7 +71,7 @@ namespace Titanium.Web.Proxy.Http
get
{
string url = RequestUriString8.GetString();
if (getUriScheme(RequestUriString8).Length == 0)
if (UriExtensions.GetScheme(RequestUriString8).Length == 0)
{
string? hostAndPath = Host ?? Authority.GetString();
......@@ -105,7 +105,7 @@ namespace Titanium.Web.Proxy.Http
{
RequestUriString8 = (ByteString)value;
var scheme = getUriScheme(RequestUriString8);
var scheme = UriExtensions.GetScheme(RequestUriString8);
if (scheme.Length > 0 && Host != null)
{
var uri = new Uri(value);
......@@ -307,47 +307,5 @@ namespace Titanium.Web.Proxy.Http
return true;
}
private ByteString getUriScheme(ByteString str)
{
if (str.Length < 3)
{
return ByteString.Empty;
}
// regex: "^[a-z]*://"
int i;
for (i = 0; i < str.Length - 3; i++)
{
byte ch = str[i];
if (ch == ':')
{
break;
}
if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
{
return ByteString.Empty;
}
}
if (str[i++] != ':')
{
return ByteString.Empty;
}
if (str[i++] != '/')
{
return ByteString.Empty;
}
if (str[i] != '/')
{
return ByteString.Empty;
}
return new ByteString(str.Data.Slice(0, i - 2));
}
}
}
......@@ -439,7 +439,7 @@ namespace Titanium.Web.Proxy
if (systemProxySettingsManager == null)
{
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
Please manually confugure you operating system to use this proxy's port and address.");
Please manually configure you operating system to use this proxy's port and address.");
}
validateEndPointAsSystemProxy(endPoint);
......
......@@ -68,9 +68,10 @@ namespace Titanium.Web.Proxy
UserData = connectArgs?.UserData
};
var request = args.HttpClient.Request;
if (isHttps)
{
args.HttpClient.Request.IsHttps = true;
request.IsHttps = true;
}
try
......@@ -81,7 +82,6 @@ namespace Titanium.Web.Proxy
await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
cancellationToken);
var request = args.HttpClient.Request;
if (connectRequest != null)
{
request.IsHttps = connectRequest.IsHttps;
......@@ -93,6 +93,12 @@ namespace Titanium.Web.Proxy
request.Method = requestLine.Method;
request.HttpVersion = requestLine.Version;
// we need this to syphon out data from connection if API user changes them.
request.SetOriginalHeaders();
// If user requested interception do it
await onBeforeRequest(args);
if (!args.IsTransparent && !args.IsSocks)
{
// proxy authorization check
......@@ -117,12 +123,6 @@ namespace Titanium.Web.Proxy
await args.GetRequestBody(cancellationToken);
}
// we need this to syphon out data from connection if API user changes them.
request.SetOriginalHeaders();
// If user requested interception do it
await onBeforeRequest(args);
var response = args.HttpClient.Response;
if (request.CancelRequest)
......
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