Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Unified Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java

Issue 784853004: [Cronet] Implement methods to get response headers in HttpURLConnection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Return empty map on exception Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java
diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java
index 9c308ae1f5fba209df0e7c83a9db181bbcb233e6..0d965000c72e1b844a83fc29c1e80cc6aac7d03b 100644
--- a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java
+++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java
@@ -114,6 +114,77 @@ public class CronetHttpURLConnection extends HttpURLConnection {
}
/**
+ * Returns an unmodifiable map of the response-header fields and values.
+ */
+ @Override
+ public Map<String, List<String>> getHeaderFields() {
+ try {
+ connect();
+ } catch (IOException e) {
+ return Collections.emptyMap();
+ }
+ return mResponseInfo.getAllHeaders();
+ }
+
+ /**
+ * Returns the value of the named header field. If called on a connection
+ * that sets the same header multiple times with possibly different values,
+ * only the last value is returned.
+ */
+ @Override
+ public final String getHeaderField(String fieldName) {
+ try {
+ connect();
+ } catch (IOException e) {
+ return null;
+ }
+ Map<String, List<String>> map = mResponseInfo.getAllHeaders();
mef 2015/01/23 17:08:02 Getting all headers on every call to getHeaderFiel
miloslav 2015/01/23 19:17:33 +1 for caching getAllHeaders in ResponseInfo. Also
xunjieli 2015/01/23 20:06:41 I don't think ResponseInfo instances are modified
miloslav 2015/01/23 20:21:55 Sorry, by ResponseInfo instances I was referring t
+ if (!map.containsKey(fieldName)) {
+ return null;
+ }
+ List<String> values = map.get(fieldName);
+ return values.get(values.size() - 1);
+ }
+
+ /**
+ * Returns the name of the header field at the given position pos, or null
+ * if there are fewer than pos fields.
+ */
+ @Override
+ public final String getHeaderFieldKey(int pos) {
+ try {
+ connect();
+ } catch (IOException e) {
+ return null;
+ }
+ List<Pair<String, String>> headers =
+ mResponseInfo.getAllHeadersAsList();
+ if (pos >= headers.size()) {
+ return null;
+ }
+ return headers.get(pos).first;
+ }
+
+ /**
+ * Returns the header value at the field position pos or null if the header
+ * has fewer than pos fields.
+ */
+ @Override
+ public final String getHeaderField(int pos) {
+ try {
+ connect();
+ } catch (IOException e) {
+ return null;
+ }
+ List<Pair<String, String>> headers =
+ mResponseInfo.getAllHeadersAsList();
+ if (pos >= headers.size()) {
+ return null;
+ }
+ return headers.get(pos).second;
+ }
+
+ /**
* Returns an InputStream for reading data from the resource pointed by this
* URLConnection.
* @throws FileNotFoundException if http response code is equal or greater

Powered by Google App Engine
This is Rietveld 408576698