Skip to content

Commit

Permalink
fix(http): support all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Zmax0 committed Jul 17, 2024
1 parent 33e744c commit 25732df
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("]");
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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";
Expand Down

0 comments on commit 25732df

Please sign in to comment.