Replace whitespaces to 8-spaces tabs
[mono.git] / mcs / class / System / System.Net / ListenerPrefix.cs
1 //
2 // System.Net.ListenerPrefix
3 //
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //      Oleg Mihailik (mihailik gmail co_m)
7 //
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if SECURITY_DEP
31
32 namespace System.Net
33 {
34         sealed class ListenerPrefix
35         {
36                 string original;
37                 string host;
38                 ushort port;
39                 string path;
40                 bool secure;
41                 IPAddress[] addresses;
42                 public HttpListener Listener;
43
44                 public ListenerPrefix(string prefix)
45                 {
46                         this.original = prefix;
47                         Parse(prefix);
48                 }
49
50                 public override string ToString()
51                 {
52                         return original;
53                 }
54
55                 public IPAddress[] Addresses
56                 {
57                         get { return addresses; }
58                         set { addresses = value; }
59                 }
60                 public bool Secure
61                 {
62                         get { return secure; }
63                 }
64
65                 public string Host
66                 {
67                         get { return host; }
68                 }
69
70                 public int Port
71                 {
72                         get { return (int)port; }
73                 }
74
75                 public string Path
76                 {
77                         get { return path; }
78                 }
79
80                 // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
81                 public override bool Equals(object o)
82                 {
83                         ListenerPrefix other = o as ListenerPrefix;
84                         if (other == null)
85                                 return false;
86
87                         return (original == other.original);
88                 }
89
90                 public override int GetHashCode()
91                 {
92                         return original.GetHashCode();
93                 }
94
95                 void Parse(string uri)
96                 {
97                         ushort default_port = 80;
98                         if (uri.StartsWith("https://"))
99                         {
100                                 default_port = 443;
101                                 secure = true;
102                         }
103
104                         int length = uri.Length;
105                         int start_host = uri.IndexOf(':') + 3;
106                         if (start_host >= length)
107                                 throw new ArgumentException("No host specified.");
108
109                         int startPort = uri.IndexOf(':', start_host, length - start_host);
110                         if (uri[start_host] == '[')
111                         {
112                                 startPort = uri.IndexOf("]:") + 1;
113                         }
114                         if (start_host == startPort)
115                                 throw new ArgumentException("No host specified.");
116
117                         int root = uri.IndexOf('/', start_host, length - start_host);
118                         if (root == -1)
119                                 throw new ArgumentException("No path specified.");
120
121                         if (startPort > 0)
122                         {
123                                 host = uri.Substring(start_host, startPort - start_host).Trim('[', ']');
124                                 port = UInt16.Parse(uri.Substring(startPort + 1, root - startPort - 1));
125                         }
126                         else
127                         {
128                                 host = uri.Substring(start_host, root - start_host).Trim('[', ']');
129                                 port = default_port;
130                         }
131                         path = uri.Substring(root);
132
133                         if (path.Length != 1)
134                                 path = path.Substring(0, path.Length - 1);
135                 }
136
137                 public static void CheckUri(string uri)
138                 {
139                         if (uri == null)
140                                 throw new ArgumentNullException("uriPrefix");
141
142                         if (!uri.StartsWith("http://") && !uri.StartsWith("https://"))
143                                 throw new ArgumentException("Only 'http' and 'https' schemes are supported.");
144
145                         int length = uri.Length;
146                         int start_host = uri.IndexOf(':') + 3;
147                         if (start_host >= length)
148                                 throw new ArgumentException("No host specified.");
149
150
151                         int startPort = uri.IndexOf(':', start_host, length - start_host);
152                         if (uri[start_host] == '[')
153                         {
154                                 startPort = uri.IndexOf("]:") + 1;
155                         }
156                         if (start_host == startPort)
157                                 throw new ArgumentException("No host specified.");
158                         int root = uri.IndexOf('/', start_host, length - start_host);
159                         if (root == -1)
160                                 throw new ArgumentException("No path specified.");
161
162                         if (startPort > 0)
163                         {
164                                 try
165                                 {
166                                         int p = Int32.Parse(uri.Substring(startPort + 1, root - startPort - 1));
167                                         if (p <= 0 || p >= 65536)
168                                                 throw new Exception();
169                                 }
170                                 catch
171                                 {
172                                         throw new ArgumentException("Invalid port.");
173                                 }
174                         }
175
176                         if (uri[uri.Length - 1] != '/')
177                                 throw new ArgumentException("The prefix must end with '/'");
178                 }
179         }
180 }
181 #endif
182