diff options
author | Ficus Kirkpatrick <ficus@android.com> | 2015-07-17 01:39:34 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-07-17 01:39:34 +0000 |
commit | 9f2034c336f699976d007006a7ac1f8b501f2294 (patch) | |
tree | 6eb501e976f860d38911bdd9b8ca83caaae4e2ee | |
parent | ef3cafe15faf2b295059b1c2784f8b88998d46ff (diff) | |
parent | 93eaf5c474fc2d6bab50ed0145b0b5941f2e0cdc (diff) | |
download | volley-9f2034c336f699976d007006a7ac1f8b501f2294.tar.gz |
am 93eaf5c4: Merge "Process response bodies only when present"
* commit '93eaf5c474fc2d6bab50ed0145b0b5941f2e0cdc':
Process response bodies only when present
-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>. |