Skip to content
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

Implement HttpContext against Core HttpContext #427

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/SystemWebAdapters/samples/MvcCoreApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Web;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSystemWebAdapters();

var app = builder.Build();

Expand Down
23 changes: 22 additions & 1 deletion src/SystemWebAdapters/src/ExcludedApis.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
M:System.Web.HttpContext.#ctor
T:System.Web.SystemWebAdaptersExtensions

M:System.Web.HttpContext.#ctor(Microsoft.AspNetCore.Http.HttpContext)
M:System.Web.HttpContext.op_Implicit(Microsoft.AspNetCore.Http.HttpContext)~System.Web.HttpContext
M:System.Web.HttpContext.op_Implicit(System.Web.HttpContext)~Microsoft.AspNetCore.Http.HttpContext

M:System.Web.HttpContextBase.op_Implicit(Microsoft.AspNetCore.Http.HttpContext)~System.Web.HttpContextBase

M:System.Web.HttpRequest.#ctor(Microsoft.AspNetCore.Http.HttpRequest)
M:System.Web.HttpRequest.op_Implicit(Microsoft.AspNetCore.Http.HttpRequest)~System.Web.HttpRequest
M:System.Web.HttpRequest.op_Implicit(System.Web.HttpRequest)~Microsoft.AspNetCore.Http.HttpRequest

M:System.Web.HttpResponse.#ctor(Microsoft.AspNetCore.Http.HttpResponse)
M:System.Web.HttpResponse.op_Implicit(Microsoft.AspNetCore.Http.HttpResponse)~System.Web.HttpResponse
M:System.Web.HttpResponse.op_Implicit(System.Web.HttpResponse)~Microsoft.AspNetCore.Http.HttpResponse

M:System.Web.SessionState.HttpSessionState.#ctor(Microsoft.AspNetCore.Http.HttpContext)

M:System.Web.HttpServerUtility.#ctor(Microsoft.AspNetCore.Http.HttpContext)

M:System.Web.Caching.Cache.#ctor(Microsoft.AspNetCore.Http.HttpContext)

M:System.Web.Caching.Cache.#ctor(Microsoft.Extensions.Caching.Memory.IMemoryCache)
6 changes: 6 additions & 0 deletions src/SystemWebAdapters/src/Framework/TypeForwards.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpBrowserCapabilities))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpContext))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpContextBase))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpContextWrapper))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpCookie))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpCookieCollection))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpRequest))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpRequestBase))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpRequestWrapper))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpResponse))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpResponseBase))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpResponseWrapper))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpServerUtility))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpServerUtilityBase))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.SameSiteMode))]
Expand Down
90 changes: 81 additions & 9 deletions src/SystemWebAdapters/src/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,107 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Security.Claims;
using System.Security.Principal;
using System.Web.Caching;
using Microsoft.AspNetCore.Http;

namespace System.Web
{
public class HttpContext : IServiceProvider
{
public static HttpContext Current => throw new NotImplementedException();
private static readonly HttpContextAccessor _accessor = new();

public HttpRequest Request => throw new NotImplementedException();
private readonly HttpContextCore _context;

public HttpResponse Response => throw new NotImplementedException();
private HttpRequest? _request;
private HttpResponse? _response;
private HttpServerUtility? _server;

public static HttpContext? Current => _accessor.HttpContext;

public HttpContext(HttpContextCore context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

_context = context;
}

public HttpRequest Request
{
get
{
if (_request is null)
{
_request = new(_context.Request);
}

return _request;
}
}

public HttpResponse Response
{
get
{
if (_response is null)
{
_response = new(_context.Response);
}

return _response;
}
}

public IDictionary Items => throw new NotImplementedException();

public HttpServerUtility Server => throw new NotImplementedException();
public HttpServerUtility Server
{
get
{
if (_server is null)
{
_server = new(_context);
}

return _server;
}
}

public Cache Cache => throw new NotImplementedException();

public IPrincipal User
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
get => _context.User;
set => _context.User = value is ClaimsPrincipal claims ? claims : new ClaimsPrincipal(value);
}

public object GetService(Type serviceType)
public object? GetService(Type service)
{
throw new NotImplementedException();
if (service == typeof(HttpRequest))
{
return Request;
}
else if (service == typeof(HttpResponse))
{
return Response;
}
else if (service == typeof(HttpServerUtility))
{
return Server;
}

return null;
}
public static implicit operator HttpContext(HttpContextCore context) => throw new NotImplementedException();

[return: NotNullIfNotNull("context")]
public static implicit operator HttpContext?(HttpContextCore? context) => context?.GetAdapter();

[return: NotNullIfNotNull("context")]
public static implicit operator HttpContextCore?(HttpContext? context) => context?._context;
}
}
9 changes: 5 additions & 4 deletions src/SystemWebAdapters/src/HttpContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections;
using System.Security.Principal;
using System.Diagnostics.CodeAnalysis;

