| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Configuration; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | using Microsoft.AspNetCore.Http; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Channels |
| | | 14 | | { |
| | | 15 | | internal class AspNetCoreReplyChannel : IReplyChannel |
| | | 16 | | { |
| | | 17 | | private readonly IServiceProvider _serviceProvider; |
| | | 18 | | private readonly HttpTransportSettings _httpSettings; |
| | | 19 | | |
| | 422 | 20 | | public AspNetCoreReplyChannel(IServiceProvider serviceProvider, HttpTransportSettings httpSettings) |
| | | 21 | | { |
| | 422 | 22 | | _serviceProvider = serviceProvider; |
| | 422 | 23 | | _httpSettings = httpSettings; |
| | 422 | 24 | | } |
| | | 25 | | |
| | | 26 | | // TODO: Verify what happens on .NET Framework. Looking at code it looks like it doesn't set this value |
| | 0 | 27 | | public EndpointAddress LocalAddress => null; |
| | | 28 | | |
| | | 29 | | // TODO: Might want to do something a bit smarter with the state and actually have a concept of opening and clos |
| | | 30 | | // connected and fire them when the service is shutting down. |
| | 700 | 31 | | public CommunicationState State { get; private set; } = CommunicationState.Created; |
| | | 32 | | |
| | 1652 | 33 | | public IServiceChannelDispatcher ChannelDispatcher { get; set; } |
| | | 34 | | |
| | | 35 | | #pragma warning disable CS0067 // The event is never used - see issue #290 |
| | | 36 | | public event EventHandler Closed; |
| | | 37 | | public event EventHandler Closing; |
| | | 38 | | public event EventHandler Faulted; |
| | | 39 | | public event EventHandler Opened; |
| | | 40 | | public event EventHandler Opening; |
| | | 41 | | #pragma warning restore CS0067 // The event is never used |
| | | 42 | | |
| | | 43 | | public void Abort() |
| | | 44 | | { |
| | | 45 | | // Can skip Closing state as there's nothing to do during the Abort call |
| | 0 | 46 | | State = CommunicationState.Closed; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | public Task CloseAsync() |
| | | 50 | | { |
| | | 51 | | // Can skip Closing state as there's nothing to do during the Close call |
| | 0 | 52 | | State = CommunicationState.Closed; |
| | 0 | 53 | | return Task.CompletedTask; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public Task CloseAsync(CancellationToken token) |
| | | 57 | | { |
| | | 58 | | // Can skip Closing state as there's nothing to do during the Close call |
| | 0 | 59 | | State = CommunicationState.Closed; |
| | 0 | 60 | | return Task.CompletedTask; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public T GetProperty<T>() where T : class |
| | | 64 | | { |
| | 1363 | 65 | | return _serviceProvider.GetService<T>(); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public Task OpenAsync() |
| | | 69 | | { |
| | | 70 | | // Can skip Opening state as there's nothing to do during the Open call |
| | 392 | 71 | | State = CommunicationState.Opened; |
| | 392 | 72 | | return Task.CompletedTask; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public Task OpenAsync(CancellationToken token) |
| | | 76 | | { |
| | | 77 | | // Can skip Opening state as there's nothing to do during the Open call |
| | 0 | 78 | | State = CommunicationState.Opened; |
| | 0 | 79 | | return Task.CompletedTask; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public Task<RequestContext> ReceiveRequestAsync() |
| | | 83 | | { |
| | 0 | 84 | | throw new NotImplementedException(); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public Task<RequestContext> ReceiveRequestAsync(CancellationToken token) |
| | | 88 | | { |
| | 0 | 89 | | throw new NotImplementedException(); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public Task<(RequestContext requestContext, bool success)> TryReceiveRequestAsync(CancellationToken token) |
| | | 93 | | { |
| | 0 | 94 | | throw new NotImplementedException(); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public Task<bool> WaitForRequestAsync(CancellationToken token) |
| | | 98 | | { |
| | 0 | 99 | | throw new NotImplementedException(); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | internal async Task HandleRequest(HttpContext context) |
| | | 103 | | { |
| | 650 | 104 | | if (ChannelDispatcher == null) |
| | | 105 | | { |
| | | 106 | | // TODO: Look for existing SR which would work here. Cleanup how the exception is thrown. |
| | 0 | 107 | | throw new InvalidOperationException("Channel Dispatcher can't be null"); |
| | | 108 | | } |
| | | 109 | | |
| | 650 | 110 | | using (var requestContext = HttpRequestContext.CreateContext(_httpSettings, context)) |
| | | 111 | | { |
| | 650 | 112 | | bool authenticationResult = await requestContext.ProcessAuthenticationAsync(); |
| | 650 | 113 | | if (!authenticationResult) |
| | | 114 | | { |
| | 0 | 115 | | return; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | try |
| | | 119 | | { |
| | 650 | 120 | | HttpInput httpInput = requestContext.GetHttpInput(true); |
| | 650 | 121 | | (Message requestMessage, Exception requestException) = await httpInput.ParseIncomingMessageAsync(); |
| | 649 | 122 | | if ((requestMessage == null) && (requestException == null)) |
| | | 123 | | { |
| | 1 | 124 | | await requestContext.SendResponseAndCloseAsync(System.Net.HttpStatusCode.BadRequest); |
| | 1 | 125 | | return; |
| | | 126 | | } |
| | | 127 | | |
| | 648 | 128 | | if (requestMessage != null) |
| | | 129 | | { |
| | 648 | 130 | | requestMessage.Properties.Add("Microsoft.AspNetCore.Http.HttpContext", context); |
| | | 131 | | } |
| | | 132 | | |
| | 648 | 133 | | requestContext.SetMessage(requestMessage, requestException); |
| | | 134 | | |
| | 648 | 135 | | await ChannelDispatcher.DispatchAsync(requestContext); |
| | 618 | 136 | | await requestContext.ReplySent; |
| | 616 | 137 | | } |
| | 31 | 138 | | catch (Exception ex) |
| | | 139 | | { |
| | 31 | 140 | | await HandleProcessInboundException(ex, requestContext); |
| | | 141 | | } |
| | 647 | 142 | | } |
| | 648 | 143 | | } |
| | | 144 | | |
| | | 145 | | private static async Task HandleProcessInboundException(Exception ex, HttpRequestContext requestContext) |
| | | 146 | | { |
| | 31 | 147 | | if (Fx.IsFatal(ex)) |
| | | 148 | | { |
| | 0 | 149 | | return; |
| | | 150 | | } |
| | | 151 | | |
| | 31 | 152 | | if (ex is ProtocolException) |
| | | 153 | | { |
| | 1 | 154 | | ProtocolException protocolException = (ProtocolException)ex; |
| | 1 | 155 | | HttpStatusCode statusCode = HttpStatusCode.BadRequest; |
| | 1 | 156 | | string statusDescription = string.Empty; |
| | 1 | 157 | | if (protocolException.Data.Contains(HttpChannelUtilities.HttpStatusCodeExceptionKey)) |
| | | 158 | | { |
| | 1 | 159 | | statusCode = (HttpStatusCode)protocolException.Data[HttpChannelUtilities.HttpStatusCodeExceptionKey] |
| | 1 | 160 | | protocolException.Data.Remove(HttpChannelUtilities.HttpStatusCodeExceptionKey); |
| | | 161 | | } |
| | 1 | 162 | | if (protocolException.Data.Contains(HttpChannelUtilities.HttpStatusDescriptionExceptionKey)) |
| | | 163 | | { |
| | 1 | 164 | | statusDescription = (string)protocolException.Data[HttpChannelUtilities.HttpStatusDescriptionExcepti |
| | 1 | 165 | | protocolException.Data.Remove(HttpChannelUtilities.HttpStatusDescriptionExceptionKey); |
| | | 166 | | } |
| | 1 | 167 | | await requestContext.SendResponseAndCloseAsync(statusCode, statusDescription); |
| | | 168 | | } |
| | | 169 | | else |
| | | 170 | | { |
| | 30 | 171 | | await requestContext.SendResponseAndCloseAsync(HttpStatusCode.BadRequest); |
| | | 172 | | } |
| | 31 | 173 | | } |
| | | 174 | | } |
| | | 175 | | } |