Skip to content

Commit

Permalink
Fixes #12436 - Allow headers size extend to maxRequestHeadersSize in …
Browse files Browse the repository at this point in the history
…http client.

Reworked contribution by @shaoxt in light of the work done in #12351.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Nov 17, 2024
1 parent 6518368 commit 72e9933
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 245 deletions.
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 @@ -39,8 +39,6 @@ public class HttpClientTransportOverHTTP extends AbstractConnectorHttpClientTran
private int headerCacheSize = 1024;
private boolean headerCacheCaseSensitive;

private int maxRequestHeadersSize = 32 * 1024;

public HttpClientTransportOverHTTP()
{
this(1);
Expand Down Expand Up @@ -129,18 +127,4 @@ public void setInitializeConnections(boolean initialize)
{
factory.setInitializeConnections(initialize);
}

/**
* @return The maximum allowed size in bytes for the HTTP request headers
*/
@ManagedAttribute("The maximum allowed size in bytes for the HTTP request headers")
public int getMaxRequestHeadersSize()
{
return maxRequestHeadersSize;
}

public void setMaxRequestHeadersSize(int maxRequestHeadersSize)
{
this.maxRequestHeadersSize = maxRequestHeadersSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import java.nio.ByteBuffer;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpClientTransport;
import org.eclipse.jetty.client.HttpRequestException;
import org.eclipse.jetty.client.transport.HttpClientTransportOverHTTP;
import org.eclipse.jetty.client.transport.HttpExchange;
import org.eclipse.jetty.client.transport.HttpRequest;
import org.eclipse.jetty.client.transport.HttpSender;
Expand Down Expand Up @@ -52,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 @@ -160,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 @@ -176,33 +176,23 @@ protected Action process() throws Exception
{
case NEED_HEADER:
{
headerBuffer = bufferPool.acquire(httpClient.getRequestBufferSize(), useDirectByteBuffers);
headerBuffer = bufferPool.acquire(requestHeadersSize, useDirectByteBuffers);
break;
}
case HEADER_OVERFLOW:
{
int maxRequestHeadersSize = -1;
//For HTTP1.1 only
HttpClientTransport transport = httpClient.getTransport();
if (transport instanceof HttpClientTransportOverHTTP httpTransport)
int maxRequestHeadersSize = httpClient.getMaxRequestHeadersSize();
if (maxRequestHeadersSize > requestHeadersSize)
{
maxRequestHeadersSize = httpTransport.getMaxRequestHeadersSize();
}
if (headerBuffer.capacity() < maxRequestHeadersSize)
{
RetainableByteBuffer newHeaderBuffer = bufferPool.acquire(maxRequestHeadersSize, useDirectByteBuffers);
headerBuffer.getByteBuffer().flip();
newHeaderBuffer.getByteBuffer().put(headerBuffer.getByteBuffer());
RetainableByteBuffer toRelease = headerBuffer;
headerBuffer = newHeaderBuffer;
toRelease.release();
generator.reset();
headerBuffer.release();
headerBuffer = bufferPool.acquire(maxRequestHeadersSize, useDirectByteBuffers);
requestHeadersSize = maxRequestHeadersSize;
break;
}
else
{
headerBuffer.release();
headerBuffer = null;
throw new IllegalArgumentException("Request header too large");
throw new IllegalArgumentException("Request headers too large");
}
}
case NEED_CHUNK:
Expand Down
Loading

0 comments on commit 72e9933

Please sign in to comment.