Merge pull request #2964 from ludovic-henry/sgen-monocontext
[mono.git] / mcs / class / referencesource / System / net / System / GenericUriParser.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="GenericUriParser.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 /*++
8 Abstract:
9
10     This is a public sealed class that exposes various Uri parsing options
11     suported by built in Uri parser
12
13 Author:
14     Alexei Vopilov    Jul 26 2004
15
16 Revision History:
17 --*/
18
19
20
21 namespace System {
22
23     using System.Globalization;
24     using System.Collections;
25     using System.Security.Permissions;
26
27     //
28     // This enum specifies the public options used to customize a hierarchical built-in parser.
29     //
30     [Flags]
31     public enum GenericUriParserOptions
32     {
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
37         Default                         = 0x0,
38
39         // Allows a free style authority that would terminate with '/'
40         GenericAuthority           = 0x1,
41
42         // Allows an empty authority foo:///
43         AllowEmptyAuthority        = 0x2,
44
45         // Disables a user info component, it implied in the case of GenericAuthority flag
46         NoUserInfo                 = 0x4,
47
48         // Disables a port component, it is implied in the case of GenericAuthority flag
49         NoPort                     = 0x8,
50
51         // Disables a query. A ? char is considered as part of the path and is escaped
52         NoQuery                    = 0x10,
53
54         // Disables a fragment. A # char is considered as part of the path or query and is escaped
55         NoFragment                 = 0x20,
56
57         // if false then converta \ to /, otheriwse does this conversion for the Path component.
58         DontConvertPathBackslashes = 0x40,
59
60         // if false, then a/./b or a/.../b becomes a/b and /a/../b becomes /b
61         DontCompressPath           = 0x80,
62
63         // if false  then a/%2e./b  becomes a/../b and then usually compressed
64         DontUnescapePathDotsAndSlashes= 0x100,
65
66         // IDN hosts supported. if true then unicode hostname is converted to IDN host 
67         //  and vice versa
68         Idn = 0x200,
69
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
72         IriParsing = 0x400
73     }
74     //
75     // A hierachical Uri parser that supports various customization options
76     //
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.
79     //
80     public class GenericUriParser: UriParser
81     {
82         //
83         // The only availabe .ctor.
84         //
85         public GenericUriParser(GenericUriParserOptions options)
86             : base(MapGenericParserOptions(options))
87         {
88         }
89         //
90         private static UriSyntaxFlags MapGenericParserOptions(GenericUriParserOptions options)
91         {
92             //
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.
95             //
96             UriSyntaxFlags flags = DefaultGenericUriParserFlags;
97
98             if ((options & GenericUriParserOptions.GenericAuthority) != 0)
99             {
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;
103             }
104
105             if ((options & GenericUriParserOptions.AllowEmptyAuthority) != 0)
106             {
107                 flags |= UriSyntaxFlags.AllowEmptyHost;
108             }
109
110             if ((options & GenericUriParserOptions.NoUserInfo) != 0)
111             {
112                 flags &= ~UriSyntaxFlags.MayHaveUserInfo;
113             }
114
115             if ((options & GenericUriParserOptions.NoPort) != 0)
116             {
117                 flags &= ~UriSyntaxFlags.MayHavePort;
118             }
119
120             if ((options & GenericUriParserOptions.NoQuery) != 0)
121             {
122                 flags &= ~UriSyntaxFlags.MayHaveQuery;
123             }
124
125             if ((options & GenericUriParserOptions.NoFragment) != 0)
126             {
127                 flags &= ~UriSyntaxFlags.MayHaveFragment;
128             }
129
130             if ((options & GenericUriParserOptions.DontConvertPathBackslashes) != 0)
131             {
132                 flags &= ~UriSyntaxFlags.ConvertPathSlashes;
133             }
134
135             if ((options & GenericUriParserOptions.DontCompressPath) != 0)
136             {
137                 flags &= ~(UriSyntaxFlags.CompressPath | UriSyntaxFlags.CanonicalizeAsFilePath);
138             }
139
140             if ((options & GenericUriParserOptions.DontUnescapePathDotsAndSlashes) != 0)
141             {
142                 flags &= ~UriSyntaxFlags.UnEscapeDotsAndSlashes;
143             }
144
145             if ((options & GenericUriParserOptions.Idn) != 0)
146             {
147                 flags |= UriSyntaxFlags.AllowIdn;
148             }
149
150             if ((options & GenericUriParserOptions.IriParsing) != 0)
151             {
152                 flags |= UriSyntaxFlags.AllowIriParsing;
153             }
154
155             return flags;
156         }
157
158         private const UriSyntaxFlags DefaultGenericUriParserFlags =
159                                                                     UriSyntaxFlags.MustHaveAuthority |
160                                                                     //
161                                                                     UriSyntaxFlags.MayHaveUserInfo |
162                                                                     UriSyntaxFlags.MayHavePort |
163                                                                     UriSyntaxFlags.MayHavePath |
164                                                                     UriSyntaxFlags.MayHaveQuery |
165                                                                     UriSyntaxFlags.MayHaveFragment |
166                                                                     //
167                                                                     UriSyntaxFlags.AllowUncHost |       //
168                                                                     UriSyntaxFlags.AllowAnInternetHost |
169                                                                     //
170                                                                     UriSyntaxFlags.PathIsRooted |
171                                                                     //
172                                                                     UriSyntaxFlags.ConvertPathSlashes |
173                                                                     UriSyntaxFlags.CompressPath |
174                                                                     UriSyntaxFlags.CanonicalizeAsFilePath |
175                                                                     UriSyntaxFlags.UnEscapeDotsAndSlashes;
176
177
178     }
179 }
180