diff --git a/urban-spork-client/src/com/urbanspork/client/ClientHttpUnificationHandler.java b/urban-spork-client/src/com/urbanspork/client/ClientHttpUnificationHandler.java index 8fa9055..23ecc90 100644 --- a/urban-spork-client/src/com/urbanspork/client/ClientHttpUnificationHandler.java +++ b/urban-spork-client/src/com/urbanspork/client/ClientHttpUnificationHandler.java @@ -28,10 +28,10 @@ public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { ctx.writeAndFlush(Unpooled.wrappedBuffer("HTTP/1.1 400 Bad Request\r\n\r\n".getBytes())).addListener(ChannelFutureListener.CLOSE); return; } - if (HttpMethod.GET == option.method()) { - new HttpRelayHandler(msg.retain()).connect(ctx.channel(), dstAddress); - } else { + if (HttpMethod.CONNECT == option.method()) { new HttpsRelayHandler().connect(ctx.channel(), dstAddress); + } else { + new HttpRelayHandler(msg.retain()).connect(ctx.channel(), dstAddress); } } diff --git a/urban-spork-common/src/com/urbanspork/common/util/HttpProxyUtil.java b/urban-spork-common/src/com/urbanspork/common/util/HttpProxyUtil.java index 6e305a8..e2174bc 100644 --- a/urban-spork-common/src/com/urbanspork/common/util/HttpProxyUtil.java +++ b/urban-spork-common/src/com/urbanspork/common/util/HttpProxyUtil.java @@ -37,7 +37,12 @@ public static Option parseOption(ByteBuf msg) { String uri = getString(msg, bStart, bEnd - bStart); String host; int port; - if (HttpMethod.GET == method) { + if (HttpMethod.CONNECT == method) { + int hEnd = uri.lastIndexOf(":"); + int pEnd = uri.length(); + host = getString(msg, bStart, hEnd); + port = getInt(msg, bStart + hEnd + 1, pEnd - hEnd - 1); + } else { int hStart = uri.indexOf("//") + 2; int hEnd = uri.lastIndexOf(":"); int hv6End = uri.lastIndexOf("]"); @@ -54,13 +59,6 @@ public static Option parseOption(ByteBuf msg) { host = getString(msg, bStart + hStart, hEnd - hStart); port = getInt(msg, bStart + pStart, pEnd - pStart); } - } else if (HttpMethod.CONNECT == method) { - int hEnd = uri.lastIndexOf(":"); - int pEnd = uri.length(); - host = getString(msg, bStart, hEnd); - port = getInt(msg, bStart + hEnd + 1, pEnd - hEnd - 1); - } else { - throw new UnsupportedOperationException("Unsupported HTTP method: " + method); } return new Option(method, InetSocketAddress.createUnresolved(host, port)); } diff --git a/urban-spork-test/test/com/urbanspork/common/util/HttpProxyUtilTest.java b/urban-spork-test/test/com/urbanspork/common/util/HttpProxyUtilTest.java index 0ca333c..3220912 100644 --- a/urban-spork-test/test/com/urbanspork/common/util/HttpProxyUtilTest.java +++ b/urban-spork-test/test/com/urbanspork/common/util/HttpProxyUtilTest.java @@ -13,16 +13,16 @@ class HttpProxyUtilTest { CONNECT www.example.com:443 HTTP/1.1 Proxy-Connection: keep-alive """, """ - GET http://www.example.com:8080/?a=b&c=d HTTP/1.1 + POST http://www.example.com:8080/?a=b&c=d HTTP/1.1 Connection: keep-alive """, """ GET http://www.example.com/?a=b&c=d HTTP/1.1 Connection: keep-alive """, """ - GET http://[::1] HTTP/1.1 + PUT http://[::1] HTTP/1.1 Connection: keep-alive """, """ - GET http://[0:0:0:0:0:0:0:1]:8080 HTTP/1.1 + OPTIONS http://[0:0:0:0:0:0:0:1]:8080 HTTP/1.1 Connection: keep-alive """}) void testParseOption(String msg) { @@ -31,15 +31,6 @@ void testParseOption(String msg) { Assertions.assertNotNull(option.address()); } - @Test - void testParseUnsupportedOption() { - ByteBuf msg = Unpooled.wrappedBuffer((""" - POST http://www.example.com HTTP/1.1 - Connection: keep-alive - """).getBytes()); - Assertions.assertThrows(UnsupportedOperationException.class, () -> HttpProxyUtil.parseOption(msg)); - } - @Test void testParseIllegalInitialLine() { String str = "GET " + (char) 10 + "http://www.example.com HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";