Deprecated DefaultResponseParser in favor of DefaultHttpResponseParser

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1334646 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-05-06 14:41:38 +00:00
parent bfe3b7201f
commit aa412a29b9
5 changed files with 136 additions and 17 deletions

View File

@ -26,7 +26,7 @@
package org.apache.http.conn.params; package org.apache.http.conn.params;
import org.apache.http.impl.conn.DefaultResponseParser; import org.apache.http.impl.conn.DefaultHttpResponseParser;
/** /**
* Parameter names for HTTP client connections. * Parameter names for HTTP client connections.
@ -53,7 +53,7 @@ public interface ConnConnectionPNames {
* Use {@link java.lang.Integer#MAX_VALUE} for unlimited number. * Use {@link java.lang.Integer#MAX_VALUE} for unlimited number.
* </p> * </p>
* *
* @deprecated Use custom {@link DefaultResponseParser} implementation * @deprecated Use custom {@link DefaultHttpResponseParser} implementation
*/ */
public static final String MAX_STATUS_LINE_GARBAGE = "http.connection.max-status-line-garbage"; public static final String MAX_STATUS_LINE_GARBAGE = "http.connection.max-status-line-garbage";

View File

@ -221,7 +221,7 @@ public class DefaultClientConnection extends SocketHttpClientConnection
final HttpResponseFactory responseFactory, final HttpResponseFactory responseFactory,
final HttpParams params) { final HttpParams params) {
// override in derived class to specify a line parser // override in derived class to specify a line parser
return new DefaultResponseParser return new DefaultHttpResponseParser
(buffer, null, responseFactory, params); (buffer, null, responseFactory, params);
} }

View File

@ -0,0 +1,120 @@
/*
* ====================================================================
* 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.annotation.ThreadSafe;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
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.io.AbstractMessageParser;
import org.apache.http.io.SessionInputBuffer;
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>
*
* @since 4.2
*/
@ThreadSafe // no public methods
public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
private final Log log = LogFactory.getLog(getClass());
private final HttpResponseFactory responseFactory;
private final CharArrayBuffer lineBuf;
public DefaultHttpResponseParser(
final SessionInputBuffer buffer,
final LineParser parser,
final HttpResponseFactory responseFactory,
final HttpParams params) {
super(buffer, parser, params);
if (responseFactory == null) {
throw new IllegalArgumentException
("Response factory may not be null");
}
this.responseFactory = responseFactory;
this.lineBuf = new CharArrayBuffer(128);
}
@Override
protected HttpResponse parseHead(
final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
//read out the HTTP status string
int count = 0;
ParserCursor cursor = null;
do {
// clear the buffer
this.lineBuf.clear();
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");
}
cursor = new ParserCursor(0, this.lineBuf.length());
if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
// Got one
break;
} else if (i == -1 || reject(this.lineBuf, count)) {
// Giving up
throw new ProtocolException("The server failed to respond with a " +
"valid HTTP response");
}
if (this.log.isDebugEnabled()) {
this.log.debug("Garbage in response: " + this.lineBuf.toString());
}
count++;
} while(true);
//create the status line from the status string
StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
return this.responseFactory.newHttpResponse(statusline, null);
}
protected boolean reject(CharArrayBuffer line, int count) {
return false;
}
}

View File

@ -34,7 +34,7 @@ import org.apache.http.annotation.ThreadSafe;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException; import org.apache.http.HttpException;
import org.apache.http.HttpResponse; import org.apache.http.HttpMessage;
import org.apache.http.HttpResponseFactory; import org.apache.http.HttpResponseFactory;
import org.apache.http.NoHttpResponseException; import org.apache.http.NoHttpResponseException;
import org.apache.http.ProtocolException; import org.apache.http.ProtocolException;
@ -58,9 +58,11 @@ import org.apache.http.util.CharArrayBuffer;
* </ul> * </ul>
* *
* @since 4.0 * @since 4.0
*
* @deprecated (4.2) use {@link DefaultHttpResponseParser}
*/ */
@ThreadSafe // no public methods @ThreadSafe // no public methods
public class DefaultResponseParser extends AbstractMessageParser<HttpResponse> { public class DefaultResponseParser extends AbstractMessageParser<HttpMessage> {
private final Log log = LogFactory.getLog(getClass()); private final Log log = LogFactory.getLog(getClass());
@ -83,10 +85,6 @@ public class DefaultResponseParser extends AbstractMessageParser<HttpResponse> {
this.maxGarbageLines = getMaxGarbageLines(params); this.maxGarbageLines = getMaxGarbageLines(params);
} }
/**
* @since 4.2
*/
@SuppressWarnings("deprecation")
protected int getMaxGarbageLines(final HttpParams params) { protected int getMaxGarbageLines(final HttpParams params) {
return params.getIntParameter( return params.getIntParameter(
org.apache.http.conn.params.ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE, org.apache.http.conn.params.ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE,
@ -94,7 +92,7 @@ public class DefaultResponseParser extends AbstractMessageParser<HttpResponse> {
} }
@Override @Override
protected HttpResponse parseHead( protected HttpMessage parseHead(
final SessionInputBuffer sessionBuffer) throws IOException, HttpException { final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
//read out the HTTP status string //read out the HTTP status string
int count = 0; int count = 0;

View File

@ -39,13 +39,14 @@ import org.apache.http.message.BasicLineParser;
import org.apache.http.mockup.SessionInputBufferMockup; import org.apache.http.mockup.SessionInputBufferMockup;
import org.apache.http.params.BasicHttpParams; import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.util.CharArrayBuffer;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** /**
* Tests for <code>DefaultResponseParser</code>. * Tests for <code>DefaultResponseParser</code>.
*/ */
public class TestDefaultResponseParser { public class TestDefaultHttpResponseParser {
@Test @Test
public void testResponseParsingWithSomeGarbage() throws Exception { public void testResponseParsingWithSomeGarbage() throws Exception {
@ -60,7 +61,7 @@ public class TestDefaultResponseParser {
HttpParams params = new BasicHttpParams(); HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params); SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
HttpMessageParser<HttpResponse> parser = new DefaultResponseParser( HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
inbuffer, inbuffer,
BasicLineParser.DEFAULT, BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(), new DefaultHttpResponseFactory(),
@ -91,15 +92,15 @@ public class TestDefaultResponseParser {
HttpParams params = new BasicHttpParams(); HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params); SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
HttpMessageParser<HttpResponse> parser = new DefaultResponseParser( HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
inbuffer, inbuffer,
BasicLineParser.DEFAULT, BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(), new DefaultHttpResponseFactory(),
params) { params) {
@Override @Override
protected int getMaxGarbageLines(HttpParams params) { protected boolean reject(final CharArrayBuffer line, int count) {
return 2; return count >= 2;
} }
}; };
@ -110,7 +111,7 @@ public class TestDefaultResponseParser {
public void testResponseParsingNoResponse() throws Exception { public void testResponseParsingNoResponse() throws Exception {
HttpParams params = new BasicHttpParams(); HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup("", "US-ASCII", params); SessionInputBuffer inbuffer = new SessionInputBufferMockup("", "US-ASCII", params);
HttpMessageParser<HttpResponse> parser = new DefaultResponseParser( HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
inbuffer, inbuffer,
BasicLineParser.DEFAULT, BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(), new DefaultHttpResponseFactory(),
@ -127,7 +128,7 @@ public class TestDefaultResponseParser {
"a lot more garbage\r\n"; "a lot more garbage\r\n";
HttpParams params = new BasicHttpParams(); HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params); SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
HttpMessageParser<HttpResponse> parser = new DefaultResponseParser( HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(
inbuffer, inbuffer,
BasicLineParser.DEFAULT, BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(), new DefaultHttpResponseFactory(),