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

Commit 7fcc8dd

Browse files
committed
small refactor
1 parent 9e02fc7 commit 7fcc8dd

File tree

7 files changed

+60
-26
lines changed

7 files changed

+60
-26
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
using System.Net;
55
using System.Threading;
66
using System.Threading.Tasks;
7-
using Titanium.Web.Proxy.Compression;
87
using Titanium.Web.Proxy.Helpers;
98
using Titanium.Web.Proxy.Http;
109
using Titanium.Web.Proxy.Http.Responses;
1110
using Titanium.Web.Proxy.Models;
12-
using Titanium.Web.Proxy.Network;
1311
using Titanium.Web.Proxy.Network.Tcp;
1412
using Titanium.Web.Proxy.StreamExtended.Network;
1513

@@ -263,7 +261,7 @@ internal async Task CopyRequestBodyAsync(IHttpStreamWriter writer, Transformatio
263261
else
264262
{
265263
await reader.CopyBodyAsync(request, false, writer, transformation, OnDataSent, cancellationToken);
266-
}
264+
}
267265
}
268266

269267
private async Task copyResponseBodyAsync(IHttpStreamWriter writer, TransformationMode transformation, CancellationToken cancellationToken)

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,16 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
5252
if (await HttpHelper.IsConnectMethod(clientStream, BufferPool, cancellationToken) == 1)
5353
{
5454
// read the first line HTTP command
55-
string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
56-
if (string.IsNullOrEmpty(httpCmd))
55+
var requestLine = await clientStream.ReadRequestLine(cancellationToken);
56+
if (requestLine.IsEmpty())
5757
{
5858
return;
5959
}
6060

61-
Request.ParseRequestLine(httpCmd!, out string _, out var httpUrl, out var version);
62-
63-
var connectRequest = new ConnectRequest(httpUrl.GetString())
61+
var connectRequest = new ConnectRequest(requestLine.RequestUri.GetString())
6462
{
65-
RequestUriString8 = httpUrl,
66-
HttpVersion = version
63+
RequestUriString8 = requestLine.RequestUri,
64+
HttpVersion = requestLine.Version
6765
};
6866

6967
await HeaderParser.ReadHeaders(clientStream, connectRequest.Headers, cancellationToken);
@@ -105,7 +103,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
105103
}
106104

107105
// write back successful CONNECT response
108-
var response = ConnectResponse.CreateSuccessfulConnectResponse(version);
106+
var response = ConnectResponse.CreateSuccessfulConnectResponse(requestLine.Version);
109107

110108
// Set ContentLength explicitly to properly handle HTTP 1.0
111109
response.ContentLength = 0;
@@ -175,7 +173,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
175173
}
176174
}
177175

178-
string connectHostname = httpUrl.GetString();
176+
string connectHostname = requestLine.RequestUri.GetString();
179177
int idx = connectHostname.IndexOf(":");
180178
if (idx >= 0)
181179
{
@@ -214,6 +212,8 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
214212

215213
// HTTPS server created - we can now decrypt the client's traffic
216214
clientStream = new HttpClientStream(sslStream, BufferPool);
215+
sslStream = null; // clientStream was created, no need to keep SSL stream reference
216+
217217
clientStream.DataRead += (o, args) => connectArgs.OnDecryptedDataSent(args.Buffer, args.Offset, args.Count);
218218
clientStream.DataWrite += (o, args) => connectArgs.OnDecryptedDataReceived(args.Buffer, args.Offset, args.Count);
219219
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,19 @@ internal async ValueTask WriteResponseAsync(Response response, CancellationToken
2929

3030
await WriteAsync(response, headerBuilder, cancellationToken);
3131
}
32+
33+
internal async ValueTask<RequestStatusInfo> ReadRequestLine(CancellationToken cancellationToken = default)
34+
{
35+
// read the first line HTTP command
36+
string? httpCmd = await ReadLineAsync(cancellationToken);
37+
if (string.IsNullOrEmpty(httpCmd))
38+
{
39+
return default;
40+
}
41+
42+
Request.ParseRequestLine(httpCmd!, out string method, out var requestUri, out var version);
43+
44+
return new RequestStatusInfo { Method = method, RequestUri = requestUri, Version = version };
45+
}
3246
}
3347
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Titanium.Web.Proxy.Models;
3+
4+
namespace Titanium.Web.Proxy.Helpers
5+
{
6+
struct RequestStatusInfo
7+
{
8+
public string Method { get; set; }
9+
10+
public ByteString RequestUri { get; set; }
11+
12+
public Version Version { get; set; }
13+
14+
public bool IsEmpty()
15+
{
16+
return Method == null && RequestUri.Length == 0 && Version == null;
17+
}
18+
}
19+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ internal override void EnsureBodyAvailable(bool throwWhenNotReadYet = true)
254254
}
255255
}
256256

