diff options
author | Jukka Zitting <jlz@google.com> | 2015-07-16 18:58:15 -0400 |
---|---|---|
committer | Jukka Zitting <jlz@google.com> | 2015-07-16 18:58:31 -0400 |
commit | d3207beaf19336cda461da53d1bdf1ba3d57df12 (patch) | |
tree | 6eb501e976f860d38911bdd9b8ca83caaae4e2ee /src/main/java/com/android/volley | |
parent | 2bffbe7eac3c1473e16ef3ef79ad6417f0b76c13 (diff) | |
download | volley-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
Diffstat (limited to 'src/main/java/com/android/volley')
-rw-r--r-- | src/main/java/com/android/volley/toolbox/HurlStack.java | 19 |
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>. |