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 #if NET_2_0 && SECURITY_DEP
32 namespace System.Net {
33 sealed class ListenerPrefix
40 IPAddress [] addresses;
41 public HttpListener Listener;
43 public ListenerPrefix (string prefix)
45 this.original = prefix;
49 public override string ToString ()
54 public IPAddress [] Addresses {
55 get { return addresses; }
56 set { addresses = value; }
59 get { return secure; }
67 get { return (int) port; }
74 // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
75 public override bool Equals (object o)
77 ListenerPrefix other = o as ListenerPrefix;
81 return (original == other.original);
84 public override int GetHashCode ()
86 return original.GetHashCode ();
89 void Parse (string uri)
91 int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
92 if (default_port == -1) {
93 default_port = (uri.StartsWith ("https://")) ? 443 : -1;
97 int length = uri.Length;
98 int start_host = uri.IndexOf (':') + 3;
99 if (start_host >= length)
100 throw new ArgumentException ("No host specified.");
102 int colon = uri.IndexOf (':', start_host, length - start_host);
105 host = uri.Substring (start_host, colon - start_host);
106 root = uri.IndexOf ('/', colon, length - colon);
107 port = (ushort) Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
108 path = uri.Substring (root);
110 root = uri.IndexOf ('/', start_host, length - start_host);
111 host = uri.Substring (start_host, root - start_host);
112 path = uri.Substring (root);
114 if (path.Length != 1)
115 path = path.Substring (0, path.Length - 1);
118 public static void CheckUri (string uri)
121 throw new ArgumentNullException ("uriPrefix");
123 int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
124 if (default_port == -1)
125 default_port = (uri.StartsWith ("https://")) ? 443 : -1;
126 if (default_port == -1)
127 throw new ArgumentException ("Only 'http' and 'https' schemes are supported.");
129 int length = uri.Length;
130 int start_host = uri.IndexOf (':') + 3;
131 if (start_host >= length)
132 throw new ArgumentException ("No host specified.");
134 int colon = uri.IndexOf (':', start_host, length - start_host);
135 if (start_host == colon)
136 throw new ArgumentException ("No host specified.");
140 root = uri.IndexOf ('/', colon, length - colon);
142 throw new ArgumentException ("No path specified.");
145 int p = Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
146 if (p <= 0 || p >= 65536)
147 throw new Exception ();
149 throw new ArgumentException ("Invalid port.");
152 root = uri.IndexOf ('/', start_host, length - start_host);
154 throw new ArgumentException ("No path specified.");
157 if (uri [uri.Length - 1] != '/')
158 throw new ArgumentException ("The prefix must end with '/'");