DefaultClientConnectionFactory to use lenient HttpResponseParser implementation
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1414679 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e2e564a1a1
commit
1179458a52
|
@ -65,7 +65,8 @@ public class DefaultClientConnectionFactory implements HttpConnectionFactory<Soc
|
|||
return new SocketClientConnectionImpl(8 * 1024,
|
||||
chardecoder, charencoder,
|
||||
cconfig.getMessageConstraints(),
|
||||
null, null, null, null);
|
||||
null, null, null,
|
||||
DefaultHttpResponseParserFactory.INSTANCE);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ package org.apache.http.impl.conn;
|
|||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.annotation.ThreadSafe;
|
||||
import org.apache.http.config.MessageConstraints;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
@ -39,25 +40,22 @@ import org.apache.http.HttpResponseFactory;
|
|||
import org.apache.http.NoHttpResponseException;
|
||||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.impl.DefaultHttpResponseFactory;
|
||||
import org.apache.http.impl.io.AbstractMessageParser;
|
||||
import org.apache.http.io.SessionInputBuffer;
|
||||
import org.apache.http.message.BasicLineParser;
|
||||
import org.apache.http.message.LineParser;
|
||||
import org.apache.http.message.ParserCursor;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.util.CharArrayBuffer;
|
||||
|
||||
/**
|
||||
* Default HTTP response parser implementation.
|
||||
* <p>
|
||||
* The following parameters can be used to customize the behavior of this
|
||||
* class:
|
||||
* <ul>
|
||||
* <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
|
||||
* <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
|
||||
* </ul>
|
||||
* Lenient HTTP response parser implementation that can skip malformed data until
|
||||
* a valid HTTP response message head is encountered.
|
||||
*
|
||||
* @since 4.2
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
@ThreadSafe // no public methods
|
||||
public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
|
||||
|
||||
|
@ -66,6 +64,11 @@ public class DefaultHttpResponseParser extends AbstractMessageParser<HttpRespons
|
|||
private final HttpResponseFactory responseFactory;
|
||||
private final CharArrayBuffer lineBuf;
|
||||
|
||||
/**
|
||||
* @deprecated (4.3) use {@link DefaultHttpResponseParser#DefaultHttpResponseParser(
|
||||
* SessionInputBuffer, LineParser, HttpResponseFactory, MessageConstraints)}
|
||||
*/
|
||||
@Deprecated
|
||||
public DefaultHttpResponseParser(
|
||||
final SessionInputBuffer buffer,
|
||||
final LineParser parser,
|
||||
|
@ -79,6 +82,41 @@ public class DefaultHttpResponseParser extends AbstractMessageParser<HttpRespons
|
|||
this.responseFactory = responseFactory;
|
||||
this.lineBuf = new CharArrayBuffer(128);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new instance of DefaultHttpResponseParser.
|
||||
*
|
||||
* @param buffer the session input buffer.
|
||||
* @param lineParser the line parser. If <code>null</code> {@link BasicLineParser#INSTANCE}
|
||||
* will be used.
|
||||
* @param responseFactory HTTP response factory. If <code>null</code>
|
||||
* {@link DefaultHttpResponseFactory#INSTANCE} will be used.
|
||||
* @param constraints the message constraints. If <code>null</code>
|
||||
* {@link MessageConstraints#DEFAULT} will be used.
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
public DefaultHttpResponseParser(
|
||||
final SessionInputBuffer buffer,
|
||||
final LineParser lineParser,
|
||||
final HttpResponseFactory responseFactory,
|
||||
final MessageConstraints constraints) {
|
||||
super(buffer, lineParser, constraints);
|
||||
this.responseFactory = responseFactory != null ? responseFactory :
|
||||
DefaultHttpResponseFactory.INSTANCE;
|
||||
this.lineBuf = new CharArrayBuffer(128);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new instance of DefaultHttpResponseParser.
|
||||
*
|
||||
* @param buffer the session input buffer.
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
public DefaultHttpResponseParser(final SessionInputBuffer buffer) {
|
||||
this(buffer, null, null, MessageConstraints.DEFAULT);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HttpResponse parseHead(
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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 org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpResponseFactory;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.config.MessageConstraints;
|
||||
import org.apache.http.impl.DefaultHttpResponseFactory;
|
||||
import org.apache.http.io.HttpMessageParser;
|
||||
import org.apache.http.io.HttpMessageParserFactory;
|
||||
import org.apache.http.io.SessionInputBuffer;
|
||||
import org.apache.http.message.BasicLineParser;
|
||||
import org.apache.http.message.LineParser;
|
||||
|
||||
/**
|
||||
* Default factory for response message parsers.
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
@Immutable
|
||||
public class DefaultHttpResponseParserFactory implements HttpMessageParserFactory<HttpResponse> {
|
||||
|
||||
public static final DefaultHttpResponseParserFactory INSTANCE = new DefaultHttpResponseParserFactory();
|
||||
|
||||
private final LineParser lineParser;
|
||||
private final HttpResponseFactory responseFactory;
|
||||
|
||||
public DefaultHttpResponseParserFactory(final LineParser lineParser,
|
||||
final HttpResponseFactory responseFactory) {
|
||||
super();
|
||||
this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
|
||||
this.responseFactory = responseFactory != null ? responseFactory
|
||||
: DefaultHttpResponseFactory.INSTANCE;
|
||||
}
|
||||
|
||||
public DefaultHttpResponseParserFactory() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
public HttpMessageParser<HttpResponse> create(final SessionInputBuffer buffer,
|
||||
final MessageConstraints constraints) {
|
||||
return new DefaultHttpResponseParser(buffer, lineParser, responseFactory, constraints);
|
||||
}
|
||||
|
||||
}
|
|
@ -25,95 +25,85 @@
|
|||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.mockup;
|
||||
package org.apache.http.impl.conn;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
|
||||
import org.apache.http.impl.io.AbstractSessionInputBuffer;
|
||||
import org.apache.http.params.BasicHttpParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.config.MessageConstraints;
|
||||
import org.apache.http.impl.io.HttpTransportMetricsImpl;
|
||||
import org.apache.http.impl.io.SessionInputBufferImpl;
|
||||
|
||||
/**
|
||||
* {@link org.apache.http.io.SessionInputBuffer} mockup implementation.
|
||||
*/
|
||||
public class SessionInputBufferMockup extends AbstractSessionInputBuffer {
|
||||
public class SessionInputBufferMock extends SessionInputBufferImpl {
|
||||
|
||||
public static final int BUFFER_SIZE = 16;
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
final InputStream instream,
|
||||
int buffersize,
|
||||
final HttpParams params) {
|
||||
super();
|
||||
init(instream, buffersize, params);
|
||||
public SessionInputBufferMock(
|
||||
final InputStream instream,
|
||||
int buffersize,
|
||||
final MessageConstraints constrains,
|
||||
final CharsetDecoder decoder) {
|
||||
super(new HttpTransportMetricsImpl(), buffersize, -1, constrains, decoder);
|
||||
bind(instream);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
public SessionInputBufferMock(
|
||||
final InputStream instream,
|
||||
int buffersize) {
|
||||
this(instream, buffersize, new BasicHttpParams());
|
||||
this(instream, buffersize, null, null);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
public SessionInputBufferMock(
|
||||
final byte[] bytes,
|
||||
final HttpParams params) {
|
||||
this(bytes, BUFFER_SIZE, params);
|
||||
int buffersize,
|
||||
final MessageConstraints constrains,
|
||||
final CharsetDecoder decoder) {
|
||||
this(new ByteArrayInputStream(bytes), buffersize, constrains, decoder);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
public SessionInputBufferMock(
|
||||
final byte[] bytes,
|
||||
int buffersize,
|
||||
final MessageConstraints constrains) {
|
||||
this(new ByteArrayInputStream(bytes), buffersize, constrains, null);
|
||||
}
|
||||
|
||||
public SessionInputBufferMock(
|
||||
final byte[] bytes,
|
||||
int buffersize) {
|
||||
this(new ByteArrayInputStream(bytes), buffersize);
|
||||
}
|
||||
|
||||
public SessionInputBufferMock(
|
||||
final byte[] bytes) {
|
||||
this(bytes, BUFFER_SIZE, new BasicHttpParams());
|
||||
this(bytes, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
final byte[] bytes,
|
||||
int buffersize,
|
||||
final HttpParams params) {
|
||||
this(new ByteArrayInputStream(bytes), buffersize, params);
|
||||
public SessionInputBufferMock(
|
||||
final byte[] bytes, final Charset charset) {
|
||||
this(bytes, BUFFER_SIZE, null, charset != null ? charset.newDecoder() : null);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
final byte[] bytes,
|
||||
int buffersize) {
|
||||
this(new ByteArrayInputStream(bytes), buffersize, new BasicHttpParams());
|
||||
public SessionInputBufferMock(
|
||||
final byte[] bytes,
|
||||
final CharsetDecoder decoder) {
|
||||
this(bytes, BUFFER_SIZE, null, decoder);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
public SessionInputBufferMock(
|
||||
final String s,
|
||||
final String charset,
|
||||
int buffersize,
|
||||
final HttpParams params)
|
||||
throws UnsupportedEncodingException {
|
||||
this(s.getBytes(charset), buffersize, params);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
final String s,
|
||||
final String charset,
|
||||
int buffersize)
|
||||
throws UnsupportedEncodingException {
|
||||
this(s.getBytes(charset), buffersize, new BasicHttpParams());
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
final String s,
|
||||
final String charset,
|
||||
final HttpParams params)
|
||||
throws UnsupportedEncodingException {
|
||||
this(s.getBytes(charset), params);
|
||||
}
|
||||
|
||||
public SessionInputBufferMockup(
|
||||
final String s,
|
||||
final String charset)
|
||||
throws UnsupportedEncodingException {
|
||||
this(s.getBytes(charset), new BasicHttpParams());
|
||||
|
||||
final Charset charset) throws UnsupportedEncodingException {
|
||||
this(s.getBytes(charset.name()), charset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataAvailable(int timeout) throws IOException {
|
||||
return true;
|
||||
}
|
|
@ -27,24 +27,20 @@
|
|||
|
||||
package org.apache.http.impl.conn;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.NoHttpResponseException;
|
||||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.impl.DefaultHttpResponseFactory;
|
||||
import org.apache.http.io.HttpMessageParser;
|
||||
import org.apache.http.io.SessionInputBuffer;
|
||||
import org.apache.http.message.BasicLineParser;
|
||||
import org.apache.http.mockup.SessionInputBufferMockup;
|
||||
import org.apache.http.params.BasicHttpParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.util.CharArrayBuffer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for <code>DefaultResponseParser</code>.
|
||||
* Tests for {@link DefaultResponseParser}.
|
||||
*/
|
||||
public class TestDefaultHttpResponseParser {
|
||||
|
||||
|
@ -59,13 +55,8 @@ public class TestDefaultHttpResponseParser {
|
|||
"header2: value2\r\n" +
|
||||
"\r\n";
|
||||
|
||||
HttpParams params = new BasicHttpParams();
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
|
||||
inbuffer,
|
||||
BasicLineParser.DEFAULT,
|
||||
new DefaultHttpResponseFactory(),
|
||||
params);
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer);
|
||||
|
||||
HttpResponse response = parser.parse();
|
||||
Assert.assertNotNull(response);
|
||||
|
@ -90,18 +81,13 @@ public class TestDefaultHttpResponseParser {
|
|||
"header2: value2\r\n" +
|
||||
"\r\n";
|
||||
|
||||
HttpParams params = new BasicHttpParams();
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
|
||||
inbuffer,
|
||||
BasicLineParser.DEFAULT,
|
||||
new DefaultHttpResponseFactory(),
|
||||
params) {
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer) {
|
||||
|
||||
@Override
|
||||
protected boolean reject(final CharArrayBuffer line, int count) {
|
||||
return count >= 2;
|
||||
}
|
||||
@Override
|
||||
protected boolean reject(final CharArrayBuffer line, int count) {
|
||||
return count >= 2;
|
||||
}
|
||||
|
||||
};
|
||||
parser.parse();
|
||||
|
@ -109,13 +95,8 @@ public class TestDefaultHttpResponseParser {
|
|||
|
||||
@Test(expected=NoHttpResponseException.class)
|
||||
public void testResponseParsingNoResponse() throws Exception {
|
||||
HttpParams params = new BasicHttpParams();
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMockup("", "US-ASCII", params);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
|
||||
inbuffer,
|
||||
BasicLineParser.DEFAULT,
|
||||
new DefaultHttpResponseFactory(),
|
||||
params);
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMock("", Consts.ASCII);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer);
|
||||
parser.parse();
|
||||
}
|
||||
|
||||
|
@ -126,13 +107,8 @@ public class TestDefaultHttpResponseParser {
|
|||
"garbage\r\n" +
|
||||
"more garbage\r\n" +
|
||||
"a lot more garbage\r\n";
|
||||
HttpParams params = new BasicHttpParams();
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
|
||||
inbuffer,
|
||||
BasicLineParser.DEFAULT,
|
||||
new DefaultHttpResponseFactory(),
|
||||
params);
|
||||
SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
|
||||
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer);
|
||||
parser.parse();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue