OSDN Git Service

HTTPプロキシがポート番号指定のリクエストを正しく扱えないのを直す
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Sat, 2 Jan 2016 08:42:56 +0000 (17:42 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Mon, 4 Jan 2016 13:31:24 +0000 (22:31 +0900)
KancolleSniffer/HttpProxy.cs

index b10b8d9..8711712 100644 (file)
@@ -166,7 +166,7 @@ namespace KancolleSniffer
             }\r
 \r
             private static readonly Regex HostAndPortRegex =\r
-                new Regex("http://([^:/]+)(?::(\\d)+)?/", RegexOptions.Compiled);\r
+                new Regex("http://([^:/]+)(?::(\\d+))?/", RegexOptions.Compiled);\r
 \r
             private Socket ConnectServer()\r
             {\r
@@ -186,16 +186,29 @@ namespace KancolleSniffer
                         port = int.Parse(m.Groups[2].Value);\r
                     _session.Request.RequestLine = _session.Request.RequestLine.Remove(m.Index, m.Length - 1);\r
                 }\r
-                if (host == null)\r
-                    host = _session.Request.Host;\r
-                if (host == null)\r
-                    throw new HttpProxyAbort("Can't find destinatio host");\r
+                if (host == null && !ParseAuthority(_session.Request.Host, ref host, ref port))\r
+                    throw new HttpProxyAbort("Can't find destination host");\r
                 connect:\r
                 var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\r
                 socket.Connect(host, port);\r
                 return socket;\r
             }\r
 \r
+            private static readonly Regex AuthorityRegex = new Regex("([^:]+)(?::(\\d+))?");\r
+\r
+            private bool ParseAuthority(string authority, ref string host, ref int port)\r
+            {\r
+                if (string.IsNullOrEmpty(authority))\r
+                    return false;\r
+                var m = AuthorityRegex.Match(authority);\r
+                if (!m.Success)\r
+                    return false;\r
+                host = m.Groups[1].Value;\r
+                if (m.Groups[2].Success)\r
+                    port = int.Parse(m.Groups[2].Value);\r
+                return true;\r
+            }\r
+\r
             private void Close()\r
             {\r
                 _serverStream?.Close();\r