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

Commit 94e3df9

Browse files
authored
Merge pull request #657 from justcoding121/master
fixes
2 parents e24f3cc + 3d6e72a commit 94e3df9

File tree

12 files changed

+93
-60
lines changed

12 files changed

+93
-60
lines changed

src/Titanium.Web.Proxy/Compression/CompressionUtil.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ internal static class CompressionUtil
77
{
88
public static HttpCompression CompressionNameToEnum(string name)
99
{
10-
if (KnownHeaders.ContentEncodingGzip.Equals(name.AsSpan()))
10+
if (KnownHeaders.ContentEncodingGzip.Equals(name))
1111
return HttpCompression.Gzip;
1212

13-
if (KnownHeaders.ContentEncodingDeflate.Equals(name.AsSpan()))
13+
if (KnownHeaders.ContentEncodingDeflate.Equals(name))
1414
return HttpCompression.Deflate;
1515

16-
if (KnownHeaders.ContentEncodingBrotli.Equals(name.AsSpan()))
16+
if (KnownHeaders.ContentEncodingBrotli.Equals(name))
1717
return HttpCompression.Brotli;
1818

1919
return HttpCompression.Unsupported;

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
4747

4848
try
4949
{
50-
string? connectHostname = null;
5150
TunnelConnectSessionEventArgs? connectArgs = null;
5251

5352
// Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
@@ -62,14 +61,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
6261

6362
Request.ParseRequestLine(httpCmd!, out string _, out string httpUrl, out var version);
6463

65-
connectHostname = httpUrl;
66-
int idx = connectHostname.IndexOf(":");
67-
if (idx >= 0)
68-
{
69-
connectHostname = connectHostname.Substring(0, idx);
70-
}
71-
72-
var connectRequest = new ConnectRequest(connectHostname)
64+
var connectRequest = new ConnectRequest(httpUrl)
7365
{
7466
OriginalUrlData = HttpHeader.Encoding.GetBytes(httpUrl),
7567
HttpVersion = version
@@ -130,7 +122,7 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
130122
bool isClientHello = clientHelloInfo != null;
131123
if (clientHelloInfo != null)
132124
{
133-
connectRequest.Scheme = ProxyServer.UriSchemeHttps;
125+
connectRequest.IsHttps = true;
134126
connectRequest.TunnelType = TunnelType.Https;
135127
connectRequest.ClientHelloInfo = clientHelloInfo;
136128
}
@@ -186,6 +178,13 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
186178
}
187179
}
188180

181+
string connectHostname = httpUrl;
182+
int idx = connectHostname.IndexOf(":");
183+
if (idx >= 0)
184+
{
185+
connectHostname = connectHostname.Substring(0, idx);
186+
}
187+
189188
X509Certificate2? certificate = null;
190189
try
191190
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace Titanium.Web.Proxy.Http
99
/// </summary>
1010
public class ConnectRequest : Request
1111
{
12-
public ConnectRequest(string hostname)
12+
public ConnectRequest(string authority)
1313
{
1414
Method = "CONNECT";
15-
Hostname = hostname;
15+
Authority = authority;
1616
}
1717

1818
public TunnelType TunnelType { get; internal set; }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar
121121
else
122122
{
123123
url = Request.RequestUri.GetOriginalPathAndQuery();
124+
if (url == string.Empty)
125+
{
126+
url = "/";
127+
}
124128
}
125129

126130
var headerBuilder = new HeaderBuilder();

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

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class Request : RequestResponseBase
2222
/// <summary>
2323
/// Is Https?
2424
/// </summary>
25-
public bool IsHttps => RequestUri.Scheme == ProxyServer.UriSchemeHttps;
25+
public bool IsHttps { get; internal set; }
2626

2727
private ByteString originalUrlData;
28-
private protected ByteString UrlData;
28+
private ByteString urlData;
2929

3030
internal ByteString OriginalUrlData
3131
{
@@ -37,9 +37,21 @@ internal ByteString OriginalUrlData
3737
}
3838
}
3939

40-
internal string Scheme { get; set; } = ProxyServer.UriSchemeHttp;
40+
private protected ByteString UrlData
41+
{
42+
get => urlData;
43+
set
44+
{
45+
urlData = value;
46+
var scheme = getUriScheme(UrlData);
47+
if (scheme.Length > 0)
48+
{
49+
IsHttps = scheme.Equals(ProxyServer.UriSchemeHttps8);
50+
}
51+
}
52+
}
4153

42-
internal string? Hostname { get; set; }
54+
internal string? Authority { get; set; }
4355

4456
/// <summary>
4557
/// The original request Url.
@@ -53,21 +65,21 @@ public Uri RequestUri
5365
{
5466
get
5567
{
56-
string url;
57-
if (startsWithUriScheme(UrlData))
58-
{
59-
url = UrlData.GetString();
60-
}
61-
else
68+
string url = UrlData.GetString();
69+
if (getUriScheme(UrlData).Length == 0)
6270
{
63-
string? host = Host ?? Hostname;
64-
string? hostAndPath = host;
65-
if (UrlData.Length > 0 && UrlData[0] == '/')
71+
string? hostAndPath = Host ?? Authority;
72+
73+
if (url.StartsWith("/"))
74+
{
75+
hostAndPath += url;
76+
}
77+
else
6678
{
67-
hostAndPath += UrlData.GetString();
79+
//throw new Exception($"Invalid URL: '{url}'");
6880
}
6981

70-
url = string.Concat(Scheme == ProxyServer.UriSchemeHttps ? "https://" : "http://", hostAndPath);
82+
url = string.Concat(IsHttps ? "https://" : "http://", hostAndPath);
7183
}
7284

7385
try
@@ -87,7 +99,16 @@ public Uri RequestUri
8799
public string Url
88100
{
89101
get => UrlData.GetString();
90-
set => UrlData = value.GetByteString();
102+
set
103+
{
104+
UrlData = value.GetByteString();
105+
106+
if (Host != null)
107+
{
108+
var uri = new Uri(value);
109+
Host = uri.Authority;
110+
}
111+
}
91112
}
92113

93114
[Obsolete("This property is obsolete. Use Url property instead")]
@@ -147,7 +168,7 @@ public bool ExpectContinue
147168
get
148169
{
149170
string? headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.Expect);
150-
return headerValue != null && headerValue.Equals(KnownHeaders.Expect100Continue);
171+
return KnownHeaders.Expect100Continue.Equals(headerValue);
151172
}
152173
}
153174

@@ -290,11 +311,11 @@ private static bool isAllUpper(string input)
290311
return true;
291312
}
292313

293-
private bool startsWithUriScheme(ByteString str)
314+
private ByteString getUriScheme(ByteString str)
294315
{
295316
if (str.Length < 3)
296317
{
297-
return false;
318+
return ByteString.Empty;
298319
}
299320

300321
// regex: "^[a-z]*://"
@@ -310,26 +331,26 @@ private bool startsWithUriScheme(ByteString str)
310331

311332
if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
312333
{
313-
return false;
334+
return ByteString.Empty;
314335
}
315336
}
316337

317338
if (str[i++] != ':')
318339
{
319-
return false;
340+
return ByteString.Empty;
320341
}
321342

322343
if (str[i++] != '/')
323344
{
324-
return false;
345+
return ByteString.Empty;
325346
}
326347

327348
if (str[i] != '/')
328349
{
329-
return false;
350+
return ByteString.Empty;
330351
}
331352

332-
return true;
353+
return new ByteString(str.Data.Slice(0, i - 2));
333354
}
334355
}
335356
}

