You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2025/02/12 15:29:49 UTC
(httpcomponents-core) 02/02: Bug fix: Corrected handling of invalid HTTP protocol version by HTTP/1.1 async client protocol handler
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch 5.3.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
commit 18751e953171e95c6e962426166d866fcfe5c274
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Wed Feb 12 16:23:21 2025 +0100
Bug fix: Corrected handling of invalid HTTP protocol version by HTTP/1.1 async client protocol handler
---
.../hc/core5/testing/nio/Http1IntegrationTest.java | 42 ++++++++++++++++++++++
.../http/impl/nio/ServerHttp1StreamHandler.java | 15 ++++----
2 files changed, 49 insertions(+), 8 deletions(-)
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
index 64001b2ac..6822c6598 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
@@ -85,11 +85,15 @@
import org.apache.hc.core5.http.impl.Http1StreamListener;
import org.apache.hc.core5.http.impl.HttpProcessors;
import org.apache.hc.core5.http.impl.nio.AbstractContentEncoder;
+import org.apache.hc.core5.http.impl.nio.AbstractMessageWriter;
import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestFactory;
import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParser;
import org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexer;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.BasicHttpResponse;
+import org.apache.hc.core5.http.message.BasicLineFormatter;
+import org.apache.hc.core5.http.message.LineFormatter;
+import org.apache.hc.core5.http.message.RequestLine;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
@@ -1955,4 +1959,42 @@ protected HttpRequest createMessage(final CharArrayBuffer buffer) throws HttpExc
Assertions.assertInstanceOf(ConnectionClosedException.class, executionException.getCause());
}
+ @Test
+ void testInvalidProtocolVersion() throws Exception {
+ final Http1TestServer server = resources.server();
+ final Http1TestClient client = resources.client();
+
+ server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
+ final InetSocketAddress serverEndpoint = server.start();
+
+ final LineFormatter lineFormatter = BasicLineFormatter.INSTANCE;
+ client.configure(() -> new AbstractMessageWriter<HttpRequest>(lineFormatter) {
+
+ @Override
+ protected void writeHeadLine(final HttpRequest message, final CharArrayBuffer lineBuf) throws IOException {
+ lineBuf.clear();
+ lineFormatter.formatRequestLine(lineBuf, new RequestLine(
+ message.getMethod(),
+ message.getRequestUri(),
+ new HttpVersion(2, 1)));
+ }
+
+ });
+ client.start();
+ final Future<ClientSessionEndpoint> connectFuture = client.connect(
+ "localhost", serverEndpoint.getPort(), TIMEOUT);
+ final ClientSessionEndpoint streamEndpoint = connectFuture.get();
+
+ final HttpRequest request = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/hello"));
+
+ final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(
+ new BasicRequestProducer(request, null),
+ new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+ final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+ Assertions.assertNotNull(result);
+ final HttpResponse response = result.getHead();
+ Assertions.assertNotNull(response);
+ Assertions.assertEquals(505, response.getCode());
+ }
+
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java
index dd0744d58..4d2fa9115 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java
@@ -253,15 +253,14 @@ void consumeHeader(final HttpRequest request, final EntityDetails requestEntityD
}
receivedRequest = request;
requestState = requestEntityDetails == null ? MessageState.COMPLETE : MessageState.BODY;
-
- final ProtocolVersion transportVersion = request.getVersion();
- if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
- throw new UnsupportedHttpVersionException(transportVersion);
- }
- context.setProtocolVersion(transportVersion != null ? transportVersion : http1Config.getVersion());
- context.setRequest(request);
-
try {
+ final ProtocolVersion transportVersion = request.getVersion();
+ if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
+ throw new UnsupportedHttpVersionException(transportVersion);
+ }
+ context.setProtocolVersion(transportVersion != null ? transportVersion : http1Config.getVersion());
+ context.setRequest(request);
+
httpProcessor.process(request, requestEntityDetails, context);
AsyncServerExchangeHandler handler;