-
Notifications
You must be signed in to change notification settings - Fork 354
Implement some HttpRequest APIs against core #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e5d603b
Implement some HttpRequest APIs against core
twsouthwick c3de8ba
Add .NET 6.0 target for better nullability info
twsouthwick 7893400
Remove resource file
twsouthwick fa5b059
delinq UserLanguages
twsouthwick 766315e
Don't store RawUrl early
twsouthwick a654bf1
remove extra usings
twsouthwick cb0db76
pr feedback
twsouthwick b12f3b2
Use unescaped for apprelative
twsouthwick 00b27b0
Update IsLocal
twsouthwick c4ee544
remove unneeded comments
twsouthwick b5132db
Use PathBase.Value
twsouthwick 0c98398
Use StringSegment.Value
twsouthwick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,180 @@ | ||
// 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.Headers; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace System.Web | ||
{ | ||
public class HttpRequest | ||
{ | ||
private readonly HttpRequestCore _request; | ||
|
||
private RequestHeaders? _typedHeaders; | ||
private string[]? _userLanguages; | ||
|
||
public HttpRequest(HttpRequestCore request) | ||
{ | ||
_request = request; | ||
} | ||
|
||
public string Path => throw new NotImplementedException(); | ||
public string? Path => _request.Path.Value; | ||
|
||
public NameValueCollection Headers => throw new NotImplementedException(); | ||
|
||
public Uri Url => throw new NotImplementedException(); | ||
public Uri Url => new(_request.GetEncodedUrl()); | ||
|
||
// TODO: https://github.com/aspnet/AspLabs/pull/435#discussion_r807128348 | ||
public string RawUrl => throw new NotImplementedException(); | ||
|
||
public string HttpMethod => throw new NotImplementedException(); | ||
public string HttpMethod => _request.Method; | ||
|
||
public string UserHostAddress => throw new NotImplementedException(); | ||
public string? UserHostAddress => _request.HttpContext.Connection.RemoteIpAddress?.ToString(); | ||
|
||
public string[] UserLanguages => throw new NotImplementedException(); | ||
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++) | ||
{ | ||
userLanguages[i] = qualityArray[i].Value.Value; | ||
} | ||
|
||
ArrayPool<StringWithQualityHeaderValue>.Shared.Return(qualityArray); | ||
|
||
_userLanguages = userLanguages; | ||
} | ||
} | ||
|
||
return _userLanguages; | ||
} | ||
} | ||
|
||
public string UserAgent => _request.Headers["User-Agent"]; | ||
public string UserAgent => _request.Headers[HeaderNames.UserAgent]; | ||
|
||
public string RequestType => HttpMethod; | ||
|
||
public NameValueCollection Form => throw new NotImplementedException(); | ||
|
||
public HttpCookieCollection Cookies => throw new NotImplementedException(); | ||
|
||
public int ContentLength => throw new NotImplementedException(); | ||
public int ContentLength => (int)(_request.ContentLength ?? 0); | ||
|
||
public string ContentType | ||
public string? ContentType | ||
{ | ||
get => throw new NotImplementedException(); | ||
set => throw new NotImplementedException(); | ||
get => _request.ContentType; | ||
set => _request.ContentType = value; | ||
} | ||
|
||
public Stream InputStream => throw new NotImplementedException(); | ||
|
||
public NameValueCollection ServerVariables => throw new NotImplementedException(); | ||
|
||
public bool IsSecureConnection => throw new NotImplementedException(); | ||
public bool IsSecureConnection => _request.IsHttps; | ||
|
||
public NameValueCollection QueryString => throw new NotImplementedException(); | ||
|
||
public bool IsLocal => throw new NotImplementedException(); | ||
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 => throw new NotImplementedException(); | ||
public string AppRelativeCurrentExecutionFilePath => $"~{_request.Path.Value}"; | ||
|
||
public string ApplicationPath => throw new NotImplementedException(); | ||
public string? ApplicationPath => _request.PathBase.Value; | ||
|
||
public Uri UrlReferrer => throw new NotImplementedException(); | ||
public Uri? UrlReferrer => TypedHeaders.Referer; | ||
|
||
public int TotalBytes => throw new NotImplementedException(); | ||
// TODO: Since System.Web buffered by default, TotalBytes probably also worked for Chunked requests. We'll want to revisit this when we look at the request body buffering. | ||
public int TotalBytes => (int)_request.ContentLength.GetValueOrDefault(); | ||
|
||
public bool IsAuthenticated => throw new NotImplementedException(); | ||
public bool IsAuthenticated => LogonUserIdentity?.IsAuthenticated ?? false; | ||
|
||
public IIdentity LogonUserIdentity => throw new NotImplementedException(); | ||
public IIdentity? LogonUserIdentity => _request.HttpContext.User.Identity; | ||
|
||
public Encoding ContentEncoding => throw new NotImplementedException(); | ||
public Encoding? ContentEncoding => TypedHeaders.ContentType?.Encoding; | ||
|
||
public string UserHostName => throw new NotImplementedException(); | ||
public string? UserHostName => _request.HttpContext.Connection.RemoteIpAddress?.ToString(); | ||
|
||
public HttpBrowserCapabilities Browser => throw new NotImplementedException(); | ||
|
||
public byte[] BinaryRead(int count) => throw new NotImplementedException(); | ||
|
||
public void Abort() => throw new NotImplementedException(); | ||
public void Abort() => _request.HttpContext.Abort(); | ||
|
||
private RequestHeaders TypedHeaders | ||
{ | ||
get | ||
{ | ||
if (_typedHeaders is null) | ||
{ | ||
_typedHeaders = new(_request.Headers); | ||
} | ||
|
||
return _typedHeaders; | ||
} | ||
} | ||
|
||
[return: NotNullIfNotNull("request")] | ||
public static implicit operator HttpRequest?(HttpRequestCore? request) => request.GetAdapter(); | ||
|
||
[return: NotNullIfNotNull("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); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.