src/Titanium.Web.Proxy/Http2/Http2Helper.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
263263

264264
request.HttpVersion = HttpVersion.Version20;
265265
request.Method = method.GetString();
266+
request.IsHttps = headerListener.Scheme == ProxyServer.UriSchemeHttps;
267+
request.Authority = headerListener.Authority.GetString();
266268
request.OriginalUrlData = path;
267-
request.Scheme = headerListener.Scheme;
268-
request.Hostname = headerListener.Authority.GetString();
269269

270270
//request.RequestUri = headerListener.GetUri();
271271
}
@@ -468,9 +468,10 @@ private static async Task sendHeader(Http2Settings settings, Http2FrameHeader fr
468468

469469
if (rr is Request request)
470470
{
471+
var uri = request.RequestUri;
471472
encoder.EncodeHeader(writer, StaticTable.KnownHeaderMethod, request.Method.GetByteString());
472-
encoder.EncodeHeader(writer, StaticTable.KnownHeaderAuhtority, request.RequestUri.Host.GetByteString());
473-
encoder.EncodeHeader(writer, StaticTable.KnownHeaderScheme, request.RequestUri.Scheme.GetByteString());
473+
encoder.EncodeHeader(writer, StaticTable.KnownHeaderAuhtority, uri.Authority.GetByteString());
474+
encoder.EncodeHeader(writer, StaticTable.KnownHeaderScheme, uri.Scheme.GetByteString());
474475
encoder.EncodeHeader(writer, StaticTable.KnownHeaderPath, request.Url.GetByteString(), false,
475476
HpackUtil.IndexType.None, false);
476477
}
@@ -572,10 +573,6 @@ class Http2Settings
572573

573574
class MyHeaderListener : IHeaderListener
574575
{
575-
private static ByteString SchemeHttp = (ByteString)ProxyServer.UriSchemeHttp;
576-
577-
private static ByteString SchemeHttps = (ByteString)ProxyServer.UriSchemeHttps;
578-
579576
private readonly Action<ByteString, ByteString> addHeaderFunc;
580577

581578
public ByteString Method { get; private set; }
@@ -592,12 +589,12 @@ public string Scheme
592589
{
593590
get
594591
{
595-
if (scheme.Equals(SchemeHttp))
592+
if (scheme.Equals(ProxyServer.UriSchemeHttp8))
596593
{
597594
return ProxyServer.UriSchemeHttp;
598595
}
599596

600-
if (scheme.Equals(SchemeHttps))
597+
if (scheme.Equals(ProxyServer.UriSchemeHttps8))
601598
{
602599
return ProxyServer.UriSchemeHttps;
603600
}

src/Titanium.Web.Proxy/Models/ExplicitProxyEndPoint.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System.Diagnostics;
2+
using System.Net;
23
using System.Threading.Tasks;
34
using Titanium.Web.Proxy.EventArguments;
45
using Titanium.Web.Proxy.Extensions;
@@ -9,6 +10,7 @@ namespace Titanium.Web.Proxy.Models
910
/// A proxy endpoint that the client is aware of.
1011
/// So client application know that it is communicating with a proxy server.
1112
/// </summary>
13+
[DebuggerDisplay("Explicit: {IpAddress}:{Port}")]
1214
public class ExplicitProxyEndPoint : ProxyEndPoint
1315
{
1416
/// <summary>

src/Titanium.Web.Proxy/Models/TransparentProxyEndPoint.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System.Diagnostics;
2+
using System.Net;
23
using System.Threading.Tasks;
34
using Titanium.Web.Proxy.EventArguments;
45
using Titanium.Web.Proxy.Extensions;
@@ -9,6 +10,7 @@ namespace Titanium.Web.Proxy.Models
910
/// A proxy end point client is not aware of.
1011
/// Useful when requests are redirected to this proxy end point through port forwarding via router.
1112
/// </summary>
13+
[DebuggerDisplay("Explicit: {IpAddress}:{Port}")]
1214
public class TransparentProxyEndPoint : ProxyEndPoint
1315
{
1416
/// <summary>

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEve
125125

126126
session.CustomUpStreamProxyUsed = customUpStreamProxy;
127127

128+
var uri = session.HttpClient.Request.RequestUri;
128129
return GetConnectionCacheKey(
129-
session.HttpClient.Request.RequestUri.Host,
130-
session.HttpClient.Request.RequestUri.Port,
130+
uri.Host,
131+
uri.Port,
131132
isHttps, applicationProtocols,
132133
session.HttpClient.UpStreamEndPoint ?? server.UpStreamEndPoint,
133134
customUpStreamProxy ?? (isHttps ? server.UpStreamHttpsProxy : server.UpStreamHttpProxy));
@@ -179,9 +180,10 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server,
179180

180181
session.CustomUpStreamProxyUsed = customUpStreamProxy;
181182

183+
var uri = session.HttpClient.Request.RequestUri;
182184
return await GetServerConnection(
183-
session.HttpClient.Request.RequestUri.Host,
184-
session.HttpClient.Request.RequestUri.Port,
185+
uri.Host,
186+
uri.Port,
185187
session.HttpClient.Request.HttpVersion,
186188
isHttps, applicationProtocols, isConnect,
187189
server, session, session.HttpClient.UpStreamEndPoint ?? server.UpStreamEndPoint,
@@ -372,9 +374,11 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
372374
if (useUpstreamProxy && (isConnect || isHttps))
373375
{
374376
var writer = new HttpRequestWriter(stream, proxyServer.BufferPool);
375-
var connectRequest = new ConnectRequest(remoteHostName)
377+
string authority = $"{remoteHostName}:{remotePort}";
378+
var connectRequest = new ConnectRequest(authority)
376379
{
377-
OriginalUrlData = HttpHeader.Encoding.GetBytes($"{remoteHostName}:{remotePort}"),
380+
IsHttps = isHttps,
381+
OriginalUrlData = HttpHeader.Encoding.GetBytes(authority),
378382
HttpVersion = httpVersion
379383
};
380384

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public partial class ProxyServer : IDisposable
3232
internal static readonly string UriSchemeHttp = Uri.UriSchemeHttp;
3333
internal static readonly string UriSchemeHttps = Uri.UriSchemeHttps;
3434

35+
internal static ByteString UriSchemeHttp8 = (ByteString)UriSchemeHttp;
36+
internal static ByteString UriSchemeHttps8 = (ByteString)UriSchemeHttps;
37+
38+
3539
/// <summary>
3640
/// A default exception log func.
3741
/// </summary>

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
8484
var request = args.HttpClient.Request;
8585
if (connectRequest != null)
8686
{
87-
request.Scheme = connectRequest.Scheme;
88-
request.Hostname = connectRequest.Hostname;
87+
request.IsHttps = connectRequest.IsHttps;
88+
request.Authority = connectRequest.Authority;
8989
}
9090

9191
request.OriginalUrlData = HttpHeader.Encoding.GetBytes(httpUrl);

tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpContinueClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task<Response> Post(string server, int port, string content)
3434

3535
var buffer = new byte[1024];
3636
var responseMsg = string.Empty;
37-
Response response = null;
37+
Response response;
3838

3939
while ((response = HttpMessageParsing.ParseResponse(responseMsg)) == null)
4040
{

0 commit comments

Comments
 (0)