namespace System.Web
{
Expand All @@ -26,9 +27,9 @@ public virtual IPrincipal User

public HttpServerUtilityBase Server => throw new NotImplementedException();

public virtual object GetService(Type serviceType)
{
throw new NotImplementedException();
}
public virtual object? GetService(Type serviceType) => throw new NotImplementedException();

[return: NotNullIfNotNull("context")]
public static implicit operator HttpContextBase?(HttpContextCore? context) => context?.GetAdapterBase();
}
}
60 changes: 60 additions & 0 deletions src/SystemWebAdapters/src/HttpContextWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Security.Principal;

namespace System.Web
{
public class HttpContextWrapper : HttpContextBase
{
private readonly HttpContext _context;

private HttpRequestBase? _request;
private HttpResponseBase? _response;

public HttpContextWrapper(HttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}

_context = httpContext;
}

public override IDictionary Items => _context.Items;

public override HttpRequestBase Request
{
get
{
if (_request is null)
{
_request = new HttpRequestWrapper(_context.Request);
}

return _request;
}
}

public override HttpResponseBase Response
{
get
{
if (_response is null)
{
_response = new HttpResponseWrapper(_context.Response);
}

return _response;
}
}

public override IPrincipal User
{
get => _context.User;
set => _context.User = value;
}
}
}
16 changes: 15 additions & 1 deletion src/SystemWebAdapters/src/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security.Principal;
using System.Text;
Expand All @@ -10,6 +11,13 @@ namespace System.Web
{
public class HttpRequest
{
private readonly HttpRequestCore _request;

public HttpRequest(HttpRequestCore request)
{
_request = request;
}

public string Path => throw new NotImplementedException();

public NameValueCollection Headers => throw new NotImplementedException();
Expand All @@ -24,7 +32,7 @@ public class HttpRequest

public string[] UserLanguages => throw new NotImplementedException();

public string UserAgent => throw new NotImplementedException();
public string UserAgent => _request.Headers["User-Agent"];

public string RequestType => HttpMethod;

Expand Down Expand Up @@ -71,5 +79,11 @@ public string ContentType
public byte[] BinaryRead(int count) => throw new NotImplementedException();

public void Abort() => throw new NotImplementedException();

[return: NotNullIfNotNull("request")]
public static implicit operator HttpRequest?(HttpRequestCore? request) => request.GetAdapter();

[return: NotNullIfNotNull("request")]
public static implicit operator HttpRequestCore?(HttpRequest? request) => request?._request;
}
}
70 changes: 70 additions & 0 deletions src/SystemWebAdapters/src/HttpRequestWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Specialized;
using System.IO;
using System.Security.Principal;
using System.Text;

namespace System.Web
{
public class HttpRequestWrapper : HttpRequestBase
{
private readonly HttpRequest _request;

public HttpRequestWrapper(HttpRequest request)
{
_request = request;
}

public override void Abort() => _request.Abort();

public override byte[] BinaryRead(int count) => _request.BinaryRead(count);

public override Encoding ContentEncoding => _request.ContentEncoding;

public override int ContentLength => _request.ContentLength;

public override string ContentType
{
get => _request.ContentType;
set => _request.ContentType = value;
}

public override HttpCookieCollection Cookies => _request.Cookies;

public override NameValueCollection Headers => _request.Headers;

public override string HttpMethod => _request.HttpMethod;

public override Stream InputStream => _request.InputStream;

public override bool IsAuthenticated => _request.IsAuthenticated;

public override bool IsLocal => _request.IsLocal;

public override IIdentity LogonUserIdentity => _request.LogonUserIdentity;

public override string Path => _request.Path;

public override NameValueCollection QueryString => _request.QueryString;

public override string RawUrl => _request.RawUrl;

public override string RequestType => _request.RequestType;

public override int TotalBytes => _request.TotalBytes;

public override Uri Url => _request.Url;

public override Uri UrlReferrer => _request.UrlReferrer;

public override string UserAgent => _request.UserAgent;

public override string UserHostAddress => _request.UserHostAddress;

public override string UserHostName => _request.UserHostName;

public override string[] UserLanguages => _request.UserLanguages;
}
}
Loading