| | | 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 CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Collections.Generic; |
| | | 9 | | using CoreWCF.Description; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | using CoreWCF.Web; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Dispatcher |
| | | 14 | | { |
| | | 15 | | public class WebHttpDispatchOperationSelector : IDispatchOperationSelector |
| | | 16 | | { |
| | | 17 | | public const string HttpOperationSelectorUriMatchedPropertyName = "UriMatched"; |
| | | 18 | | internal const string HttpOperationSelectorDataPropertyName = "HttpOperationSelectorData"; |
| | | 19 | | |
| | | 20 | | public const string HttpOperationNamePropertyName = "HttpOperationName"; |
| | | 21 | | internal const string RedirectOperationName = ""; // always unhandled invoker |
| | | 22 | | internal const string RedirectPropertyName = "WebHttpRedirect"; |
| | | 23 | | |
| | 37 | 24 | | private readonly string _catchAllOperationName = ""; // user UT=* Method=* operation, else unhandled invoker |
| | | 25 | | |
| | | 26 | | private readonly Dictionary<string, UriTemplateTable> _methodSpecificTables; // indexed by the http method name |
| | | 27 | | private readonly UriTemplateTable _wildcardTable; // this is one of the methodSpecificTables, special-cased for |
| | | 28 | | private readonly Dictionary<string, UriTemplate> _templates; |
| | | 29 | | private readonly UriTemplateTable _helpUriTable; |
| | | 30 | | |
| | 37 | 31 | | public WebHttpDispatchOperationSelector(ServiceEndpoint endpoint) |
| | | 32 | | { |
| | 37 | 33 | | if (endpoint == null) |
| | | 34 | | { |
| | 0 | 35 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpoint)); |
| | | 36 | | } |
| | | 37 | | |
| | 37 | 38 | | if (endpoint.Address == null) |
| | | 39 | | { |
| | 0 | 40 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 41 | | SR.EndpointAddressCannotBeNull)); |
| | | 42 | | } |
| | | 43 | | |
| | 37 | 44 | | Uri baseUri = endpoint.Address.Uri; |
| | 37 | 45 | | _methodSpecificTables = new Dictionary<string, UriTemplateTable>(); |
| | 37 | 46 | | _templates = new Dictionary<string, UriTemplate>(); |
| | | 47 | | |
| | 37 | 48 | | WebHttpBehavior webHttpBehavior = ((KeyedByTypeCollection<IEndpointBehavior>)endpoint.EndpointBehaviors).Fin |
| | 37 | 49 | | if (webHttpBehavior != null && webHttpBehavior.HelpEnabled) |
| | | 50 | | { |
| | 0 | 51 | | _helpUriTable = new UriTemplateTable(endpoint.ListenUri, HelpPage.GetOperationTemplatePairs()); |
| | | 52 | | } |
| | | 53 | | |
| | 37 | 54 | | Dictionary<WCFKey, string> alreadyHaves = new Dictionary<WCFKey, string>(); |
| | | 55 | | |
| | 400 | 56 | | foreach (OperationDescription od in endpoint.Contract.Operations) |
| | | 57 | | { |
| | | 58 | | // ignore callback operations |
| | 163 | 59 | | if (od.Messages[0].Direction == MessageDirection.Input) |
| | | 60 | | { |
| | 163 | 61 | | string method = WebHttpBehavior.GetWebMethod(od); |
| | 163 | 62 | | string path = UriTemplateClientFormatter.GetUTStringOrDefault(od); |
| | | 63 | | |
| | | 64 | | // |
| | | 65 | | |
| | 163 | 66 | | if (UriTemplateHelpers.IsWildcardPath(path) && (method == WebHttpBehavior.WildcardMethod)) |
| | | 67 | | { |
| | 8 | 68 | | if (_catchAllOperationName != "") |
| | | 69 | | { |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 71 | | new InvalidOperationException( |
| | 0 | 72 | | SR.Format(SR.MultipleOperationsInContractWithPathMethod, |
| | 0 | 73 | | endpoint.Contract.Name, path, method))); |
| | | 74 | | } |
| | 8 | 75 | | _catchAllOperationName = od.Name; |
| | | 76 | | } |
| | | 77 | | |
| | 163 | 78 | | UriTemplate ut = new UriTemplate(path); |
| | 163 | 79 | | WCFKey wcfKey = new WCFKey(ut, method); |
| | 163 | 80 | | if (alreadyHaves.ContainsKey(wcfKey)) |
| | | 81 | | { |
| | 0 | 82 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 83 | | new InvalidOperationException( |
| | 0 | 84 | | SR.Format(SR.MultipleOperationsInContractWithPathMethod, |
| | 0 | 85 | | endpoint.Contract.Name, path, method))); |
| | | 86 | | } |
| | | 87 | | |
| | 163 | 88 | | alreadyHaves.Add(wcfKey, od.Name); |
| | | 89 | | |
| | 163 | 90 | | if (!_methodSpecificTables.TryGetValue(method, out UriTemplateTable methodSpecificTable)) |
| | | 91 | | { |
| | 47 | 92 | | methodSpecificTable = new UriTemplateTable(baseUri); |
| | 47 | 93 | | _methodSpecificTables.Add(method, methodSpecificTable); |
| | | 94 | | } |
| | | 95 | | |
| | 163 | 96 | | methodSpecificTable.KeyValuePairs.Add(new KeyValuePair<UriTemplate, object>(ut, od.Name)); |
| | 163 | 97 | | _templates.Add(od.Name, ut); |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | 37 | 101 | | if (_methodSpecificTables.Count == 0) |
| | | 102 | | { |
| | 0 | 103 | | _methodSpecificTables = null; |
| | | 104 | | } |
| | | 105 | | else |
| | | 106 | | { |
| | | 107 | | // freeze all the tables because they should not be modified after this point |
| | 168 | 108 | | foreach (UriTemplateTable table in _methodSpecificTables.Values) |
| | | 109 | | { |
| | 47 | 110 | | table.MakeReadOnly(true /* allowDuplicateEquivalentUriTemplates */); |
| | | 111 | | } |
| | | 112 | | |
| | 37 | 113 | | if (!_methodSpecificTables.TryGetValue(WebHttpBehavior.WildcardMethod, out _wildcardTable)) |
| | | 114 | | { |
| | 29 | 115 | | _wildcardTable = null; |
| | | 116 | | } |
| | | 117 | | } |
| | 37 | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | protected WebHttpDispatchOperationSelector() |
| | | 121 | | { |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | public virtual UriTemplate GetUriTemplate(string operationName) |
| | | 125 | | { |
| | 0 | 126 | | if (operationName == null) |
| | | 127 | | { |
| | 0 | 128 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operationName)); |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | if (!_templates.TryGetValue(operationName, out UriTemplate result)) |
| | | 132 | | { |
| | 0 | 133 | | return null; |
| | | 134 | | } |
| | | 135 | | else |
| | | 136 | | { |
| | 0 | 137 | | return result; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | public string SelectOperation(ref Message message) |
| | | 142 | | { |
| | 35 | 143 | | if (message == null) |
| | | 144 | | { |
| | 0 | 145 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 146 | | } |
| | | 147 | | |
| | 35 | 148 | | string result = SelectOperation(ref message, out bool uriMatched); |
| | 35 | 149 | | message.Properties.Add(HttpOperationSelectorUriMatchedPropertyName, uriMatched); |
| | 35 | 150 | | if (result != null) |
| | | 151 | | { |
| | 35 | 152 | | message.Properties.Add(HttpOperationNamePropertyName, result); |
| | | 153 | | // if (DiagnosticUtility.ShouldTraceInformation) |
| | | 154 | | // { |
| | | 155 | | //#pragma warning disable 56506 // Microsoft, Message.Headers is never null |
| | | 156 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.WebRequestMatchesOperation, SR2.GetS |
| | | 157 | | //#pragma warning restore 56506 |
| | | 158 | | // } |
| | | 159 | | } |
| | | 160 | | |
| | 35 | 161 | | return result; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | protected virtual string SelectOperation(ref Message message, out bool uriMatched) |
| | | 165 | | { |
| | 35 | 166 | | if (message == null) |
| | | 167 | | { |
| | 0 | 168 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 169 | | } |
| | | 170 | | |
| | 35 | 171 | | uriMatched = false; |
| | 35 | 172 | | if (_methodSpecificTables == null) |
| | | 173 | | { |
| | 0 | 174 | | return _catchAllOperationName; |
| | | 175 | | } |
| | | 176 | | |
| | 35 | 177 | | if (!message.Properties.ContainsKey(HttpRequestMessageProperty.Name)) |
| | | 178 | | { |
| | 0 | 179 | | return _catchAllOperationName; |
| | | 180 | | } |
| | | 181 | | |
| | 35 | 182 | | if (!(message.Properties[HttpRequestMessageProperty.Name] is HttpRequestMessageProperty prop)) |
| | | 183 | | { |
| | 0 | 184 | | return _catchAllOperationName; |
| | | 185 | | } |
| | | 186 | | |
| | 35 | 187 | | string method = prop.Method; |
| | | 188 | | |
| | 35 | 189 | | Uri to = message.Headers.To; |
| | | 190 | | |
| | 35 | 191 | | if (to == null) |
| | | 192 | | { |
| | 0 | 193 | | return _catchAllOperationName; |
| | | 194 | | } |
| | | 195 | | |
| | 35 | 196 | | if (_helpUriTable != null) |
| | | 197 | | { |
| | 0 | 198 | | UriTemplateMatch match = _helpUriTable.MatchSingle(to); |
| | 0 | 199 | | if (match != null) |
| | | 200 | | { |
| | 0 | 201 | | uriMatched = true; |
| | 0 | 202 | | AddUriTemplateMatch(match, prop, message); |
| | 0 | 203 | | if (method == WebHttpBehavior.GET) |
| | | 204 | | { |
| | 0 | 205 | | return HelpOperationInvoker.OperationName; |
| | | 206 | | } |
| | 0 | 207 | | message.Properties.Add(HttpOperationSelectorDataPropertyName, |
| | 0 | 208 | | new WebHttpDispatchOperationSelectorData() { AllowedMethods = new List<string>() { WebHttpBehavi |
| | 0 | 209 | | return _catchAllOperationName; |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | |
| | 35 | 213 | | bool methodMatchesExactly = _methodSpecificTables.TryGetValue(method, out UriTemplateTable methodSpecificTab |
| | 35 | 214 | | if (methodMatchesExactly) |
| | | 215 | | { |
| | 35 | 216 | | uriMatched = CanUriMatch(methodSpecificTable, to, prop, message, out string operationName); |
| | 35 | 217 | | if (uriMatched) |
| | | 218 | | { |
| | 34 | 219 | | return operationName; |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | |
| | 1 | 223 | | if (_wildcardTable != null) |
| | | 224 | | { |
| | 1 | 225 | | uriMatched = CanUriMatch(_wildcardTable, to, prop, message, out string operationName); |
| | 1 | 226 | | if (uriMatched) |
| | | 227 | | { |
| | 1 | 228 | | return operationName; |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | 0 | 232 | | if (ShouldRedirectToUriWithSlashAtTheEnd(methodSpecificTable, message, to)) |
| | | 233 | | { |
| | 0 | 234 | | return RedirectOperationName; |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | // the {method, uri} pair does not match anything the service supports. |
| | | 238 | | // we know at this point that we'll return some kind of error code, but we |
| | | 239 | | // should go through all methods for the uri to see if any method is supported |
| | | 240 | | // so that that information could be returned to the user as well |
| | | 241 | | |
| | 0 | 242 | | List<string> allowedMethods = null; |
| | 0 | 243 | | foreach (KeyValuePair<string, UriTemplateTable> pair in _methodSpecificTables) |
| | | 244 | | { |
| | 0 | 245 | | if (pair.Key == method || pair.Key == WebHttpBehavior.WildcardMethod) |
| | | 246 | | { |
| | | 247 | | // the uri must not match the uri template |
| | | 248 | | continue; |
| | | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | UriTemplateTable table = pair.Value; |
| | 0 | 252 | | if (table.MatchSingle(to) != null) |
| | | 253 | | { |
| | 0 | 254 | | if (allowedMethods == null) |
| | | 255 | | { |
| | 0 | 256 | | allowedMethods = new List<string>(); |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | // |
| | | 260 | | |
| | 0 | 261 | | if (!allowedMethods.Contains(pair.Key)) |
| | | 262 | | { |
| | 0 | 263 | | allowedMethods.Add(pair.Key); |
| | | 264 | | } |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | if (allowedMethods != null) |
| | | 269 | | { |
| | 0 | 270 | | uriMatched = true; |
| | 0 | 271 | | message.Properties.Add(HttpOperationSelectorDataPropertyName, |
| | 0 | 272 | | new WebHttpDispatchOperationSelectorData() { AllowedMethods = allowedMethods }); |
| | | 273 | | } |
| | | 274 | | |
| | 0 | 275 | | return _catchAllOperationName; |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | private bool CanUriMatch(UriTemplateTable methodSpecificTable, Uri to, HttpRequestMessageProperty prop, Message |
| | | 279 | | { |
| | 36 | 280 | | operationName = null; |
| | 36 | 281 | | UriTemplateMatch result = methodSpecificTable.MatchSingle(to); |
| | | 282 | | |
| | 36 | 283 | | if (result != null) |
| | | 284 | | { |
| | 35 | 285 | | operationName = result.Data as string; |
| | | 286 | | Fx.Assert(operationName != null, "bad result"); |
| | 35 | 287 | | AddUriTemplateMatch(result, prop, message); |
| | 35 | 288 | | return true; |
| | | 289 | | } |
| | | 290 | | |
| | 1 | 291 | | return false; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | private void AddUriTemplateMatch(UriTemplateMatch match, HttpRequestMessageProperty requestProp, Message message |
| | | 295 | | { |
| | 35 | 296 | | match.SetBaseUri(match.BaseUri, requestProp); |
| | 35 | 297 | | message.Properties.Add(IncomingWebRequestContext.UriTemplateMatchResultsPropertyName, match); |
| | 35 | 298 | | } |
| | | 299 | | |
| | | 300 | | private bool ShouldRedirectToUriWithSlashAtTheEnd(UriTemplateTable methodSpecificTable, Message message, Uri to) |
| | | 301 | | { |
| | 0 | 302 | | UriBuilder ub = new UriBuilder(to); |
| | 0 | 303 | | if (ub.Path.EndsWith("/", StringComparison.Ordinal)) |
| | | 304 | | { |
| | 0 | 305 | | return false; |
| | | 306 | | } |
| | | 307 | | |
| | 0 | 308 | | ub.Path = ub.Path + "/"; |
| | 0 | 309 | | Uri originalPlusSlash = ub.Uri; |
| | | 310 | | |
| | 0 | 311 | | bool result = false; |
| | 0 | 312 | | if (methodSpecificTable != null && methodSpecificTable.MatchSingle(originalPlusSlash) != null) |
| | | 313 | | { |
| | | 314 | | // as an optimization, we check the table that matched the request's method |
| | | 315 | | // first, as it is more probable that a hit happens there |
| | 0 | 316 | | result = true; |
| | | 317 | | } |
| | | 318 | | else |
| | | 319 | | { |
| | | 320 | | // back-compat: |
| | | 321 | | // we will redirect as long as there is any method |
| | | 322 | | // - not necessary the one the user is looking for - |
| | | 323 | | // that matches the uri with a slash at the end |
| | | 324 | | |
| | 0 | 325 | | foreach (KeyValuePair<string, UriTemplateTable> pair in _methodSpecificTables) |
| | | 326 | | { |
| | 0 | 327 | | UriTemplateTable table = pair.Value; |
| | 0 | 328 | | if (table != methodSpecificTable && table.MatchSingle(originalPlusSlash) != null) |
| | | 329 | | { |
| | 0 | 330 | | result = true; |
| | 0 | 331 | | break; |
| | | 332 | | } |
| | | 333 | | } |
| | | 334 | | } |
| | | 335 | | |
| | 0 | 336 | | if (result) |
| | | 337 | | { |
| | 0 | 338 | | string hostAndPort = GetAuthority(message); |
| | 0 | 339 | | originalPlusSlash = UriTemplate.RewriteUri(ub.Uri, hostAndPort); |
| | 0 | 340 | | message.Properties.Add(RedirectPropertyName, originalPlusSlash); |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | return result; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | private static string GetAuthority(Message message) |
| | | 347 | | { |
| | 0 | 348 | | string hostName = null; |
| | 0 | 349 | | if (message.Properties.TryGetValue(HttpRequestMessageProperty.Name, out HttpRequestMessageProperty requestPr |
| | | 350 | | { |
| | 0 | 351 | | hostName = requestProperty.Headers[HttpRequestHeader.Host]; |
| | 0 | 352 | | if (!string.IsNullOrEmpty(hostName)) |
| | | 353 | | { |
| | 0 | 354 | | return hostName; |
| | | 355 | | } |
| | | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | return hostName; |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | // to enforce that no two ops have same UriTemplate & Method |
| | | 362 | | internal class WCFKey |
| | | 363 | | { |
| | | 364 | | private readonly string _method; |
| | | 365 | | private readonly UriTemplate _uriTemplate; |
| | | 366 | | |
| | 163 | 367 | | public WCFKey(UriTemplate uriTemplate, string method) |
| | | 368 | | { |
| | 163 | 369 | | _uriTemplate = uriTemplate; |
| | 163 | 370 | | _method = method; |
| | 163 | 371 | | } |
| | | 372 | | |
| | | 373 | | public override bool Equals(object obj) |
| | | 374 | | { |
| | 0 | 375 | | if (!(obj is WCFKey other)) |
| | | 376 | | { |
| | 0 | 377 | | return false; |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | return _uriTemplate.IsEquivalentTo(other._uriTemplate) && _method == other._method; |
| | | 381 | | } |
| | | 382 | | |
| | 289 | 383 | | public override int GetHashCode() => UriTemplateEquivalenceComparer.Instance.GetHashCode(_uriTemplate); |
| | | 384 | | } |
| | | 385 | | } |
| | | 386 | | } |