Commit 7fcc8ddb authored by Honfika's avatar Honfika

small refactor

parent 9e02fc77
......@@ -4,12 +4,10 @@ using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Compression;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Http.Responses;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network;
using Titanium.Web.Proxy.Network.Tcp;
using Titanium.Web.Proxy.StreamExtended.Network;
......@@ -263,7 +261,7 @@ namespace Titanium.Web.Proxy.EventArguments
else
{
await reader.CopyBodyAsync(request, false, writer, transformation, OnDataSent, cancellationToken);
}
}
}
private async Task copyResponseBodyAsync(IHttpStreamWriter writer, TransformationMode transformation, CancellationToken cancellationToken)
......
......@@ -52,18 +52,16 @@ namespace Titanium.Web.Proxy
if (await HttpHelper.IsConnectMethod(clientStream, BufferPool, cancellationToken) == 1)
{
// read the first line HTTP command
string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
if (string.IsNullOrEmpty(httpCmd))
var requestLine = await clientStream.ReadRequestLine(cancellationToken);
if (requestLine.IsEmpty())
{
return;
}
Request.ParseRequestLine(httpCmd!, out string _, out var httpUrl, out var version);
var connectRequest = new ConnectRequest(httpUrl.GetString())
var connectRequest = new ConnectRequest(requestLine.RequestUri.GetString())
{
RequestUriString8 = httpUrl,
HttpVersion = version
RequestUriString8 = requestLine.RequestUri,
HttpVersion = requestLine.Version
};
await HeaderParser.ReadHeaders(clientStream, connectRequest.Headers, cancellationToken);
......@@ -105,7 +103,7 @@ namespace Titanium.Web.Proxy
}
// write back successful CONNECT response
var response = ConnectResponse.CreateSuccessfulConnectResponse(version);
var response = ConnectResponse.CreateSuccessfulConnectResponse(requestLine.Version);
// Set ContentLength explicitly to properly handle HTTP 1.0
response.ContentLength = 0;
......@@ -175,7 +173,7 @@ namespace Titanium.Web.Proxy
}
}
string connectHostname = httpUrl.GetString();
string connectHostname = requestLine.RequestUri.GetString();
int idx = connectHostname.IndexOf(":");
if (idx >= 0)
{
......@@ -214,6 +212,8 @@ namespace Titanium.Web.Proxy
// HTTPS server created - we can now decrypt the client's traffic
clientStream = new HttpClientStream(sslStream, BufferPool);
sslStream = null; // clientStream was created, no need to keep SSL stream reference
clientStream.DataRead += (o, args) => connectArgs.OnDecryptedDataSent(args.Buffer, args.Offset, args.Count);
clientStream.DataWrite += (o, args) => connectArgs.OnDecryptedDataReceived(args.Buffer, args.Offset, args.Count);
}
......
......@@ -29,5 +29,19 @@ namespace Titanium.Web.Proxy.Helpers
await WriteAsync(response, headerBuilder, cancellationToken);
}
internal async ValueTask<RequestStatusInfo> ReadRequestLine(CancellationToken cancellationToken = default)
{
// read the first line HTTP command
string? httpCmd = await ReadLineAsync(cancellationToken);
if (string.IsNullOrEmpty(httpCmd))
{
return default;
}
Request.ParseRequestLine(httpCmd!, out string method, out var requestUri, out var version);
return new RequestStatusInfo { Method = method, RequestUri = requestUri, Version = version };
}
}
}
using System;
using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Helpers
{
struct RequestStatusInfo
{
public string Method { get; set; }
public ByteString RequestUri { get; set; }
public Version Version { get; set; }
public bool IsEmpty()
{
return Method == null && RequestUri.Length == 0 && Version == null;
}
}
}
......@@ -254,7 +254,7 @@ namespace Titanium.Web.Proxy.Http
}
}
internal static void ParseRequestLine(string httpCmd, out string httpMethod, out ByteString httpUrl,
internal static void ParseRequestLine(string httpCmd, out string method, out ByteString requestUri,
out Version version)
{
int firstSpace = httpCmd.IndexOf(' ');
......@@ -269,21 +269,21 @@ namespace Titanium.Web.Proxy.Http
// break up the line into three components (method, remote URL & Http Version)
// Find the request Verb
httpMethod = httpCmd.Substring(0, firstSpace);
if (!isAllUpper(httpMethod))
method = httpCmd.Substring(0, firstSpace);
if (!isAllUpper(method))
{
httpMethod = httpMethod.ToUpper();
method = method.ToUpper();
}
version = HttpHeader.Version11;
if (firstSpace == lastSpace)
{
httpUrl = (ByteString)httpCmd.AsSpan(firstSpace + 1).ToString();
requestUri = (ByteString)httpCmd.AsSpan(firstSpace + 1).ToString();
}
else
{
httpUrl = (ByteString)httpCmd.AsSpan(firstSpace + 1, lastSpace - firstSpace - 1).ToString();
requestUri = (ByteString)httpCmd.AsSpan(firstSpace + 1, lastSpace - firstSpace - 1).ToString();
// parse the HTTP version
var httpVersion = httpCmd.AsSpan(lastSpace + 1);
......
......@@ -196,7 +196,7 @@ namespace Titanium.Web.Proxy.Http
/// Use the encoding specified to decode the byte[] data to string
/// </summary>
[Browsable(false)]
public string BodyString => bodyString ?? (bodyString = Encoding.GetString(Body));
public string BodyString => bodyString ??= Encoding.GetString(Body);
/// <summary>
/// Was the body read by user?
......
......@@ -58,8 +58,8 @@ namespace Titanium.Web.Proxy
}
// read the request line
string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
if (string.IsNullOrEmpty(httpCmd))
var requestLine = await clientStream.ReadRequestLine(cancellationToken);
if (requestLine.IsEmpty())
{
return;
}
......@@ -73,8 +73,6 @@ namespace Titanium.Web.Proxy
{
try
{
Request.ParseRequestLine(httpCmd!, out string httpMethod, out ByteString httpUrl, out var version);
// Read the request headers in to unique and non-unique header collections
await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
cancellationToken);
......@@ -86,10 +84,10 @@ namespace Titanium.Web.Proxy
request.Authority = connectRequest.Authority;
}
request.RequestUriString8 = httpUrl;
request.RequestUriString8 = requestLine.RequestUri;
request.Method = httpMethod;
request.HttpVersion = version;
request.Method = requestLine.Method;
request.HttpVersion = requestLine.Version;
if (!args.IsTransparent)
{
......@@ -332,6 +330,11 @@ namespace Titanium.Web.Proxy
}
else if (!request.ExpectationFailed)
{
if (args.ClientStream.IsClosed)
{
;
}
// get the request body unless an unsuccessful 100 continue request was made
await args.CopyRequestBodyAsync(args.HttpClient.Connection.Stream, TransformationMode.None, cancellationToken);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment