-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRequest.cs
264 lines (222 loc) · 7.64 KB
/
Request.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.ComponentModel;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Http;
/// <summary>
/// Http(s) request object
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public class Request : RequestResponseBase
{
private ByteString requestUriString8;
/// <summary>
/// Request Method.
/// </summary>
public string? Method { get; set; }
/// <summary>
/// Is Https?
/// </summary>
public bool IsHttps { get; internal set; }
internal ByteString RequestUriString8
{
get => requestUriString8;
set
{
requestUriString8 = value;
var scheme = UriExtensions.GetScheme(value);
if (scheme.Length > 0) IsHttps = scheme.Equals(ProxyServer.UriSchemeHttps8);
}
}
internal ByteString Authority { get; set; }
/// <summary>
/// Request HTTP Uri.
/// </summary>
public Uri RequestUri
{
get
{
var url = Url;
try
{
return new Uri(url);
}
catch (Exception ex)
{
throw new Exception($"Invalid URI: '{url}'", ex);
}
}
set => Url = value.OriginalString;
}
/// <summary>
/// The request url as it is in the HTTP header
/// </summary>
public string Url
{
get
{
var url = RequestUriString8.GetString();
if (UriExtensions.GetScheme(RequestUriString8).Length == 0)
{
var hostAndPath = Host ?? Authority.GetString();
if (url.StartsWith("/"))
{
hostAndPath += url;
}
url = string.Concat(IsHttps ? "https://" : "http://", hostAndPath);
}
return url;
}
set => RequestUriString = value;
}
/// <summary>
/// The request uri as it is in the HTTP header
/// </summary>
public string RequestUriString
{
get => RequestUriString8.GetString();
set
{
RequestUriString8 = (ByteString)value;
var scheme = UriExtensions.GetScheme(RequestUriString8);
if (scheme.Length > 0 && Host != null)
{
var uri = new Uri(value);
Host = uri.Authority;
Authority = ByteString.Empty;
}
}
}
/// <summary>
/// Has request body?
/// </summary>
public override bool HasBody
{
get
{
var contentLength = ContentLength;
// If content length is set to 0 the request has no body
if (contentLength == 0) return false;
// Has body only if request is chunked or content length >0
if (IsChunked || contentLength > 0) return true;
// has body if POST and when version is http/1.0
if (Method == "POST" && HttpVersion == HttpHeader.Version10) return true;
return false;
}
}
/// <summary>
/// Http hostname header value if exists.
/// Note: Changing this does NOT change host in RequestUri.
/// Users can set new RequestUri separately.
/// </summary>
public string? Host
{
get => Headers.GetHeaderValueOrNull(KnownHeaders.Host);
set => Headers.SetOrAddHeaderValue(KnownHeaders.Host, value);
}
/// <summary>
/// Does this request has a 100-continue header?
/// </summary>
public bool ExpectContinue
{
get
{
var headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.Expect);
return KnownHeaders.Expect100Continue.Equals(headerValue);
}
}
/// <summary>
/// Does this request contain multipart/form-data?
/// </summary>
public bool IsMultipartFormData => ContentType?.StartsWith("multipart/form-data") == true;
/// <summary>
/// Cancels the client HTTP request without sending to server.
/// This should be set when API user responds with custom response.
/// </summary>
internal bool CancelRequest { get; set; }
/// <summary>
/// Does this request has an upgrade to websocket header?
/// </summary>
public bool UpgradeToWebSocket
{
get
{
var headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.Upgrade);
if (headerValue == null) return false;
return headerValue.EqualsIgnoreCase(KnownHeaders.UpgradeWebsocket.String);
}
}
/// <summary>
/// Did server respond positively for 100 continue request?
/// </summary>
public bool ExpectationSucceeded { get; internal set; }
/// <summary>
/// Did server respond negatively for 100 continue request?
/// </summary>
public bool ExpectationFailed { get; internal set; }
/// <summary>
/// Gets the header text.
/// </summary>
public override string HeaderText
{
get
{
var headerBuilder = new HeaderBuilder();
headerBuilder.WriteRequestLine(Method!, RequestUriString, HttpVersion);
headerBuilder.WriteHeaders(Headers);
return headerBuilder.GetString(HttpHeader.Encoding);
}
}
internal override void EnsureBodyAvailable(bool throwWhenNotReadYet = true)
{
if (BodyInternal != null) return;
// GET request don't have a request body to read
if (!HasBody)
throw new BodyNotFoundException("Request don't have a body. " +
"Please verify that this request is a Http POST/PUT/PATCH and request " +
"content length is greater than zero before accessing the body.");
if (!IsBodyRead)
{
if (Locked) throw new Exception("You cannot get the request body after request is made to server.");
if (throwWhenNotReadYet)
throw new Exception("Request body is not read yet. " +
"Use SessionEventArgs.GetRequestBody() or SessionEventArgs.GetRequestBodyAsString() " +
"method to read the request body.");
}
}
internal static void ParseRequestLine(string httpCmd, out string method, out ByteString requestUri,
out Version version)
{
var firstSpace = httpCmd.IndexOf(' ');
if (firstSpace == -1)
// does not contain at least 2 parts
throw new Exception("Invalid HTTP request line: " + httpCmd);
var lastSpace = httpCmd.LastIndexOf(' ');
// break up the line into three components (method, remote URL & Http Version)
// Find the request Verb
method = httpCmd.Substring(0, firstSpace);
if (!IsAllUpper(method)) method = method.ToUpper();
version = HttpHeader.Version11;
if (firstSpace == lastSpace)
{
requestUri = (ByteString)httpCmd.AsSpan(firstSpace + 1).ToString();
}
else
{
requestUri = (ByteString)httpCmd.AsSpan(firstSpace + 1, lastSpace - firstSpace - 1).ToString();
// parse the HTTP version
var httpVersion = httpCmd.AsSpan(lastSpace + 1);
if (httpVersion.EqualsIgnoreCase("HTTP/1.0".AsSpan(0))) version = HttpHeader.Version10;
}
}
private static bool IsAllUpper(string input)
{
for (var i = 0; i < input.Length; i++)
{
var ch = input[i];
if (ch < 'A' || ch > 'Z') return false;
}
return true;
}
}