Merge pull request #4781 from kumpera/unaligned-cleanup-1
[mono.git] / mcs / class / System / System.Net / ListenerPrefix.cs
index c9c1077b1e2f813582583a0731b62d46fe6e27f4..f7cb7b381458a7b3b8d621f76111e478cddc7cad 100644 (file)
@@ -27,8 +27,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if SECURITY_DEP
-
 namespace System.Net {
        sealed class ListenerPrefix
        {
@@ -99,19 +97,26 @@ namespace System.Net {
                        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);
+                               host = uri.Substring (start_host, root - start_host).Trim ('[', ']');
                                port = default_port;
-                               path = uri.Substring (root);
                        }
+                       path = uri.Substring (root);
+
                        if (path.Length != 1)
                                path = path.Substring (0, path.Length - 1);
                }
@@ -121,7 +126,7 @@ namespace System.Net {
                        if (uri == null)
                                throw new ArgumentNullException ("uriPrefix");
 
-                       if(!uri.StartsWith ("http://") && !uri.StartsWith ("https://"))
+                       if (!uri.StartsWith ("http://") && !uri.StartsWith ("https://"))
                                throw new ArgumentException ("Only 'http' and 'https' schemes are supported.");
 
                        int length = uri.Length;
@@ -129,27 +134,23 @@ namespace System.Net {
                        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] != '/')
@@ -157,5 +158,3 @@ namespace System.Net {
                }
        }
 }
-#endif
-