HTTPCORE-106: Moved garbage tolerant HTTP response parser to HttpClient
git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@562715 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3476e5f3e2
commit
6475dd23d8
|
@ -82,6 +82,25 @@ public final class HttpConnectionManagerParams {
|
||||||
*/
|
*/
|
||||||
public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
|
public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the maximum number of ignorable lines before we expect
|
||||||
|
* a HTTP response's status code.
|
||||||
|
* <p>
|
||||||
|
* With HTTP/1.1 persistent connections, the problem arises that
|
||||||
|
* broken scripts could return a wrong Content-Length
|
||||||
|
* (there are more bytes sent than specified).<br />
|
||||||
|
* Unfortunately, in some cases, this is not possible after the bad response,
|
||||||
|
* but only before the next one. <br />
|
||||||
|
* So, HttpClient must be able to skip those surplus lines this way.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* Set this to 0 to disallow any garbage/empty lines before the status line.<br />
|
||||||
|
* To specify no limit, use {@link java.lang.Integer#MAX_VALUE} (default in lenient mode).
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* This parameter expects a value of type {@link Integer}.
|
||||||
|
*/
|
||||||
|
public static final String MAX_STATUS_LINE_GARBAGE = "http.connection.max-status-line-garbage";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the default maximum number of connections allowed for routes.
|
* Sets the default maximum number of connections allowed for routes.
|
||||||
|
|
|
@ -42,8 +42,10 @@ import org.apache.http.HttpException;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.HttpRequest;
|
import org.apache.http.HttpRequest;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.HttpResponseFactory;
|
||||||
import org.apache.http.params.HttpParams;
|
import org.apache.http.params.HttpParams;
|
||||||
import org.apache.http.impl.SocketHttpClientConnection;
|
import org.apache.http.impl.SocketHttpClientConnection;
|
||||||
|
import org.apache.http.io.HttpMessageParser;
|
||||||
import org.apache.http.io.SessionInputBuffer;
|
import org.apache.http.io.SessionInputBuffer;
|
||||||
import org.apache.http.io.SessionOutputBuffer;
|
import org.apache.http.io.SessionOutputBuffer;
|
||||||
|
|
||||||
|
@ -166,6 +168,14 @@ public class DefaultClientConnection extends SocketHttpClientConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected HttpMessageParser createResponseParser(
|
||||||
|
final SessionInputBuffer buffer,
|
||||||
|
final HttpResponseFactory responseFactory,
|
||||||
|
final HttpParams params) {
|
||||||
|
return new DefaultResponseParser(buffer, responseFactory, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// non-javadoc, see interface OperatedClientConnection
|
// non-javadoc, see interface OperatedClientConnection
|
||||||
public void open(Socket sock, HttpHost target,
|
public void open(Socket sock, HttpHost target,
|
||||||
boolean secure, HttpParams params)
|
boolean secure, HttpParams params)
|
||||||
|
|
|
@ -0,0 +1,118 @@
|
||||||
|
/*
|
||||||
|
* $HeadURL$
|
||||||
|
* $Revision$
|
||||||
|
* $Date$
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* 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.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.impl.conn;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.http.HttpException;
|
||||||
|
import org.apache.http.HttpMessage;
|
||||||
|
import org.apache.http.HttpResponseFactory;
|
||||||
|
import org.apache.http.NoHttpResponseException;
|
||||||
|
import org.apache.http.ProtocolException;
|
||||||
|
import org.apache.http.StatusLine;
|
||||||
|
import org.apache.http.conn.params.HttpConnectionManagerParams;
|
||||||
|
import org.apache.http.impl.io.AbstractMessageParser;
|
||||||
|
import org.apache.http.io.SessionInputBuffer;
|
||||||
|
import org.apache.http.message.BasicStatusLine;
|
||||||
|
import org.apache.http.params.HttpParams;
|
||||||
|
import org.apache.http.protocol.HTTP;
|
||||||
|
import org.apache.http.util.CharArrayBuffer;
|
||||||
|
|
||||||
|
public class DefaultResponseParser extends AbstractMessageParser {
|
||||||
|
|
||||||
|
private final HttpResponseFactory responseFactory;
|
||||||
|
private final CharArrayBuffer lineBuf;
|
||||||
|
private final int maxGarbageLines;
|
||||||
|
|
||||||
|
public DefaultResponseParser(
|
||||||
|
final SessionInputBuffer buffer,
|
||||||
|
final HttpResponseFactory responseFactory,
|
||||||
|
final HttpParams params) {
|
||||||
|
super(buffer, params);
|
||||||
|
if (responseFactory == null) {
|
||||||
|
throw new IllegalArgumentException("Response factory may not be null");
|
||||||
|
}
|
||||||
|
this.responseFactory = responseFactory;
|
||||||
|
this.lineBuf = new CharArrayBuffer(128);
|
||||||
|
this.maxGarbageLines = params.getIntParameter(
|
||||||
|
HttpConnectionManagerParams.MAX_STATUS_LINE_GARBAGE, Integer.MAX_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if the string starts with 'HTTP' signature.
|
||||||
|
* @param buffer buffer to test
|
||||||
|
* @return <tt>true</tt> if the line starts with 'HTTP'
|
||||||
|
* signature, <tt>false</tt> otherwise.
|
||||||
|
*/
|
||||||
|
protected static boolean startsWithHTTP(final CharArrayBuffer buffer) {
|
||||||
|
try {
|
||||||
|
int i = 0;
|
||||||
|
while (HTTP.isWhitespace(buffer.charAt(i))) {
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
return buffer.charAt(i) == 'H'
|
||||||
|
&& buffer.charAt(i + 1) == 'T'
|
||||||
|
&& buffer.charAt(i + 2) == 'T'
|
||||||
|
&& buffer.charAt(i + 3) == 'P';
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected HttpMessage parseHead(
|
||||||
|
final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
|
||||||
|
// clear the buffer
|
||||||
|
this.lineBuf.clear();
|
||||||
|
//read out the HTTP status string
|
||||||
|
int count = 0;
|
||||||
|
do {
|
||||||
|
int i = sessionBuffer.readLine(this.lineBuf);
|
||||||
|
if (i == -1 && count == 0) {
|
||||||
|
// The server just dropped connection on us
|
||||||
|
throw new NoHttpResponseException("The target server failed to respond");
|
||||||
|
}
|
||||||
|
if (startsWithHTTP(this.lineBuf)) {
|
||||||
|
// Got one
|
||||||
|
break;
|
||||||
|
} else if (i == -1 || count >= this.maxGarbageLines) {
|
||||||
|
// Giving up
|
||||||
|
throw new ProtocolException("The server failed to respond with a " +
|
||||||
|
"valid HTTP response");
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
} while(true);
|
||||||
|
//create the status line from the status string
|
||||||
|
StatusLine statusline = BasicStatusLine.parse(this.lineBuf, 0, this.lineBuf.length());
|
||||||
|
return this.responseFactory.newHttpResponse(statusline, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue