Response Header too large - Jetty Embedded version 9

非 Y 不嫁゛ 提交于 2019-12-10 17:50:29

问题


I'm getting an exception of "Response Header too large" when using Jetty, the exception is thrown at the Client and only when the size of the jsonValue is large (greater than 1500 bytes). If the jsonvalue is smaller, everything works fine.

This is the code I have, it is very simple.

Server Code:

Server server = new Server(8080);
ServletHandler handler = new ServletHandler();
server.setHandler(handler);

handler.addServletWithMapping(StoreServlet.class, "/store");

server.start();
server.join();

StoreServlet class code:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().println("<h1>Store SimpleServlet</h1>");
}

Client Code:

HttpClient httpClient = new HttpClient();
httpClient.start();

ContentResponse response = httpClient.POST("http://0.0.0.0:" + host.getPort() + "/store").param("p", jsonValue).send();

StactTrace:

java.util.concurrent.ExecutionException: java.io.IOException: Response header too large
    at org.eclipse.jetty.client.util.FutureResponseListener.getResult(FutureResponseListener.java:118)
    at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:101)
    at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:589)
    at com.paywithbytes.communication.http.client.HttpClientHelper.doPost(HttpClientHelper.java:54)
    at com.paywithbytes.communication.http.client.HttpClientHelper.main(HttpClientHelper.java:35)
Caused by: java.io.IOException: Response header too large
    at org.eclipse.jetty.http.HttpGenerator.generateRequest(HttpGenerator.java:249)
    at org.eclipse.jetty.client.http.HttpSenderOverHTTP.sendHeaders(HttpSenderOverHTTP.java:78)
    at org.eclipse.jetty.client.HttpSender.send(HttpSender.java:191)
    at org.eclipse.jetty.client.http.HttpChannelOverHTTP.send(HttpChannelOverHTTP.java:52)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP$Delegate.send(HttpConnectionOverHTTP.java:168)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.send(HttpConnectionOverHTTP.java:69)
    at org.eclipse.jetty.client.http.HttpDestinationOverHTTP.send(HttpDestinationOverHTTP.java:36)
    at org.eclipse.jetty.client.http.HttpDestinationOverHTTP.send(HttpDestinationOverHTTP.java:26)
    at org.eclipse.jetty.client.PoolingHttpDestination$2.run(PoolingHttpDestination.java:130)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.nio.BufferOverflowException
    at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:183)
    at java.nio.ByteBuffer.put(ByteBuffer.java:832)
    at org.eclipse.jetty.http.HttpGenerator.generateRequestLine(HttpGenerator.java:500)
    at org.eclipse.jetty.http.HttpGenerator.generateRequest(HttpGenerator.java:218)
    ... 11 more

The answer selected was perfect, I just want to add that when sending a large file it is better to use this method:

Request request = httpClient.POST("http://0.0.0.0:" + host.getPort() + "/store").content(
            new BytesContentProvider(jsonValue.getBytes()), "text/plain");

Otherwise, if sending a large jsonValue as a POST param, you will get:

414 Request-URI Too Long

回答1:


Using maven jetty plugin version: 9.1.3.v20140225

You are going to need a custom value for your "request buffer size". By default it is 4096 bytes.

You can use the method setRequestBufferSize of the HttpClient class (see javadoc: org.eclipse.jetty.client.HttpClient)

HttpClient httpClient = new HttpClient();
httpClient.setRequestBufferSize(/*your custom value*/);
httpClient.start();

ContentResponse response = httpClient.POST("http://0.0.0.0:" + host.getPort() + "/store").param("p", jsonValue).send();

Your custom value will depend on how many bytes you are sending plus a few more, which are always sent by the HTTP client implementation. For example, in my case and using the above code I am sending the following:

POST /?p=YOUR_JSON_VALUE HTTP/1.1
Accept-Encoding: gzip
Host: 0.0.0.0:8080/store
User-Agent: Jetty/9.1.3.v20140225



回答2:


I just ran into a problem with seemingly the same symptoms but a different cause. In my case, I was trying to post messages containing 3MB of data to a servlet. Increasing the request buffer size did not resolve the problem.

The problem turned out that I was reusing the same request object instead of making a new request object per actual request.



来源:https://stackoverflow.com/questions/22471808/response-header-too-large-jetty-embedded-version-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!