1 //------------------------------------------------------------------------------
2 // <copyright file="GenericUriParser.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
10 This is a public sealed class that exposes various Uri parsing options
11 suported by built in Uri parser
14 Alexei Vopilov Jul 26 2004
23 using System.Globalization;
24 using System.Collections;
25 using System.Security.Permissions;
28 // This enum specifies the public options used to customize a hierarchical built-in parser.
31 public enum GenericUriParserOptions
33 // A hierarchical URI, allows a userinfo, non empty Inet-based authority, path, query and fragment
34 // The URI path gets agressively compressed means dots, slashes and backslashes are unescaped,
35 // backslashesare converted, and then it compresses the path. It also removes trailing dots,
36 // empty segments and dots-only segments
39 // Allows a free style authority that would terminate with '/'
40 GenericAuthority = 0x1,
42 // Allows an empty authority foo:///
43 AllowEmptyAuthority = 0x2,
45 // Disables a user info component, it implied in the case of GenericAuthority flag
48 // Disables a port component, it is implied in the case of GenericAuthority flag
51 // Disables a query. A ? char is considered as part of the path and is escaped
54 // Disables a fragment. A # char is considered as part of the path or query and is escaped
57 // if false then converta \ to /, otheriwse does this conversion for the Path component.
58 DontConvertPathBackslashes = 0x40,
60 // if false, then a/./b or a/.../b becomes a/b and /a/../b becomes /b
61 DontCompressPath = 0x80,
63 // if false then a/%2e./b becomes a/../b and then usually compressed
64 DontUnescapePathDotsAndSlashes= 0x100,
66 // IDN hosts supported. if true then unicode hostname is converted to IDN host
70 // Iri strict parsing flag. Makes sense for Unicode. If true then string is
71 // normalized, bidi control characters are removed, unicode char limits are checked
75 // A hierachical Uri parser that supports various customization options
77 // ATTN: This type must be compile-time registered with UriParser.CheckSetIsSimpleFlag() method
78 // to avoid calling into the user code if there is no one.
80 public class GenericUriParser: UriParser
83 // The only availabe .ctor.
85 public GenericUriParser(GenericUriParserOptions options)
86 : base(MapGenericParserOptions(options))
90 private static UriSyntaxFlags MapGenericParserOptions(GenericUriParserOptions options)
93 // Here we map public flags to internal ones
94 // Note an instacne of this parser is always a "simple parser" since the class is sealed.
96 UriSyntaxFlags flags = DefaultGenericUriParserFlags;
98 if ((options & GenericUriParserOptions.GenericAuthority) != 0)
100 // Disable some options that are not compatible with generic authority
101 flags &= ~(UriSyntaxFlags.MayHaveUserInfo | UriSyntaxFlags.MayHavePort | UriSyntaxFlags.AllowUncHost | UriSyntaxFlags.AllowAnInternetHost);
102 flags |= UriSyntaxFlags.AllowAnyOtherHost;
105 if ((options & GenericUriParserOptions.AllowEmptyAuthority) != 0)
107 flags |= UriSyntaxFlags.AllowEmptyHost;
110 if ((options & GenericUriParserOptions.NoUserInfo) != 0)
112 flags &= ~UriSyntaxFlags.MayHaveUserInfo;
115 if ((options & GenericUriParserOptions.NoPort) != 0)
117 flags &= ~UriSyntaxFlags.MayHavePort;
120 if ((options & GenericUriParserOptions.NoQuery) != 0)
122 flags &= ~UriSyntaxFlags.MayHaveQuery;
125 if ((options & GenericUriParserOptions.NoFragment) != 0)
127 flags &= ~UriSyntaxFlags.MayHaveFragment;
130 if ((options & GenericUriParserOptions.DontConvertPathBackslashes) != 0)
132 flags &= ~UriSyntaxFlags.ConvertPathSlashes;
135 if ((options & GenericUriParserOptions.DontCompressPath) != 0)
137 flags &= ~(UriSyntaxFlags.CompressPath | UriSyntaxFlags.CanonicalizeAsFilePath);
140 if ((options & GenericUriParserOptions.DontUnescapePathDotsAndSlashes) != 0)
142 flags &= ~UriSyntaxFlags.UnEscapeDotsAndSlashes;
145 if ((options & GenericUriParserOptions.Idn) != 0)
147 flags |= UriSyntaxFlags.AllowIdn;
150 if ((options & GenericUriParserOptions.IriParsing) != 0)
152 flags |= UriSyntaxFlags.AllowIriParsing;
158 private const UriSyntaxFlags DefaultGenericUriParserFlags =
159 UriSyntaxFlags.MustHaveAuthority |
161 UriSyntaxFlags.MayHaveUserInfo |
162 UriSyntaxFlags.MayHavePort |
163 UriSyntaxFlags.MayHavePath |
164 UriSyntaxFlags.MayHaveQuery |
165 UriSyntaxFlags.MayHaveFragment |
167 UriSyntaxFlags.AllowUncHost | //
168 UriSyntaxFlags.AllowAnInternetHost |
170 UriSyntaxFlags.PathIsRooted |
172 UriSyntaxFlags.ConvertPathSlashes |
173 UriSyntaxFlags.CompressPath |
174 UriSyntaxFlags.CanonicalizeAsFilePath |
175 UriSyntaxFlags.UnEscapeDotsAndSlashes;