| | | 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 | | namespace System |
| | | 5 | | { |
| | | 6 | | |
| | | 7 | | // |
| | | 8 | | // This enum specifies the public options used to customize a hierarchical built-in parser. |
| | | 9 | | // |
| | | 10 | | [Flags] |
| | | 11 | | public enum GenericUriParserOptions |
| | | 12 | | { |
| | | 13 | | // A hierarchical URI, allows a userinfo, non empty Inet-based authority, path, query and fragment |
| | | 14 | | // The URI path gets aggressively compressed means dots, slashes and backslashes are unescaped, |
| | | 15 | | // backslashesare converted, and then it compresses the path. It also removes trailing dots, |
| | | 16 | | // empty segments and dots-only segments |
| | | 17 | | Default = 0x0, |
| | | 18 | | |
| | | 19 | | // Allows a free style authority that would terminate with '/' |
| | | 20 | | GenericAuthority = 0x1, |
| | | 21 | | |
| | | 22 | | // Allows an empty authority foo:/// |
| | | 23 | | AllowEmptyAuthority = 0x2, |
| | | 24 | | |
| | | 25 | | // Disables a user info component, it implied in the case of GenericAuthority flag |
| | | 26 | | NoUserInfo = 0x4, |
| | | 27 | | |
| | | 28 | | // Disables a port component, it is implied in the case of GenericAuthority flag |
| | | 29 | | NoPort = 0x8, |
| | | 30 | | |
| | | 31 | | // Disables a query. A ? char is considered as part of the path and is escaped |
| | | 32 | | NoQuery = 0x10, |
| | | 33 | | |
| | | 34 | | // Disables a fragment. A # char is considered as part of the path or query and is escaped |
| | | 35 | | NoFragment = 0x20, |
| | | 36 | | |
| | | 37 | | // if false then converta \ to /, otherwise does this conversion for the Path component. |
| | | 38 | | DontConvertPathBackslashes = 0x40, |
| | | 39 | | |
| | | 40 | | // if false, then a/./b or a/.../b becomes a/b and /a/../b becomes /b |
| | | 41 | | DontCompressPath = 0x80, |
| | | 42 | | |
| | | 43 | | // if false then a/%2e./b becomes a/../b and then usually compressed |
| | | 44 | | DontUnescapePathDotsAndSlashes= 0x100, |
| | | 45 | | |
| | | 46 | | // IDN hosts supported. if true then unicode hostname is converted to IDN host |
| | | 47 | | // and vice versa |
| | | 48 | | Idn = 0x200, |
| | | 49 | | |
| | | 50 | | // Iri strict parsing flag. Makes sense for Unicode. If true then string is |
| | | 51 | | // normalized, bidi control characters are removed, unicode char limits are checked |
| | | 52 | | IriParsing = 0x400 |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public class GenericUriParser : UriParser |
| | | 56 | | { |
| | 0 | 57 | | public GenericUriParser(GenericUriParserOptions options) : base(MapGenericParserOptions(options)) |
| | 0 | 58 | | { |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static UriSyntaxFlags MapGenericParserOptions(GenericUriParserOptions options) |
| | 0 | 62 | | { |
| | | 63 | | // |
| | | 64 | | // Here we map public flags to internal ones |
| | | 65 | | // Note an instacne of this parser is always a "simple parser" since the class is sealed. |
| | | 66 | | // |
| | 0 | 67 | | UriSyntaxFlags flags = DefaultGenericUriParserFlags; |
| | | 68 | | |
| | 0 | 69 | | if ((options & GenericUriParserOptions.GenericAuthority) != 0) |
| | 0 | 70 | | { |
| | | 71 | | // Disable some options that are not compatible with generic authority |
| | 0 | 72 | | flags &= ~(UriSyntaxFlags.MayHaveUserInfo | UriSyntaxFlags.MayHavePort | UriSyntaxFlags.AllowUncHost | U |
| | 0 | 73 | | flags |= UriSyntaxFlags.AllowAnyOtherHost; |
| | 0 | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if ((options & GenericUriParserOptions.AllowEmptyAuthority) != 0) |
| | 0 | 77 | | { |
| | 0 | 78 | | flags |= UriSyntaxFlags.AllowEmptyHost; |
| | 0 | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | if ((options & GenericUriParserOptions.NoUserInfo) != 0) |
| | 0 | 82 | | { |
| | 0 | 83 | | flags &= ~UriSyntaxFlags.MayHaveUserInfo; |
| | 0 | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | if ((options & GenericUriParserOptions.NoPort) != 0) |
| | 0 | 87 | | { |
| | 0 | 88 | | flags &= ~UriSyntaxFlags.MayHavePort; |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | if ((options & GenericUriParserOptions.NoQuery) != 0) |
| | 0 | 92 | | { |
| | 0 | 93 | | flags &= ~UriSyntaxFlags.MayHaveQuery; |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | if ((options & GenericUriParserOptions.NoFragment) != 0) |
| | 0 | 97 | | { |
| | 0 | 98 | | flags &= ~UriSyntaxFlags.MayHaveFragment; |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | if ((options & GenericUriParserOptions.DontConvertPathBackslashes) != 0) |
| | 0 | 102 | | { |
| | 0 | 103 | | flags &= ~UriSyntaxFlags.ConvertPathSlashes; |
| | 0 | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | if ((options & GenericUriParserOptions.DontCompressPath) != 0) |
| | 0 | 107 | | { |
| | 0 | 108 | | flags &= ~(UriSyntaxFlags.CompressPath | UriSyntaxFlags.CanonicalizeAsFilePath); |
| | 0 | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | if ((options & GenericUriParserOptions.DontUnescapePathDotsAndSlashes) != 0) |
| | 0 | 112 | | { |
| | 0 | 113 | | flags &= ~UriSyntaxFlags.UnEscapeDotsAndSlashes; |
| | 0 | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | if ((options & GenericUriParserOptions.Idn) != 0) |
| | 0 | 117 | | { |
| | 0 | 118 | | flags |= UriSyntaxFlags.AllowIdn; |
| | 0 | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | if ((options & GenericUriParserOptions.IriParsing) != 0) |
| | 0 | 122 | | { |
| | 0 | 123 | | flags |= UriSyntaxFlags.AllowIriParsing; |
| | 0 | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | return flags; |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | private const UriSyntaxFlags DefaultGenericUriParserFlags = |
| | | 130 | | UriSyntaxFlags.MustHaveAuthority | |
| | | 131 | | UriSyntaxFlags.MayHaveUserInfo | |
| | | 132 | | UriSyntaxFlags.MayHavePort | |
| | | 133 | | UriSyntaxFlags.MayHavePath | |
| | | 134 | | UriSyntaxFlags.MayHaveQuery | |
| | | 135 | | UriSyntaxFlags.MayHaveFragment | |
| | | 136 | | UriSyntaxFlags.AllowUncHost | |
| | | 137 | | UriSyntaxFlags.AllowAnInternetHost | |
| | | 138 | | UriSyntaxFlags.PathIsRooted | |
| | | 139 | | UriSyntaxFlags.ConvertPathSlashes | |
| | | 140 | | UriSyntaxFlags.CompressPath | |
| | | 141 | | UriSyntaxFlags.CanonicalizeAsFilePath | |
| | | 142 | | UriSyntaxFlags.UnEscapeDotsAndSlashes; |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |