Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] HttpClient handling of response that doesn't send full response according to Content-Length

I've noticed a difference in behaviour from Jetty HttpClient version 7.2.0 to 7.5.4 when a web server indicates a Content-Length, sends back some data (but not the full amount) and then closes the connection. With version 7.2.0 onException() wouldn't be called and onResponseComplete() would, but in 7.5.4 onException() gets called but not onResponseComplete(). Note that this only seems to happen when using HTTP 1.0 with the HttpClient.

Is this change in behaviour expected? Which is the correct behaviour? I see this in the the HTTP spec ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html):

" When a Content-Length is given in a message where a message-body is allowed, its field value MUST exactly match the number of OCTETs in the message-body. HTTP/1.1 user agents MUST notify the user when an invalid length is received and detected."

However, browsers seem to handle the situation without an error. https://www.fidelity.com is an example of a web page that sets the Content-Length but doesn't send the full amount.

I've attached a test program that I was using to reproduce this.

Thanks,
Chris
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpSchemes;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

public class JettyTest
{
	public static void main(String[] args)
	throws Exception
	{
		HttpClient client = new HttpClient();
		ContentExchange exchange = new ContentExchange(true);
		try
		{
			client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
			client.setThreadPool(new QueuedThreadPool());
			client.setTimeout(30000);
			client.start();
			
			exchange.setScheme(HttpSchemes.HTTPS_BUFFER);
			exchange.setMethod("GET");
			exchange.setVersion("HTTP/1.0");
			exchange.addRequestHeader(HttpHeaders.HOST, "www.fidelity.com");
			exchange.setURL("https://www.fidelity.com/";);
			exchange.setRequestHeader("Accept-Encoding", "gzip");
			
	        client.send(exchange);

	        exchange.waitForDone();
	        System.out.println("Return code: " + exchange.getResponseStatus());
	        System.out.println("Content-Length: " + exchange.getResponseFields().getStringField("Content-Length"));
	        System.out.println("Received length: " + exchange.getResponseContent().length());
		}
		catch (Exception e)
		{
			System.out.println("Exception: " + e.getMessage());
		}
		client.stop();
	}
}

Back to the top