You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ab...@apache.org on 2023/09/30 20:14:18 UTC
[httpcomponents-core] branch 5.3.x updated: HTTPCORE-756: server-side request / response protocol conformance checks (#434)
This is an automated email from the ASF dual-hosted git repository.
abernal pushed a commit to branch 5.3.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/5.3.x by this push:
new 02a3e04db HTTPCORE-756: server-side request / response protocol conformance checks (#434)
02a3e04db is described below
commit 02a3e04dbdca870f786af70996c24d53225310c5
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Sep 30 22:14:12 2023 +0200
HTTPCORE-756: server-side request / response protocol conformance checks (#434)
---
.../apache/hc/core5/http2/impl/H2Processors.java | 8 ++-
.../http2/impl/nio/ServerH2StreamHandler.java | 1 -
.../apache/hc/core5/http/impl/HttpProcessors.java | 12 ++--
.../apache/hc/core5/http/impl/ServerSupport.java | 16 -----
.../apache/hc/core5/http/impl/io/HttpService.java | 1 -
.../http/impl/nio/ServerHttp1StreamHandler.java | 1 -
...stValidateHost.java => RequestConformance.java} | 44 ++++++------
.../core5/http/protocol/RequestValidateHost.java | 2 +
...{ResponseDate.java => ResponseConformance.java} | 26 ++++---
.../core5/http/protocol/ResponseConnControl.java | 2 +
.../hc/core5/http/protocol/ResponseContent.java | 2 +
.../hc/core5/http/protocol/ResponseDate.java | 2 +
.../http/protocol/TestStandardInterceptors.java | 83 ++++++++++++++++++++++
13 files changed, 140 insertions(+), 60 deletions(-)
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/H2Processors.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/H2Processors.java
index e1969a62f..b911dc94e 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/H2Processors.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/H2Processors.java
@@ -29,8 +29,10 @@ package org.apache.hc.core5.http2.impl;
import org.apache.hc.core5.http.impl.HttpProcessors;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.http.protocol.HttpProcessorBuilder;
+import org.apache.hc.core5.http.protocol.RequestConformance;
import org.apache.hc.core5.http.protocol.RequestExpectContinue;
import org.apache.hc.core5.http.protocol.RequestUserAgent;
+import org.apache.hc.core5.http.protocol.ResponseConformance;
import org.apache.hc.core5.http.protocol.ResponseDate;
import org.apache.hc.core5.http.protocol.ResponseServer;
import org.apache.hc.core5.http2.protocol.H2RequestConnControl;
@@ -52,13 +54,15 @@ public final class H2Processors {
public static HttpProcessorBuilder customServer(final String serverInfo) {
return HttpProcessorBuilder.create()
.addAll(
- new ResponseDate(),
+ ResponseConformance.INSTANCE,
+ ResponseDate.INSTANCE,
new ResponseServer(!TextUtils.isBlank(serverInfo) ? serverInfo :
VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", H2Processors.class)),
H2ResponseContent.INSTANCE,
H2ResponseConnControl.INSTANCE)
.addAll(
- H2RequestValidateHost.INSTANCE);
+ H2RequestValidateHost.INSTANCE,
+ RequestConformance.INSTANCE);
}
public static HttpProcessor server(final String serverInfo) {
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java
index 27446e80b..f7326e7c1 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java
@@ -123,7 +123,6 @@ class ServerH2StreamHandler implements H2StreamHandler {
@Override
public void sendResponse(
final HttpResponse response, final EntityDetails responseEntityDetails, final HttpContext httpContext) throws HttpException, IOException {
- ServerSupport.validateResponse(response, responseEntityDetails);
commitResponse(response, responseEntityDetails);
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/HttpProcessors.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/HttpProcessors.java
index 142a33c0d..49616b7ed 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/HttpProcessors.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/HttpProcessors.java
@@ -28,12 +28,14 @@ package org.apache.hc.core5.http.impl;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.http.protocol.HttpProcessorBuilder;
+import org.apache.hc.core5.http.protocol.RequestConformance;
import org.apache.hc.core5.http.protocol.RequestConnControl;
import org.apache.hc.core5.http.protocol.RequestContent;
import org.apache.hc.core5.http.protocol.RequestExpectContinue;
import org.apache.hc.core5.http.protocol.RequestTargetHost;
import org.apache.hc.core5.http.protocol.RequestUserAgent;
import org.apache.hc.core5.http.protocol.RequestValidateHost;
+import org.apache.hc.core5.http.protocol.ResponseConformance;
import org.apache.hc.core5.http.protocol.ResponseConnControl;
import org.apache.hc.core5.http.protocol.ResponseContent;
import org.apache.hc.core5.http.protocol.ResponseDate;
@@ -60,13 +62,15 @@ public final class HttpProcessors {
public static HttpProcessorBuilder customServer(final String serverInfo) {
return HttpProcessorBuilder.create()
.addAll(
- new ResponseDate(),
+ ResponseConformance.INSTANCE,
+ ResponseDate.INSTANCE,
new ResponseServer(!TextUtils.isBlank(serverInfo) ? serverInfo :
VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", HttpProcessors.class)),
- new ResponseContent(),
- new ResponseConnControl())
+ ResponseContent.INSTANCE,
+ ResponseConnControl.INSTANCE)
.addAll(
- new RequestValidateHost());
+ RequestValidateHost.INSTANCE,
+ RequestConformance.INSTANCE);
}
/**
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/ServerSupport.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/ServerSupport.java
index 85b1ee058..137923e4d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/ServerSupport.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/ServerSupport.java
@@ -27,9 +27,6 @@
package org.apache.hc.core5.http.impl;
import org.apache.hc.core5.annotation.Internal;
-import org.apache.hc.core5.http.EntityDetails;
-import org.apache.hc.core5.http.HttpException;
-import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.MethodNotSupportedException;
import org.apache.hc.core5.http.MisdirectedRequestException;
@@ -46,19 +43,6 @@ import org.apache.hc.core5.http.UnsupportedHttpVersionException;
@Internal
public class ServerSupport {
- public static void validateResponse(
- final HttpResponse response,
- final EntityDetails responseEntityDetails) throws HttpException {
- final int status = response.getCode();
- switch (status) {
- case HttpStatus.SC_NO_CONTENT:
- case HttpStatus.SC_NOT_MODIFIED:
- if (responseEntityDetails != null) {
- throw new HttpException("Response " + status + " must not enclose an entity");
- }
- }
- }
-
public static String toErrorMessage(final Exception ex) {
final String message = ex.getMessage();
return message != null ? message : ex.toString();
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java
index 3a7c18218..7ae6c0cca 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java
@@ -215,7 +215,6 @@ public class HttpService {
if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
throw new UnsupportedHttpVersionException(transportVersion);
}
- ServerSupport.validateResponse(response, response.getEntity());
context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
processor.process(response, response.getEntity(), context);
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 65b4e4cf5..0bc6b0112 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
@@ -125,7 +125,6 @@ class ServerHttp1StreamHandler implements ResourceHolder {
@Override
public void sendResponse(
final HttpResponse response, final EntityDetails responseEntityDetails, final HttpContext httpContext) throws HttpException, IOException {
- ServerSupport.validateResponse(response, responseEntityDetails);
commitResponse(response, responseEntityDetails);
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestConformance.java
similarity index 62%
copy from httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
copy to httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestConformance.java
index 1cf2d1c69..b81b4cc8b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestConformance.java
@@ -28,33 +28,31 @@
package org.apache.hc.core5.http.protocol;
import java.io.IOException;
-import java.net.URISyntaxException;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.EntityDetails;
-import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
-import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpRequestInterceptor;
-import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.ProtocolException;
-import org.apache.hc.core5.http.ProtocolVersion;
+import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.TextUtils;
/**
- * RequestTargetHost is responsible for copying {@code Host} header value to
- * {@link HttpRequest#setAuthority(URIAuthority)} of the incoming message.
- * This interceptor is required for server side protocol processors.
+ * ResponseConformance is responsible for making the protocol conformance checks
+ * of incoming request messages.
*
- * @since 5.0
+ * @since 5.3
*/
@Contract(threading = ThreadingBehavior.IMMUTABLE)
-public class RequestValidateHost implements HttpRequestInterceptor {
+public class RequestConformance implements HttpRequestInterceptor {
- public RequestValidateHost() {
+ public static final RequestConformance INSTANCE = new RequestConformance();
+
+ public RequestConformance() {
super();
}
@@ -63,19 +61,17 @@ public class RequestValidateHost implements HttpRequestInterceptor {
throws HttpException, IOException {
Args.notNull(request, "HTTP request");
- final Header header = request.getHeader(HttpHeaders.HOST);
- if (header != null) {
- final URIAuthority authority;
- try {
- authority = URIAuthority.create(header.getValue());
- } catch (final URISyntaxException ex) {
- throw new ProtocolException(ex.getMessage(), ex);
- }
- request.setAuthority(authority);
- } else {
- final ProtocolVersion version = request.getVersion() != null ? request.getVersion() : HttpVersion.HTTP_1_1;
- if (version.greaterEquals(HttpVersion.HTTP_1_1)) {
- throw new ProtocolException("Host header is absent");
+ if (TextUtils.isBlank(request.getScheme())) {
+ throw new ProtocolException("Request scheme is not set");
+ }
+ if (TextUtils.isBlank(request.getPath())) {
+ throw new ProtocolException("Request path is not set");
+ }
+ final URIAuthority authority = request.getAuthority();
+ if (authority != null && (URIScheme.HTTP.same(request.getScheme()) || URIScheme.HTTPS.same(request.getScheme()))) {
+ final String hostName = authority.getHostName();
+ if (TextUtils.isBlank(hostName)) {
+ throw new ProtocolException("Request host is empty");
}
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
index 1cf2d1c69..39fed61df 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
@@ -54,6 +54,8 @@ import org.apache.hc.core5.util.Args;
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class RequestValidateHost implements HttpRequestInterceptor {
+ public static final RequestValidateHost INSTANCE = new RequestValidateHost();
+
public RequestValidateHost() {
super();
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseDate.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseConformance.java
similarity index 73%
copy from httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseDate.java
copy to httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseConformance.java
index 05998fcc3..d70724f7b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseDate.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseConformance.java
@@ -33,23 +33,24 @@ import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.HttpException;
-import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpResponseInterceptor;
import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.util.Args;
/**
- * ResponseDate is responsible for adding {@code Date} header to the
- * outgoing responses. This interceptor is recommended for server side protocol
- * processors.
+ * ResponseConformance is responsible for making the protocol conformance checks
+ * of outgoing response messages.
*
- * @since 4.0
+ * @since 5.3
*/
-@Contract(threading = ThreadingBehavior.SAFE)
-public class ResponseDate implements HttpResponseInterceptor {
+@Contract(threading = ThreadingBehavior.IMMUTABLE)
+public class ResponseConformance implements HttpResponseInterceptor {
- public ResponseDate() {
+ public static final ResponseConformance INSTANCE = new ResponseConformance();
+
+ public ResponseConformance() {
super();
}
@@ -58,9 +59,12 @@ public class ResponseDate implements HttpResponseInterceptor {
throws HttpException, IOException {
Args.notNull(response, "HTTP response");
final int status = response.getCode();
- if ((status >= HttpStatus.SC_OK) &&
- !response.containsHeader(HttpHeaders.DATE)) {
- response.setHeader(HttpHeaders.DATE, HttpDateGenerator.INSTANCE.getCurrentDate());
+ switch (status) {
+ case HttpStatus.SC_NO_CONTENT:
+ case HttpStatus.SC_NOT_MODIFIED:
+ if (entity != null) {
+ throw new ProtocolException("Response " + status + " must not enclose an entity");
+ }
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseConnControl.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseConnControl.java
index f26c4732d..305c0517b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseConnControl.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseConnControl.java
@@ -57,6 +57,8 @@ import org.apache.hc.core5.util.Args;
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class ResponseConnControl implements HttpResponseInterceptor {
+ public static final ResponseConnControl INSTANCE = new ResponseConnControl();
+
public ResponseConnControl() {
super();
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseContent.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseContent.java
index ba7083a49..b560a740b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseContent.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseContent.java
@@ -57,6 +57,8 @@ import org.apache.hc.core5.util.Args;
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class ResponseContent implements HttpResponseInterceptor {
+ public static final ResponseContent INSTANCE = new ResponseContent();
+
private final boolean overwrite;
/**
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseDate.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseDate.java
index 05998fcc3..7a841e265 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseDate.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/ResponseDate.java
@@ -49,6 +49,8 @@ import org.apache.hc.core5.util.Args;
@Contract(threading = ThreadingBehavior.SAFE)
public class ResponseDate implements HttpResponseInterceptor {
+ public static final ResponseDate INSTANCE = new ResponseDate();
+
public ResponseDate() {
super();
}
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java b/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java
index 7e00f9556..c17620f18 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java
@@ -1009,4 +1009,87 @@ public class TestStandardInterceptors {
Assertions.assertThrows(ProtocolException.class, () ->
interceptor.process(request, request.getEntity(), context));
}
+
+ @Test
+ public void testRequestConformance() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
+ request.setScheme("http");
+ request.setAuthority(new URIAuthority("somehost", 8888));
+ request.setPath("/path");
+ final RequestConformance interceptor = new RequestConformance();
+ interceptor.process(request, request.getEntity(), context);
+ }
+
+ @Test
+ public void testRequestConformanceSchemeMissing() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
+ request.setAuthority(new URIAuthority("somehost", 8888));
+ request.setPath("/path");
+ final RequestConformance interceptor = new RequestConformance();
+ Assertions.assertThrows(ProtocolException.class, () ->
+ interceptor.process(request, request.getEntity(), context));
+ }
+
+ @Test
+ public void testRequestConformancePathMissing() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
+ request.setScheme("http");
+ request.setAuthority(new URIAuthority("somehost", 8888));
+ request.setPath("");
+ final RequestConformance interceptor = new RequestConformance();
+ Assertions.assertThrows(ProtocolException.class, () ->
+ interceptor.process(request, request.getEntity(), context));
+ }
+
+ @Test
+ public void testRequestConformanceHostMissing() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
+ request.setScheme("http");
+ request.setAuthority(new URIAuthority("", -1));
+ request.setPath("/path");
+ final RequestConformance interceptor = new RequestConformance();
+ Assertions.assertThrows(ProtocolException.class, () ->
+ interceptor.process(request, request.getEntity(), context));
+ }
+
+ @Test
+ public void testResponseConformanceNoContent() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content");
+ final ResponseConformance interceptor = new ResponseConformance();
+ interceptor.process(response, response.getEntity(), context);
+ }
+
+ @Test
+ public void testResponseConformanceNotModified() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
+ final ResponseConformance interceptor = new ResponseConformance();
+ interceptor.process(response, response.getEntity(), context);
+ }
+
+ @Test
+ public void testResponseConformanceNoContentWithEntity() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content");
+ response.setEntity(new StringEntity("stuff"));
+ final ResponseConformance interceptor = new ResponseConformance();
+ Assertions.assertThrows(ProtocolException.class, () ->
+ interceptor.process(response, response.getEntity(), context));
+ }
+
+ @Test
+ public void testResponseConformanceNotModifiedWithEntity() throws Exception {
+ final HttpContext context = new BasicHttpContext(null);
+ final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
+ response.setEntity(new StringEntity("stuff"));
+ final ResponseConformance interceptor = new ResponseConformance();
+ Assertions.assertThrows(ProtocolException.class, () ->
+ interceptor.process(response, response.getEntity(), context));
+ }
+
}