Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions samples/CoreApp/SessionExampleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void SetValue(HttpContext context, byte[] data)

builder.MapGet("/count", (HttpContextCore ctx) =>
{
var context = (HttpContext)ctx;
var context = ctx.GetAdapter();

if (context.Session!["callCount"] is not int count)
{
Expand All @@ -69,7 +69,7 @@ static void SetValue(HttpContext context, byte[] data)

builder.MapGet("/item", (HttpContextCore ctx) =>
{
var context = (HttpContext)ctx;
var context = ctx.GetAdapter();

var result = context.Session!["item"];
context.Session!["item"] = default(int);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ M:System.Web.HttpResponse.op_Implicit(Microsoft.AspNetCore.Http.HttpResponse)~Sy
M:System.Web.HttpResponse.op_Implicit(System.Web.HttpResponse)~Microsoft.AspNetCore.Http.HttpResponse
M:System.Web.HttpResponseBase.op_Implicit(Microsoft.AspNetCore.Http.HttpResponse)~System.Web.HttpResponseBase
M:System.Web.HttpRequestBase.op_Implicit(Microsoft.AspNetCore.Http.HttpRequest)~System.Web.HttpRequestBase
T:Microsoft.AspNetCore.SystemWebAdapters.SystemWebAdapterExtensions

T:Microsoft.AspNetCore.SystemWebAdapters.SessionState.ISessionState

# We manually type forward this only for .NET 4.7.2+
T:System.Web.SameSiteMode

T:Microsoft.AspNetCore.SystemWebAdapters.IBrowserCapabilitiesFactory
T:Microsoft.AspNetCore.SystemWebAdapters.IHttpBrowserCapabilityFeature

# We provide implementations of this on .NET Standard 2.0 and .NET 6.0
T:System.Web.IHtmlString

Expand Down
16 changes: 15 additions & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.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;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
Expand All @@ -11,6 +12,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.SystemWebAdapters.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand Down Expand Up @@ -38,7 +40,19 @@ internal HttpContext(HttpContextCore context)

public HttpResponse Response => _response ??= new(_context.Response);

public IDictionary Items => _items ??= _context.Items.AsNonGeneric();
public IDictionary Items
{
get
{
if (_items is null)
{
var items = _context.Items;
_items = items is IDictionary d ? d : new NonGenericDictionaryWrapper(items);
}

return _items;
}
}

public HttpServerUtility Server => _server ??= new(_context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ICollection Keys
{
if (_keys is null)
{
_keys = _original.Keys.AsNonGeneric();
_keys = AsNonGeneric(_original.Keys);
}

return _keys;
Expand All @@ -49,7 +49,7 @@ public ICollection Values
{
if (_values is null)
{
_values = _original.Values.AsNonGeneric();
_values = AsNonGeneric(_original.Values);
}

return _values;
Expand Down Expand Up @@ -78,6 +78,9 @@ public void CopyTo(Array array, int index)
}
}

private static ICollection AsNonGeneric<T>(ICollection<T> collection)
=> collection is ICollection c ? c : new NonGenericCollectionWrapper<T>(collection);

public void Remove(object key) => _original.Remove(key);

public IDictionaryEnumerator GetEnumerator() => new DictionaryEnumerator(_original);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,38 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Web;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SystemWebAdapters.Internal;

namespace Microsoft.AspNetCore.SystemWebAdapters;

internal static class InternalSystemWebExtensions
public static class SystemWebAdapterExtensions
{
[return: NotNullIfNotNull(nameof(request))]
internal static HttpRequest? GetAdapter(this HttpRequestCore? request)
public static HttpRequest? GetAdapter(this HttpRequestCore? request)
=> request?.HttpContext.GetAdapter().Request;

[return: NotNullIfNotNull(nameof(request))]
internal static HttpRequestBase? GetAdapterBase(this HttpRequestCore? request)
public static HttpRequestBase? GetAdapterBase(this HttpRequestCore? request)
=> request?.HttpContext.GetAdapterBase().Request;

[return: NotNullIfNotNull(nameof(request))]
internal static HttpRequestCore? UnwrapAdapter(this HttpRequest? request) => request;
public static HttpRequestCore? UnwrapAdapter(this HttpRequest? request) => request;

[return: NotNullIfNotNull(nameof(response))]
internal static HttpResponse? GetAdapter(this HttpResponseCore? response)
public static HttpResponse? GetAdapter(this HttpResponseCore? response)
=> response?.HttpContext.GetAdapter().Response;

[return: NotNullIfNotNull("request")]
internal static HttpResponseBase? GetAdapterBase(this HttpResponseCore? response)
[return: NotNullIfNotNull(nameof(response))]
public static HttpResponseBase? GetAdapterBase(this HttpResponseCore? response)
=> response?.HttpContext.GetAdapterBase().Response;

[return: NotNullIfNotNull(nameof(response))]
internal static HttpResponseCore? UnwrapAdapter(this HttpResponse? response) => response;

internal static IDictionary AsNonGeneric(this IDictionary<object, object?> dictionary)
=> dictionary is IDictionary d ? d : new NonGenericDictionaryWrapper(dictionary);

internal static ICollection AsNonGeneric<T>(this ICollection<T> collection)
=> collection is ICollection c ? c : new NonGenericCollectionWrapper<T>(collection);
public static HttpResponseCore? UnwrapAdapter(this HttpResponse? response) => response;

[return: NotNullIfNotNull(nameof(context))]
internal static HttpContext? GetAdapter(this HttpContextCore? context)
public static HttpContext? GetAdapter(this HttpContextCore? context)
{
if (context is null)
{
Expand All @@ -61,10 +52,10 @@ internal static ICollection AsNonGeneric<T>(this ICollection<T> collection)
}

[return: NotNullIfNotNull(nameof(context))]
internal static HttpContextCore? UnwrapAdapter(this HttpContext? context) => context;
public static HttpContextCore? UnwrapAdapter(this HttpContext? context) => context;

[return: NotNullIfNotNull(nameof(context))]
internal static HttpContextBase? GetAdapterBase(this HttpContextCore? context)
public static HttpContextBase? GetAdapterBase(this HttpContextCore? context)
{
if (context is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Task RequestInfo()
Assert.Equal("/", context.Request.Path);
Assert.Empty(context.Request.Query);

var adapter = (System.Web.HttpRequest)context.Request;
var adapter = context.Request.GetAdapter();

Assert.Equal("", adapter.PathInfo);
Assert.Equal("/", adapter.FilePath);
Expand All @@ -33,7 +33,7 @@ public Task RequestInfo()
public Task RewriteRequestPath()
=> RunTest("/", context =>
{
var adapter = (System.Web.HttpContext)context;
var adapter = context.GetAdapter();

adapter.RewritePath("/some/path?q=1");

Expand All @@ -56,7 +56,7 @@ public Task RewriteRequestPath()
public Task RewriteRequestPathInfo()
=> RunTest("/", context =>
{
var adapter = (System.Web.HttpContext)context;
var adapter = context.GetAdapter();

adapter.RewritePath("/some/path", "/pathInfo", "q=1");

Expand All @@ -79,7 +79,7 @@ public Task RewriteRequestPathInfo()
public Task RewritePathViaCoreApis()
=> RunTest("/", context =>
{
var adapter = (System.Web.HttpContext)context;
var adapter = context.GetAdapter();

// This is the same as RewriteRequestPathInfo as above to get a custom PathInfo
adapter.RewritePath("/some/path", "/pathInfo", "q=1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -46,7 +44,7 @@ public async Task BufferedOutputIsFlushedOnceWithStart()
var result = await RunAsync(async context =>
{
context.Response.Write(ContentValue);
await ((HttpResponseCore)context.Response).StartAsync();
await context.Response.UnwrapAdapter().StartAsync();
}, builder => builder.BufferResponseStream());

Assert.Equal(ContentValue, result);
Expand All @@ -58,7 +56,7 @@ public async Task BufferedOutputIsFlushedOnceWithComplete()
var result = await RunAsync(async context =>
{
context.Response.Write(ContentValue);
await ((HttpResponseCore)context.Response).CompleteAsync();
await context.Response.UnwrapAdapter().CompleteAsync();
}, builder => builder.BufferResponseStream());

Assert.Equal(ContentValue, result);
Expand Down