//
// Author:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
+// Oleg Mihailik (mihailik gmail co_m)
//
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
//
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-#if NET_2_0
+
namespace System.Net {
sealed class ListenerPrefix
{
void Parse (string uri)
{
- int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
- if (default_port == -1) {
- default_port = (uri.StartsWith ("https://")) ? 443 : -1;
+ ushort default_port = 80;
+ if (uri.StartsWith ("https://")) {
+ default_port = 443;
secure = true;
- throw new NotSupportedException ("We don't support https yet.");
}
int length = uri.Length;
if (start_host >= length)
throw new ArgumentException ("No host specified.");
- int colon = uri.IndexOf (':', start_host, length - start_host);
- int root;
- if (colon > 0) {
- host = uri.Substring (start_host, colon - start_host);
- root = uri.IndexOf ('/', colon, length - colon);
- port = (ushort) Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
- path = uri.Substring (root);
+ int startPort = uri.IndexOf (':', start_host, length - start_host);
+ if (uri [start_host] == '[') {
+ startPort = uri.IndexOf ("]:") + 1;
+ }
+ if (start_host == startPort)
+ throw new ArgumentException ("No host specified.");
+
+ int root = uri.IndexOf ('/', start_host, length - start_host);
+ if (root == -1)
+ throw new ArgumentException ("No path specified.");
+
+ if (startPort > 0) {
+ host = uri.Substring (start_host, startPort - start_host).Trim ('[', ']');
+ port = UInt16.Parse (uri.Substring (startPort + 1, root - startPort - 1));
} else {
- root = uri.IndexOf ('/', start_host, length - start_host);
- host = uri.Substring (start_host, root - start_host);
- path = uri.Substring (root);
+ host = uri.Substring (start_host, root - start_host).Trim ('[', ']');
+ port = default_port;
}
+ path = uri.Substring (root);
+
+ if (path.Length != 1)
+ path = path.Substring (0, path.Length - 1);
}
public static void CheckUri (string uri)
if (uri == null)
throw new ArgumentNullException ("uriPrefix");
- int default_port = (uri.StartsWith ("http://")) ? 80 : -1;
- if (default_port == -1)
- default_port = (uri.StartsWith ("https://")) ? 443 : -1;
- if (default_port == -1)
+ if (!uri.StartsWith ("http://") && !uri.StartsWith ("https://"))
throw new ArgumentException ("Only 'http' and 'https' schemes are supported.");
int length = uri.Length;
if (start_host >= length)
throw new ArgumentException ("No host specified.");
- int colon = uri.IndexOf (':', start_host, length - start_host);
- if (start_host == colon)
+ int startPort = uri.IndexOf (':', start_host, length - start_host);
+ if (uri [start_host] == '[')
+ startPort = uri.IndexOf ("]:") + 1;
+ if (start_host == startPort)
throw new ArgumentException ("No host specified.");
+ int root = uri.IndexOf ('/', start_host, length - start_host);
+ if (root == -1)
+ throw new ArgumentException ("No path specified.");
- int root;
- if (colon > 0) {
- root = uri.IndexOf ('/', colon, length - colon);
- if (root == -1)
- throw new ArgumentException ("No path specified.");
-
+ if (startPort > 0) {
try {
- int p = Int32.Parse (uri.Substring (colon + 1, root - colon - 1));
+ int p = Int32.Parse (uri.Substring (startPort + 1, root - startPort - 1));
if (p <= 0 || p >= 65536)
throw new Exception ();
} catch {
throw new ArgumentException ("Invalid port.");
}
- } else {
- root = uri.IndexOf ('/', start_host, length - start_host);
- if (root == -1)
- throw new ArgumentException ("No path specified.");
}
if (uri [uri.Length - 1] != '/')
}
}
}
-#endif
-