aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/volley/toolbox/HurlStack.java
diff options
context:
space:
mode:
authorAnonymous <no-reply@google.com>2017-09-25 10:46:08 -0700
committerJeff Davidson <jpd@google.com>2017-09-25 10:50:44 -0700
commitac8d9a1d940de5b5335c82c56dd42dec728dd443 (patch)
treefb13707e6dfdd4c3a4cbee1f66749bf6066b0dad /src/main/java/com/android/volley/toolbox/HurlStack.java
parent8b42d211a58fc5b434921a51cae2c99a806e00eb (diff)
downloadvolley-ac8d9a1d940de5b5335c82c56dd42dec728dd443.tar.gz
Import of Volley from GitHub to AOSP.
- 5fb28f66748df4f89b49c1493693d1f65c6bb23e Fix NPEs/compile errors introduced by header changes. (#96) by Jeff Davidson <jpd236@cornell.edu> - e16a426da3bcffb1a8de1700ddbe69201540d93c Fix RequestQueueIntegrationTest flakiness. (#94) by Jeff Davidson <jpd236@cornell.edu> - 96feb3b09a6301b9573212027af21db7be5c8be1 Improve Volley header handling. (#91) by Jeff Davidson <jpd236@cornell.edu> - a794c075a62ddf438178b2e15cc79ab5502588fb For waiting requests, use ArrayList instead of LinkedList... by Ulrike Hager <uhager42@gmail.com> - 787ef0cc731c28b528f744dea64bd5429f99e153 Specify .aar packaging in SNAPSHOT POM. by Jeff Davidson <jpd236@cornell.edu> - b2bb59ab2ff08f4d468303071f050ce938349379 Fix soft TTL for duplicate requests (#73) by Ulrike Hager <uhager42@gmail.com> - b33a53f1793b475842f91a0fe166749118afcfc0 Deprecate Volley's use of Apache HTTP. (#75) by Jeff Davidson <jpd236@cornell.edu> GitOrigin-RevId: 5fb28f66748df4f89b49c1493693d1f65c6bb23e Change-Id: Ia04d2967e9923d2430a04f2474aa69ce82e114ce
Diffstat (limited to 'src/main/java/com/android/volley/toolbox/HurlStack.java')
-rw-r--r--src/main/java/com/android/volley/toolbox/HurlStack.java72
1 files changed, 33 insertions, 39 deletions
diff --git a/src/main/java/com/android/volley/toolbox/HurlStack.java b/src/main/java/com/android/volley/toolbox/HurlStack.java
index 66f441d..a975a71 100644
--- a/src/main/java/com/android/volley/toolbox/HurlStack.java
+++ b/src/main/java/com/android/volley/toolbox/HurlStack.java
@@ -17,29 +17,19 @@
package com.android.volley.toolbox;
import com.android.volley.AuthFailureError;
+import com.android.volley.Header;
import com.android.volley.Request;
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;
-import org.apache.http.message.BasicHeader;
-import org.apache.http.message.BasicHttpResponse;
-import org.apache.http.message.BasicStatusLine;
-
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
@@ -47,9 +37,9 @@ import javax.net.ssl.SSLSocketFactory;
/**
* An {@link HttpStack} based on {@link HttpURLConnection}.
*/
-public class HurlStack implements HttpStack {
+public class HurlStack extends BaseHttpStack {
- private static final String HEADER_CONTENT_TYPE = "Content-Type";
+ private static final int HTTP_CONTINUE = 100;
/**
* An interface for transforming URLs before use.
@@ -86,10 +76,10 @@ public class HurlStack implements HttpStack {
}
@Override
- public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
+ public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
- HashMap<String, String> map = new HashMap<String, String>();
+ HashMap<String, String> map = new HashMap<>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
@@ -106,26 +96,34 @@ public class HurlStack implements HttpStack {
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
- ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
- StatusLine responseStatus = new BasicStatusLine(protocolVersion,
- connection.getResponseCode(), connection.getResponseMessage());
- BasicHttpResponse response = new BasicHttpResponse(responseStatus);
- if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
- response.setEntity(entityFromConnection(connection));
+
+ if (!hasResponseBody(request.getMethod(), responseCode)) {
+ return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields()));
}
- for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
- if (header.getKey() != null) {
- Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
- response.addHeader(h);
+
+ return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields()),
+ connection.getContentLength(), inputStreamFromConnection(connection));
+ }
+
+ // VisibleForTesting
+ static List<Header> convertHeaders(Map<String, List<String>> responseHeaders) {
+ List<Header> headerList = new ArrayList<>(responseHeaders.size());
+ for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) {
+ // HttpUrlConnection includes the status line as a header with a null key; omit it here
+ // since it's not really a header and the rest of Volley assumes non-null keys.
+ if (entry.getKey() != null) {
+ for (String value : entry.getValue()) {
+ headerList.add(new Header(entry.getKey(), value));
+ }
}
}
- return response;
+ return headerList;
}
/**
@@ -137,29 +135,24 @@ public class HurlStack implements HttpStack {
*/
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;
+ && !(HTTP_CONTINUE <= responseCode && responseCode < HttpURLConnection.HTTP_OK)
+ && responseCode != HttpURLConnection.HTTP_NO_CONTENT
+ && responseCode != HttpURLConnection.HTTP_NOT_MODIFIED;
}
/**
- * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
+ * Initializes an {@link InputStream} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
- private static HttpEntity entityFromConnection(HttpURLConnection connection) {
- BasicHttpEntity entity = new BasicHttpEntity();
+ private static InputStream inputStreamFromConnection(HttpURLConnection connection) {
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
- entity.setContent(inputStream);
- entity.setContentLength(connection.getContentLength());
- entity.setContentEncoding(connection.getContentEncoding());
- entity.setContentType(connection.getContentType());
- return entity;
+ return inputStream;
}
/**
@@ -261,7 +254,8 @@ public class HurlStack implements HttpStack {
// since this is handled by HttpURLConnection using the size of the prepared
// output stream.
connection.setDoOutput(true);
- connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
+ connection.addRequestProperty(
+ HttpHeaderParser.HEADER_CONTENT_TYPE, request.getBodyContentType());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(body);
out.close();