forked from dotnet/systemweb-adapters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.cs
308 lines (232 loc) · 10.8 KB
/
HttpRequest.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Security.Principal;
using System.Text;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.SystemWebAdapters.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
namespace System.Web
{
public class HttpRequest
{
private readonly HttpRequestCore _request;
private RequestHeaders? _typedHeaders;
private string[]? _userLanguages;
private string[]? _acceptTypes;
private NameValueCollection? _headers;
private NameValueCollection? _serverVariables;
private NameValueCollection? _form;
private NameValueCollection? _query;
private HttpFileCollection? _files;
private HttpCookieCollection? _cookies;
private NameValueCollection? _params;
private HttpBrowserCapabilities? _browser;
internal HttpRequest(HttpRequestCore request)
{
_request = request;
}
internal RequestHeaders TypedHeaders => _typedHeaders ??= new(_request.Headers);
public string Path => _request.HttpContext.Features.GetRequired<IHttpRequestPathFeature>().Path;
public string PathInfo => _request.HttpContext.Features.GetRequired<IHttpRequestPathFeature>().PathInfo;
public string FilePath => _request.HttpContext.Features.GetRequired<IHttpRequestPathFeature>().FilePath;
[SuppressMessage("Design", "CA1056:URI-like properties should not be strings", Justification = Constants.ApiFromAspNet)]
public string RawUrl => _request.HttpContext.Features.GetRequired<IHttpRequestPathFeature>().RawUrl;
public string CurrentExecutionFilePath => _request.HttpContext.Features.GetRequired<IHttpRequestPathFeature>().CurrentExecutionFilePath;
public NameValueCollection Headers => _headers ??= _request.Headers.ToNameValueCollection();
public Uri Url => new(_request.GetEncodedUrl());
public ReadEntityBodyMode ReadEntityBodyMode => _request.HttpContext.Features.GetRequired<IHttpRequestInputStreamFeature>().Mode;
public Stream GetBufferlessInputStream() => _request.HttpContext.Features.GetRequired<IHttpRequestInputStreamFeature>().GetBufferlessInputStream();
public Stream GetBufferedInputStream() => _request.HttpContext.Features.GetRequired<IHttpRequestInputStreamFeature>().GetBufferedInputStream();
public string HttpMethod => _request.Method;
public string? UserHostAddress => _request.HttpContext.Connection.RemoteIpAddress?.ToString();
[SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = Constants.ApiFromAspNet)]
public string[] UserLanguages
{
get
{
if (_userLanguages is null)
{
var languages = TypedHeaders.AcceptLanguage;
var length = languages.Count;
if (length == 0)
{
_userLanguages = Array.Empty<string>();
}
else
{
var qualityArray = ArrayPool<StringWithQualityHeaderValue>.Shared.Rent(length);
var userLanguages = new string[length];
languages.CopyTo(qualityArray, 0);
Array.Sort(qualityArray, 0, length, StringWithQualityHeaderValueComparer.Instance);
for (var i = 0; i < length; i++)
{
if (qualityArray[i].Value.Value is { } language)
{
userLanguages[i] = language;
}
}
ArrayPool<StringWithQualityHeaderValue>.Shared.Return(qualityArray);
_userLanguages = userLanguages;
}
}
return _userLanguages;
}
}
public string? UserAgent => _request.Headers[HeaderNames.UserAgent];
public string RequestType => HttpMethod;
public NameValueCollection Form => _form ??= _request.HasFormContentType ? _request.Form.ToNameValueCollection() : StringValuesReadOnlyDictionaryNameValueCollection.Empty;
public HttpCookieCollection Cookies => _cookies ??= new(_request.Cookies);
public HttpFileCollection Files => _files ??= _request.HasFormContentType ? new(_request.Form.Files) : HttpFileCollection.Empty;
public int ContentLength => (int)(_request.ContentLength ?? 0);
[SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = Constants.ApiFromAspNet)]
public string[] AcceptTypes
{
get
{
if (_acceptTypes is null)
{
var accept = TypedHeaders.Accept;
if (accept.Count == 0)
{
_acceptTypes = Array.Empty<string>();
}
else
{
_acceptTypes = new string[accept.Count];
for (var i = 0; i < accept.Count; i++)
{
if (accept[i].MediaType.Value is { } value)
{
_acceptTypes[i] = value;
}
}
}
}
return _acceptTypes;
}
}
public string? ContentType
{
get => _request.ContentType;
set => _request.ContentType = value;
}
public Stream InputStream => _request.HttpContext.Features.GetRequired<IHttpRequestInputStreamFeature>().InputStream;
public NameValueCollection ServerVariables => _serverVariables ??= _request.HttpContext.Features.GetRequired<IServerVariablesFeature>().ToNameValueCollection();
public bool IsSecureConnection => _request.IsHttps;
public NameValueCollection QueryString => _query ??= _request.Query.ToNameValueCollection();
public bool IsLocal
{
get
{
var connectionInfo = _request.HttpContext.Connection;
// If unknown, assume not local
if (connectionInfo.RemoteIpAddress is null)
{
return false;
}
// Check if localhost
if (IPAddress.IsLoopback(connectionInfo.RemoteIpAddress))
{
return true;
}
return connectionInfo.RemoteIpAddress.Equals(connectionInfo.LocalIpAddress);
}
}
public string AppRelativeCurrentExecutionFilePath => $"~{FilePath}";
public string ApplicationPath => _request.HttpContext.RequestServices.GetRequiredService<IOptions<SystemWebAdaptersOptions>>().Value.AppDomainAppVirtualPath;
public Uri? UrlReferrer => TypedHeaders.Referer;
public int TotalBytes => (int)InputStream.Length;
public bool IsAuthenticated => LogonUserIdentity?.IsAuthenticated ?? false;
public IIdentity? LogonUserIdentity => _request.HttpContext.User.Identity;
public Encoding? ContentEncoding => TypedHeaders.ContentType?.Encoding;
public string? UserHostName => _request.HttpContext.Connection.RemoteIpAddress?.ToString();
public HttpBrowserCapabilities Browser => _browser ??= new(_request.HttpContext);
public string? this[string key] => Params[key];
public NameValueCollection Params => _params ??= new ParamsCollection(_request);
public byte[] BinaryRead(int count)
{
#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfNegative(count);
#else
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
#endif
if (count == 0)
{
return Array.Empty<byte>();
}
var buffer = new byte[count];
var read = InputStream.Read(buffer);
if (read == 0)
{
return Array.Empty<byte>();
}
if (read < count)
{
Array.Resize(ref buffer, read);
}
return buffer;
}
public void SaveAs(string filename, bool includeHeaders)
{
using var f = new FileStream(filename, FileMode.Create);
if (includeHeaders)
{
using var w = new StreamWriter(f, leaveOpen: true);
w.Write(HttpMethod);
w.Write(" ");
w.Write(Path);
// Includes the leading '?' if non-empty
w.Write(_request.QueryString);
w.Write(" ");
w.WriteLine(_request.Protocol);
foreach (var header in _request.Headers)
{
w.Write(header.Key);
w.Write(": ");
w.WriteLine(header.Value);
}
w.WriteLine();
}
WriteTo(GetBufferedInputStream(), f);
}
/// <summary>
/// Copies the entire stream, but ensures the position is reset to what it was when starting
/// </summary>
private static void WriteTo(Stream source, Stream destination)
{
var currentPosition = source.Position;
source.Position = 0;
source.CopyTo(destination);
source.Position = currentPosition;
}
public void Abort() => _request.HttpContext.Abort();
[return: NotNullIfNotNull(nameof(request))]
public static implicit operator HttpRequest?(HttpRequestCore? request) => request.GetAdapter();
[return: NotNullIfNotNull(nameof(request))]
public static implicit operator HttpRequestCore?(HttpRequest? request) => request?._request;
private class StringWithQualityHeaderValueComparer : IComparer<StringWithQualityHeaderValue>
{
public static StringWithQualityHeaderValueComparer Instance { get; } = new();
public int Compare(StringWithQualityHeaderValue? x, StringWithQualityHeaderValue? y)
{
var xValue = x?.Quality ?? 1;
var yValue = y?.Quality ?? 1;
return yValue.CompareTo(xValue);
}
}
}
}