Skip to content

Commit

Permalink
volley edit to get all response headers like set-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiecasey committed Oct 30, 2013
1 parent c69c01b commit 9f3cf1c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .classpath
Expand Up @@ -3,6 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
1 change: 1 addition & 0 deletions project.properties
Expand Up @@ -13,3 +13,4 @@ target=android-14
# Make sure to pass a valid value to renderscript
# https://code.google.com/p/android/issues/detail?id=40487
renderscript.opt.level=O0
android.library=true
23 changes: 21 additions & 2 deletions src/com/android/volley/NetworkResponse.java
Expand Up @@ -16,11 +16,12 @@

package com.android.volley;

import org.apache.http.HttpStatus;

import java.util.Collections;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpStatus;

/**
* Data and headers returned from {@link Network#performRequest(Request)}.
*/
Expand All @@ -38,6 +39,16 @@ public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,
this.data = data;
this.headers = headers;
this.notModified = notModified;
this.apacheHeaders=null;
}

public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,
boolean notModified, Header[] apacheHeaders) {
this.statusCode = statusCode;
this.data = data;
this.headers = headers;
this.notModified = notModified;
this.apacheHeaders=apacheHeaders;
}

public NetworkResponse(byte[] data) {
Expand All @@ -59,4 +70,12 @@ public NetworkResponse(byte[] data, Map<String, String> headers) {

/** True if the server returned a 304 (Not Modified). */
public final boolean notModified;

/** added by georgiecasey
/* https://github.com/georgiecasey
/* fix for getting duplicate header responses like multiple Set-Cookie
/* http://stackoverflow.com/questions/18998361/android-volley-duplicate-set-cookie-is-overriden
*/
public final Header[] apacheHeaders;

}
43 changes: 24 additions & 19 deletions src/com/android/volley/toolbox/BasicNetwork.java
Expand Up @@ -16,7 +16,24 @@

package com.android.volley.toolbox;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.cookie.DateUtils;

import android.os.SystemClock;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Cache;
Expand All @@ -31,22 +48,6 @@
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.cookie.DateUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* A network performing Volley requests over an {@link HttpStack}.
*/
Expand Down Expand Up @@ -91,14 +92,18 @@ public NetworkResponse performRequest(Request<?> request) throws VolleyError {
Map<String, String> headers = new HashMap<String, String>();
addCacheHeaders(headers, request.getCacheEntry());
httpResponse = mHttpStack.performRequest(request, headers);
Header[] header_test=httpResponse.getAllHeaders();
for (int i = 0; i < header_test.length; i++) {
Log.d("GEORGEI_VOLLEY",header_test[i].getName() + " - " +header_test[i].getValue());
}
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();

responseHeaders = convertHeaders(httpResponse.getAllHeaders());
// Handle cache validation.
if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,
request.getCacheEntry().data, responseHeaders, true);
request.getCacheEntry().data, responseHeaders, true, httpResponse.getAllHeaders());
}

// Some responses such as 204s do not have content. We must check.
Expand All @@ -117,7 +122,7 @@ public NetworkResponse performRequest(Request<?> request) throws VolleyError {
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
return new NetworkResponse(statusCode, responseContents, responseHeaders, false, httpResponse.getAllHeaders());
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (ConnectTimeoutException e) {
Expand All @@ -135,7 +140,7 @@ public NetworkResponse performRequest(Request<?> request) throws VolleyError {
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents,
responseHeaders, false);
responseHeaders, false, httpResponse.getAllHeaders());
if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
statusCode == HttpStatus.SC_FORBIDDEN) {
attemptRetryOnException("auth",
Expand Down
10 changes: 6 additions & 4 deletions src/com/android/volley/toolbox/HurlStack.java
Expand Up @@ -117,10 +117,12 @@ public HttpResponse performRequest(Request<?> request, Map<String, String> addit
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
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);
}
if (header.getKey() != null) {
for (String headerValue : header.getValue()) {
Header h = new BasicHeader(header.getKey(), headerValue);
response.addHeader(h);
}
}
}
return response;
}
Expand Down

5 comments on commit 9f3cf1c

@georgiecasey
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the commit with the changes to Volley to fix the duplicate headers problem.

@chrisghanem
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works Great! Thanks!

@faken
Copy link

@faken faken commented on 9f3cf1c Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty nice fix - thank you!

@laurencedawson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Noticed a small bug, you'll need to modify DiskBasedCache & Cache to ensure the "apacheHeaders" array is saved to disk when the request is cached. If I get time later I'll share the modifications!

@georgiecasey
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laurencedawson cool, submit a pull request if you get it done.

Please sign in to comment.