summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJukka Zitting <jlz@google.com>2015-07-16 18:58:15 -0400
committerJukka Zitting <jlz@google.com>2015-07-16 18:58:31 -0400
commitd3207beaf19336cda461da53d1bdf1ba3d57df12 (patch)
tree6eb501e976f860d38911bdd9b8ca83caaae4e2ee
parent2bffbe7eac3c1473e16ef3ef79ad6417f0b76c13 (diff)
downloadvolley-d3207beaf19336cda461da53d1bdf1ba3d57df12.tar.gz
Process response bodies only when present
Responses to HEAD requests and any informational (1xx), no content (204) and not modified (304) responses contain no bodies. This change skips the response.setEntity(entityFromConnection(connection)) call when dealing with such responses. This fixes a problem in BasicNetwork where a response without a body is detected by checking whether the entity is null. Bug: 176674 - volley java.lang.IllegalStateException: Content has not been provided Change-Id: I1b0ce77461ce5bedc7f6a53fd63311ea93ec5af0
-rw-r--r--src/main/java/com/android/volley/toolbox/HurlStack.java19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/main/java/com/android/volley/toolbox/HurlStack.java b/src/main/java/com/android/volley/toolbox/HurlStack.java
index 31d57f0..1be202e 100644
--- a/src/main/java/com/android/volley/toolbox/HurlStack.java
+++ b/src/main/java/com/android/volley/toolbox/HurlStack.java
@@ -23,6 +23,7 @@ import com.android.volley.Request.Method;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
@@ -115,7 +116,9 @@ public class HurlStack implements HttpStack {
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
- response.setEntity(entityFromConnection(connection));
+ if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
+ response.setEntity(entityFromConnection(connection));
+ }
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
@@ -126,6 +129,20 @@ public class HurlStack implements HttpStack {
}
/**
+ * Checks if a response message contains a body.
+ * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
+ * @param requestMethod request method
+ * @param responseCode response status code
+ * @return whether the response has a body
+ */
+ private static boolean hasResponseBody(int requestMethod, int responseCode) {
+ return requestMethod != Request.Method.HEAD
+ && !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK)
+ && responseCode != HttpStatus.SC_NO_CONTENT
+ && responseCode != HttpStatus.SC_NOT_MODIFIED;
+ }
+
+ /**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.