| | | 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.Collections.Generic; |
| | | 6 | | using System.Net; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | using System.Security; |
| | | 9 | | using System.Security.Permissions; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Web |
| | | 12 | | { |
| | | 13 | | [Serializable] |
| | | 14 | | public class WebFaultException : FaultException, IWebFaultException |
| | | 15 | | { |
| | | 16 | | internal const string WebFaultCodeNamespace = "http://schemas.microsoft.com/2009/WebFault"; |
| | | 17 | | |
| | | 18 | | public WebFaultException(HttpStatusCode statusCode) |
| | 0 | 19 | | : base(GetDefaultReason(statusCode), GetFaultCode(statusCode)) |
| | | 20 | | { |
| | 0 | 21 | | StatusCode = statusCode; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | protected WebFaultException(SerializationInfo info, StreamingContext context) |
| | 0 | 25 | | : base(info, context) |
| | | 26 | | { |
| | 0 | 27 | | StatusCode = (HttpStatusCode)info.GetValue("statusCode", typeof(HttpStatusCode)); |
| | 0 | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | public HttpStatusCode StatusCode { get; private set; } |
| | | 31 | | |
| | 0 | 32 | | Type IWebFaultException.DetailType => null; |
| | | 33 | | |
| | 0 | 34 | | object IWebFaultException.DetailObject => null; |
| | | 35 | | |
| | 0 | 36 | | Type[] IWebFaultException.KnownTypes => null; |
| | | 37 | | |
| | | 38 | | public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| | | 39 | | { |
| | 0 | 40 | | base.GetObjectData(info, context); |
| | | 41 | | |
| | 0 | 42 | | info.AddValue("statusCode", StatusCode); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | internal static FaultCode GetFaultCode(HttpStatusCode statusCode) |
| | | 46 | | { |
| | 0 | 47 | | if ((int)statusCode >= (int)HttpStatusCode.InternalServerError) |
| | | 48 | | { |
| | 0 | 49 | | return FaultCode.CreateReceiverFaultCode(statusCode.ToString(), WebFaultCodeNamespace); |
| | | 50 | | } |
| | | 51 | | else |
| | | 52 | | { |
| | 0 | 53 | | return FaultCode.CreateSenderFaultCode(statusCode.ToString(), WebFaultCodeNamespace); |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | // These reasons come from section 6.1.1 of http://www.ietf.org/rfc/rfc2616.txt |
| | | 58 | | internal static string GetDefaultReason(HttpStatusCode statusCode) |
| | | 59 | | { |
| | 0 | 60 | | switch ((int)statusCode) |
| | | 61 | | { |
| | 0 | 62 | | case 100: return "Continue"; |
| | 0 | 63 | | case 101: return "Switching Protocols"; |
| | 0 | 64 | | case 200: return "OK"; |
| | 0 | 65 | | case 201: return "Created"; |
| | 0 | 66 | | case 202: return "Accepted"; |
| | 0 | 67 | | case 203: return "Non-Authoritative Information"; |
| | 0 | 68 | | case 204: return "No Content"; |
| | 0 | 69 | | case 205: return "Reset Content"; |
| | 0 | 70 | | case 206: return "Partial Content"; |
| | 0 | 71 | | case 300: return "Multiple Choices"; |
| | 0 | 72 | | case 301: return "Moved Permanently"; |
| | 0 | 73 | | case 302: return "Found"; |
| | 0 | 74 | | case 303: return "See Other"; |
| | 0 | 75 | | case 304: return "Not Modified"; |
| | 0 | 76 | | case 305: return "Use Proxy"; |
| | 0 | 77 | | case 307: return "Temporary Redirect"; |
| | 0 | 78 | | case 400: return "Bad Request"; |
| | 0 | 79 | | case 401: return "Unauthorized"; |
| | 0 | 80 | | case 402: return "Payment Required"; |
| | 0 | 81 | | case 403: return "Forbidden"; |
| | 0 | 82 | | case 404: return "Not Found"; |
| | 0 | 83 | | case 405: return "Method Not Allowed"; |
| | 0 | 84 | | case 406: return "Not Acceptable"; |
| | 0 | 85 | | case 407: return "Proxy Authentication Required"; |
| | 0 | 86 | | case 408: return "Request Time-out"; |
| | 0 | 87 | | case 409: return "Conflict"; |
| | 0 | 88 | | case 410: return "Gone"; |
| | 0 | 89 | | case 411: return "Length Required"; |
| | 0 | 90 | | case 412: return "Precondition Failed"; |
| | 0 | 91 | | case 413: return "Request Entity Too Large"; |
| | 0 | 92 | | case 414: return "Request-URI Too Large"; |
| | 0 | 93 | | case 415: return "Unsupported Media Type"; |
| | 0 | 94 | | case 416: return "Requested range not satisfiable"; |
| | 0 | 95 | | case 417: return "Expectation Failed"; |
| | 0 | 96 | | case 500: return "Internal Server Error"; |
| | 0 | 97 | | case 501: return "Not Implemented"; |
| | 0 | 98 | | case 502: return "Bad Gateway"; |
| | 0 | 99 | | case 503: return "Service Unavailable"; |
| | 0 | 100 | | case 504: return "Gateway Time-out"; |
| | 0 | 101 | | case 505: return "HTTP Version not supported"; |
| | | 102 | | default: |
| | | 103 | | { |
| | 0 | 104 | | int errorClass = ((int)statusCode) / 100; |
| | | 105 | | switch (errorClass) |
| | | 106 | | { |
| | 0 | 107 | | case 1: return "Informational"; |
| | 0 | 108 | | case 2: return "Success"; |
| | 0 | 109 | | case 3: return "Redirection"; |
| | 0 | 110 | | case 4: return "Client Error"; |
| | 0 | 111 | | case 5: return "Server Error"; |
| | 0 | 112 | | default: return null; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | [Serializable] |
| | | 120 | | public class WebFaultException<T> : FaultException<T>, IWebFaultException |
| | | 121 | | { |
| | | 122 | | private Type[] _knownTypes; |
| | | 123 | | |
| | | 124 | | public WebFaultException(T detail, HttpStatusCode statusCode) |
| | | 125 | | : base(detail, WebFaultException.GetDefaultReason(statusCode), WebFaultException.GetFaultCode(statusCode)) |
| | | 126 | | { |
| | | 127 | | StatusCode = statusCode; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public WebFaultException(T detail, HttpStatusCode statusCode, IEnumerable<Type> knownTypes) |
| | | 131 | | : base(detail, WebFaultException.GetDefaultReason(statusCode), WebFaultException.GetFaultCode(statusCode)) |
| | | 132 | | { |
| | | 133 | | StatusCode = statusCode; |
| | | 134 | | _knownTypes = (knownTypes == null) ? null : new List<Type>(knownTypes).ToArray(); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | protected WebFaultException(SerializationInfo info, StreamingContext context) |
| | | 138 | | : base(info, context) |
| | | 139 | | { |
| | | 140 | | StatusCode = (HttpStatusCode)info.GetValue("statusCode", typeof(HttpStatusCode)); |
| | | 141 | | _knownTypes = (Type[])info.GetValue("knownTypes", typeof(Type[])); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | public HttpStatusCode StatusCode { get; private set; } |
| | | 145 | | |
| | | 146 | | Type IWebFaultException.DetailType => typeof(T); |
| | | 147 | | |
| | | 148 | | object IWebFaultException.DetailObject => Detail; |
| | | 149 | | |
| | | 150 | | Type[] IWebFaultException.KnownTypes => _knownTypes; |
| | | 151 | | |
| | | 152 | | public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| | | 153 | | { |
| | | 154 | | base.GetObjectData(info, context); |
| | | 155 | | |
| | | 156 | | info.AddValue("statusCode", StatusCode); |
| | | 157 | | info.AddValue("knownTypes", _knownTypes); |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | } |