From aa412a29b934ef51c69ba41701e9bceca8568793 Mon Sep 17 00:00:00 2001
From: Oleg Kalnichevski
Date: Sun, 6 May 2012 14:41:38 +0000
Subject: [PATCH] Deprecated DefaultResponseParser in favor of
DefaultHttpResponseParser
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1334646 13f79535-47bb-0310-9956-ffa450edef68
---
.../conn/params/ConnConnectionPNames.java | 4 +-
.../impl/conn/DefaultClientConnection.java | 2 +-
.../impl/conn/DefaultHttpResponseParser.java | 120 ++++++++++++++++++
.../http/impl/conn/DefaultResponseParser.java | 12 +-
...ava => TestDefaultHttpResponseParser.java} | 15 ++-
5 files changed, 136 insertions(+), 17 deletions(-)
create mode 100644 httpclient/src/main/java/org/apache/http/impl/conn/DefaultHttpResponseParser.java
rename httpclient/src/test/java/org/apache/http/impl/conn/{TestDefaultResponseParser.java => TestDefaultHttpResponseParser.java} (89%)
diff --git a/httpclient/src/main/java/org/apache/http/conn/params/ConnConnectionPNames.java b/httpclient/src/main/java/org/apache/http/conn/params/ConnConnectionPNames.java
index 7c46bf1f5..a6d66b00f 100644
--- a/httpclient/src/main/java/org/apache/http/conn/params/ConnConnectionPNames.java
+++ b/httpclient/src/main/java/org/apache/http/conn/params/ConnConnectionPNames.java
@@ -26,7 +26,7 @@
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.
@@ -53,7 +53,7 @@ public interface ConnConnectionPNames {
* Use {@link java.lang.Integer#MAX_VALUE} for unlimited number.
*
*
- * @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";
diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java
index b6ed73729..4b612d9de 100644
--- a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java
+++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java
@@ -221,7 +221,7 @@ public class DefaultClientConnection extends SocketHttpClientConnection
final HttpResponseFactory responseFactory,
final HttpParams params) {
// override in derived class to specify a line parser
- return new DefaultResponseParser
+ return new DefaultHttpResponseParser
(buffer, null, responseFactory, params);
}
diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultHttpResponseParser.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultHttpResponseParser.java
new file mode 100644
index 000000000..98b983175
--- /dev/null
+++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultHttpResponseParser.java
@@ -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
+ * .
+ *
+ */
+
+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.
+ *
+ * The following parameters can be used to customize the behavior of this
+ * class:
+ *
+ * - {@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}
+ * - {@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}
+ *
+ *
+ * @since 4.2
+ */
+@ThreadSafe // no public methods
+public class DefaultHttpResponseParser extends AbstractMessageParser {
+
+ 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;
+ }
+
+}
diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java
index 001b965b5..894fa13ad 100644
--- a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java
+++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java
@@ -34,7 +34,7 @@ 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.HttpMessage;
import org.apache.http.HttpResponseFactory;
import org.apache.http.NoHttpResponseException;
import org.apache.http.ProtocolException;
@@ -58,9 +58,11 @@ import org.apache.http.util.CharArrayBuffer;
*
*
* @since 4.0
+ *
+ * @deprecated (4.2) use {@link DefaultHttpResponseParser}
*/
@ThreadSafe // no public methods
-public class DefaultResponseParser extends AbstractMessageParser {
+public class DefaultResponseParser extends AbstractMessageParser {
private final Log log = LogFactory.getLog(getClass());
@@ -83,10 +85,6 @@ public class DefaultResponseParser extends AbstractMessageParser {
this.maxGarbageLines = getMaxGarbageLines(params);
}
- /**
- * @since 4.2
- */
- @SuppressWarnings("deprecation")
protected int getMaxGarbageLines(final HttpParams params) {
return params.getIntParameter(
org.apache.http.conn.params.ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE,
@@ -94,7 +92,7 @@ public class DefaultResponseParser extends AbstractMessageParser {
}
@Override
- protected HttpResponse parseHead(
+ protected HttpMessage parseHead(
final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
//read out the HTTP status string
int count = 0;
diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestDefaultResponseParser.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestDefaultHttpResponseParser.java
similarity index 89%
rename from httpclient/src/test/java/org/apache/http/impl/conn/TestDefaultResponseParser.java
rename to httpclient/src/test/java/org/apache/http/impl/conn/TestDefaultHttpResponseParser.java
index 4fb2766f0..4286a56c9 100644
--- a/httpclient/src/test/java/org/apache/http/impl/conn/TestDefaultResponseParser.java
+++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestDefaultHttpResponseParser.java
@@ -39,13 +39,14 @@ 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 DefaultResponseParser
.
*/
-public class TestDefaultResponseParser {
+public class TestDefaultHttpResponseParser {
@Test
public void testResponseParsingWithSomeGarbage() throws Exception {
@@ -60,7 +61,7 @@ public class TestDefaultResponseParser {
HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
- HttpMessageParser parser = new DefaultResponseParser(
+ HttpMessageParser parser = new DefaultHttpResponseParser(
inbuffer,
BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(),
@@ -91,15 +92,15 @@ public class TestDefaultResponseParser {
HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
- HttpMessageParser parser = new DefaultResponseParser(
+ HttpMessageParser parser = new DefaultHttpResponseParser(
inbuffer,
BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(),
params) {
@Override
- protected int getMaxGarbageLines(HttpParams params) {
- return 2;
+ protected boolean reject(final CharArrayBuffer line, int count) {
+ return count >= 2;
}
};
@@ -110,7 +111,7 @@ public class TestDefaultResponseParser {
public void testResponseParsingNoResponse() throws Exception {
HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup("", "US-ASCII", params);
- HttpMessageParser parser = new DefaultResponseParser(
+ HttpMessageParser parser = new DefaultHttpResponseParser(
inbuffer,
BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(),
@@ -127,7 +128,7 @@ public class TestDefaultResponseParser {
"a lot more garbage\r\n";
HttpParams params = new BasicHttpParams();
SessionInputBuffer inbuffer = new SessionInputBufferMockup(s, "US-ASCII", params);
- HttpMessageParser parser = new DefaultResponseParser(
+ HttpMessageParser parser = new DefaultHttpResponseParser(
inbuffer,
BasicLineParser.DEFAULT,
new DefaultHttpResponseFactory(),