Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 6f57a31

Browse files
committed
fixes
1 parent 27791e0 commit 6f57a31

File tree

6 files changed

+75
-57
lines changed

6 files changed

+75
-57
lines changed

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
103103
}
104104

105105
// write back successful CONNECT response
106-
var response = ConnectResponse.CreateSuccessfulConnectResponse(requestLine.Version);
106+
var response = ConnectResponse.CreateSuccessfulConnectResponse(connectRequest.HttpVersion);
107107

108108
// Set ContentLength explicitly to properly handle HTTP 1.0
109109
response.ContentLength = 0;
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,59 @@
11
using System;
2+
using Titanium.Web.Proxy.Models;
23

34
namespace Titanium.Web.Proxy.Extensions
45
{
56
internal static class UriExtensions
67
{
7-
internal static string GetOriginalPathAndQuery(this Uri uri)
8+
public static string GetOriginalPathAndQuery(this Uri uri)
89
{
910
string leftPart = uri.GetLeftPart(UriPartial.Authority);
1011
if (uri.OriginalString.StartsWith(leftPart))
1112
return uri.OriginalString.Substring(leftPart.Length);
1213

1314
return uri.IsWellFormedOriginalString() ? uri.PathAndQuery : uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
1415
}
16+
17+
public static ByteString GetScheme(ByteString str)
18+
{
19+
if (str.Length < 3)
20+
{
21+
return ByteString.Empty;
22+
}
23+
24+
// regex: "^[a-z]*://"
25+
int i;
26+
27+
for (i = 0; i < str.Length - 3; i++)
28+
{
29+
byte ch = str[i];
30+
if (ch == ':')
31+
{
32+
break;
33+
}
34+
35+
if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
36+
{
37+
return ByteString.Empty;
38+
}
39+
}
40+
41+
if (str[i++] != ':')
42+
{
43+
return ByteString.Empty;
44+
}
45+
46+
if (str[i++] != '/')
47+
{
48+
return ByteString.Empty;
49+
}
50+
51+
if (str[i] != '/')
52+
{
53+
return ByteString.Empty;
54+
}
55+
56+
return new ByteString(str.Data.Slice(0, i - 2));
57+
}
1558
}
1659
}

src/Titanium.Web.Proxy/Http/HttpWebClient.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net;
33
using System.Threading;
44
using System.Threading.Tasks;
5+
using Titanium.Web.Proxy.Extensions;
56
using Titanium.Web.Proxy.Models;
67
using Titanium.Web.Proxy.Network.Tcp;
78

@@ -113,10 +114,21 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar
113114
string? upstreamProxyPassword = null;
114115

115116
string url;
116-
if (!useUpstreamProxy || isTransparent)
117+
if (isTransparent)
117118
{
118119
url = Request.RequestUriString;
119120
}
121+
else if (!useUpstreamProxy)
122+
{
123+
if (UriExtensions.GetScheme(Request.RequestUriString8).Length == 0)
124+
{
125+
url = Request.RequestUriString;
126+
}
127+
else
128+
{
129+
url = Request.RequestUri.GetOriginalPathAndQuery();
130+
}
131+
}
120132
else
121133
{
122134
url = Request.RequestUri.ToString();
@@ -129,6 +141,11 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar
129141
}
130142
}
131143

144+
if (url == string.Empty)
145+
{
146+
url = "/";
147+
}
148+
132149
// prepare the request & headers
133150
var headerBuilder = new HeaderBuilder();
134151
headerBuilder.WriteRequestLine(Request.Method, url, Request.HttpVersion);

src/Titanium.Web.Proxy/Http/Request.cs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal ByteString RequestUriString8
3030
set
3131
{
3232
requestUriString8 = value;
33-
var scheme = getUriScheme(value);
33+
var scheme = UriExtensions.GetScheme(value);
3434
if (scheme.Length > 0)
3535
{
3636
IsHttps = scheme.Equals(ProxyServer.UriSchemeHttps8);
@@ -71,7 +71,7 @@ public string Url
7171
get
7272
{
7373
string url = RequestUriString8.GetString();
74-
if (getUriScheme(RequestUriString8).Length == 0)
74+
if (UriExtensions.GetScheme(RequestUriString8).Length == 0)
7575
{
7676
string? hostAndPath = Host ?? Authority.GetString();
7777

@@ -105,7 +105,7 @@ public string RequestUriString
105105
{
106106
RequestUriString8 = (ByteString)value;
107107

108-
var scheme = getUriScheme(RequestUriString8);
108+
var scheme = UriExtensions.GetScheme(RequestUriString8);
109109
if (scheme.Length > 0 && Host != null)
110110
{
111111
var uri = new Uri(value);
@@ -307,47 +307,5 @@ private static bool isAllUpper(string input)
307307

308308
return true;
309309
}
310-
311-
private ByteString getUriScheme(ByteString str)
312-
{
313-
if (str.Length < 3)
314-
{
315-
return ByteString.Empty;
316-
}
317-
318-
// regex: "^[a-z]*://"
319-
int i;
320-
321-
for (i = 0; i < str.Length - 3; i++)
322-
{
323-
byte ch = str[i];
324-
if (ch == ':')
325-
{
326-
break;
327-
}
328-
329-
if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
330-
{
331-
return ByteString.Empty;
332-
}
333-
}
334-
335-
if (str[i++] != ':')
336-
{
337-
return ByteString.Empty;
338-
}
339-
340-
if (str[i++] != '/')
341-
{
342-
return ByteString.Empty;
343-
}
344-
345-
if (str[i] != '/')
346-
{
347-
return ByteString.Empty;
348-
}
349-
350-
return new ByteString(str.Data.Slice(0, i - 2));
351-
}
352310
}
353311
}

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public void SetAsSystemProxy(ExplicitProxyEndPoint endPoint, ProxyProtocolType p
439439
if (systemProxySettingsManager == null)
440440
{
441441
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
442-
Please manually confugure you operating system to use this proxy's port and address.");
442+
Please manually configure you operating system to use this proxy's port and address.");
443443
}
444444

445445
validateEndPointAsSystemProxy(endPoint);

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, HttpClientSt
6868
UserData = connectArgs?.UserData
6969
};
7070

71+
var request = args.HttpClient.Request;
7172
if (isHttps)
7273
{
73-
args.HttpClient.Request.IsHttps = true;
74+
request.IsHttps = true;
7475
}
7576

7677
try
@@ -81,7 +82,6 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, HttpClientSt
8182
await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
8283
cancellationToken);
8384

84-
var request = args.HttpClient.Request;
8585
if (connectRequest != null)
8686
{
8787
request.IsHttps = connectRequest.IsHttps;
@@ -93,6 +93,12 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
9393
request.Method = requestLine.Method;
9494
request.HttpVersion = requestLine.Version;
9595

96+
// we need this to syphon out data from connection if API user changes them.
97+
request.SetOriginalHeaders();
98+
99+
// If user requested interception do it
100+
await onBeforeRequest(args);
101+
96102
if (!args.IsTransparent && !args.IsSocks)
97103
{
98104
// proxy authorization check
@@ -117,12 +123,6 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
117123
await args.GetRequestBody(cancellationToken);
118124
}
119125

120-
// we need this to syphon out data from connection if API user changes them.
121-
request.SetOriginalHeaders();
122-
123-
// If user requested interception do it
124-
await onBeforeRequest(args);
125-
126126
var response = args.HttpClient.Response;
127127

128128
if (request.CancelRequest)

0 commit comments

Comments
 (0)