This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 592
/
HttpWebClient.cs
194 lines (160 loc) · 5.95 KB
/
HttpWebClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network.Tcp;
namespace Titanium.Web.Proxy.Http;
/// <summary>
/// Used to communicate with the server over HTTP(S)
/// </summary>
public class HttpWebClient
{
private TcpServerConnection? connection;
internal HttpWebClient(ConnectRequest? connectRequest, Request request, Lazy<int> processIdFunc)
{
ConnectRequest = connectRequest;
Request = request;
Response = new Response();
ProcessId = processIdFunc;
}
/// <summary>
/// Connection to server
/// </summary>
internal TcpServerConnection Connection
{
get
{
if (connection == null) throw new Exception("Connection is null");
return connection;
}
}
internal bool HasConnection => connection != null;
/// <summary>
/// Should we close the server connection at the end of this HTTP request/response session.
/// </summary>
internal bool CloseServerConnection { get; set; }
/// <summary>
/// Stores internal data for the session.
/// </summary>
internal InternalDataStore Data { get; } = new();
/// <summary>
/// Gets or sets the user data.
/// </summary>
public object? UserData { get; set; }
/// <summary>
/// Override UpStreamEndPoint for this request; Local NIC via request is made
/// </summary>
public IPEndPoint? UpStreamEndPoint { get; set; }
/// <summary>
/// Headers passed with Connect.
/// </summary>
public ConnectRequest? ConnectRequest { get; }
/// <summary>
/// Web Request.
/// </summary>
public Request Request { get; }
/// <summary>
/// Web Response.
/// </summary>
public Response Response { get; internal set; }
/// <summary>
/// PID of the process that is created the current session when client is running in this machine
/// If client is remote then this will return
/// </summary>
public Lazy<int> ProcessId { get; internal set; }
/// <summary>
/// Is Https?
/// </summary>
public bool IsHttps => Request.IsHttps;
/// <summary>
/// Set the tcp connection to server used by this webclient
/// </summary>
/// <param name="serverConnection">Instance of <see cref="TcpServerConnection" /></param>
internal void SetConnection(TcpServerConnection serverConnection)
{
serverConnection.LastAccess = DateTime.UtcNow;
connection = serverConnection;
}
/// <summary>
/// Prepare and send the http(s) request
/// </summary>
/// <returns></returns>
internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTransparent,
CancellationToken cancellationToken)
{
var upstreamProxy = Connection.UpStreamProxy;
var useUpstreamProxy = upstreamProxy != null && upstreamProxy.ProxyType == ExternalProxyType.Http &&
!Connection.IsHttps;
var serverStream = Connection.Stream;
string? upstreamProxyUserName = null;
string? upstreamProxyPassword = null;
string url;
if (isTransparent)
{
url = Request.RequestUriString;
}
else if (!useUpstreamProxy)
{
if (UriExtensions.GetScheme(Request.RequestUriString8).Length == 0)
url = Request.RequestUriString;
else
url = Request.RequestUri.GetOriginalPathAndQuery();
}
else
{
url = Request.RequestUri.ToString();
// Send Authentication to Upstream proxy if needed
if (!string.IsNullOrEmpty(upstreamProxy!.UserName) && upstreamProxy.Password != null)
{
upstreamProxyUserName = upstreamProxy.UserName;
upstreamProxyPassword = upstreamProxy.Password;
}
}
if (url == string.Empty) url = "/";
// prepare the request & headers
var headerBuilder = new HeaderBuilder();
headerBuilder.WriteRequestLine(Request.Method, url, Request.HttpVersion);
headerBuilder.WriteHeaders(Request.Headers, !isTransparent, upstreamProxyUserName, upstreamProxyPassword);
// write request headers
await serverStream.WriteHeadersAsync(headerBuilder, cancellationToken);
if (enable100ContinueBehaviour && Request.ExpectContinue)
{
// wait for expectation response from server
await ReceiveResponse(cancellationToken);
if (Response.StatusCode == (int)HttpStatusCode.Continue)
Request.ExpectationSucceeded = true;
else
Request.ExpectationFailed = true;
}
}
/// <summary>
/// Receive and parse the http response from server
/// </summary>
/// <returns></returns>
internal async Task ReceiveResponse(CancellationToken cancellationToken)
{
// return if this is already read
if (Response.StatusCode != 0) return;
Response.RequestMethod = Request.Method;
var httpStatus = await Connection.Stream.ReadResponseStatus(cancellationToken);
Response.HttpVersion = httpStatus.Version;
Response.StatusCode = httpStatus.StatusCode;
Response.StatusDescription = httpStatus.Description;
// Read the response headers in to unique and non-unique header collections
await HeaderParser.ReadHeaders(Connection.Stream, Response.Headers, cancellationToken);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
internal void FinishSession()
{
connection = null;
ConnectRequest?.FinishSession();
Request?.FinishSession();
Response?.FinishSession();
Data.Clear();
UserData = null;
}
}