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

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: Self review 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 cdd238c1b42fdce2ef9ee946b5c9a52552834d50..f024c9bbec90632e6d95822b26f874d062fc6c2c 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
@@ -4,6 +4,8 @@
package org.chromium.net.urlconnection;
+import android.util.Pair;
+
import org.chromium.net.ExtendedResponseInfo;
import org.chromium.net.ResponseInfo;
import org.chromium.net.UrlRequest;
@@ -18,6 +20,8 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Map;
/**
* An implementation of HttpURLConnection that uses Cronet to send requests and
@@ -102,6 +106,79 @@ 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) {
+ // Default implementation returns an empty map on K, but returns
+ // null on L.
+ return null;
+ }
+ 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();
+ 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
mmenke 2015/01/16 19:43:13 nit: Suggest a comma after pos.
xunjieli 2015/01/16 20:01:12 Done.
+ * 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