-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathHttpRequest.cs
More file actions
357 lines (316 loc) · 10.5 KB
/
HttpRequest.cs
File metadata and controls
357 lines (316 loc) · 10.5 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.IO;
#if !WINDOWS_UWP
using System.Security.Cryptography.X509Certificates;
#endif
using System.Threading.Tasks;
using Waher.Content;
using Waher.Content.Binary;
using Waher.Networking.HTTP.HeaderFields;
using Waher.Networking.HTTP.HTTP2;
using Waher.Runtime.IO;
using Waher.Security;
namespace Waher.Networking.HTTP
{
/// <summary>
/// Represents an HTTP request.
/// </summary>
public class HttpRequest : IDisposable, IHostReference
{
private readonly HttpRequestHeader header;
private Stream dataStream;
private readonly HttpServer server;
private readonly string remoteEndPoint;
private readonly string localEndPoint;
private readonly Http2Stream http2Stream;
private IUser user = null;
private SessionVariables session = null;
private string subPath = string.Empty;
private HttpResource resource = null;
private HttpResponse response = null;
private Guid? requestId = null;
private ContentResponse decoded = null;
internal HttpClientConnection clientConnection = null;
internal bool tempSession = false;
internal bool defaultEncrypted = false;
/// <summary>
/// Represents an HTTP request.
/// </summary>
/// <param name="Server">HTTP Server receiving the request.</param>
/// <param name="Header">HTTP Request header.</param>
/// <param name="Data">Stream to data content, if available, or null, if request does not have a message body.</param>
/// <param name="RemoteEndPoint">Remote end-point.</param>
/// <param name="LocalEndPoint">Local end-point.</param>
public HttpRequest(HttpServer Server, HttpRequestHeader Header, Stream Data, string RemoteEndPoint,
string LocalEndPoint)
: this(Server, Header, Data, RemoteEndPoint, LocalEndPoint, false, null)
{
}
/// <summary>
/// Represents an HTTP request.
/// </summary>
/// <param name="Server">HTTP Server receiving the request.</param>
/// <param name="Header">HTTP Request header.</param>
/// <param name="Data">Stream to data content, if available, or null, if request does not have a message body.</param>
/// <param name="RemoteEndPoint">Remote end-point.</param>
/// <param name="LocalEndPoint">Local end-point.</param>
/// <param name="Http2Stream">HTTP/2 stream</param>
public HttpRequest(HttpServer Server, HttpRequestHeader Header, Stream Data, string RemoteEndPoint,
string LocalEndPoint, Http2Stream Http2Stream)
: this(Server, Header, Data, RemoteEndPoint, LocalEndPoint, false, Http2Stream)
{
}
/// <summary>
/// Represents an HTTP request.
/// </summary>
/// <param name="Server">HTTP Server receiving the request.</param>
/// <param name="Header">HTTP Request header.</param>
/// <param name="Data">Stream to data content, if available, or null, if request does not have a message body.</param>
/// <param name="RemoteEndPoint">Remote end-point.</param>
/// <param name="LocalEndPoint">Local end-point.</param>
/// <param name="DefaultEncrypted">If underlying transport is encrypted by default.</param>
public HttpRequest(HttpServer Server, HttpRequestHeader Header, Stream Data, string RemoteEndPoint,
string LocalEndPoint, bool DefaultEncrypted)
: this(Server, Header, Data, RemoteEndPoint, LocalEndPoint, DefaultEncrypted, null)
{
}
/// <summary>
/// Represents an HTTP request.
/// </summary>
/// <param name="Server">HTTP Server receiving the request.</param>
/// <param name="Header">HTTP Request header.</param>
/// <param name="Data">Stream to data content, if available, or null, if request does not have a message body.</param>
/// <param name="RemoteEndPoint">Remote end-point.</param>
/// <param name="LocalEndPoint">Local end-point.</param>
/// <param name="DefaultEncrypted">If underlying transport is encrypted by default.</param>
/// <param name="Http2Stream">HTTP/2 stream</param>
public HttpRequest(HttpServer Server, HttpRequestHeader Header, Stream Data, string RemoteEndPoint,
string LocalEndPoint, bool DefaultEncrypted, Http2Stream Http2Stream)
{
this.server = Server;
this.header = Header;
this.dataStream = Data;
this.remoteEndPoint = RemoteEndPoint;
this.localEndPoint = LocalEndPoint;
this.defaultEncrypted = DefaultEncrypted;
this.http2Stream = Http2Stream;
if (!(this.dataStream is null))
this.dataStream.Position = 0;
}
/// <summary>
/// If the request has data.
/// </summary>
public bool HasData => !(this.dataStream is null);
/// <summary>
/// HTTP Server receiving the request.
/// </summary>
public HttpServer Server => this.server;
/// <summary>
/// HTTP/2 stream
/// </summary>
public Http2Stream Http2Stream => this.http2Stream;
/// <summary>
/// Decodes data sent in request.
/// </summary>
/// <returns>Decoded data.</returns>
[Obsolete("Use DecodeDataAsync() instead, for better performance processing asynchronous elements in parallel environments.")]
public ContentResponse DecodeData()
{
return this.DecodeDataAsync().Result;
}
/// <summary>
/// Decodes data sent in request.
/// </summary>
/// <returns>Decoded data.</returns>
public async Task<ContentResponse> DecodeDataAsync()
{
if (!(this.decoded is null))
return this.decoded;
byte[] Data = await this.ReadDataAsync();
if (Data is null)
{
Data = Array.Empty<byte>();
this.decoded = new ContentResponse(BinaryCodec.DefaultContentType, Data, Data);
return this.decoded;
}
HttpFieldContentType ContentType = this.header.ContentType;
if (ContentType is null)
{
this.decoded = new ContentResponse(BinaryCodec.DefaultContentType, Data, Data);
return this.decoded;
}
this.decoded = await InternetContent.DecodeAsync(ContentType.Type, Data,
ContentType.Encoding, ContentType.Fields, new Uri(this.header.GetURL(false, false)));
return this.decoded;
}
/// <summary>
/// Reads posted binary data
/// </summary>
/// <returns>Binary data, undecoded.</returns>
/// <exception cref="OutOfMemoryException">If posted object is too large for in-memory processing.</exception>
public async Task<byte[]> ReadDataAsync()
{
if (this.dataStream is null)
return null;
this.dataStream.Position = 0;
return await this.dataStream.ReadAllAsync();
}
/// <summary>
/// Request header.
/// </summary>
public HttpRequestHeader Header => this.header;
/// <summary>
/// Data stream, if data is available, or null if data is not available.
/// </summary>
public Stream DataStream => this.dataStream;
/// <summary>
/// Sub-path. If a resource is found handling the request, this property contains the trailing sub-path of the full path,
/// relative to the path of the resource object.
/// </summary>
public string SubPath
{
get => this.subPath;
set => this.subPath = value;
}
/// <summary>
/// Authenticated user, if available, or null if not available.
/// </summary>
public IUser User
{
get => this.user;
set => this.user = value;
}
/// <summary>
/// Contains session states, if the resource requires sessions, or null otherwise.
/// </summary>
public SessionVariables Session
{
get => this.session;
set => this.session = value;
}
/// <summary>
/// Resource being accessed.
/// </summary>
public HttpResource Resource
{
get => this.resource;
set => this.resource = value;
}
/// <summary>
/// ID of request.
/// </summary>
public Guid RequestId
{
get
{
if (!this.requestId.HasValue)
this.requestId = Guid.NewGuid();
return this.requestId.Value;
}
}
/// <summary>
/// Remote end-point.
/// </summary>
public string RemoteEndPoint => this.remoteEndPoint;
/// <summary>
/// Local end-point.
/// </summary>
public string LocalEndPoint => this.localEndPoint;
/// <summary>
/// HTTP Response object, if one has been assigned to the request.
/// </summary>
public HttpResponse Response
{
get => this.response;
internal set => this.response = value;
}
/// <summary>
/// Host reference. (Value of Host header, without the port number)
/// </summary>
public string Host
{
get
{
string s = this.header?.Host?.Value;
if (string.IsNullOrEmpty(s))
return s;
return s.RemovePortNumber();
}
}
#if WINDOWS_UWP
/// <summary>
/// If the connection is encrypted or not.
/// </summary>
public bool Encrypted => this.defaultEncrypted;
/// <summary>
/// Cipher strength
/// </summary>
public int CipherStrength => 0;
#else
/// <summary>
/// Remote client certificate, if any, associated with the request.
/// </summary>
public X509Certificate RemoteCertificate => this.clientConnection?.Client?.RemoteCertificate;
/// <summary>
/// If the Remote client certificate, if any, is valid.
/// </summary>
public bool RemoteCertificateValid => this.clientConnection?.Client?.RemoteCertificateValid ?? false;
/// <summary>
/// If the connection is encrypted or not.
/// </summary>
public bool Encrypted => this.clientConnection?.Encrypted ?? this.defaultEncrypted;
/// <summary>
/// Cipher strength
/// </summary>
public int CipherStrength
{
get
{
BinaryTcpClient Client = this.clientConnection.Client;
return Client is null ? 0 : Math.Min(Client.CipherStrength, Client.KeyExchangeStrength);
}
}
#endif
/// <summary>
/// Tries to get a file name for a resource, if local.
/// </summary>
/// <param name="Resource">Resource</param>
/// <param name="Host">Optional host, if available.</param>
/// <param name="FileName">File name, if resource identified as a local resource.</param>
/// <returns>If successful in identifying a local file name for the resource.</returns>
public bool TryGetLocalResourceFileName(string Resource, string Host, out string FileName)
{
if (string.IsNullOrEmpty(Host))
Host = this.Host;
return this.server.TryGetLocalResourceFileName(Resource, Host, out FileName);
}
/// <summary>
/// Disposes of the request.
/// </summary>
public void Dispose()
{
this.dataStream?.Dispose();
this.dataStream = null;
}
/// <summary>
/// Gets the session variables from the cookie, if available.
/// </summary>
/// <returns>Session variables, if cookie found. If no session is found, a
/// temporary session is created.</returns>
public SessionVariables GetSessionFromCookie()
{
if (this.session is null)
{
string HttpSessionID = HttpResource.GetSessionId(this, this.response);
if (!string.IsNullOrEmpty(HttpSessionID))
this.session = this.server.GetSession(HttpSessionID);
if (this.session is null)
{
this.session = HttpServer.CreateSessionVariables();
this.tempSession = true;
}
}
return this.session;
}
}
}