Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #12436 - Allow headers size extend to maxRequestHeadersSize in http client. #12544

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public class HttpClient extends ContainerLifeCycle implements AutoCloseable
private String defaultRequestContentType = "application/octet-stream";
private boolean useInputDirectByteBuffers = true;
private boolean useOutputDirectByteBuffers = true;
private int maxRequestHeadersSize = 8192;
private int maxResponseHeadersSize = -1;
private Sweeper destinationSweeper;

Expand Down Expand Up @@ -1071,6 +1072,24 @@ public void setUseInputDirectByteBuffers(boolean useInputDirectByteBuffers)
this.useInputDirectByteBuffers = useInputDirectByteBuffers;
}

/**
* @return the max size in bytes of the request headers
*/
@ManagedAttribute("The max size in bytes of the request headers")
public int getMaxRequestHeadersSize()
{
return maxRequestHeadersSize;
}

/**
* Set the max size in bytes of the request headers.
* @param maxRequestHeadersSize the max size in bytes of the request headers
*/
public void setMaxRequestHeadersSize(int maxRequestHeadersSize)
{
this.maxRequestHeadersSize = maxRequestHeadersSize;
}

/**
* @return whether to use direct ByteBuffers for writing
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class HttpSenderOverHTTP extends HttpSender
public HttpSenderOverHTTP(HttpChannelOverHTTP channel)
{
super(channel);
generator.setMaxHeaderBytes(channel.getHttpDestination().getHttpClient().getMaxRequestHeadersSize());
}

@Override
Expand Down Expand Up @@ -158,6 +159,7 @@ protected Action process() throws Exception
HttpClient httpClient = getHttpChannel().getHttpDestination().getHttpClient();
HttpExchange exchange = getHttpExchange();
ByteBufferPool bufferPool = httpClient.getByteBufferPool();
int requestHeadersSize = httpClient.getRequestBufferSize();
boolean useDirectByteBuffers = httpClient.isUseOutputDirectByteBuffers();
while (true)
{
Expand All @@ -174,14 +176,24 @@ protected Action process() throws Exception
{
case NEED_HEADER:
{
headerBuffer = bufferPool.acquire(httpClient.getRequestBufferSize(), useDirectByteBuffers);
headerBuffer = bufferPool.acquire(requestHeadersSize, useDirectByteBuffers);
break;
}
case HEADER_OVERFLOW:
{
headerBuffer.release();
headerBuffer = null;
throw new IllegalArgumentException("Request header too large");
int maxRequestHeadersSize = httpClient.getMaxRequestHeadersSize();
if (maxRequestHeadersSize > requestHeadersSize)
{
generator.reset();
headerBuffer.release();
headerBuffer = bufferPool.acquire(maxRequestHeadersSize, useDirectByteBuffers);
requestHeadersSize = maxRequestHeadersSize;
break;
}
else
{
throw new IllegalArgumentException("Request headers too large");
}
}
case NEED_CHUNK:
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.RetainableByteBuffer;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.internal.HttpChannelState;
import org.eclipse.jetty.toolchain.test.Net;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
Expand Down Expand Up @@ -1936,6 +1938,48 @@ protected void service(org.eclipse.jetty.server.Request request, org.eclipse.jet
assertEquals(HttpStatus.OK_200, response.getStatus());
}

@ParameterizedTest
@ArgumentsSource(ScenarioProvider.class)
public void testRequestHeadersSizeOverflow(Scenario scenario) throws Exception
{
start(scenario, new EmptyServerHandler());

RetainableByteBuffer.Mutable buffer = client.getByteBufferPool().acquire(client.getRequestBufferSize(), false);
int capacity = buffer.capacity();
buffer.release();
client.setMaxRequestHeadersSize(3 * capacity);
connector.getBean(HttpConnectionFactory.class).getHttpConfiguration().setRequestHeaderSize(3 * capacity);

ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scenario.getScheme())
// Overflow the request headers size, but don't exceed the max.
.agent("A".repeat(2 * capacity))
.timeout(5, TimeUnit.SECONDS)
.send();

assertEquals(HttpStatus.OK_200, response.getStatus());
}

@ParameterizedTest
@ArgumentsSource(ScenarioProvider.class)
public void testMaxRequestHeadersSize(Scenario scenario) throws Exception
{
start(scenario, new EmptyServerHandler());

RetainableByteBuffer.Mutable buffer = client.getByteBufferPool().acquire(client.getRequestBufferSize(), false);
int capacity = buffer.capacity();
buffer.release();
client.setMaxRequestHeadersSize(2 * capacity);
connector.getBean(HttpConnectionFactory.class).getHttpConfiguration().setRequestHeaderSize(4 * capacity);

assertThrows(ExecutionException.class, () -> client.newRequest("localhost", connector.getLocalPort())
.scheme(scenario.getScheme())
// Overflow the max request headers size.
.agent("A".repeat(3 * capacity))
.timeout(5, TimeUnit.SECONDS)
.send());
}

private void assertCopyRequest(Request original)
{
Request copy = client.copyRequest(original, original.getURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static void configure(HttpClient httpClient, HTTP2Client http2Client)
http2Client.setUseOutputDirectByteBuffers(httpClient.isUseOutputDirectByteBuffers());
http2Client.setConnectBlocking(httpClient.isConnectBlocking());
http2Client.setBindAddress(httpClient.getBindAddress());
http2Client.setMaxRequestHeadersSize(httpClient.getRequestBufferSize());
http2Client.setMaxRequestHeadersSize(httpClient.getMaxRequestHeadersSize());
http2Client.setMaxResponseHeadersSize(httpClient.getMaxResponseHeadersSize());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void testPropertiesAreForwarded(HTTP2Client http2Client, HttpClientTrans
assertEquals(httpClient.getIdleTimeout(), http2Client.getIdleTimeout());
assertEquals(httpClient.isUseInputDirectByteBuffers(), http2Client.isUseInputDirectByteBuffers());
assertEquals(httpClient.isUseOutputDirectByteBuffers(), http2Client.isUseOutputDirectByteBuffers());
assertEquals(httpClient.getRequestBufferSize(), http2Client.getMaxRequestHeadersSize());
assertEquals(httpClient.getMaxRequestHeadersSize(), http2Client.getMaxRequestHeadersSize());
assertEquals(httpClient.getMaxResponseHeadersSize(), http2Client.getMaxResponseHeadersSize());
}
assertTrue(http2Client.isStopped());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static void configure(HttpClient httpClient, HTTP3Client http3Client)
configuration.setInputBufferSize(httpClient.getResponseBufferSize());
configuration.setUseInputDirectByteBuffers(httpClient.isUseInputDirectByteBuffers());
configuration.setUseOutputDirectByteBuffers(httpClient.isUseOutputDirectByteBuffers());
configuration.setMaxRequestHeadersSize(httpClient.getMaxRequestHeadersSize());
configuration.setMaxResponseHeadersSize(httpClient.getMaxResponseHeadersSize());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private void testPropertiesAreForwarded(HTTP3Client http3Client, HttpClientTrans
HTTP3Configuration http3Configuration = http3Client.getHTTP3Configuration();
assertEquals(httpClient.isUseInputDirectByteBuffers(), http3Configuration.isUseInputDirectByteBuffers());
assertEquals(httpClient.isUseOutputDirectByteBuffers(), http3Configuration.isUseOutputDirectByteBuffers());
assertEquals(httpClient.getMaxRequestHeadersSize(), http3Configuration.getMaxRequestHeadersSize());
assertEquals(httpClient.getMaxResponseHeadersSize(), http3Configuration.getMaxResponseHeadersSize());
}
assertTrue(http3Client.isStopped());
Expand Down
Loading