aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/android/volley/toolbox/HttpHeaderParser.java')
-rw-r--r--src/main/java/com/android/volley/toolbox/HttpHeaderParser.java66
1 files changed, 57 insertions, 9 deletions
diff --git a/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java b/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java
index f53063c..211c329 100644
--- a/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java
+++ b/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java
@@ -17,19 +17,31 @@
package com.android.volley.toolbox;
import com.android.volley.Cache;
+import com.android.volley.Header;
import com.android.volley.NetworkResponse;
-
-import org.apache.http.impl.cookie.DateParseException;
-import org.apache.http.impl.cookie.DateUtils;
-import org.apache.http.protocol.HTTP;
-
+import com.android.volley.VolleyLog;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
import java.util.Map;
+import java.util.TimeZone;
+import java.util.TreeMap;
/**
* Utility methods for parsing HTTP headers.
*/
public class HttpHeaderParser {
+ static final String HEADER_CONTENT_TYPE = "Content-Type";
+
+ private static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
+
+ private static final String RFC1123_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
+
/**
* Extracts a {@link com.android.volley.Cache.Entry} from a {@link NetworkResponse}.
*
@@ -116,6 +128,7 @@ public class HttpHeaderParser {
entry.serverDate = serverDate;
entry.lastModified = lastModified;
entry.responseHeaders = headers;
+ entry.allResponseHeaders = response.allHeaders;
return entry;
}
@@ -126,13 +139,26 @@ public class HttpHeaderParser {
public static long parseDateAsEpoch(String dateStr) {
try {
// Parse date in RFC1123 format if this header contains one
- return DateUtils.parseDate(dateStr).getTime();
- } catch (DateParseException e) {
+ return newRfc1123Formatter().parse(dateStr).getTime();
+ } catch (ParseException e) {
// Date in invalid format, fallback to 0
+ VolleyLog.e(e, "Unable to parse dateStr: %s, falling back to 0", dateStr);
return 0;
}
}
+ /** Format an epoch date in RFC1123 format. */
+ static String formatEpochAsRfc1123(long epoch) {
+ return newRfc1123Formatter().format(new Date(epoch));
+ }
+
+ private static SimpleDateFormat newRfc1123Formatter() {
+ SimpleDateFormat formatter =
+ new SimpleDateFormat(RFC1123_FORMAT, Locale.US);
+ formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
+ return formatter;
+ }
+
/**
* Retrieve a charset from headers
*
@@ -142,7 +168,7 @@ public class HttpHeaderParser {
* or the defaultCharset if none can be found.
*/
public static String parseCharset(Map<String, String> headers, String defaultCharset) {
- String contentType = headers.get(HTTP.CONTENT_TYPE);
+ String contentType = headers.get(HEADER_CONTENT_TYPE);
if (contentType != null) {
String[] params = contentType.split(";");
for (int i = 1; i < params.length; i++) {
@@ -163,6 +189,28 @@ public class HttpHeaderParser {
* or the HTTP default (ISO-8859-1) if none can be found.
*/
public static String parseCharset(Map<String, String> headers) {
- return parseCharset(headers, HTTP.DEFAULT_CONTENT_CHARSET);
+ return parseCharset(headers, DEFAULT_CONTENT_CHARSET);
+ }
+
+ // Note - these are copied from NetworkResponse to avoid making them public (as needed to access
+ // them from the .toolbox package), which would mean they'd become part of the Volley API.
+ // TODO: Consider obfuscating official releases so we can share utility methods between Volley
+ // and Toolbox without making them public APIs.
+
+ static Map<String, String> toHeaderMap(List<Header> allHeaders) {
+ Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
+ // Later elements in the list take precedence.
+ for (Header header : allHeaders) {
+ headers.put(header.getName(), header.getValue());
+ }
+ return headers;
+ }
+
+ static List<Header> toAllHeaderList(Map<String, String> headers) {
+ List<Header> allHeaders = new ArrayList<>(headers.size());
+ for (Map.Entry<String, String> header : headers.entrySet()) {
+ allHeaders.add(new Header(header.getKey(), header.getValue()));
+ }
+ return allHeaders;
}
}