| | | 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.ObjectModel; |
| | | 6 | | using System.Collections.Specialized; |
| | | 7 | | using System.Net; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF |
| | | 12 | | { |
| | | 13 | | public class UriTemplateMatch |
| | | 14 | | { |
| | | 15 | | private Uri _baseUri; |
| | | 16 | | private NameValueCollection _boundVariables; |
| | | 17 | | private NameValueCollection _queryParameters; |
| | | 18 | | private Collection<string> _relativePathSegments; |
| | | 19 | | private Collection<string> _wildcardPathSegments; |
| | 35 | 20 | | private int _wildcardSegmentsStartOffset = -1; |
| | | 21 | | private Uri _originalBaseUri; |
| | | 22 | | private HttpRequestMessageProperty _requestProp; |
| | | 23 | | |
| | 35 | 24 | | public UriTemplateMatch() |
| | | 25 | | { |
| | 35 | 26 | | } |
| | | 27 | | |
| | | 28 | | public Uri BaseUri // the base address, untouched |
| | | 29 | | { |
| | | 30 | | get |
| | | 31 | | { |
| | 35 | 32 | | if (_baseUri == null && _originalBaseUri != null) |
| | | 33 | | { |
| | 0 | 34 | | _baseUri = UriTemplate.RewriteUri(_originalBaseUri, _requestProp.Headers[HttpRequestHeader.Host]); |
| | | 35 | | } |
| | | 36 | | |
| | 35 | 37 | | return _baseUri; |
| | | 38 | | } |
| | | 39 | | set |
| | | 40 | | { |
| | 35 | 41 | | _baseUri = value; |
| | 35 | 42 | | _originalBaseUri = null; |
| | 35 | 43 | | _requestProp = null; |
| | 35 | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public NameValueCollection BoundVariables // result of TryLookup, values are decoded |
| | | 48 | | { |
| | | 49 | | get |
| | | 50 | | { |
| | 75 | 51 | | if (_boundVariables == null) |
| | | 52 | | { |
| | 35 | 53 | | _boundVariables = new NameValueCollection(); |
| | | 54 | | } |
| | | 55 | | |
| | 75 | 56 | | return _boundVariables; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 70 | 60 | | public object Data { get; set; } |
| | | 61 | | |
| | | 62 | | public NameValueCollection QueryParameters // the result of UrlUtility.ParseQueryString (keys and values are de |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 1 | 66 | | if (_queryParameters == null) |
| | | 67 | | { |
| | 0 | 68 | | PopulateQueryParameters(); |
| | | 69 | | } |
| | | 70 | | |
| | 1 | 71 | | return _queryParameters; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public Collection<string> RelativePathSegments // entire Path (after the base address), decoded |
| | | 76 | | { |
| | | 77 | | get |
| | | 78 | | { |
| | 40 | 79 | | if (_relativePathSegments == null) |
| | | 80 | | { |
| | 0 | 81 | | _relativePathSegments = new Collection<string>(); |
| | | 82 | | } |
| | 40 | 83 | | return _relativePathSegments; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public Uri RequestUri // uri on the wire, untouched |
| | | 88 | | { |
| | 1 | 89 | | get; |
| | 35 | 90 | | set; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | public UriTemplate Template // which one got matched |
| | | 94 | | { |
| | 0 | 95 | | get; |
| | 35 | 96 | | set; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public Collection<string> WildcardPathSegments // just the Path part matched by "*", decoded |
| | | 100 | | { |
| | | 101 | | get |
| | | 102 | | { |
| | 0 | 103 | | if (_wildcardPathSegments == null) |
| | | 104 | | { |
| | 0 | 105 | | PopulateWildcardSegments(); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return _wildcardPathSegments; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | internal void SetQueryParameters(NameValueCollection queryParameters) |
| | | 113 | | { |
| | 1 | 114 | | _queryParameters = new NameValueCollection(queryParameters); |
| | 1 | 115 | | } |
| | | 116 | | |
| | | 117 | | internal void SetRelativePathSegments(Collection<string> segments) |
| | | 118 | | { |
| | | 119 | | Fx.Assert(segments != null, "segments != null"); |
| | | 120 | | |
| | 35 | 121 | | _relativePathSegments = segments; |
| | 35 | 122 | | } |
| | | 123 | | |
| | | 124 | | internal void SetWildcardPathSegmentsStart(int startOffset) |
| | | 125 | | { |
| | | 126 | | Fx.Assert(startOffset >= 0, "startOffset >= 0"); |
| | | 127 | | |
| | 35 | 128 | | _wildcardSegmentsStartOffset = startOffset; |
| | 35 | 129 | | } |
| | | 130 | | |
| | | 131 | | internal void SetBaseUri(Uri originalBaseUri, HttpRequestMessageProperty requestProp) |
| | | 132 | | { |
| | 35 | 133 | | _baseUri = null; |
| | 35 | 134 | | _originalBaseUri = originalBaseUri; |
| | 35 | 135 | | _requestProp = requestProp; |
| | 35 | 136 | | } |
| | | 137 | | |
| | | 138 | | private void PopulateQueryParameters() |
| | | 139 | | { |
| | 0 | 140 | | if (RequestUri != null) |
| | | 141 | | { |
| | 0 | 142 | | _queryParameters = UriTemplateHelpers.ParseQueryString(this.RequestUri.Query); |
| | | 143 | | } |
| | | 144 | | else |
| | | 145 | | { |
| | 0 | 146 | | _queryParameters = new NameValueCollection(); |
| | | 147 | | } |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | private void PopulateWildcardSegments() |
| | | 151 | | { |
| | 0 | 152 | | if (_wildcardSegmentsStartOffset != -1) |
| | | 153 | | { |
| | 0 | 154 | | _wildcardPathSegments = new Collection<string>(); |
| | 0 | 155 | | for (int i = _wildcardSegmentsStartOffset; i < RelativePathSegments.Count; ++i) |
| | | 156 | | { |
| | 0 | 157 | | _wildcardPathSegments.Add(RelativePathSegments[i]); |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | else |
| | | 161 | | { |
| | 0 | 162 | | _wildcardPathSegments = new Collection<string>(); |
| | | 163 | | } |
| | 0 | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |