From 4bd056dd7ee7b0dbcf8cb14babf8d9e494a68680 Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Mon, 23 Nov 2026 17:32:18 +0100 Subject: [PATCH 1/2] Add a wire-level test of the Expect: 100-continue handshake (#363) The HTTP server connector answers "Expect: 100-continue" with an interim "100 Continue" response before the client sends the body. Nothing pinned this behavior; the new test checks it with a raw socket: the interim response comes before the body, a client that sends the body immediately is still served, and no interim response is sent without the Expect header. Test only, no production code change. Co-Authored-By: Claude Sonnet 5.5 --- .../connector/ExpectContinueTestCase.java | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java diff --git a/org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java b/org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java new file mode 100644 index 0000000000..81ecdc54be --- /dev/null +++ b/org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java @@ -0,0 +1,156 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * The content of this file is subject to the terms of the Apache 2.0 open + * source license available at https://www.opensource.org/licenses/apache-2.0 + *

+ * Restlet is a registered trademark of QlikTech International AB. + */ +package org.restlet.engine.connector; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.restlet.Context; +import org.restlet.Request; +import org.restlet.Response; +import org.restlet.Restlet; +import org.restlet.Server; +import org.restlet.data.MediaType; +import org.restlet.data.Protocol; +import org.restlet.data.Status; +import org.restlet.engine.Engine; + +/** + * Tests, on the wire, the "Expect: 100-continue" handshake of the HTTP server connector (#363): the + * server sends the interim "100 Continue" response before the client sends the body, then the final + * response. + */ +class ExpectContinueTestCase { + + private static final int TIMEOUT_MS = 10_000; + + private static final String HEAD = + "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n" + + "Connection: close\r\n"; + + private Server server; + + @BeforeEach + void setUp() throws Exception { + Engine.clearThreadLocalVariables(); + Engine.register(false); + Engine.getInstance().getRegisteredServers().add(new HttpServerHelper(null)); + + server = + new Server( + new Context(), + Protocol.HTTP, + 0, + new Restlet() { + @Override + public void handle(Request request, Response response) { + try { + String body = request.getEntity().getText(); + response.setEntity("echo:" + body, MediaType.TEXT_PLAIN); + } catch (IOException e) { + response.setStatus(Status.SERVER_ERROR_INTERNAL, e); + } + } + }); + server.start(); + } + + @AfterEach + void tearDown() throws Exception { + if (server != null) { + server.stop(); + } + Engine.clearThreadLocalVariables(); + } + + /** Reads up to the end of the next response head (the blank line), without waiting for EOF. */ + private static String readHead(InputStream in) throws IOException { + ByteArrayOutputStream head = new ByteArrayOutputStream(); + int n; + while ((n = in.read()) != -1) { + head.write(n); + String text = head.toString(StandardCharsets.ISO_8859_1); + if (text.endsWith("\r\n\r\n")) { + break; + } + } + return head.toString(StandardCharsets.ISO_8859_1); + } + + /** Reads until the server closes the connection ("Connection: close"). */ + private static String readToEnd(InputStream in) throws IOException { + return new String(in.readAllBytes(), StandardCharsets.ISO_8859_1); + } + + private static void send(Socket socket, String text) throws IOException { + OutputStream out = socket.getOutputStream(); + out.write(text.getBytes(StandardCharsets.ISO_8859_1)); + out.flush(); + } + + private Socket connect() throws IOException { + Socket socket = new Socket("localhost", server.getActualPort()); + socket.setSoTimeout(TIMEOUT_MS); + return socket; + } + + @Test + void expectContinue_isAnsweredByAnInterimResponseBeforeTheBodyIsSent() throws Exception { + try (Socket socket = connect()) { + // The headers only: the client waits for the server's go-ahead before sending the body + send(socket, HEAD + "Expect: 100-continue\r\n\r\n"); + + String interim = readHead(socket.getInputStream()); + assertTrue(interim.startsWith("HTTP/1.1 100 Continue"), interim); + + send(socket, "hello"); + String finalResponse = readToEnd(socket.getInputStream()); + assertTrue(finalResponse.startsWith("HTTP/1.1 200"), finalResponse); + assertTrue(finalResponse.endsWith("echo:hello"), finalResponse); + } + } + + @Test + void expectContinue_withTheBodySentImmediately_isStillHandled() throws Exception { + try (Socket socket = connect()) { + // A client that does not wait for the go-ahead: the interim response is optional + send(socket, HEAD + "Expect: 100-continue\r\n\r\nhello"); + + String response = readToEnd(socket.getInputStream()); + String finalResponse = + response.startsWith("HTTP/1.1 100") + ? response.substring(response.indexOf("\r\n\r\n") + 4) + : response; + assertTrue(finalResponse.startsWith("HTTP/1.1 200"), response); + assertTrue(finalResponse.endsWith("echo:hello"), response); + } + } + + @Test + void withoutExpect_noInterimResponseIsSent() throws Exception { + try (Socket socket = connect()) { + send(socket, HEAD + "\r\nhello"); + + String response = readToEnd(socket.getInputStream()); + assertTrue(response.startsWith("HTTP/1.1 200"), response); + assertFalse(response.contains("100 Continue"), response); + assertEquals("echo:hello", response.substring(response.indexOf("\r\n\r\n") + 4)); + } + } +} From 99bea8784ff9b51a2a11c42f4fe1c15e2f3e31f1 Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Mon, 23 Nov 2026 17:45:15 +0100 Subject: [PATCH 2/2] Fix the Sonar issue reported on the Expect: 100-continue test Use a text block for the request head (S6126), converted to CRLF line endings. No behavior change. Co-Authored-By: Claude Sonnet 5.5 --- .../engine/connector/ExpectContinueTestCase.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java b/org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java index 81ecdc54be..9396a8f094 100644 --- a/org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java +++ b/org.restlet/src/test/java/org/restlet/engine/connector/ExpectContinueTestCase.java @@ -40,9 +40,16 @@ class ExpectContinueTestCase { private static final int TIMEOUT_MS = 10_000; + /** The request line and headers, with the CRLF line endings required by HTTP. */ private static final String HEAD = - "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n" - + "Connection: close\r\n"; + """ + POST / HTTP/1.1 + Host: localhost + Content-Type: text/plain + Content-Length: 5 + Connection: close + """ + .replace("\n", "\r\n"); private Server server;