257-
internal static void ParseRequestLine(string httpCmd, out string httpMethod, out ByteString httpUrl,
257+
internal static void ParseRequestLine(string httpCmd, out string method, out ByteString requestUri,
258258
out Version version)
259259
{
260260
int firstSpace = httpCmd.IndexOf(' ');
@@ -269,21 +269,21 @@ internal static void ParseRequestLine(string httpCmd, out string httpMethod, out
269269
// break up the line into three components (method, remote URL & Http Version)
270270

271271
// Find the request Verb
272-
httpMethod = httpCmd.Substring(0, firstSpace);
273-
if (!isAllUpper(httpMethod))
272+
method = httpCmd.Substring(0, firstSpace);
273+
if (!isAllUpper(method))
274274
{
275-
httpMethod = httpMethod.ToUpper();
275+
method = method.ToUpper();
276276
}
277277

278278
version = HttpHeader.Version11;
279279

280280
if (firstSpace == lastSpace)
281281
{
282-
httpUrl = (ByteString)httpCmd.AsSpan(firstSpace + 1).ToString();
282+
requestUri = (ByteString)httpCmd.AsSpan(firstSpace + 1).ToString();
283283
}
284284
else
285285
{
286-
httpUrl = (ByteString)httpCmd.AsSpan(firstSpace + 1, lastSpace - firstSpace - 1).ToString();
286+
requestUri = (ByteString)httpCmd.AsSpan(firstSpace + 1, lastSpace - firstSpace - 1).ToString();
287287

288288
// parse the HTTP version
289289
var httpVersion = httpCmd.AsSpan(lastSpace + 1);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ internal set
196196
/// Use the encoding specified to decode the byte[] data to string
197197
/// </summary>
198198
[Browsable(false)]
199-
public string BodyString => bodyString ?? (bodyString = Encoding.GetString(Body));
199+
public string BodyString => bodyString ??= Encoding.GetString(Body);
200200

201201
/// <summary>
202202
/// Was the body read by user?

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, TcpClientCon
5858
}
5959

6060
// read the request line
61-
string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
62-
if (string.IsNullOrEmpty(httpCmd))
61+
var requestLine = await clientStream.ReadRequestLine(cancellationToken);
62+
if (requestLine.IsEmpty())
6363
{
6464
return;
6565
}
@@ -73,8 +73,6 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, TcpClientCon
7373
{
7474
try
7575
{
76-
Request.ParseRequestLine(httpCmd!, out string httpMethod, out ByteString httpUrl, out var version);
77-
7876
// Read the request headers in to unique and non-unique header collections
7977
await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
8078
cancellationToken);
@@ -86,10 +84,10 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
8684
request.Authority = connectRequest.Authority;
8785
}
8886

89-
request.RequestUriString8 = httpUrl;
87+
request.RequestUriString8 = requestLine.RequestUri;
9088

91-
request.Method = httpMethod;
92-
request.HttpVersion = version;
89+
request.Method = requestLine.Method;
90+
request.HttpVersion = requestLine.Version;
9391

9492
if (!args.IsTransparent)
9593
{
@@ -332,6 +330,11 @@ await args.HttpClient.SendRequest(Enable100ContinueBehaviour, args.IsTransparent
332330
}
333331
else if (!request.ExpectationFailed)
334332
{
333+
if (args.ClientStream.IsClosed)
334+
{
335+
;
336+
}
337+
335338
// get the request body unless an unsuccessful 100 continue request was made
336339
await args.CopyRequestBodyAsync(args.HttpClient.Connection.Stream, TransformationMode.None, cancellationToken);
337340
}

0 commit comments

Comments
 (0)