Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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.AsSystemWeb();

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.AsSystemWeb();

var result = context.Session!["item"];
context.Session!["item"] = default(int);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public HttpApplicationFeature(HttpContextCore context, IHttpResponseEndFeature p
private HttpApplication InitializeApplication(HttpContextCore context)
{
var app = _pool.Get();
app.Context = context;
app.Context = context.AsSystemWeb();
_contextOrApplication = app;
return app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SessionEventsMiddleware(RequestDelegate next)
public async Task InvokeAsync(HttpContextCore context)
{
var app = context.Features.GetRequired<IHttpApplicationFeature>();
var session = context.GetAdapter().Session;
var session = context.AsSystemWeb().Session;

if (session is { IsNewSession: true })
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Task InvokeAsync(HttpContext context)
var context = (HttpContext)state;

WriteDefaultContentType(context);
context.Response.GetAdapter().Cache.UpdateHeaders();
context.Response.AsSystemWeb().Cache.UpdateHeaders();

return Task.CompletedTask;
}, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.AspNetCore.Http.Features;

namespace Microsoft.AspNetCore.SystemWebAdapters;

internal static class FeatureCollectionExtensions
{
internal static TFeature GetRequired<TFeature>(this IFeatureCollection features)
{
if (features.Get<TFeature>() is TFeature feature)
{
return feature;
}

throw new InvalidOperationException($"Feature {typeof(TFeature)} is not available");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ M:System.Web.HttpRequestBase.op_Implicit(Microsoft.AspNetCore.Http.HttpRequest)~

T:Microsoft.AspNetCore.SystemWebAdapters.SessionState.ISessionState
T:Microsoft.AspNetCore.SystemWebAdapters.SystemWebAdaptersOptions
T:Microsoft.AspNetCore.SystemWebAdapters.SystemWebAdaptersConversionExtensions

# We manually type forward this only for .NET 4.7.2+
T:System.Web.SameSiteMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void UpdateCacheControl(CacheControlHeaderValue cacheControl)
cacheControl.NoCache = GetCacheability() == HttpCacheability.NoCache;
}

private ResponseHeaders Headers => _context.GetAdapter().Response.TypedHeaders;
private ResponseHeaders Headers => _context.AsSystemWeb().Response.TypedHeaders;

public HttpCacheVaryByHeaders VaryByHeaders => _varyByHeaders ??= new();

Expand Down
17 changes: 15 additions & 2 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.SystemWebAdapters.Features;
using Microsoft.AspNetCore.SystemWebAdapters.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand Down Expand Up @@ -44,7 +45,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 Expand Up @@ -161,7 +174,7 @@ public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target)
}

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

[return: NotNullIfNotNull(nameof(context))]
public static implicit operator HttpContextCore?(HttpContext? context) => context?._context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public virtual IPrincipal User
public virtual object? GetService(Type serviceType) => throw new NotImplementedException();

[return: NotNullIfNotNull(nameof(context))]
public static implicit operator HttpContextBase?(HttpContextCore? context) => context?.GetAdapterBase();
public static implicit operator HttpContextBase?(HttpContextCore? context) => context?.AsSystemWebBase();

public virtual Caching.Cache Cache => throw new NotImplementedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ internal HttpCookieCollection(IRequestCookieCollection cookies)

internal HttpCookieCollection(HttpResponse response)
{
response.UnwrapAdapter().OnStarting(static state =>
response.AsAspNetCore().OnStarting(static state =>
{
var response = (HttpResponse)state;
var headers = response.UnwrapAdapter().Headers;
var headers = response.AsAspNetCore().Headers;
var isShareable = false;

for (var i = 0; i < response.Cookies.Count; i++)
Expand All @@ -54,7 +54,7 @@ internal HttpCookieCollection(HttpResponse response)
// Additionally, we should not set this header during an SSL request, as certain versions
// of IE don't handle it properly and simply refuse to render the page. More info:
// http://blogs.msdn.com/b/ieinternals/archive/2009/10/02/internet-explorer-cannot-download-over-https-when-no-cache.aspx
if (!isShareable && !response.UnwrapAdapter().HttpContext.Request.IsHttps && response.TypedHeaders.CacheControl is { Public: true } cacheControl)
if (!isShareable && !response.AsAspNetCore().HttpContext.Request.IsHttps && response.TypedHeaders.CacheControl is { Public: true } cacheControl)
{
cacheControl.NoCache = true;
cacheControl.NoCacheHeaders.Add(HeaderNames.SetCookie);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private static void WriteTo(Stream source, Stream destination)
public void Abort() => _request.HttpContext.Abort();

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

[return: NotNullIfNotNull(nameof(request))]
public static implicit operator HttpRequestCore?(HttpRequest? request) => request?._request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ public virtual string? ContentType
public virtual void Abort() => throw new NotImplementedException();

[return: NotNullIfNotNull(nameof(request))]
public static implicit operator HttpRequestBase?(HttpRequestCore? request) => request?.GetAdapterBase();
public static implicit operator HttpRequestBase?(HttpRequestCore? request) => request?.HttpContext.AsSystemWebBase().Request;
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void TransmitFile(string filename, long offset, long length)
=> _response.SendFileAsync(filename, offset, length >= 0 ? length : null).GetAwaiter().GetResult();

[return: NotNullIfNotNull(nameof(response))]
public static implicit operator HttpResponse?(HttpResponseCore? response) => response?.GetAdapter();
public static implicit operator HttpResponse?(HttpResponseCore? response) => response?.AsSystemWeb();

[return: NotNullIfNotNull(nameof(response))]
public static implicit operator HttpResponseCore?(HttpResponse? response) => response?._response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ public virtual Stream Filter
public virtual void TransmitFile(string filename, long offset, long length) => throw new NotImplementedException();

[return: NotNullIfNotNull(nameof(response))]
public static implicit operator HttpResponseBase?(HttpResponseCore? response) => response?.GetAdapterBase();
public static implicit operator HttpResponseBase?(HttpResponseCore? response) => response?.HttpContext.AsSystemWebBase().Response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ internal HttpServerUtility(HttpContextCore context)
public string MapPath(string? path)
=> _context.RequestServices.GetRequiredService<IMapPathUtility>().MapPath(_context.Request.Path, path);

public Exception? GetLastError() => _context.GetAdapter().Error;
public Exception? GetLastError() => _context.AsSystemWeb().Error;

public void ClearError() => _context.GetAdapter().ClearError();
public void ClearError() => _context.AsSystemWeb().ClearError();

/// <summary>
/// This method is similar to <see cref="WebEncoders.Base64UrlDecode(string)"/> but handles the trailing character that <see cref="UrlTokenEncode(byte[])"/>
Expand Down Expand Up @@ -113,7 +113,7 @@ public static string UrlTokenEncode(byte[] input)
public void Transfer(string path, bool preserveForm)
{
_context.Features.GetRequired<ITransferRequestFeature>().Transfer(path, preserveForm);
_context.Response.GetAdapter().End();
_context.Response.AsSystemWeb().End();
}

[Obsolete(Constants.TransferRequest.Message, DiagnosticId = Constants.TransferRequest.DiagnosticId)]
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

This file was deleted.

Loading