Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

fetch multiple header values for the same header name #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/main/java/com/android/volley/toolbox/BasicNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -178,7 +178,7 @@ public NetworkResponse performRequest(Request<?> request) throws VolleyError {
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(networkResponse);
throw new NetworkError();
}
}
}
Expand Down Expand Up @@ -273,8 +273,13 @@ private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError
*/
protected static Map<String, String> convertHeaders(Header[] headers) {
Map<String, String> result = new TreeMap<String, String>(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;
}
Expand Down
31 changes: 23 additions & 8 deletions src/main/java/com/android/volley/toolbox/HurlStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -115,19 +116,33 @@ public HttpResponse performRequest(Request<?> request, Map<String, String> 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<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 response;
}

private Header[] headersFromHeaderFields(Map<String, List<String>> headerFields) {
List<Header> headersList = new ArrayList<Header>();
for (Entry<String, List<String>> headerField : headerFields.entrySet()) {
headersList.addAll(headersFromHeaderField(headerField));
}
return headersList.toArray(new Header[headersList.size()]);
}

private List<Header> headersFromHeaderField(Entry<String, List<String>> headerField) {
List<Header> headers = new ArrayList<Header>();
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 <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
Expand All @@ -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
*/
Expand Down