1 #define EMBEDDED_IN_1_0
4 // System.Net.ListenerPrefix
7 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Oleg Mihailik (mihailik gmail co_m)
10 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System; using System.Net; namespace MonoHttp {
35 sealed class ListenerPrefix
42 IPAddress [] addresses;
43 public HttpListener Listener;
45 public ListenerPrefix (string prefix)
47 this.original = prefix;
51 public override string ToString ()
56 public IPAddress [] Addresses {
57 get { return addresses; }
58 set { addresses = value; }
61 get { return secure; }
69 get { return (int) port; }
76 // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
77 public override bool Equals (object o)
79 ListenerPrefix other = o as ListenerPrefix;
83 return (original == other.original);
86 public override int GetHashCode ()
88 return original.GetHashCode ();
91 void Parse (string uri)
93 int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
94 if (default_port == -1) {
95 default_port = (uri.StartsWith ("https://")) ? 443 : -1;
99 int length = uri.Length;
100 int start_host = uri.IndexOf (':') + 3;
101 if (start_host >= length)
102 throw new ArgumentException ("No host specified.");
104 int colon = uri.IndexOf (':', start_host, length - start_host);
107 host = uri.Substring (start_host, colon - start_host);
108 root = uri.IndexOf ('/', colon, length - colon);
109 port = (ushort) Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
110 path = uri.Substring (root);
112 root = uri.IndexOf ('/', start_host, length - start_host);
113 host = uri.Substring (start_host, root - start_host);
114 path = uri.Substring (root);
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 '/'");