Skip to content

Commit

Permalink
Provide a request to the Router tunnel method.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Aug 22, 2023
1 parent 7137ade commit b3488d1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
8 changes: 4 additions & 4 deletions rawhttp-core/src/main/java/rawhttp/core/server/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ default Optional<RawHttpResponse<Void>> continueResponse(RequestLine requestLine
}

/**
* Tunnel the client to the provided URI.
* Tunnel the client as asked by a CONNECT HTTP request.
* <p>
* This method is called when a client requests to CONNECT to another location.
* By default, the client is closed and an {@link UnsupportedOperationException} is thrown.
* <p>
* This method is called from a request Thread, so it's advisable that implementations that
* support tunneling fork the handling to a different Thread immediately.
* <p>
* See <a href="https://www.rfc-editor.org/rfc/rfc9110#CONNECT">RFC-9110 Section 9.3.6</a>.
*
* @param client requesting tunneling.
* @param request the CONNECT request
* @param client requesting tunneling.
* @throws IOException if an IO problem occurs
*/
default void tunnel(Socket client) throws IOException {
default void tunnel(RawHttpRequest request, Socket client) throws IOException {
client.close();
throw new UnsupportedOperationException("CONNECT request is not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private void handle(Socket client, ServerSocket serverSocket) {
}
if (request.getMethod().equalsIgnoreCase("CONNECT") &&
response.getStartLine().isSuccess()) {
router.tunnel(client);
router.tunnel(request, client);
break; // now it's between the client and the router
}
} catch (SocketTimeoutException e) {
Expand Down
4 changes: 4 additions & 0 deletions samples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ test {
enabled project.hasProperty('run-samples')
}

compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}

dependencies {
implementation project(':rawhttp-core')
implementation project(':rawhttp-httpcomponents')
Expand Down
25 changes: 15 additions & 10 deletions samples/src/test/kotlin/rawhttp/samples/http-connect-sample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ object TunnelingRouter : Router {
}

// the server lets us know if we need to start a tunnel for the client
override fun tunnel(client: Socket) {
// in this example, we only read 32 bytes
val input = ByteArray(32)
client.getInputStream().read(input)
// revert the bytes and send it back
input.reverse()
client.getOutputStream().use {
it.write(input)
}
override fun tunnel(request: RawHttpRequest, client: Socket) {
// always free the request thread by handling the tunnel on another Thread
Thread {
// in this example, we only read 32 bytes
val input = ByteArray(32)
client.getInputStream().read(input)
// revert the bytes and send it back
input.reverse()
client.getOutputStream().use {
it.write(input)
}
}.start()
}
}

Expand Down Expand Up @@ -92,7 +95,9 @@ class HttpConnectSample {
clientSocket!!.getOutputStream().write((1..32).map { it.toByte() }.toByteArray())

// the server should reverse the message as that's what the Router implementation does
val tunnelResponse = clientSocket!!.getInputStream().readAllBytes()
val tunnelResponse = ByteArray(32).apply {
clientSocket!!.getInputStream().read(this)
}

tunnelResponse.toList() shouldContainExactly (32 downTo 1).map { it.toByte() }
}
Expand Down

0 comments on commit b3488d1

Please sign in to comment.