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

Commit 2b38dec

Browse files
authored
Merge pull request #732 from justcoding121/master
stable
2 parents 82ac963 + fcb8301 commit 2b38dec

File tree

8 files changed

+94
-72
lines changed

8 files changed

+94
-72
lines changed

docs/api/Titanium.Web.Proxy.Http.HttpWebClient.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ <h3 id="properties">Properties
128128
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_ConnectRequest.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.ConnectRequest%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
129129
</span>
130130
<span class="small pull-right mobile-hide">
131-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L66">View Source</a>
131+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L67">View Source</a>
132132
</span>
133133
<a id="Titanium_Web_Proxy_Http_HttpWebClient_ConnectRequest_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ConnectRequest*"></a>
134134
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_ConnectRequest" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ConnectRequest">ConnectRequest</h4>
@@ -159,7 +159,7 @@ <h5 class="propertyValue">Property Value</h5>
159159
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_IsHttps.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.IsHttps%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
160160
</span>
161161
<span class="small pull-right mobile-hide">
162-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L87">View Source</a>
162+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L88">View Source</a>
163163
</span>
164164
<a id="Titanium_Web_Proxy_Http_HttpWebClient_IsHttps_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.IsHttps*"></a>
165165
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_IsHttps" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.IsHttps">IsHttps</h4>
@@ -190,7 +190,7 @@ <h5 class="propertyValue">Property Value</h5>
190190
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_ProcessId.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.ProcessId%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
191191
</span>
192192
<span class="small pull-right mobile-hide">
193-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L82">View Source</a>
193+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L83">View Source</a>
194194
</span>
195195
<a id="Titanium_Web_Proxy_Http_HttpWebClient_ProcessId_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ProcessId*"></a>
196196
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_ProcessId" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ProcessId">ProcessId</h4>
@@ -222,7 +222,7 @@ <h5 class="propertyValue">Property Value</h5>
222222
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_Request.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.Request%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
223223
</span>
224224
<span class="small pull-right mobile-hide">
225-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L71">View Source</a>
225+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L72">View Source</a>
226226
</span>
227227
<a id="Titanium_Web_Proxy_Http_HttpWebClient_Request_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Request*"></a>
228228
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_Request" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Request">Request</h4>
@@ -253,7 +253,7 @@ <h5 class="propertyValue">Property Value</h5>
253253
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_Response.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.Response%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
254254
</span>
255255
<span class="small pull-right mobile-hide">
256-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L76">View Source</a>
256+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L77">View Source</a>
257257
</span>
258258
<a id="Titanium_Web_Proxy_Http_HttpWebClient_Response_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Response*"></a>
259259
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_Response" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Response">Response</h4>
@@ -284,7 +284,7 @@ <h5 class="propertyValue">Property Value</h5>
284284
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_UpStreamEndPoint.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.UpStreamEndPoint%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
285285
</span>
286286
<span class="small pull-right mobile-hide">
287-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L61">View Source</a>
287+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L62">View Source</a>
288288
</span>
289289
<a id="Titanium_Web_Proxy_Http_HttpWebClient_UpStreamEndPoint_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UpStreamEndPoint*"></a>
290290
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_UpStreamEndPoint" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UpStreamEndPoint">UpStreamEndPoint</h4>
@@ -315,7 +315,7 @@ <h5 class="propertyValue">Property Value</h5>
315315
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_UserData.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.UserData%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
316316
</span>
317317
<span class="small pull-right mobile-hide">
318-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L56">View Source</a>
318+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L57">View Source</a>
319319
</span>
320320
<a id="Titanium_Web_Proxy_Http_HttpWebClient_UserData_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UserData*"></a>
321321
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_UserData" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UserData">UserData</h4>
@@ -352,7 +352,7 @@ <h5 class="propertyValue">Property Value</h5>
352352
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A" class="contribution-link">Improve this Doc</a>
353353
</li>
354354
<li>
355-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L13" class="contribution-link">View Source</a>
355+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L14" class="contribution-link">View Source</a>
356356
</li>
357357
</ul>
358358
</div>

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/Network/DefaultCertificateDiskCache.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public void SaveRootCertificate(string pathOrName, string password, X509Certific
3030
/// <inheritdoc />
3131
public X509Certificate2? LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
3232
{
33-
string path = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
34-
return loadCertificate(path, string.Empty, storageFlags);
33+
string filePath = Path.Combine(getCertificatePath(false), subjectName + defaultCertificateFileExtension);
34+
return loadCertificate(filePath, string.Empty, storageFlags);
3535
}
3636

3737
/// <inheritdoc />
3838
public void SaveCertificate(string subjectName, X509Certificate2 certificate)
3939
{
40-
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
40+
string filePath = Path.Combine(getCertificatePath(true), subjectName + defaultCertificateFileExtension);
4141
byte[] exported = certificate.Export(X509ContentType.Pkcs12);
4242
File.WriteAllBytes(filePath, exported);
4343
}
@@ -46,9 +46,13 @@ public void Clear()
4646
{
4747
try
4848
{
49-
Directory.Delete(getCertificatePath(), true);
49+
string path = getCertificatePath(false);
50+
if (Directory.Exists(path))
51+
{
52+
Directory.Delete(path, true);
53+
}
5054
}
51-
catch (DirectoryNotFoundException)
55+
catch (Exception)
5256
{
5357
// do nothing
5458
}
@@ -89,14 +93,14 @@ private string getRootCertificatePath(string pathOrName)
8993
string.IsNullOrEmpty(pathOrName) ? defaultRootCertificateFileName : pathOrName);
9094
}
9195

92-
private string getCertificatePath()
96+
private string getCertificatePath(bool create)
9397
{
9498
if (certificatePath == null)
9599
{
96100
string path = getRootCertificateDirectory();
97101

98102
string certPath = Path.Combine(path, defaultCertificateDirectoryName);
99-
if (!Directory.Exists(certPath))
103+
if (create && !Directory.Exists(certPath))
100104
{
101105
Directory.CreateDirectory(certPath);
102106
}

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)