diff options
author | Anonymous <no-reply@google.com> | 2020-10-02 16:21:32 -0700 |
---|---|---|
committer | Jeff Davidson <jpd@google.com> | 2020-10-07 01:33:22 +0000 |
commit | b6bd7aa39db62c517d97ff0a97d8d073bdf83a47 (patch) | |
tree | b9b7a6b9928136e83fda62aaa8b8887df88839b5 /src/main/java/com/android/volley/toolbox/HttpHeaderParser.java | |
parent | dc9062756c4835ac62ade2f3fb8ace20e8dbf3a6 (diff) | |
download | volley-b6bd7aa39db62c517d97ff0a97d8d073bdf83a47.tar.gz |
Import of Volley from GitHub to AOSP.
Includes AsyncRequestQueue and asynchronous infrastructure, but the Cronet-based components are
excluded from Android.bp as Cronet is not broadly exposed for application usage.
- ae701b33e718782e914108f9b6d1f1bb5f1ab7f8 Fix empty responses when content length is unknown. by Jeff Davidson <jpd@google.com>
- 055fc535255367e5556d6274aadcbb5cd3be5ed3 Add hook to allow customization of UrlRequest.Builder. by Jeff Davidson <jpd@google.com>
- bc64707525d43b2ddc1cad08a3445cc34111c47f Set Content-Type header for requests with bodies. (#371) by Jeff Davidson <jpd@google.com>
- 4083c43bcfc8cc31be709574f6d1b923c2bf869d Add cURL logging support to CronetHttpStack. (#370) by Jeff Davidson <jpd@google.com>
- 46a4f9040126940be79b10633be470dea32958a3 Fix dependencies from base Volley package to toolbox pack... by Jeff Davidson <jpd@google.com>
- cde6d4367210819f357b1397d17b4656b773a0d9 Provide a ScheduledExecutorService to AsyncRequestQueue/A... by Jeff Davidson <jpd@google.com>
- f582ee6f7b7067b6bce21239f904f3066b778dad Move CronetHttpStack tests to the expected location. (#368) by Jeff Davidson <jpd@google.com>
- a4954c6661b01a5950f973d873b8e21d2a5dcc40 Revert DiskBasedCache. (#366) by Jeff Davidson <jpd@google.com>
- 7b0a3119b499989c31611a8fb944015aa9a400c5 AsyncRequestQueue implementation + some tests (#361) by sphill99 <s.phill99@gmail.com>
- d777e8a7bc4136b311913178021894adec73bfdb Created builder for DiskBasedAsyncCache (#360) by sphill99 <s.phill99@gmail.com>
- 3ff2427da1db5c5a46c1dfa0aaf7c097258be7ce AsyncNetwork implementation as well as unit testing (#357) by sphill99 <s.phill99@gmail.com>
- bb85de2f41a1bf9fe8b1ef15a41a9057947a0724 Added licensing headers for all newly created files (#358) by sphill99 <s.phill99@gmail.com>
- 82c828d65a8f317a55a1a7b067a60df8fe566a02 AsyncHttpStack and Cronet implementation. Limited unit te... by sphill99 <s.phill99@gmail.com>
- d0fb8dea486cc9b164cf140661225160dd5454d7 Implementations for rest of AsyncCache, tests for cache m... by sphill99 <s.phill99@gmail.com>
- 94e3a4da70cf1803426de6a2579676966c670d78 Include license in generated POM file. (#352) by Jeff Davidson <jpd@google.com>
- cae1570bc5d96cd2b840b39e5c742a397c76b45f Put implementation for async cache that uses callback met... by sphill99 <s.phill99@gmail.com>
- a791d09a6747ca4923352405b20c229eafad5866 Get Implementation in AsyncCache that takes in a callback... by sphill99 <s.phill99@gmail.com>
- 504e41a1c749ebbf3b63b6ab58533c8219cb6df1 Retry on NoConnectionError (#342) by sphill99 <s.phill99@gmail.com>
- 40d4ffb7ec5dab4cfb32d6d19db8f45419742671 Fixed testCompile and testApi deprecations (#341) by sphill99 <s.phill99@gmail.com>
- 7c767521db6aab17cb706d114570a557dc1c1efa Fix Android Studio project import of Volley. (#335) by Jeff Davidson <jpd@google.com>
GitOrigin-RevId: ae701b33e718782e914108f9b6d1f1bb5f1ab7f8
Change-Id: I8f5792f679e88591c213156dcdd13482105e1c53
Diffstat (limited to 'src/main/java/com/android/volley/toolbox/HttpHeaderParser.java')
-rw-r--r-- | src/main/java/com/android/volley/toolbox/HttpHeaderParser.java | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java b/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java index 1b410af..0b29e80 100644 --- a/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java +++ b/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java @@ -17,6 +17,8 @@ package com.android.volley.toolbox; import androidx.annotation.Nullable; +import androidx.annotation.RestrictTo; +import androidx.annotation.RestrictTo.Scope; import com.android.volley.Cache; import com.android.volley.Header; import com.android.volley.NetworkResponse; @@ -24,17 +26,22 @@ import com.android.volley.VolleyLog; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.TimeZone; import java.util.TreeMap; +import java.util.TreeSet; /** Utility methods for parsing HTTP headers. */ public class HttpHeaderParser { - static final String HEADER_CONTENT_TYPE = "Content-Type"; + @RestrictTo({Scope.LIBRARY_GROUP}) + public static final String HEADER_CONTENT_TYPE = "Content-Type"; private static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1"; @@ -226,4 +233,69 @@ public class HttpHeaderParser { } return allHeaders; } + + /** + * Combine cache headers with network response headers for an HTTP 304 response. + * + * <p>An HTTP 304 response does not have all header fields. We have to use the header fields + * from the cache entry plus the new ones from the response. See also: + * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5 + * + * @param responseHeaders Headers from the network response. + * @param entry The cached response. + * @return The combined list of headers. + */ + static List<Header> combineHeaders(List<Header> responseHeaders, Cache.Entry entry) { + // First, create a case-insensitive set of header names from the network + // response. + Set<String> headerNamesFromNetworkResponse = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + if (!responseHeaders.isEmpty()) { + for (Header header : responseHeaders) { + headerNamesFromNetworkResponse.add(header.getName()); + } + } + + // Second, add headers from the cache entry to the network response as long as + // they didn't appear in the network response, which should take precedence. + List<Header> combinedHeaders = new ArrayList<>(responseHeaders); + if (entry.allResponseHeaders != null) { + if (!entry.allResponseHeaders.isEmpty()) { + for (Header header : entry.allResponseHeaders) { + if (!headerNamesFromNetworkResponse.contains(header.getName())) { + combinedHeaders.add(header); + } + } + } + } else { + // Legacy caches only have entry.responseHeaders. + if (!entry.responseHeaders.isEmpty()) { + for (Map.Entry<String, String> header : entry.responseHeaders.entrySet()) { + if (!headerNamesFromNetworkResponse.contains(header.getKey())) { + combinedHeaders.add(new Header(header.getKey(), header.getValue())); + } + } + } + } + return combinedHeaders; + } + + static Map<String, String> getCacheHeaders(Cache.Entry entry) { + // If there's no cache entry, we're done. + if (entry == null) { + return Collections.emptyMap(); + } + + Map<String, String> headers = new HashMap<>(); + + if (entry.etag != null) { + headers.put("If-None-Match", entry.etag); + } + + if (entry.lastModified > 0) { + headers.put( + "If-Modified-Since", HttpHeaderParser.formatEpochAsRfc1123(entry.lastModified)); + } + + return headers; + } } |