f857bab573c57aa375f55b3a11480b924c191ce9
[mono.git] / mcs / class / System.Runtime.Remoting / MonoHttp / ListenerPrefix.cs
1 #define EMBEDDED_IN_1_0
2
3 //
4 // System.Net.ListenerPrefix
5 //
6 // Author:
7 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
8 //      Oleg Mihailik (mihailik gmail co_m)
9 //
10 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 #if EMBEDDED_IN_1_0
33
34 using System; using System.Net; namespace MonoHttp {
35         sealed class ListenerPrefix
36         {
37                 string original;
38                 string host;
39                 ushort port;
40                 string path;
41                 bool secure;
42                 IPAddress [] addresses;
43                 public HttpListener Listener;
44
45                 public ListenerPrefix (string prefix)
46                 {
47                         this.original = prefix;
48                         Parse (prefix);
49                 }
50
51                 public override string ToString ()
52                 {
53                         return original;
54                 }
55
56                 public IPAddress [] Addresses {
57                         get { return addresses; }
58                         set { addresses = value; }
59                 }
60                 public bool Secure {
61                         get { return secure; }
62                 }
63
64                 public string Host {
65                         get { return host; }
66                 }
67
68                 public int Port {
69                         get { return (int) port; }
70                 }
71
72                 public string Path {
73                         get { return path; }
74                 }
75
76                 // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
77                 public override bool Equals (object o)
78                 {
79                         ListenerPrefix other = o as ListenerPrefix;
80                         if (other == null)
81                                 return false;
82
83                         return (original == other.original);
84                 }
85
86                 public override int GetHashCode ()
87                 {
88                         return original.GetHashCode ();
89                 }
90
91                 void Parse (string uri)
92                 {
93                         int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
94                         if (default_port == -1) {
95                                 default_port = (uri.StartsWith ("https://")) ? 443 : -1;
96                                 secure = true;
97                         }
98
99                         int length = uri.Length;
100                         int start_host = uri.IndexOf (':') + 3;
101                         if (start_host >= length)
102                                 throw new ArgumentException ("No host specified.");
103
104                         int colon = uri.IndexOf (':', start_host, length - start_host);
105                         int root;
106                         if (colon > 0) {
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);
111                         } else {
112                                 root = uri.IndexOf ('/', start_host, length - start_host);
113                                 host = uri.Substring (start_host, root - start_host);
114                                 path = uri.Substring (root);
115                         }
116                 }
117
118                 public static void CheckUri (string uri)
119                 {
120                         if (uri == null)
121                                 throw new ArgumentNullException ("uriPrefix");
122
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.");
128
129                         int length = uri.Length;
130                         int start_host = uri.IndexOf (':') + 3;
131                         if (start_host >= length)
132                                 throw new ArgumentException ("No host specified.");
133
134                         int colon = uri.IndexOf (':', start_host, length - start_host);
135                         if (start_host == colon)
136                                 throw new ArgumentException ("No host specified.");
137
138                         int root;
139                         if (colon > 0) {
140                                 root = uri.IndexOf ('/', colon, length - colon);
141                                 if (root == -1)
142                                         throw new ArgumentException ("No path specified.");
143
144                                 try {
145                                         int p = Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
146                                         if (p <= 0 || p >= 65536)
147                                                 throw new Exception ();
148                                 } catch {
149                                         throw new ArgumentException ("Invalid port.");
150                                 }
151                         } else {
152                                 root = uri.IndexOf ('/', start_host, length - start_host);
153                                 if (root == -1)
154                                         throw new ArgumentException ("No path specified.");
155                         }
156
157                         if (uri [uri.Length - 1] != '/')
158                                 throw new ArgumentException ("The prefix must end with '/'");
159                 }
160         }
161 }
162 #endif
163