2 // System.Net.ListenerPrefix
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Oleg Mihailik (mihailik gmail co_m)
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 namespace System.Net {
31 sealed class ListenerPrefix
38 IPAddress [] addresses;
39 public HttpListener Listener;
41 public ListenerPrefix (string prefix)
43 this.original = prefix;
47 public override string ToString ()
52 public IPAddress [] Addresses {
53 get { return addresses; }
54 set { addresses = value; }
57 get { return secure; }
65 get { return (int) port; }
72 // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
73 public override bool Equals (object o)
75 ListenerPrefix other = o as ListenerPrefix;
79 return (original == other.original);
82 public override int GetHashCode ()
84 return original.GetHashCode ();
87 void Parse (string uri)
89 ushort default_port = 80;
90 if (uri.StartsWith ("https://")) {
95 int length = uri.Length;
96 int start_host = uri.IndexOf (':') + 3;
97 if (start_host >= length)
98 throw new ArgumentException ("No host specified.");
100 int startPort = uri.IndexOf (':', start_host, length - start_host);
101 if (uri [start_host] == '[') {
102 startPort = uri.IndexOf ("]:") + 1;
104 if (start_host == startPort)
105 throw new ArgumentException ("No host specified.");
107 int root = uri.IndexOf ('/', start_host, length - start_host);
109 throw new ArgumentException ("No path specified.");
112 host = uri.Substring (start_host, startPort - start_host).Trim ('[', ']');
113 port = UInt16.Parse (uri.Substring (startPort + 1, root - startPort - 1));
115 host = uri.Substring (start_host, root - start_host).Trim ('[', ']');
118 path = uri.Substring (root);
120 if (path.Length != 1)
121 path = path.Substring (0, path.Length - 1);
124 public static void CheckUri (string uri)
127 throw new ArgumentNullException ("uriPrefix");
129 if (!uri.StartsWith ("http://") && !uri.StartsWith ("https://"))
130 throw new ArgumentException ("Only 'http' and 'https' schemes are supported.");
132 int length = uri.Length;
133 int start_host = uri.IndexOf (':') + 3;
134 if (start_host >= length)
135 throw new ArgumentException ("No host specified.");
137 int startPort = uri.IndexOf (':', start_host, length - start_host);
138 if (uri [start_host] == '[')
139 startPort = uri.IndexOf ("]:") + 1;
140 if (start_host == startPort)
141 throw new ArgumentException ("No host specified.");
142 int root = uri.IndexOf ('/', start_host, length - start_host);
144 throw new ArgumentException ("No path specified.");
148 int p = Int32.Parse (uri.Substring (startPort + 1, root - startPort - 1));
149 if (p <= 0 || p >= 65536)
150 throw new Exception ();
152 throw new ArgumentException ("Invalid port.");
156 if (uri [uri.Length - 1] != '/')
157 throw new ArgumentException ("The prefix must end with '/'");