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

Commit e24f3cc

Browse files
authored
Merge pull request #656 from justcoding121/master
better uri handling
2 parents 183f960 + 1b0bfec commit e24f3cc

36 files changed

+415
-174
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ namespace Titanium.Web.Proxy.Compression
1010
/// </summary>
1111
internal static class CompressionFactory
1212
{
13-
internal static Stream Create(string type, Stream stream, bool leaveOpen = true)
13+
internal static Stream Create(HttpCompression type, Stream stream, bool leaveOpen = true)
1414
{
1515
switch (type)
1616
{
17-
case KnownHeaders.ContentEncodingGzip:
17+
case HttpCompression.Gzip:
1818
return new GZipStream(stream, CompressionMode.Compress, leaveOpen);
19-
case KnownHeaders.ContentEncodingDeflate:
19+
case HttpCompression.Deflate:
2020
return new DeflateStream(stream, CompressionMode.Compress, leaveOpen);
21-
case KnownHeaders.ContentEncodingBrotli:
21+
case HttpCompression.Brotli:
2222
return new BrotliSharpLib.BrotliStream(stream, CompressionMode.Compress, leaveOpen);
2323
default:
2424
throw new Exception($"Unsupported compression mode: {type}");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Titanium.Web.Proxy.Http;
3+
4+
namespace Titanium.Web.Proxy.Compression
5+
{
6+
internal static class CompressionUtil
7+
{
8+
public static HttpCompression CompressionNameToEnum(string name)
9+
{
10+
if (KnownHeaders.ContentEncodingGzip.Equals(name.AsSpan()))
11+
return HttpCompression.Gzip;
12+
13+
if (KnownHeaders.ContentEncodingDeflate.Equals(name.AsSpan()))
14+
return HttpCompression.Deflate;
15+
16+
if (KnownHeaders.ContentEncodingBrotli.Equals(name.AsSpan()))
17+
return HttpCompression.Brotli;
18+
19+
return HttpCompression.Unsupported;
20+
}
21+
}
22+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ namespace Titanium.Web.Proxy.Compression
1010
/// </summary>
1111
internal class DecompressionFactory
1212
{
13-
internal static Stream Create(string type, Stream stream, bool leaveOpen = true)
13+
internal static Stream Create(HttpCompression type, Stream stream, bool leaveOpen = true)
1414
{
1515
switch (type)
1616
{
17-
case KnownHeaders.ContentEncodingGzip:
17+
case HttpCompression.Gzip:
1818
return new GZipStream(stream, CompressionMode.Decompress, leaveOpen);
19-
case KnownHeaders.ContentEncodingDeflate:
19+
case HttpCompression.Deflate:
2020
return new DeflateStream(stream, CompressionMode.Decompress, leaveOpen);
21-
case KnownHeaders.ContentEncodingBrotli:
21+
case HttpCompression.Brotli:
2222
return new BrotliSharpLib.BrotliStream(stream, CompressionMode.Decompress, leaveOpen);
2323
default:
2424
throw new Exception($"Unsupported decompression mode: {type}");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.IO.Compression;
2+
3+
namespace Titanium.Web.Proxy.Compression
4+
{
5+
internal enum HttpCompression
6+
{
7+
Unsupported,
8+
Gzip,
9+
Deflate,
10+
Brotli,
11+
}
12+
}

src/Titanium.Web.Proxy/EventArguments/LimitedStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void getNextChunk()
5454

5555
readChunkTrail = true;
5656

57-
string? chunkHead = baseStream.ReadLineAsync().Result;
57+
string? chunkHead = baseStream.ReadLineAsync().Result!;
5858
int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
5959
if (idx >= 0)
6060
{

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SessionEventArgs : SessionEventArgsBase
3838
/// Constructor to initialize the proxy
3939
/// </summary>
4040
internal SessionEventArgs(ProxyServer server, ProxyEndPoint endPoint, ProxyClient proxyClient, ConnectRequest? connectRequest, CancellationTokenSource cancellationTokenSource)
41-
: base(server, endPoint, proxyClient, connectRequest, null, cancellationTokenSource)
41+
: base(server, endPoint, proxyClient, connectRequest, new Request(), cancellationTokenSource)
4242
{
4343
}
4444

@@ -304,7 +304,7 @@ private async Task copyBodyAsync(bool isRequest, bool useOriginalHeaderValues, H
304304

305305
if (transformation == TransformationMode.Uncompress && contentEncoding != null)
306306
{
307-
s = decompressStream = DecompressionFactory.Create(contentEncoding, s);
307+
s = decompressStream = DecompressionFactory.Create(CompressionUtil.CompressionNameToEnum(contentEncoding), s);
308308
}
309309

310310
try

src/Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class SessionEventArgsBase : EventArgs, IDisposable
4141
/// Initializes a new instance of the <see cref="SessionEventArgsBase" /> class.
4242
/// </summary>
4343
private protected SessionEventArgsBase(ProxyServer server, ProxyEndPoint endPoint,
44-
ProxyClient proxyClient, ConnectRequest? connectRequest, Request? request, CancellationTokenSource cancellationTokenSource)
44+
ProxyClient proxyClient, ConnectRequest? connectRequest, Request request, CancellationTokenSource cancellationTokenSource)
4545
{
4646
BufferPool = server.BufferPool;
4747
ExceptionFunc = server.ExceptionFunc;

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
6262

6363
Request.ParseRequestLine(httpCmd!, out string _, out string httpUrl, out var version);
6464

65-
var httpRemoteUri = new Uri("http://" + httpUrl);
66-
connectHostname = httpRemoteUri.Host;
65+
connectHostname = httpUrl;
66+
int idx = connectHostname.IndexOf(":");
67+
if (idx >= 0)
68+
{
69+
connectHostname = connectHostname.Substring(0, idx);
70+
}
6771

68-
var connectRequest = new ConnectRequest
72+
var connectRequest = new ConnectRequest(connectHostname)
6973
{
70-
RequestUri = httpRemoteUri,
7174
OriginalUrlData = HttpHeader.Encoding.GetBytes(httpUrl),
7275
HttpVersion = version
7376
};
@@ -127,6 +130,7 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
127130
bool isClientHello = clientHelloInfo != null;
128131
if (clientHelloInfo != null)
129132
{
133+
connectRequest.Scheme = ProxyServer.UriSchemeHttps;
130134
connectRequest.TunnelType = TunnelType.Https;
131135
connectRequest.ClientHelloInfo = clientHelloInfo;
132136
}
@@ -136,7 +140,6 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
136140
if (decryptSsl && clientHelloInfo != null)
137141
{
138142
clientConnection.SslProtocol = clientHelloInfo.SslProtocol;
139-
connectRequest.RequestUri = new Uri("https://" + httpUrl);
140143

141144
bool http2Supported = false;
142145

@@ -355,7 +358,7 @@ await Http2Helper.SendHttp2(clientStream, connection.Stream,
355358

356359
// Now create the request
357360
await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
358-
cancellationTokenSource, connectHostname, connectArgs, prefetchConnectionTask);
361+
cancellationTokenSource, connectArgs, prefetchConnectionTask);
359362
}
360363
catch (ProxyException e)
361364
{

src/Titanium.Web.Proxy/Extensions/StreamExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal static async Task CopyToAsync(this Stream input, Stream output, Action<
6262
}
6363
}
6464

65-
private static async Task<T> withCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
65+
private static async Task<T> withCancellation<T>(this Task<T> task, CancellationToken cancellationToken) where T : struct
6666
{
6767
var tcs = new TaskCompletionSource<bool>();
6868
using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))

src/Titanium.Web.Proxy/Helpers/HttpHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal static Encoding GetEncodingFromContentType(string? contentType)
7878
{
7979
var parameter = p.Span;
8080
int equalsIndex = parameter.IndexOf('=');
81-
if (equalsIndex != -1 && parameter.Slice(0, equalsIndex).TrimStart().EqualsIgnoreCase(KnownHeaders.ContentTypeCharset.AsSpan()))
81+
if (equalsIndex != -1 && KnownHeaders.ContentTypeCharset.Equals(parameter.Slice(0, equalsIndex).TrimStart()))
8282
{
8383
var value = parameter.Slice(equalsIndex + 1);
8484
if (value.EqualsIgnoreCase("x-user-defined".AsSpan()))
@@ -113,7 +113,7 @@ internal static ReadOnlyMemory<char> GetBoundaryFromContentType(string? contentT
113113
foreach (var parameter in new SemicolonSplitEnumerator(contentType))
114114
{
115115
int equalsIndex = parameter.Span.IndexOf('=');
116-
if (equalsIndex != -1 && parameter.Span.Slice(0, equalsIndex).TrimStart().EqualsIgnoreCase(KnownHeaders.ContentTypeBoundary.AsSpan()))
116+
if (equalsIndex != -1 && KnownHeaders.ContentTypeBoundary.Equals(parameter.Span.Slice(0, equalsIndex).TrimStart()))
117117
{
118118
var value = parameter.Slice(equalsIndex + 1);
119119
if (value.Length > 2 && value.Span[0] == '"' && value.Span[value.Length - 1] == '"')

0 commit comments

Comments
 (0)