Index: components/cronet/android/java/src/org/chromium/net/UrlResponseInfo.java |
diff --git a/components/cronet/android/java/src/org/chromium/net/UrlResponseInfo.java b/components/cronet/android/java/src/org/chromium/net/UrlResponseInfo.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d3411658ca81e9159ba3ab54c5af590f4ec1a9f6 |
--- /dev/null |
+++ b/components/cronet/android/java/src/org/chromium/net/UrlResponseInfo.java |
@@ -0,0 +1,171 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.net; |
+ |
+import java.util.ArrayList; |
+import java.util.Collections; |
+import java.util.List; |
+import java.util.Locale; |
+import java.util.Map; |
+import java.util.TreeMap; |
+import java.util.concurrent.atomic.AtomicLong; |
+ |
+/** |
+ * Contains basic information about a response. Sent on every call to the {@link |
+ * UrlRequestListener}. |
+ * Each call to {@link UrlRequestListener#onReceivedRedirect |
+ * UrlRequestListener.onReceivedRedirect()} |
+ * gets a different copy of UrlResponseInfo describing particular redirect response. |
xunjieli
2015/10/05 20:39:02
Suggest the following changes:
"Sent on every ca
mef
2015/10/05 22:09:18
Done.
|
+ * The received bytes count is not valid until request has completion or cancelation. |
xunjieli
2015/10/05 20:39:02
Suggest moving this comment to mReceivedBytesCount
mef
2015/10/05 22:09:18
Done.
|
+ */ |
+public final class UrlResponseInfo { |
+ private final List<String> mResponseInfoUrlChain; |
+ private final int mHttpStatusCode; |
+ private final String mHttpStatusText; |
+ private final boolean mWasCached; |
+ private final String mNegotiatedProtocol; |
+ private final String mProxyServer; |
+ private final List<Map.Entry<String, String>> mAllHeadersList; |
+ private Map<String, List<String>> mResponseHeaders; |
+ private final AtomicLong mReceivedBytesCount = new AtomicLong(); |
+ |
+ UrlResponseInfo(List<String> urlChain, int httpStatusCode, String httpStatusText, |
+ List<Map.Entry<String, String>> allHeadersList, boolean wasCached, |
+ String negotiatedProtocol, String proxyServer) { |
+ mResponseInfoUrlChain = Collections.unmodifiableList(urlChain); |
+ mHttpStatusCode = httpStatusCode; |
+ mHttpStatusText = httpStatusText; |
+ mAllHeadersList = Collections.unmodifiableList(allHeadersList); |
+ mWasCached = wasCached; |
+ mNegotiatedProtocol = negotiatedProtocol; |
+ mProxyServer = proxyServer; |
+ } |
+ |
+ /** |
+ * Returns the URL the response is for. This is the URL after following |
+ * redirects, so it may not be the originally requested URL. |
+ * @return the URL the response is for. |
+ */ |
+ public String getUrl() { |
+ return mResponseInfoUrlChain.get(mResponseInfoUrlChain.size() - 1); |
+ } |
+ |
+ /** |
+ * Returns the URL chain. The first entry is the origianlly requested URL; |
+ * the following entries are redirects followed. |
+ * @return the URL chain. |
+ */ |
+ public List<String> getUrlChain() { |
+ return mResponseInfoUrlChain; |
+ } |
+ |
+ /** |
+ * Returns the HTTP status code. When a resource is retrieved from the cache, |
+ * whether it was revalidated or not, the original status code is returned. |
+ * @return the HTTP status code. |
+ */ |
+ public int getHttpStatusCode() { |
+ return mHttpStatusCode; |
+ } |
+ |
+ /** |
+ * Returns the HTTP status text of the status line. For example, if the |
+ * request has a "HTTP/1.1 200 OK" response, this method returns "OK". |
+ * @return the HTTP status text of the status line. |
+ */ |
+ public String getHttpStatusText() { |
+ return mHttpStatusText; |
+ } |
+ |
+ /** |
+ * Returns an unmodifiable list of response header field and value pairs. |
+ * The headers are in the same order they are received over the wire. |
+ * @return an unmodifiable list of response header field and value pairs. |
+ */ |
+ public List<Map.Entry<String, String>> getAllHeadersAsList() { |
+ return mAllHeadersList; |
+ } |
+ |
+ /** |
+ * Returns an unmodifiable map of the response-header fields and values. |
+ * Each list of values for a single header field is in the same order they |
+ * were received over the wire. |
+ * @return an unmodifiable map of the response-header fields and values. |
+ */ |
+ public Map<String, List<String>> getAllHeaders() { |
+ if (mResponseHeaders != null) { |
+ return mResponseHeaders; |
+ } |
+ Map<String, List<String>> map = |
+ new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER); |
+ for (Map.Entry<String, String> entry : mAllHeadersList) { |
+ List<String> values = new ArrayList<String>(); |
+ if (map.containsKey(entry.getKey())) { |
+ values.addAll(map.get(entry.getKey())); |
+ } |
+ values.add(entry.getValue()); |
+ map.put(entry.getKey(), Collections.unmodifiableList(values)); |
+ } |
+ mResponseHeaders = Collections.unmodifiableMap(map); |
+ return mResponseHeaders; |
+ } |
+ |
+ /** |
+ * Returns {@code true} if the response came from the cache, including |
+ * requests that were revalidated over the network before being retrieved |
+ * from the cache. |
+ * @return {@code true} if the response came from the cache, {@code false} |
+ * otherwise. |
+ */ |
+ public boolean wasCached() { |
+ return mWasCached; |
+ } |
+ |
+ /** |
+ * Returns the protocol (e.g. "quic/1+spdy/3") negotiated with the server. |
+ * Returns an empty string if no protocol was negotiated, the protocol is |
+ * not known, or when using plain HTTP or HTTPS. |
+ * @return the protocol negotiated with the server. |
+ */ |
+ // TODO(mef): Figure out what this returns in the cached case, both with |
+ // and without a revalidation request. |
+ public String getNegotiatedProtocol() { |
+ return mNegotiatedProtocol; |
+ } |
+ |
+ /** |
+ * Returns the proxy server that was used for the request. |
+ * @return the proxy server that was used for the request. |
+ */ |
+ public String getProxyServer() { |
+ return mProxyServer; |
+ } |
+ |
+ /** |
+ * Returns a minimum count of bytes received from the network to process this |
+ * request. This count may ignore certain overheads (e.g. IP and TCP/UDP framing, SSL |
+ * handshake and framing, proxy handling). This count is taken prior to gzip and SDCH |
+ * decompression. |
+ * Available on request completion or cancelation. |
xunjieli
2015/10/05 20:39:02
Maybe add a TODO here for implementating it for ca
mef
2015/10/05 22:09:18
Done.
|
+ */ |
+ public long getReceivedBytesCount() { |
+ return mReceivedBytesCount.get(); |
+ } |
+ |
+ public String toString() { |
xunjieli
2015/10/05 20:39:02
add @Override
mef
2015/10/05 22:09:18
Done.
|
+ return String.format(Locale.ROOT, "UrlResponseInfo[%s]: urlChain = %s, " |
+ + "httpStatus = %d %s, headers = %s, wasCached = %b, " |
+ + "negotiatedProtocol = %s, proxyServer= %s, receivedBytesCount = %d", |
+ getUrl(), getUrlChain().toString(), getHttpStatusCode(), getHttpStatusText(), |
+ getAllHeadersAsList().toString(), wasCached(), getNegotiatedProtocol(), |
+ getProxyServer(), getReceivedBytesCount()); |
+ } |
+ |
+ // Set ReceivedBytesCount up requirest completion or cancelation. |
xunjieli
2015/10/05 20:39:02
nit: there is a typo in this comment.
mef
2015/10/05 22:09:18
Done.
|
+ void setReceivedBytesCount(long currentReceivedBytesCount) { |
+ mReceivedBytesCount.set(currentReceivedBytesCount); |
+ } |
+} |
+; |