New test.
[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 #if NET_2_0
30 namespace System.Net {
31         sealed class ListenerPrefix
32         {
33                 string original;
34                 string host;
35                 ushort port;
36                 string path;
37                 bool secure;
38                 IPAddress [] addresses;
39                 public HttpListener Listener;
40
41                 public ListenerPrefix (string prefix)
42                 {
43                         this.original = prefix;
44                         Parse (prefix);
45                 }
46
47                 public override string ToString ()
48                 {
49                         return original;
50                 }
51
52                 public IPAddress [] Addresses {
53                         get { return addresses; }
54                         set { addresses = value; }
55                 }
56                 public bool Secure {
57                         get { return secure; }
58                 }
59
60                 public string Host {
61                         get { return host; }
62                 }
63
64                 public int Port {
65                         get { return (int) port; }
66                 }
67
68                 public string Path {
69                         get { return path; }
70                 }
71
72                 // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
73                 public override bool Equals (object o)
74                 {
75                         ListenerPrefix other = o as ListenerPrefix;
76                         if (other == null)
77                                 return false;
78
79                         return (original == other.original);
80                 }
81
82                 public override int GetHashCode ()
83                 {
84                         return original.GetHashCode ();
85                 }
86
87                 void Parse (string uri)
88                 {
89                         int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
90                         if (default_port == -1) {
91                                 default_port = (uri.StartsWith ("https://")) ? 443 : -1;
92                                 secure = true;
93                                 throw new NotSupportedException ("We don't support https yet.");
94                         }
95
96                         int length = uri.Length;
97                         int start_host = uri.IndexOf (':') + 3;
98                         if (start_host >= length)
99                                 throw new ArgumentException ("No host specified.");
100
101                         int colon = uri.IndexOf (':', start_host, length - start_host);
102                         int root;
103                         if (colon > 0) {
104                                 host = uri.Substring (start_host, colon - start_host);
105                                 root = uri.IndexOf ('/', colon, length - colon);
106                                 port = (ushort) Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
107                                 path = uri.Substring (root);
108                         } else {
109                                 root = uri.IndexOf ('/', start_host, length - start_host);
110                                 host = uri.Substring (start_host, root - start_host);
111                                 path = uri.Substring (root);
112                         }
113                 }
114
115                 public static void CheckUri (string uri)
116                 {
117                         if (uri == null)
118                                 throw new ArgumentNullException ("uriPrefix");
119
120                         int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
121                         if (default_port == -1)
122                                 default_port = (uri.StartsWith ("https://")) ? 443 : -1;
123                         if (default_port == -1)
124                                 throw new ArgumentException ("Only 'http' and 'https' schemes are supported.");
125
126                         int length = uri.Length;
127                         int start_host = uri.IndexOf (':') + 3;
128                         if (start_host >= length)
129                                 throw new ArgumentException ("No host specified.");
130
131                         int colon = uri.IndexOf (':', start_host, length - start_host);
132                         if (start_host == colon)
133                                 throw new ArgumentException ("No host specified.");
134
135                         int root;
136                         if (colon > 0) {
137                                 root = uri.IndexOf ('/', colon, length - colon);
138                                 if (root == -1)
139                                         throw new ArgumentException ("No path specified.");
140
141                                 try {
142                                         int p = Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
143                                         if (p <= 0 || p >= 65536)
144                                                 throw new Exception ();
145                                 } catch {
146                                         throw new ArgumentException ("Invalid port.");
147                                 }
148                         } else {
149                                 root = uri.IndexOf ('/', start_host, length - start_host);
150                                 if (root == -1)
151                                         throw new ArgumentException ("No path specified.");
152                         }
153
154                         if (uri [uri.Length - 1] != '/')
155                                 throw new ArgumentException ("The prefix must end with '/'");
156                 }
157         }
158 }
159 #endif
160