From eb6c14d3ec597243b071642df5a48d6e472f448f Mon Sep 17 00:00:00 2001 From: David Hamm Date: Fri, 26 Jun 2015 11:17:10 +0200 Subject: [PATCH] fetch multiple header values for the same header name --- .../android/volley/toolbox/BasicNetwork.java | 19 +++++++----- .../com/android/volley/toolbox/HurlStack.java | 31 ++++++++++++++----- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/android/volley/toolbox/BasicNetwork.java b/src/main/java/com/android/volley/toolbox/BasicNetwork.java index 90b981ba..eb283b2f 100644 --- a/src/main/java/com/android/volley/toolbox/BasicNetwork.java +++ b/src/main/java/com/android/volley/toolbox/BasicNetwork.java @@ -56,9 +56,9 @@ public class BasicNetwork implements Network { protected static final boolean DEBUG = VolleyLog.DEBUG; - private static int SLOW_REQUEST_THRESHOLD_MS = 3000; + private static final int SLOW_REQUEST_THRESHOLD_MS = 3000; - private static int DEFAULT_POOL_SIZE = 4096; + private static final int DEFAULT_POOL_SIZE = 4096; protected final HttpStack mHttpStack; @@ -149,8 +149,8 @@ public NetworkResponse performRequest(Request request) throws VolleyError { } catch (MalformedURLException e) { throw new RuntimeException("Bad URL " + request.getUrl(), e); } catch (IOException e) { - int statusCode = 0; - NetworkResponse networkResponse = null; + int statusCode; + NetworkResponse networkResponse; if (httpResponse != null) { statusCode = httpResponse.getStatusLine().getStatusCode(); } else { @@ -178,7 +178,7 @@ public NetworkResponse performRequest(Request request) throws VolleyError { throw new ServerError(networkResponse); } } else { - throw new NetworkError(networkResponse); + throw new NetworkError(); } } } @@ -273,8 +273,13 @@ private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError */ protected static Map convertHeaders(Header[] headers) { Map result = new TreeMap(String.CASE_INSENSITIVE_ORDER); - for (int i = 0; i < headers.length; i++) { - result.put(headers[i].getName(), headers[i].getValue()); + for (Header header : headers) { + String value = result.get(header.getName()); + if (value == null) { + result.put(header.getName(), header.getValue()); + } else { + result.put(header.getName(), value + ", " + header.getValue()); + } } return result; } diff --git a/src/main/java/com/android/volley/toolbox/HurlStack.java b/src/main/java/com/android/volley/toolbox/HurlStack.java index 31d57f0d..9c268e9b 100644 --- a/src/main/java/com/android/volley/toolbox/HurlStack.java +++ b/src/main/java/com/android/volley/toolbox/HurlStack.java @@ -35,6 +35,7 @@ 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; @@ -58,7 +59,7 @@ public interface UrlRewriter { * Returns a URL to use instead of the provided one, or null to indicate * this URL should not be used at all. */ - public String rewriteUrl(String originalUrl); + String rewriteUrl(String originalUrl); } private final UrlRewriter mUrlRewriter; @@ -115,19 +116,33 @@ public HttpResponse performRequest(Request request, Map addit StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); + response.setHeaders(headersFromHeaderFields(connection.getHeaderFields())); response.setEntity(entityFromConnection(connection)); - for (Entry> header : connection.getHeaderFields().entrySet()) { - if (header.getKey() != null) { - Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); - response.addHeader(h); + return response; + } + + private Header[] headersFromHeaderFields(Map> headerFields) { + List
headersList = new ArrayList
(); + for (Entry> headerField : headerFields.entrySet()) { + headersList.addAll(headersFromHeaderField(headerField)); + } + return headersList.toArray(new Header[headersList.size()]); + } + + private List
headersFromHeaderField(Entry> headerField) { + List
headers = new ArrayList
(); + String headerName = headerField.getKey(); + if (headerName != null) { + for (String headerValue : headerField.getValue()) { + headers.add(new BasicHeader(headerName, headerValue)); } } - return response; + return headers; } /** * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}. - * @param connection + * @param connection the connection where the HttpEntity gets created from * @return an HttpEntity populated with data from connection. */ private static HttpEntity entityFromConnection(HttpURLConnection connection) { @@ -154,7 +169,7 @@ protected HttpURLConnection createConnection(URL url) throws IOException { /** * Opens an {@link HttpURLConnection} with parameters. - * @param url + * @param url the url to which the connection should be opened * @return an open connection * @throws IOException */