Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions java/org/apache/coyote/http2/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.coyote.http2;

import java.util.Set;

/**
* HTTP/2 protocol constants.
*/
Expand Down Expand Up @@ -61,6 +63,13 @@ public class Constants {
*/
static final int DEFAULT_MAX_TRAILER_SIZE = 8 * 1024;

// Header fields
/**
* Connection-specific header fields. RFC 9113, section 8.2.2 forbids them in HTTP/2 messages.
*/
static final Set<String> CONNECTION_SPECIFIC_HEADERS =
Set.of("connection", "proxy-connection", "keep-alive", "transfer-encoding", "upgrade");

/**
* Private constructor to prevent instantiation.
*/
Expand Down
12 changes: 1 addition & 11 deletions java/org/apache/coyote/http2/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -70,19 +68,11 @@ class Stream extends AbstractNonZeroStream implements HeaderEmitter {

private static final Integer HTTP_UPGRADE_STREAM = Integer.valueOf(1);

private static final Set<String> HTTP_CONNECTION_SPECIFIC_HEADERS = new HashSet<>();

static {
Response response = new Response();
response.setStatus(100);
StreamProcessor.prepareHeaders(null, response, true, null, null);
ACK_HEADERS = response.getMimeHeaders();

HTTP_CONNECTION_SPECIFIC_HEADERS.add("connection");
HTTP_CONNECTION_SPECIFIC_HEADERS.add("proxy-connection");
HTTP_CONNECTION_SPECIFIC_HEADERS.add("keep-alive");
HTTP_CONNECTION_SPECIFIC_HEADERS.add("transfer-encoding");
HTTP_CONNECTION_SPECIFIC_HEADERS.add("upgrade");
}

private volatile long contentLengthReceived = 0;
Expand Down Expand Up @@ -385,7 +375,7 @@ public final void emitHeader(String name, String value) throws HpackException {
}


if (HTTP_CONNECTION_SPECIFIC_HEADERS.contains(name)) {
if (Constants.CONNECTION_SPECIFIC_HEADERS.contains(name)) {
headerException = new StreamException(
sm.getString("stream.header.connection", getConnectionId(), getIdAsString(), name),
Http2Error.PROTOCOL_ERROR, getIdAsInt());
Expand Down
5 changes: 5 additions & 0 deletions java/org/apache/coyote/http2/StreamProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ static void prepareHeaders(Request coyoteRequest, Response coyoteResponse, boole
// Add the pseudo header for status
headers.addValue(":status").setString(Integer.toString(statusCode));

// RFC 9113, section 8.2.2: HTTP/2 messages must not contain connection-specific header fields. The application
// may have set them, or copied them from an HTTP/1.x response.
for (String name : Constants.CONNECTION_SPECIFIC_HEADERS) {
headers.removeHeader(name);
}

// Compression can't be used with sendfile
// Need to check for compression (and set headers appropriately) before
Expand Down
99 changes: 99 additions & 0 deletions test/org/apache/coyote/http2/TestHttp2Section_8_2_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.http2;

import java.io.IOException;
import java.nio.ByteBuffer;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.junit.Assert;
import org.junit.Test;

import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;

/**
* Unit tests for Section 8.2.2 of <a href="https://tools.ietf.org/html/rfc9113">RFC 9113</a>. <br>
* The order of the tests in this class is determined by the order of the requirements in the RFC.
*/
public class TestHttp2Section_8_2_2 extends Http2TestBase {

/*
* An endpoint MUST NOT generate an HTTP/2 message containing connection-specific header fields. Applications that
* set them (or proxy them from an HTTP/1.x origin) must not cause the response to be malformed.
*/
@Test
public void testConnectionSpecificResponseHeadersRemoved() throws Exception {
enableHttp2();

Tomcat tomcat = getTomcatInstance();

Context ctxt = getProgrammaticRootContext();
Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
ctxt.addServletMapping("/simple", "simple");
Tomcat.addServlet(ctxt, "connection", new ConnectionHeadersServlet());
ctxt.addServletMapping("/connection", "connection");

tomcat.start();

openClientConnection();
doHttpUpgrade();
sendClientPreface();
validateHttp2InitialResponse();

byte[] frameHeader = new byte[9];
ByteBuffer headersPayload = ByteBuffer.allocate(128);
buildGetRequest(frameHeader, headersPayload, null, 3, "/connection");
writeFrame(frameHeader, headersPayload);

// Headers
parser.readFrame();
// Body
parser.readFrame();

String trace = output.getTrace();
Assert.assertTrue(trace, trace.contains("3-Header-[x-other]-[value]\n"));
Assert.assertFalse(trace, trace.contains("3-Header-[connection]-"));
Assert.assertFalse(trace, trace.contains("3-Header-[proxy-connection]-"));
Assert.assertFalse(trace, trace.contains("3-Header-[keep-alive]-"));
Assert.assertFalse(trace, trace.contains("3-Header-[transfer-encoding]-"));
Assert.assertFalse(trace, trace.contains("3-Header-[upgrade]-"));
}


private static class ConnectionHeadersServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
resp.setHeader("Connection", "close");
resp.setHeader("Proxy-Connection", "keep-alive");
resp.setHeader("Keep-Alive", "timeout=5");
resp.setHeader("Transfer-Encoding", "chunked");
resp.setHeader("Upgrade", "websocket");
resp.setHeader("X-Other", "value");
resp.getWriter().print("OK");
}
}
}
Loading