|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Hosting.Server; |
| 7 | +using Microsoft.AspNetCore.Http.Features; |
| 8 | +using Microsoft.AspNetCore.Http.Features.Authentication; |
| 9 | +using Microsoft.Extensions.ObjectPool; |
| 10 | +using static KestrelPureOwin.Constants; |
| 11 | + |
| 12 | +namespace KestrelPureOwin |
| 13 | +{ |
| 14 | + using AppFunc = Func<IDictionary<string, object>, Task>; |
| 15 | + |
| 16 | + internal class OwinApplication : IHttpApplication<Dictionary<string, object>> |
| 17 | + { |
| 18 | + private static readonly ObjectPoolProvider PoolProvider = new DefaultObjectPoolProvider(); |
| 19 | + |
| 20 | + public OwinApplication(AppFunc application) |
| 21 | + { |
| 22 | + Application = application; |
| 23 | + EnvironmentPool = PoolProvider.Create<Dictionary<string, object>>(); |
| 24 | + } |
| 25 | + |
| 26 | + private AppFunc Application { get; } |
| 27 | + |
| 28 | + private ObjectPool<Dictionary<string, object>> EnvironmentPool { get; } |
| 29 | + |
| 30 | + public Dictionary<string, object> CreateContext(IFeatureCollection features) |
| 31 | + { |
| 32 | + var environment = EnvironmentPool.Get(); |
| 33 | + |
| 34 | + var request = features.Get<IHttpRequestFeature>(); |
| 35 | + var response = features.Get<IHttpResponseFeature>(); |
| 36 | + var connection = features.Get<IHttpConnectionFeature>(); |
| 37 | + var lifetime = features.Get<IHttpRequestLifetimeFeature>(); |
| 38 | + var identifier = features.Get<IHttpRequestIdentifierFeature>(); |
| 39 | + var authentication = features.Get<IHttpAuthenticationFeature>(); |
| 40 | + |
| 41 | + environment.Clear(); |
| 42 | + |
| 43 | + environment.Set(Owin.Request.Body, request.Body); |
| 44 | + environment.Set(Owin.Request.Headers, new OwinHeaderDictionary(request.Headers)); |
| 45 | + environment.Set(Owin.Request.Method, request.Method); |
| 46 | + environment.Set(Owin.Request.Path, request.Path); |
| 47 | + environment.Set(Owin.Request.PathBase, request.PathBase); |
| 48 | + environment.Set(Owin.Request.Protocol, request.Protocol); |
| 49 | + environment.Set(Owin.Request.QueryString, request.QueryString.TrimStart('?')); |
| 50 | + environment.Set(Owin.Request.Scheme, request.Scheme); |
| 51 | + environment.Set(Owin.Request.User, authentication?.User); |
| 52 | + environment.Set(Owin.Request.Id, identifier?.TraceIdentifier); |
| 53 | + |
| 54 | + environment.Set(Owin.Response.Body, response.Body); |
| 55 | + environment.Set(Owin.Response.Headers, new OwinHeaderDictionary(response.Headers)); |
| 56 | + environment.Set(Owin.Response.StatusCode, response.StatusCode); |
| 57 | + environment.Set(Owin.Response.ReasonPhrase, response.ReasonPhrase); |
| 58 | + environment.Set(Owin.Response.Protocol, request.Protocol); |
| 59 | + |
| 60 | + environment.Set(Owin.CallCancelled, lifetime?.RequestAborted ?? CancellationToken.None); |
| 61 | + environment.Set(Owin.OwinVersion, "1.1"); |
| 62 | + |
| 63 | + environment.Set(Server.OnSendingHeaders, CreateCallback(response.OnStarting)); |
| 64 | + environment.Set(Server.RemoteIpAddress, connection?.RemoteIpAddress.ToString()); |
| 65 | + environment.Set(Server.RemotePort, connection?.RemotePort.ToString()); |
| 66 | + environment.Set(Server.LocalIpAddress, connection?.LocalIpAddress.ToString()); |
| 67 | + environment.Set(Server.LocalPort, connection?.LocalPort.ToString()); |
| 68 | + environment.Set(Server.User, authentication?.User); |
| 69 | + environment.Set(Server.Features, features); |
| 70 | + |
| 71 | + response.OnStarting(SetStatus, (response, environment)); |
| 72 | + |
| 73 | + return environment; |
| 74 | + } |
| 75 | + |
| 76 | + private static Task SetStatus(object state) |
| 77 | + { |
| 78 | + var (response, environment) = ((IHttpResponseFeature, Dictionary<string, object>)) state; |
| 79 | + |
| 80 | + response.StatusCode = environment.Get<int>(Owin.Response.StatusCode); |
| 81 | + response.ReasonPhrase = environment.Get<string>(Owin.Response.ReasonPhrase); |
| 82 | + |
| 83 | + return Tasks.Completed; |
| 84 | + } |
| 85 | + |
| 86 | + public async Task ProcessRequestAsync(Dictionary<string, object> environment) |
| 87 | + { |
| 88 | + await Application.Invoke(environment).ConfigureAwait(false); |
| 89 | + |
| 90 | + var features = environment.Get<IFeatureCollection>(Server.Features); |
| 91 | + |
| 92 | + var response = features.Get<IHttpResponseFeature>(); |
| 93 | + |
| 94 | + var owinHeaders = environment.Get<IDictionary<string, string[]>>(Owin.Response.Headers); |
| 95 | + |
| 96 | + response.Body = environment.Get<Stream>(Owin.Response.Body); |
| 97 | + response.Headers = new ReverseOwinHeaderDictionary(owinHeaders); |
| 98 | + } |
| 99 | + |
| 100 | + public void DisposeContext(Dictionary<string, object> environment, Exception exception) |
| 101 | + { |
| 102 | + EnvironmentPool.Return(environment); |
| 103 | + } |
| 104 | + |
| 105 | + private static Action<Action<object>, object> CreateCallback(Action<Func<object, Task>, object> onStarting) |
| 106 | + { |
| 107 | + return (callback, state) => onStarting(WrapCallback(callback), state); |
| 108 | + } |
| 109 | + |
| 110 | + private static Func<object, Task> WrapCallback(Action<object> callback) |
| 111 | + { |
| 112 | + return state => |
| 113 | + { |
| 114 | + callback(state); |
| 115 | + return Tasks.Completed; |
| 116 | + }; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments