| | | 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.IO; |
| | | 6 | | using System.Net; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | using System.Runtime.Serialization.Json; |
| | | 9 | | using System.Xml.Linq; |
| | | 10 | | using System.Xml.Serialization; |
| | | 11 | | using CoreWCF.Channels; |
| | | 12 | | using CoreWCF.Description; |
| | | 13 | | using CoreWCF.Runtime; |
| | | 14 | | using CoreWCF.Web; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Dispatcher |
| | | 17 | | { |
| | | 18 | | internal class WebErrorHandler : IErrorHandler |
| | | 19 | | { |
| | | 20 | | private const string HtmlMediaType = "text/html"; |
| | | 21 | | |
| | | 22 | | private readonly WebHttpBehavior _webHttpBehavior; |
| | | 23 | | private readonly ContractDescription _contractDescription; |
| | | 24 | | private readonly bool _includeExceptionDetailInFaults; |
| | | 25 | | |
| | 37 | 26 | | public WebErrorHandler(WebHttpBehavior webHttpBehavior, ContractDescription contractDescription, bool includeExc |
| | | 27 | | { |
| | 37 | 28 | | _webHttpBehavior = webHttpBehavior; |
| | 37 | 29 | | _contractDescription = contractDescription; |
| | 37 | 30 | | _includeExceptionDetailInFaults = includeExceptionDetailInFaults; |
| | 37 | 31 | | } |
| | | 32 | | |
| | 4 | 33 | | public bool HandleError(Exception error) => false; |
| | | 34 | | |
| | | 35 | | public void ProvideFault(Exception error, MessageVersion version, ref Message fault) |
| | | 36 | | { |
| | 4 | 37 | | if (version != MessageVersion.None || error == null) |
| | | 38 | | { |
| | 0 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | // If the exception is not derived from FaultException and the fault message is already present |
| | | 43 | | // then only another error handler could have provided the fault so we should not replace it |
| | 4 | 44 | | FaultException errorAsFaultException = error as FaultException; |
| | 4 | 45 | | if (errorAsFaultException == null && fault != null) |
| | | 46 | | { |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | try |
| | | 51 | | { |
| | 4 | 52 | | if (error is IWebFaultException webFaultException) |
| | | 53 | | { |
| | 0 | 54 | | WebOperationContext context = WebOperationContext.Current; |
| | 0 | 55 | | context.OutgoingResponse.StatusCode = webFaultException.StatusCode; |
| | 0 | 56 | | if (OperationContext.Current.IncomingMessageProperties.TryGetValue(WebHttpDispatchOperationSelector. |
| | | 57 | | { |
| | 0 | 58 | | OperationDescription description = _contractDescription.Operations.Find(operationName); |
| | 0 | 59 | | bool isXmlSerializerFaultFormat = WebHttpBehavior.IsXmlSerializerFaultFormat(description); |
| | 0 | 60 | | if (isXmlSerializerFaultFormat && WebOperationContext.Current.OutgoingResponse.Format == WebMess |
| | | 61 | | { |
| | 0 | 62 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | WebMessageFormat? nullableFormat = !isXmlSerializerFaultFormat ? context.OutgoingResponse.Format |
| | 0 | 66 | | WebMessageFormat format = nullableFormat.HasValue ? nullableFormat.Value : _webHttpBehavior.GetR |
| | 0 | 67 | | if (webFaultException.DetailObject != null) |
| | | 68 | | { |
| | | 69 | | switch (format) |
| | | 70 | | { |
| | | 71 | | case WebMessageFormat.Json: |
| | 0 | 72 | | fault = context.CreateJsonResponse(webFaultException.DetailObject, new DataContractJ |
| | 0 | 73 | | break; |
| | | 74 | | case WebMessageFormat.Xml: |
| | 0 | 75 | | if (isXmlSerializerFaultFormat) |
| | | 76 | | { |
| | 0 | 77 | | fault = context.CreateXmlResponse(webFaultException.DetailObject, new XmlSeriali |
| | | 78 | | } |
| | | 79 | | else |
| | | 80 | | { |
| | 0 | 81 | | fault = context.CreateXmlResponse(webFaultException.DetailObject, new DataContra |
| | | 82 | | } |
| | 0 | 83 | | break; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | else |
| | | 87 | | { |
| | 0 | 88 | | if (OperationContext.Current.OutgoingMessageProperties.TryGetValue(HttpResponseMessageProper |
| | 0 | 89 | | property != null) |
| | | 90 | | { |
| | 0 | 91 | | property.SuppressEntityBody = true; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (format == WebMessageFormat.Json) |
| | | 95 | | { |
| | 0 | 96 | | fault.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.Jso |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 0 | 102 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Opera |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | else |
| | | 106 | | { |
| | 4 | 107 | | fault = CreateHtmlResponse(error); |
| | | 108 | | } |
| | | 109 | | |
| | 4 | 110 | | } |
| | 0 | 111 | | catch (Exception ex) |
| | | 112 | | { |
| | 0 | 113 | | if (Fx.IsFatal(ex)) |
| | | 114 | | { |
| | 0 | 115 | | throw; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 119 | | //{ |
| | | 120 | | // DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR.Format(SR.HelpPageFailedT |
| | | 121 | | //} |
| | | 122 | | |
| | 0 | 123 | | WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; |
| | 0 | 124 | | fault = CreateHtmlResponse(ex); |
| | 0 | 125 | | } |
| | 4 | 126 | | } |
| | | 127 | | |
| | | 128 | | private Message CreateHtmlResponse(Exception error) |
| | | 129 | | { |
| | | 130 | | // Note: WebOperationContext may not be present in case of an invalid HTTP request |
| | 4 | 131 | | Uri helpUri = null; |
| | 4 | 132 | | if (WebOperationContext.Current != null) |
| | | 133 | | { |
| | 4 | 134 | | helpUri = _webHttpBehavior.HelpUri != null ? UriTemplate.RewriteUri(_webHttpBehavior.HelpUri, WebOperati |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | StreamBodyWriter bodyWriter; |
| | 4 | 138 | | if (_includeExceptionDetailInFaults) |
| | | 139 | | { |
| | 6 | 140 | | bodyWriter = ActionOfStreamBodyWriter.CreateStreamBodyWriter(s => HelpHtmlBuilder.CreateServerErrorPage( |
| | | 141 | | } |
| | | 142 | | else |
| | | 143 | | { |
| | 2 | 144 | | bodyWriter = ActionOfStreamBodyWriter.CreateStreamBodyWriter(s => HelpHtmlBuilder.CreateServerErrorPage( |
| | | 145 | | } |
| | 4 | 146 | | Message response = new HttpStreamMessage(bodyWriter); |
| | 4 | 147 | | response.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); |
| | | 148 | | |
| | 4 | 149 | | HttpResponseMessageProperty responseProperty = GetResponseProperty(WebOperationContext.Current, response); |
| | 4 | 150 | | if (responseProperty.StatusCode == HttpStatusCode.OK) |
| | | 151 | | { |
| | 4 | 152 | | responseProperty.StatusCode = HttpStatusCode.BadRequest; |
| | | 153 | | } |
| | | 154 | | |
| | 4 | 155 | | responseProperty.Headers[HttpResponseHeader.ContentType] = HtmlMediaType; |
| | 4 | 156 | | return response; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | private static HttpResponseMessageProperty GetResponseProperty(WebOperationContext currentContext, Message respo |
| | | 160 | | { |
| | | 161 | | HttpResponseMessageProperty responseProperty; |
| | 4 | 162 | | if (currentContext != null) |
| | | 163 | | { |
| | 4 | 164 | | responseProperty = currentContext.OutgoingResponse.MessageProperty; |
| | | 165 | | } |
| | | 166 | | else |
| | | 167 | | { |
| | 0 | 168 | | responseProperty = new HttpResponseMessageProperty(); |
| | 0 | 169 | | response.Properties.Add(HttpResponseMessageProperty.Name, responseProperty); |
| | | 170 | | } |
| | | 171 | | |
| | 4 | 172 | | return responseProperty; |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | } |