| | | 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.Diagnostics; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Runtime.Serialization; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | // TODO: Make internal again |
| | | 14 | | [DataContract] |
| | | 15 | | public sealed class BaseUriWithWildcard |
| | | 16 | | { |
| | | 17 | | [DataMember] |
| | | 18 | | private readonly Uri _baseAddress; |
| | | 19 | | private const char segmentDelimiter = '/'; |
| | | 20 | | |
| | | 21 | | [DataMember] |
| | | 22 | | private HostNameComparisonMode _hostNameComparisonMode; |
| | | 23 | | private const string plus = "+"; |
| | | 24 | | private const string star = "*"; |
| | | 25 | | private const int HttpUriDefaultPort = 80; |
| | | 26 | | private const int HttpsUriDefaultPort = 443; |
| | | 27 | | |
| | | 28 | | // Derived from [DataMember] fields |
| | | 29 | | private Comparand _comparand; |
| | | 30 | | private int _hashCode; |
| | | 31 | | |
| | 297 | 32 | | public BaseUriWithWildcard(Uri baseAddress, HostNameComparisonMode hostNameComparisonMode) |
| | | 33 | | { |
| | 297 | 34 | | _baseAddress = baseAddress; |
| | 297 | 35 | | _hostNameComparisonMode = hostNameComparisonMode; |
| | 297 | 36 | | SetComparisonAddressAndHashCode(); |
| | | 37 | | |
| | | 38 | | // Note the Uri may contain query string for WSDL purpose. |
| | | 39 | | // So do not check IsValid(). |
| | 297 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | private BaseUriWithWildcard(string protocol, int defaultPort, string binding, int segmentCount, string path, str |
| | | 43 | | { |
| | 0 | 44 | | string[] urlParameters = SplitBinding(binding); |
| | | 45 | | |
| | 0 | 46 | | if (urlParameters.Length != segmentCount) |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 49 | | new UriFormatException(SR.Format(SR.Hosting_MisformattedBinding, binding, protocol, sampleBinding))) |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | int currentIndex = segmentCount - 1; |
| | 0 | 53 | | string host = ParseHostAndHostNameComparisonMode(urlParameters[currentIndex]); |
| | | 54 | | |
| | 0 | 55 | | int port = -1; |
| | | 56 | | |
| | 0 | 57 | | if (--currentIndex >= 0) |
| | | 58 | | { |
| | 0 | 59 | | string portString = urlParameters[currentIndex].Trim(); |
| | | 60 | | |
| | 0 | 61 | | if (!string.IsNullOrEmpty(portString) && |
| | 0 | 62 | | !int.TryParse(portString, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out port)) |
| | | 63 | | { |
| | 0 | 64 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new UriFormatException(SR.Format(SR.Hostin |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | if (port == defaultPort) |
| | | 68 | | { |
| | | 69 | | // Set to -1 so that Uri does not show it in the string. |
| | 0 | 70 | | port = -1; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | try |
| | | 74 | | { |
| | | 75 | | Fx.Assert(path != null, "path should never be null here"); |
| | 0 | 76 | | _baseAddress = new UriBuilder(protocol, host, port, path).Uri; |
| | 0 | 77 | | } |
| | | 78 | | catch (Exception exception) |
| | | 79 | | { |
| | 0 | 80 | | if (Fx.IsFatal(exception)) |
| | | 81 | | { |
| | 0 | 82 | | throw; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | DiagnosticUtility.TraceHandledException(exception, TraceEventType.Error); |
| | | 86 | | |
| | 0 | 87 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new UriFormatException(SR.Format(SR.Hosting_Mi |
| | 0 | 88 | | protocol))); |
| | | 89 | | } |
| | 0 | 90 | | SetComparisonAddressAndHashCode(); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | // TODO: Make internal again |
| | | 94 | | public Uri BaseAddress |
| | | 95 | | { |
| | 260 | 96 | | get { return _baseAddress; } |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | // TODO: Make internal again |
| | | 100 | | public HostNameComparisonMode HostNameComparisonMode |
| | | 101 | | { |
| | 739 | 102 | | get { return _hostNameComparisonMode; } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private static string[] SplitBinding(string binding) |
| | | 106 | | { |
| | 0 | 107 | | bool parsingIPv6Address = false; |
| | | 108 | | const char splitChar = ':', startIPv6Address = '[', endIPv6Address = ']'; |
| | | 109 | | |
| | 0 | 110 | | List<int> splitLocations = null; |
| | | 111 | | |
| | 0 | 112 | | for (int i = 0; i < binding.Length; i++) |
| | | 113 | | { |
| | 0 | 114 | | if (parsingIPv6Address && binding[i] == endIPv6Address) |
| | | 115 | | { |
| | 0 | 116 | | parsingIPv6Address = false; |
| | | 117 | | } |
| | 0 | 118 | | else if (binding[i] == startIPv6Address) |
| | | 119 | | { |
| | 0 | 120 | | parsingIPv6Address = true; |
| | | 121 | | } |
| | 0 | 122 | | else if (!parsingIPv6Address && binding[i] == splitChar) |
| | | 123 | | { |
| | 0 | 124 | | if (splitLocations == null) |
| | | 125 | | { |
| | 0 | 126 | | splitLocations = new List<int>(); |
| | | 127 | | } |
| | 0 | 128 | | splitLocations.Add(i); |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | string[] tokens; |
| | 0 | 133 | | if (splitLocations == null) |
| | | 134 | | { |
| | 0 | 135 | | tokens = new[] { binding }; |
| | | 136 | | } |
| | | 137 | | else |
| | | 138 | | { |
| | 0 | 139 | | tokens = new string[splitLocations.Count + 1]; |
| | 0 | 140 | | int startIndex = 0; |
| | 0 | 141 | | for (int i = 0; i < tokens.Length; i++) |
| | | 142 | | { |
| | 0 | 143 | | if (i < splitLocations.Count) |
| | | 144 | | { |
| | 0 | 145 | | int nextSplitIndex = splitLocations[i]; |
| | 0 | 146 | | tokens[i] = binding.Substring(startIndex, nextSplitIndex - startIndex); |
| | 0 | 147 | | startIndex = nextSplitIndex + 1; |
| | | 148 | | } |
| | | 149 | | else //splitting the last segment |
| | | 150 | | { |
| | 0 | 151 | | if (startIndex < binding.Length) |
| | | 152 | | { |
| | 0 | 153 | | tokens[i] = binding.Substring(startIndex, binding.Length - startIndex); |
| | | 154 | | } |
| | | 155 | | else |
| | | 156 | | { |
| | | 157 | | //splitChar was the last character in the string |
| | 0 | 158 | | tokens[i] = string.Empty; |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | } |
| | 0 | 163 | | return tokens; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | internal static BaseUriWithWildcard CreateHostedUri(string protocol, string binding, string path) |
| | | 167 | | { |
| | | 168 | | Fx.Assert(protocol != null, "caller must verify"); |
| | | 169 | | |
| | 0 | 170 | | if (binding == null) |
| | | 171 | | { |
| | 0 | 172 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(binding)); |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | if (path == null) |
| | | 176 | | { |
| | 0 | 177 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(path)); |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (protocol.Equals(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase)) |
| | | 181 | | { |
| | | 182 | | // For http, binding format is: "<ipAddress>:<port>:<hostName>" |
| | | 183 | | // as specified in http://www.microsoft.com/resources/documentation/WindowsServ/2003/standard/proddocs/e |
| | 0 | 184 | | return new BaseUriWithWildcard(Uri.UriSchemeHttp, HttpUriDefaultPort, binding, 3, path, ":80:"); |
| | | 185 | | } |
| | 0 | 186 | | if (protocol.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase)) |
| | | 187 | | { |
| | | 188 | | // For https, binding format is the same as http |
| | 0 | 189 | | return new BaseUriWithWildcard(Uri.UriSchemeHttps, HttpsUriDefaultPort, binding, 3, path, ":443:"); |
| | | 190 | | } |
| | 0 | 191 | | if (protocol.Equals(Uri.UriSchemeNetTcp, StringComparison.OrdinalIgnoreCase)) |
| | | 192 | | { |
| | | 193 | | // For net.tcp, binding format is: "<port>:<hostName>" |
| | 0 | 194 | | return new BaseUriWithWildcard(Uri.UriSchemeNetTcp, TransportDefaults.TcpUriDefaultPort, binding, 2, pat |
| | | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new UriFormatException(SR.Format(SR.Hosting_NotSup |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | public override bool Equals(object o) |
| | | 201 | | { |
| | 37 | 202 | | if (!(o is BaseUriWithWildcard other) || other._hashCode != _hashCode || other._hostNameComparisonMode != _h |
| | 37 | 203 | | other._comparand.Port != _comparand.Port) |
| | | 204 | | { |
| | 0 | 205 | | return false; |
| | | 206 | | } |
| | 37 | 207 | | if (!ReferenceEquals(other._comparand.Scheme, _comparand.Scheme)) |
| | | 208 | | { |
| | 0 | 209 | | return false; |
| | | 210 | | } |
| | 37 | 211 | | return _comparand.Address.Equals(other._comparand.Address); |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | public override int GetHashCode() |
| | | 215 | | { |
| | 490 | 216 | | return _hashCode; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | internal bool IsBaseOf(Uri fullAddress) |
| | | 220 | | { |
| | 0 | 221 | | if (_baseAddress.Scheme != (object)fullAddress.Scheme) |
| | | 222 | | { |
| | 0 | 223 | | return false; |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | if (_baseAddress.Port != fullAddress.Port) |
| | | 227 | | { |
| | 0 | 228 | | return false; |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | if (HostNameComparisonMode == HostNameComparisonMode.Exact) |
| | | 232 | | { |
| | 0 | 233 | | if (string.Compare(_baseAddress.Host, fullAddress.Host, StringComparison.OrdinalIgnoreCase) != 0) |
| | | 234 | | { |
| | 0 | 235 | | return false; |
| | | 236 | | } |
| | | 237 | | } |
| | 0 | 238 | | string s1 = _baseAddress.GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped |
| | 0 | 239 | | string s2 = fullAddress.GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped) |
| | | 240 | | |
| | 0 | 241 | | if (s1.Length > s2.Length) |
| | | 242 | | { |
| | 0 | 243 | | return false; |
| | | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | if (s1.Length < s2.Length && |
| | 0 | 247 | | s1[s1.Length - 1] != segmentDelimiter && |
| | 0 | 248 | | s2[s1.Length] != segmentDelimiter) |
| | | 249 | | { |
| | | 250 | | // Matching over segments |
| | 0 | 251 | | return false; |
| | | 252 | | } |
| | 0 | 253 | | return string.Compare(s2, 0, s1, 0, s1.Length, StringComparison.OrdinalIgnoreCase) == 0; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | [OnDeserialized] |
| | | 257 | | internal void OnDeserialized(StreamingContext context) |
| | | 258 | | { |
| | 0 | 259 | | UriSchemeKeyedCollection.ValidateBaseAddress(_baseAddress, "context"); |
| | | 260 | | |
| | 0 | 261 | | if (!HostNameComparisonModeHelper.IsDefined(HostNameComparisonMode)) |
| | | 262 | | { |
| | 0 | 263 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(context), SR.Hosting_BaseUriDeserial |
| | | 264 | | } |
| | 0 | 265 | | SetComparisonAddressAndHashCode(); |
| | 0 | 266 | | } |
| | | 267 | | |
| | | 268 | | private string ParseHostAndHostNameComparisonMode(string host) |
| | | 269 | | { |
| | 0 | 270 | | if (string.IsNullOrEmpty(host) || host.Equals(star)) |
| | | 271 | | { |
| | 0 | 272 | | _hostNameComparisonMode = HostNameComparisonMode.WeakWildcard; |
| | 0 | 273 | | host = DnsCache.MachineName; |
| | | 274 | | } |
| | 0 | 275 | | else if (host.Equals(plus)) |
| | | 276 | | { |
| | 0 | 277 | | _hostNameComparisonMode = HostNameComparisonMode.StrongWildcard; |
| | 0 | 278 | | host = DnsCache.MachineName; |
| | | 279 | | } |
| | | 280 | | else |
| | | 281 | | { |
| | 0 | 282 | | _hostNameComparisonMode = HostNameComparisonMode.Exact; |
| | | 283 | | } |
| | 0 | 284 | | return host; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | private void SetComparisonAddressAndHashCode() |
| | | 288 | | { |
| | 297 | 289 | | if (HostNameComparisonMode == HostNameComparisonMode.Exact) |
| | | 290 | | { |
| | | 291 | | // Use canonical string representation of the full base address for comparison |
| | 1 | 292 | | _comparand.Address = _baseAddress.ToString(); |
| | | 293 | | } |
| | | 294 | | else |
| | | 295 | | { |
| | | 296 | | // Use canonical string representation of the absolute path for comparison |
| | 296 | 297 | | _comparand.Address = _baseAddress.GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFor |
| | | 298 | | } |
| | | 299 | | |
| | 297 | 300 | | _comparand.Port = _baseAddress.Port; |
| | 297 | 301 | | _comparand.Scheme = _baseAddress.Scheme; |
| | | 302 | | |
| | 297 | 303 | | if ((_comparand.Port == -1) && (_comparand.Scheme == (object)Uri.UriSchemeNetTcp)) |
| | | 304 | | { |
| | | 305 | | // Compensate for the fact that the Uri type doesn't know about our default TCP port number |
| | 0 | 306 | | _comparand.Port = TransportDefaults.TcpUriDefaultPort; |
| | | 307 | | } |
| | 297 | 308 | | _hashCode = _comparand.Address.GetHashCode() ^ _comparand.Port ^ (int)HostNameComparisonMode; |
| | 297 | 309 | | } |
| | | 310 | | |
| | | 311 | | public override string ToString() |
| | | 312 | | { |
| | 0 | 313 | | return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", HostNameComparisonMode, BaseAddress); |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | private struct Comparand |
| | | 317 | | { |
| | | 318 | | public string Address; |
| | | 319 | | public int Port; |
| | | 320 | | public string Scheme; |
| | | 321 | | } |
| | | 322 | | } |
| | | 323 | | } |