| | | 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.Collections.Specialized; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Description; |
| | | 9 | | using CoreWCF.Web; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Dispatcher |
| | | 12 | | { |
| | | 13 | | internal class UriTemplateDispatchFormatter : IDispatchMessageFormatter |
| | | 14 | | { |
| | | 15 | | internal Dictionary<int, string> _pathMapping; |
| | | 16 | | internal Dictionary<int, KeyValuePair<string, Type>> _queryMapping; |
| | | 17 | | private readonly Uri _baseAddress; |
| | | 18 | | private readonly IDispatchMessageFormatter _inner; |
| | | 19 | | private readonly string _operationName; |
| | | 20 | | private readonly QueryStringConverter _qsc; |
| | | 21 | | private readonly int _totalNumUTVars; |
| | | 22 | | private readonly UriTemplate _uriTemplate; |
| | | 23 | | |
| | 326 | 24 | | public UriTemplateDispatchFormatter(OperationDescription operationDescription, IDispatchMessageFormatter inner, |
| | | 25 | | { |
| | 326 | 26 | | _inner = inner; |
| | 326 | 27 | | _qsc = qsc; |
| | 326 | 28 | | _baseAddress = baseAddress; |
| | 326 | 29 | | _operationName = operationDescription.Name; |
| | 326 | 30 | | UriTemplateClientFormatter.Populate(out _pathMapping, |
| | 326 | 31 | | out _queryMapping, |
| | 326 | 32 | | out _totalNumUTVars, |
| | 326 | 33 | | out _uriTemplate, |
| | 326 | 34 | | operationDescription, |
| | 326 | 35 | | qsc, |
| | 326 | 36 | | contractName); |
| | 326 | 37 | | } |
| | | 38 | | |
| | | 39 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 40 | | { |
| | 33 | 41 | | object[] innerParameters = new object[parameters.Length - _totalNumUTVars]; |
| | 33 | 42 | | if (innerParameters.Length != 0) |
| | | 43 | | { |
| | 6 | 44 | | _inner.DeserializeRequest(message, innerParameters); |
| | | 45 | | } |
| | | 46 | | |
| | 33 | 47 | | int j = 0; |
| | 33 | 48 | | UriTemplateMatch utmr = null; |
| | 33 | 49 | | string UTMRName = IncomingWebRequestContext.UriTemplateMatchResultsPropertyName; |
| | 33 | 50 | | if (message.Properties.ContainsKey(UTMRName)) |
| | | 51 | | { |
| | 33 | 52 | | utmr = message.Properties[UTMRName] as UriTemplateMatch; |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | 0 | 56 | | if (message.Headers.To != null && message.Headers.To.IsAbsoluteUri) |
| | | 57 | | { |
| | 0 | 58 | | utmr = _uriTemplate.Match(_baseAddress, message.Headers.To); |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 33 | 62 | | NameValueCollection nvc = (utmr == null) ? new NameValueCollection() : utmr.BoundVariables; |
| | 90 | 63 | | for (int i = 0; i < parameters.Length; ++i) |
| | | 64 | | { |
| | 12 | 65 | | if (_pathMapping.ContainsKey(i) && utmr != null) |
| | | 66 | | { |
| | 5 | 67 | | parameters[i] = nvc[_pathMapping[i]]; |
| | | 68 | | } |
| | 7 | 69 | | else if (_queryMapping.ContainsKey(i) && utmr != null) |
| | | 70 | | { |
| | 1 | 71 | | string queryVal = nvc[_queryMapping[i].Key]; |
| | 1 | 72 | | parameters[i] = _qsc.ConvertStringToValue(queryVal, _queryMapping[i].Value); |
| | | 73 | | } |
| | | 74 | | else |
| | | 75 | | { |
| | 6 | 76 | | parameters[i] = innerParameters[j]; |
| | 6 | 77 | | ++j; |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 82 | | //{ |
| | | 83 | | // if (utmr != null) |
| | | 84 | | // { |
| | | 85 | | // foreach (string key in utmr.QueryParameters.Keys) |
| | | 86 | | // { |
| | | 87 | | // bool isParameterIgnored = true; |
| | | 88 | | // foreach (KeyValuePair<string, Type> kvp in this._queryMapping.Values) |
| | | 89 | | // { |
| | | 90 | | // if (String.Compare(key, kvp.Key, StringComparison.OrdinalIgnoreCase) == 0) |
| | | 91 | | // { |
| | | 92 | | // isParameterIgnored = false; |
| | | 93 | | // break; |
| | | 94 | | // } |
| | | 95 | | // } |
| | | 96 | | // if (isParameterIgnored) |
| | | 97 | | // { |
| | | 98 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.WebUnknownQueryParameterIgno |
| | | 99 | | // } |
| | | 100 | | // } |
| | | 101 | | // } |
| | | 102 | | //} |
| | 33 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) => throw Diagno |
| | | 106 | | } |
| | | 107 | | } |