| | | 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.Diagnostics; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Xml.Linq; |
| | | 10 | | using CoreWCF.Channels; |
| | | 11 | | using CoreWCF.Web; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Dispatcher |
| | | 14 | | { |
| | | 15 | | internal class HttpUnhandledOperationInvoker : IOperationInvoker |
| | | 16 | | { |
| | | 17 | | private const string HtmlContentType = "text/html; charset=UTF-8"; |
| | | 18 | | private const string HtmlMediaType = "text/html"; |
| | | 19 | | |
| | 0 | 20 | | public bool IsSynchronous => true; |
| | | 21 | | |
| | 0 | 22 | | public object[] AllocateInputs() => new object[1]; |
| | | 23 | | |
| | 37 | 24 | | public Uri HelpUri { get; set; } |
| | | 25 | | |
| | | 26 | | public ValueTask<(object returnValue, object[] outputs)> InvokeAsync(object instance, object[] inputs) |
| | | 27 | | { |
| | 0 | 28 | | if (!(inputs[0] is Message message)) |
| | | 29 | | { |
| | 0 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 31 | | SR.Format(SR.HttpUnhandledOperationInvokerCalledWithoutMessage))); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | // We might be here because we desire a redirect... |
| | 0 | 35 | | Uri newLocation = null; |
| | 0 | 36 | | Uri to = message.Headers.To; |
| | 0 | 37 | | if (message.Properties.ContainsKey(WebHttpDispatchOperationSelector.RedirectPropertyName)) |
| | | 38 | | { |
| | 0 | 39 | | newLocation = message.Properties[WebHttpDispatchOperationSelector.RedirectPropertyName] as Uri; |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | if (newLocation != null && to != null) |
| | | 43 | | { |
| | | 44 | | // ...redirect |
| | 0 | 45 | | Message redirectResult = WebOperationContext.Current.CreateStreamResponse(s => HelpHtmlBuilder.CreateTra |
| | 0 | 46 | | WebOperationContext.Current.OutgoingResponse.Location = newLocation.AbsoluteUri; |
| | 0 | 47 | | WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.TemporaryRedirect; |
| | 0 | 48 | | WebOperationContext.Current.OutgoingResponse.ContentType = HtmlContentType; |
| | | 49 | | |
| | | 50 | | // Note that no exception is thrown along this path, even if the debugger is attached |
| | | 51 | | //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 52 | | //{ |
| | | 53 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.WebRequestRedirect, |
| | | 54 | | // SR2.GetString(SR2.TraceCodeWebRequestRedirect, to, newLocation)); |
| | | 55 | | //} |
| | 0 | 56 | | return new ValueTask<(object, object[])>((redirectResult, (object[])null)); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // otherwise we are here to issue either a 404 or a 405 |
| | 0 | 60 | | bool uriMatched = false; |
| | 0 | 61 | | if (message.Properties.ContainsKey(WebHttpDispatchOperationSelector.HttpOperationSelectorUriMatchedPropertyN |
| | | 62 | | { |
| | 0 | 63 | | uriMatched = (bool)message.Properties[WebHttpDispatchOperationSelector.HttpOperationSelectorUriMatchedPr |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | Message result = null; |
| | 0 | 67 | | Uri helpUri = HelpUri != null ? UriTemplate.RewriteUri(HelpUri, WebOperationContext.Current.IncomingRequest. |
| | 0 | 68 | | if (uriMatched) |
| | | 69 | | { |
| | 0 | 70 | | WebHttpDispatchOperationSelectorData allowedMethodsData = null; |
| | 0 | 71 | | if (message.Properties.TryGetValue(WebHttpDispatchOperationSelector.HttpOperationSelectorDataPropertyNam |
| | | 72 | | { |
| | 0 | 73 | | WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.Allow] = allowedMethodsData. |
| | | 74 | | } |
| | 0 | 75 | | result = WebOperationContext.Current.CreateStreamResponse(s => HelpHtmlBuilder.CreateMethodNotAllowedPag |
| | | 76 | | } |
| | | 77 | | else |
| | | 78 | | { |
| | 0 | 79 | | result = WebOperationContext.Current.CreateStreamResponse(s => HelpHtmlBuilder.CreateEndpointNotFound(he |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | WebOperationContext.Current.OutgoingResponse.StatusCode = uriMatched ? HttpStatusCode.MethodNotAllowed : Htt |
| | 0 | 83 | | WebOperationContext.Current.OutgoingResponse.ContentType = HtmlContentType; |
| | | 84 | | |
| | | 85 | | try |
| | | 86 | | { |
| | 0 | 87 | | if (!uriMatched) |
| | | 88 | | { |
| | 0 | 89 | | if (Debugger.IsAttached) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.For |
| | 0 | 92 | | OperationContext.Current.IncomingMessageHeaders.To))); |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | 0 | 96 | | DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR.Format(SR.WebRequestDid |
| | 0 | 97 | | OperationContext.Current.IncomingMessageHeaders.To)), TraceEventType.Warning); |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 0 | 102 | | if (Debugger.IsAttached) |
| | | 103 | | { |
| | 0 | 104 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.For |
| | 0 | 105 | | WebOperationContext.Current.IncomingRequest.Method, OperationContext.Current.IncomingMessage |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 0 | 109 | | DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR.Format(SR.WebRequestDid |
| | 0 | 110 | | WebOperationContext.Current.IncomingRequest.Method, OperationContext.Current.IncomingMes |
| | | 111 | | } |
| | | 112 | | } |
| | 0 | 113 | | } |
| | 0 | 114 | | catch (InvalidOperationException) |
| | | 115 | | { |
| | | 116 | | // catch the exception - its only used for tracing |
| | 0 | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | return new ValueTask<(object, object[])>((result, (object[])null)); |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |