| | | 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.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Net.Mime; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Description; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | using CoreWCF.Web; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Dispatcher |
| | | 13 | | { |
| | | 14 | | internal class FormatSelectingMessageInspector : IDispatchMessageInspector |
| | | 15 | | { |
| | 0 | 16 | | private static readonly IEnumerable<string> s_wildcardMediaTypes = new List<string>() { "application", "text" }; |
| | | 17 | | private readonly List<MultiplexingFormatMapping> _mappings; |
| | | 18 | | private readonly Dictionary<string, MultiplexingDispatchMessageFormatter> _formatters; |
| | | 19 | | private readonly Dictionary<string, NameValueCache<FormatContentTypePair>> _caches; |
| | | 20 | | private readonly bool _automaticFormatSelectionEnabled; |
| | | 21 | | |
| | | 22 | | // There are technically an infinite number of valid accept headers for just xml and json, |
| | | 23 | | // but to prevent DOS attacks, we need to set an upper limit. It is assumed that there would |
| | | 24 | | // never be more than two dozen valid accept headers actually used out in the wild. |
| | 0 | 25 | | private static readonly int s_maxCachedAcceptHeaders = 25; |
| | | 26 | | |
| | 0 | 27 | | public FormatSelectingMessageInspector(WebHttpBehavior webHttpBehavior, List<MultiplexingFormatMapping> mappings |
| | | 28 | | { |
| | 0 | 29 | | if (webHttpBehavior == null) |
| | | 30 | | { |
| | 0 | 31 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(webHttpBehavior)); |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | _automaticFormatSelectionEnabled = webHttpBehavior.AutomaticFormatSelectionEnabled; |
| | | 35 | | |
| | 0 | 36 | | _formatters = new Dictionary<string, MultiplexingDispatchMessageFormatter>(); |
| | | 37 | | |
| | 0 | 38 | | _caches = new Dictionary<string, NameValueCache<FormatContentTypePair>>(); |
| | | 39 | | |
| | 0 | 40 | | _mappings = mappings ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(mappings)); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public void RegisterOperation(string operationName, MultiplexingDispatchMessageFormatter formatter) |
| | | 44 | | { |
| | 0 | 45 | | if (formatter == null) |
| | | 46 | | { |
| | 0 | 47 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(formatter)); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | Fx.Assert(!_formatters.ContainsKey(operationName), "An operation should only be registered once."); |
| | | 51 | | |
| | 0 | 52 | | _formatters.Add(operationName, formatter); |
| | 0 | 53 | | _caches.Add(operationName, new NameValueCache<FormatContentTypePair>(s_maxCachedAcceptHeaders)); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) |
| | | 57 | | { |
| | 0 | 58 | | if (_automaticFormatSelectionEnabled) |
| | | 59 | | { |
| | 0 | 60 | | MessageProperties messageProperties = OperationContext.Current.IncomingMessageProperties; |
| | 0 | 61 | | if (messageProperties.ContainsKey(WebHttpDispatchOperationSelector.HttpOperationNamePropertyName)) |
| | | 62 | | { |
| | 0 | 63 | | string operationName = messageProperties[WebHttpDispatchOperationSelector.HttpOperationNamePropertyN |
| | 0 | 64 | | if (!string.IsNullOrEmpty(operationName) && _formatters.ContainsKey(operationName)) |
| | | 65 | | { |
| | | 66 | | |
| | 0 | 67 | | string acceptHeader = WebOperationContext.Current.IncomingRequest.Accept; |
| | 0 | 68 | | if (!string.IsNullOrEmpty(acceptHeader)) |
| | | 69 | | { |
| | 0 | 70 | | if (TrySetFormatFromCache(operationName, acceptHeader) || |
| | 0 | 71 | | TrySetFormatFromAcceptHeader(operationName, acceptHeader, true /* matchCharSet */) || |
| | 0 | 72 | | TrySetFormatFromAcceptHeader(operationName, acceptHeader, false /* matchCharSet */)) |
| | | 73 | | { |
| | 0 | 74 | | return null; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | if (TrySetFormatFromContentType(operationName)) |
| | | 79 | | { |
| | 0 | 80 | | return null; |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | SetFormatFromDefault(operationName); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | return null; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public void BeforeSendReply(ref Message reply, object correlationState) |
| | | 92 | | { |
| | | 93 | | // do nothing |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | private bool TrySetFormatFromCache(string operationName, string acceptHeader) |
| | | 97 | | { |
| | | 98 | | Fx.Assert(_caches.ContainsKey(operationName), "The calling method is responsible for ensuring that the 'oper |
| | | 99 | | Fx.Assert(acceptHeader != null, "The calling method is responsible for ensuring that 'acceptHeader' is not n |
| | | 100 | | |
| | 0 | 101 | | FormatContentTypePair pair = _caches[operationName].Lookup(acceptHeader.ToUpperInvariant()); |
| | 0 | 102 | | if (pair != null) |
| | | 103 | | { |
| | 0 | 104 | | SetFormatAndContentType(pair.Format, pair.ContentType); |
| | 0 | 105 | | return true; |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return false; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private bool TrySetFormatFromAcceptHeader(string operationName, string acceptHeader, bool matchCharSet) |
| | | 112 | | { |
| | | 113 | | Fx.Assert(_formatters.ContainsKey(operationName), "The calling method is responsible for ensuring that the ' |
| | | 114 | | |
| | 0 | 115 | | IList<ContentType> acceptHeaderElements = WebOperationContext.Current.IncomingRequest.GetAcceptHeaderElement |
| | | 116 | | |
| | 0 | 117 | | for (int i = 0; i < acceptHeaderElements.Count; i++) |
| | | 118 | | { |
| | 0 | 119 | | string[] typeAndSubType = acceptHeaderElements[i].MediaType.Split('/'); |
| | 0 | 120 | | string type = typeAndSubType[0].Trim().ToLowerInvariant(); |
| | 0 | 121 | | string subType = typeAndSubType[1].Trim(); |
| | | 122 | | |
| | 0 | 123 | | if ((subType[0] == '*' && subType.Length == 1) && |
| | 0 | 124 | | ((type[0] == '*' && type.Length == 1) || |
| | 0 | 125 | | s_wildcardMediaTypes.Contains(type))) |
| | | 126 | | { |
| | 0 | 127 | | SetFormatFromDefault(operationName, acceptHeader); |
| | 0 | 128 | | return true; |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | foreach (MultiplexingFormatMapping mapping in _mappings) |
| | | 132 | | { |
| | 0 | 133 | | WebMessageFormat format = mapping.MessageFormat; |
| | 0 | 134 | | if (_formatters[operationName].SupportsMessageFormat(format) && |
| | 0 | 135 | | mapping.CanFormatResponse(acceptHeaderElements[i], matchCharSet, out ContentType contentType)) |
| | | 136 | | { |
| | 0 | 137 | | string contentTypeStr = contentType.ToString(); |
| | 0 | 138 | | _caches[operationName].AddOrUpdate(acceptHeader.ToUpperInvariant(), new FormatContentTypePair(fo |
| | 0 | 139 | | SetFormatAndContentType(format, contentTypeStr); |
| | 0 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | return false; |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | private bool TrySetFormatFromContentType(string operationName) |
| | | 149 | | { |
| | | 150 | | Fx.Assert(_formatters.ContainsKey(operationName), "The calling method is responsible for ensuring that the ' |
| | | 151 | | |
| | 0 | 152 | | string contentTypeStr = WebOperationContext.Current.IncomingRequest.ContentType; |
| | 0 | 153 | | if (contentTypeStr != null) |
| | | 154 | | { |
| | 0 | 155 | | ContentType contentType = Utility.GetContentType(contentTypeStr); |
| | 0 | 156 | | if (contentType != null) |
| | | 157 | | { |
| | 0 | 158 | | foreach (MultiplexingFormatMapping mapping in _mappings) |
| | | 159 | | { |
| | 0 | 160 | | if (_formatters[operationName].SupportsMessageFormat(mapping.MessageFormat) && |
| | 0 | 161 | | mapping.CanFormatResponse(contentType, false, out ContentType responseContentType)) |
| | | 162 | | { |
| | 0 | 163 | | SetFormatAndContentType(mapping.MessageFormat, responseContentType.ToString()); |
| | 0 | 164 | | return true; |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | return false; |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | private void SetFormatFromDefault(string operationName) |
| | | 174 | | { |
| | 0 | 175 | | SetFormatFromDefault(operationName, null); |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | private void SetFormatFromDefault(string operationName, string acceptHeader) |
| | | 179 | | { |
| | | 180 | | Fx.Assert(_formatters.ContainsKey(operationName), "The calling method is responsible for ensuring that the ' |
| | 0 | 181 | | WebMessageFormat format = _formatters[operationName].DefaultFormat; |
| | | 182 | | |
| | 0 | 183 | | if (!string.IsNullOrEmpty(acceptHeader)) |
| | | 184 | | { |
| | 0 | 185 | | _caches[operationName].AddOrUpdate(acceptHeader.ToUpperInvariant(), new FormatContentTypePair(format, nu |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | WebOperationContext.Current.OutgoingResponse.Format = format; |
| | | 189 | | |
| | | 190 | | //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 191 | | //{ |
| | | 192 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.AutomaticFormatSelectedOperationDefault, |
| | | 193 | | //} |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | private void SetFormatAndContentType(WebMessageFormat format, string contentType) |
| | | 197 | | { |
| | 0 | 198 | | OutgoingWebResponseContext outgoingResponse = WebOperationContext.Current.OutgoingResponse; |
| | 0 | 199 | | outgoingResponse.Format = format; |
| | 0 | 200 | | outgoingResponse.AutomatedFormatSelectionContentType = contentType; |
| | | 201 | | |
| | | 202 | | //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 203 | | //{ |
| | | 204 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.AutomaticFormatSelectedRequestBased, SR2 |
| | | 205 | | //} |
| | 0 | 206 | | } |
| | | 207 | | |
| | | 208 | | internal class FormatContentTypePair |
| | | 209 | | { |
| | 0 | 210 | | public FormatContentTypePair(WebMessageFormat format, string contentType) |
| | | 211 | | { |
| | 0 | 212 | | Format = format; |
| | 0 | 213 | | ContentType = contentType; |
| | 0 | 214 | | } |
| | | 215 | | |
| | 0 | 216 | | public WebMessageFormat Format { get; } |
| | | 217 | | |
| | 0 | 218 | | public string ContentType { get; } |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | } |