| | | 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.Specialized; |
| | | 6 | | using System.Text; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF |
| | | 10 | | { |
| | | 11 | | // thin wrapper around string; use type system to help ensure we |
| | | 12 | | // are doing canonicalization right/consistently |
| | | 13 | | internal class UriTemplateLiteralPathSegment : UriTemplatePathSegment, IComparable<UriTemplateLiteralPathSegment> |
| | | 14 | | { |
| | | 15 | | // segment doesn't store trailing slash |
| | | 16 | | private readonly string _segment; |
| | 1 | 17 | | private static readonly Uri s_dummyUri = new Uri("http://localhost"); |
| | | 18 | | |
| | | 19 | | private UriTemplateLiteralPathSegment(string segment) |
| | 630 | 20 | | : base(segment, UriTemplatePartType.Literal, segment.EndsWith("/", StringComparison.Ordinal)) |
| | | 21 | | { |
| | | 22 | | Fx.Assert(segment != null, "bad literal segment"); |
| | 630 | 23 | | if (EndsWithSlash) |
| | | 24 | | { |
| | 119 | 25 | | _segment = segment.Remove(segment.Length - 1); |
| | | 26 | | } |
| | | 27 | | else |
| | | 28 | | { |
| | 511 | 29 | | _segment = segment; |
| | | 30 | | } |
| | 511 | 31 | | } |
| | | 32 | | |
| | | 33 | | public static new UriTemplateLiteralPathSegment CreateFromUriTemplate(string segment, UriTemplate template) |
| | | 34 | | { |
| | | 35 | | // run it through UriBuilder to escape-if-necessary it |
| | 500 | 36 | | if (string.Compare(segment, "/", StringComparison.Ordinal) == 0) |
| | | 37 | | { |
| | | 38 | | // running an empty segment through UriBuilder has unexpected/wrong results |
| | 0 | 39 | | return new UriTemplateLiteralPathSegment("/"); |
| | | 40 | | } |
| | | 41 | | |
| | 500 | 42 | | if (segment.IndexOf(UriTemplate.WildcardPath, StringComparison.Ordinal) != -1) |
| | | 43 | | { |
| | 0 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 45 | | SR.Format(SR.UTInvalidWildcardInVariableOrLiteral, template._originalTemplate, UriTemplate.WildcardP |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // '*' is not usually escaped by the Uri\UriBuilder to %2a, since we forbid passing a |
| | | 49 | | // clear character and the workaroud is to pass the escaped form, we should replace the |
| | | 50 | | // escaped form with the regular one. |
| | 500 | 51 | | segment = segment.Replace("%2a", "*").Replace("%2A", "*"); |
| | 500 | 52 | | UriBuilder ub = new UriBuilder(s_dummyUri); |
| | 500 | 53 | | ub.Path = segment; |
| | 500 | 54 | | string escapedIfNecessarySegment = ub.Uri.AbsolutePath.Substring(1); |
| | 500 | 55 | | if (escapedIfNecessarySegment == string.Empty) |
| | | 56 | | { |
| | | 57 | | // This path through UriBuilder will sometimes '----' various segments |
| | | 58 | | // such as '../' and './'. When this happens and the result is an empty |
| | | 59 | | // string, we should just throw and tell the user we don't handle that. |
| | 0 | 60 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(segment), |
| | 0 | 61 | | SR.Format(SR.UTInvalidFormatSegmentOrQueryPart, segment)); |
| | | 62 | | } |
| | | 63 | | |
| | 500 | 64 | | return new UriTemplateLiteralPathSegment(escapedIfNecessarySegment); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public static UriTemplateLiteralPathSegment CreateFromWireData(string segment) |
| | | 68 | | { |
| | 130 | 69 | | return new UriTemplateLiteralPathSegment(segment); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | public string AsUnescapedString() |
| | | 73 | | { |
| | | 74 | | Fx.Assert(_segment != null, "this should only be called by Bind\\Lookup"); |
| | 120 | 75 | | return Uri.UnescapeDataString(_segment); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public override void Bind(string[] values, ref int valueIndex, StringBuilder path) |
| | | 79 | | { |
| | 119 | 80 | | if (EndsWithSlash) |
| | | 81 | | { |
| | 4 | 82 | | path.AppendFormat("{0}/", AsUnescapedString()); |
| | | 83 | | } |
| | | 84 | | else |
| | | 85 | | { |
| | 115 | 86 | | path.Append(AsUnescapedString()); |
| | | 87 | | } |
| | 115 | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | public int CompareTo(UriTemplateLiteralPathSegment other) => StringComparer.OrdinalIgnoreCase.Compare(_segment, |
| | | 91 | | |
| | | 92 | | public override bool Equals(object obj) |
| | | 93 | | { |
| | 248 | 94 | | if (!(obj is UriTemplateLiteralPathSegment lps)) |
| | | 95 | | { |
| | | 96 | | Fx.Assert("why would we ever call this?"); |
| | 0 | 97 | | return false; |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | 248 | 101 | | return ((EndsWithSlash == lps.EndsWithSlash) && |
| | 248 | 102 | | StringComparer.OrdinalIgnoreCase.Equals(_segment, lps._segment)); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | 789 | 106 | | public override int GetHashCode() => StringComparer.OrdinalIgnoreCase.GetHashCode(_segment); |
| | | 107 | | |
| | | 108 | | public override bool IsEquivalentTo(UriTemplatePathSegment other, bool ignoreTrailingSlash) |
| | | 109 | | { |
| | 0 | 110 | | if (other == null) |
| | | 111 | | { |
| | | 112 | | Fx.Assert("why would we ever call this?"); |
| | 0 | 113 | | return false; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | if (other.Nature != UriTemplatePartType.Literal) |
| | | 117 | | { |
| | 0 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | UriTemplateLiteralPathSegment otherAsLiteral = other as UriTemplateLiteralPathSegment; |
| | | 122 | | Fx.Assert(otherAsLiteral != null, "The nature requires that this will be OK"); |
| | | 123 | | |
| | 0 | 124 | | return IsMatch(otherAsLiteral, ignoreTrailingSlash); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public override bool IsMatch(UriTemplateLiteralPathSegment segment, bool ignoreTrailingSlash) |
| | | 128 | | { |
| | 0 | 129 | | if (!ignoreTrailingSlash && (segment.EndsWithSlash != this.EndsWithSlash)) |
| | | 130 | | { |
| | 0 | 131 | | return false; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | return CompareTo(segment) == 0; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | public bool IsNullOrEmpty() => string.IsNullOrEmpty(_segment); |
| | | 138 | | |
| | | 139 | | public override void Lookup(string segment, NameValueCollection boundParameters) |
| | | 140 | | { |
| | | 141 | | Fx.Assert(StringComparer.OrdinalIgnoreCase.Compare(AsUnescapedString(), segment) == 0, |
| | | 142 | | "How can that be? Lookup is expected to be called after IsMatch"); |
| | 36 | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |