OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net; |
| 6 |
| 7 import java.util.ArrayList; |
| 8 import java.util.Collections; |
| 9 import java.util.List; |
| 10 import java.util.Locale; |
| 11 import java.util.Map; |
| 12 import java.util.TreeMap; |
| 13 import java.util.concurrent.atomic.AtomicLong; |
| 14 |
| 15 /** |
| 16 * Contains basic information about a response. Included in {@link UrlRequestLis
tener} callbacks. |
| 17 * Each {@link UrlRequestListener#onReceivedRedirect UrlRequestListener.onReceiv
edRedirect()} |
| 18 * callback gets a different copy of UrlResponseInfo describing a particular red
irect response. |
| 19 */ |
| 20 public final class UrlResponseInfo { |
| 21 private final List<String> mResponseInfoUrlChain; |
| 22 private final int mHttpStatusCode; |
| 23 private final String mHttpStatusText; |
| 24 private final boolean mWasCached; |
| 25 private final String mNegotiatedProtocol; |
| 26 private final String mProxyServer; |
| 27 private final List<Map.Entry<String, String>> mAllHeadersList; |
| 28 private Map<String, List<String>> mResponseHeaders; |
| 29 private final AtomicLong mReceivedBytesCount = new AtomicLong(); |
| 30 |
| 31 UrlResponseInfo(List<String> urlChain, int httpStatusCode, String httpStatus
Text, |
| 32 List<Map.Entry<String, String>> allHeadersList, boolean wasCached, |
| 33 String negotiatedProtocol, String proxyServer) { |
| 34 mResponseInfoUrlChain = Collections.unmodifiableList(urlChain); |
| 35 mHttpStatusCode = httpStatusCode; |
| 36 mHttpStatusText = httpStatusText; |
| 37 mAllHeadersList = Collections.unmodifiableList(allHeadersList); |
| 38 mWasCached = wasCached; |
| 39 mNegotiatedProtocol = negotiatedProtocol; |
| 40 mProxyServer = proxyServer; |
| 41 } |
| 42 |
| 43 /** |
| 44 * Returns the URL the response is for. This is the URL after following |
| 45 * redirects, so it may not be the originally requested URL. |
| 46 * @return the URL the response is for. |
| 47 */ |
| 48 public String getUrl() { |
| 49 return mResponseInfoUrlChain.get(mResponseInfoUrlChain.size() - 1); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Returns the URL chain. The first entry is the origianlly requested URL; |
| 54 * the following entries are redirects followed. |
| 55 * @return the URL chain. |
| 56 */ |
| 57 public List<String> getUrlChain() { |
| 58 return mResponseInfoUrlChain; |
| 59 } |
| 60 |
| 61 /** |
| 62 * Returns the HTTP status code. When a resource is retrieved from the cache
, |
| 63 * whether it was revalidated or not, the original status code is returned. |
| 64 * @return the HTTP status code. |
| 65 */ |
| 66 public int getHttpStatusCode() { |
| 67 return mHttpStatusCode; |
| 68 } |
| 69 |
| 70 /** |
| 71 * Returns the HTTP status text of the status line. For example, if the |
| 72 * request received a "HTTP/1.1 200 OK" response, this method returns "OK". |
| 73 * @return the HTTP status text of the status line. |
| 74 */ |
| 75 public String getHttpStatusText() { |
| 76 return mHttpStatusText; |
| 77 } |
| 78 |
| 79 /** |
| 80 * Returns an unmodifiable list of response header field and value pairs. |
| 81 * The headers are in the same order they are received over the wire. |
| 82 * @return an unmodifiable list of response header field and value pairs. |
| 83 */ |
| 84 public List<Map.Entry<String, String>> getAllHeadersAsList() { |
| 85 return mAllHeadersList; |
| 86 } |
| 87 |
| 88 /** |
| 89 * Returns an unmodifiable map of the response-header fields and values. |
| 90 * Each list of values for a single header field is in the same order they |
| 91 * were received over the wire. |
| 92 * @return an unmodifiable map of the response-header fields and values. |
| 93 */ |
| 94 public Map<String, List<String>> getAllHeaders() { |
| 95 if (mResponseHeaders != null) { |
| 96 return mResponseHeaders; |
| 97 } |
| 98 Map<String, List<String>> map = |
| 99 new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER)
; |
| 100 for (Map.Entry<String, String> entry : mAllHeadersList) { |
| 101 List<String> values = new ArrayList<String>(); |
| 102 if (map.containsKey(entry.getKey())) { |
| 103 values.addAll(map.get(entry.getKey())); |
| 104 } |
| 105 values.add(entry.getValue()); |
| 106 map.put(entry.getKey(), Collections.unmodifiableList(values)); |
| 107 } |
| 108 mResponseHeaders = Collections.unmodifiableMap(map); |
| 109 return mResponseHeaders; |
| 110 } |
| 111 |
| 112 /** |
| 113 * Returns {@code true} if the response came from the cache, including |
| 114 * requests that were revalidated over the network before being retrieved |
| 115 * from the cache. |
| 116 * @return {@code true} if the response came from the cache, {@code false} |
| 117 * otherwise. |
| 118 */ |
| 119 public boolean wasCached() { |
| 120 return mWasCached; |
| 121 } |
| 122 |
| 123 /** |
| 124 * Returns the protocol (for example 'quic/1+spdy/3') negotiated with the se
rver. |
| 125 * Returns an empty string if no protocol was negotiated, the protocol is |
| 126 * not known, or when using plain HTTP or HTTPS. |
| 127 * @return the protocol negotiated with the server. |
| 128 */ |
| 129 // TODO(mef): Figure out what this returns in the cached case, both with |
| 130 // and without a revalidation request. |
| 131 public String getNegotiatedProtocol() { |
| 132 return mNegotiatedProtocol; |
| 133 } |
| 134 |
| 135 /** |
| 136 * Returns the proxy server that was used for the request. |
| 137 * @return the proxy server that was used for the request. |
| 138 */ |
| 139 public String getProxyServer() { |
| 140 return mProxyServer; |
| 141 } |
| 142 |
| 143 /** |
| 144 * Returns a minimum count of bytes received from the network to process thi
s |
| 145 * request. This count may ignore certain overheads (for example IP and TCP/
UDP framing, |
| 146 * SSL handshake and framing, proxy handling). This count is taken prior to
decompression |
| 147 * (for example GZIP and SDCH) and includes headers and data from all redire
cts. |
| 148 * |
| 149 * This value may change (even for one {@link UrlResponseInfo} instance) as
the request |
| 150 * progresses until completion, when {@link UrlRequestListener#onSucceeded}
or |
| 151 * {@link UrlRequestListener#onFailed} is called. |
| 152 */ |
| 153 public long getReceivedBytesCount() { |
| 154 return mReceivedBytesCount.get(); |
| 155 } |
| 156 |
| 157 @Override |
| 158 public String toString() { |
| 159 return String.format(Locale.ROOT, "UrlResponseInfo[%s]: urlChain = %s, " |
| 160 + "httpStatus = %d %s, headers = %s, wasCached = %b, " |
| 161 + "negotiatedProtocol = %s, proxyServer= %s, receivedByt
esCount = %d", |
| 162 getUrl(), getUrlChain().toString(), getHttpStatusCode(), getHttp
StatusText(), |
| 163 getAllHeadersAsList().toString(), wasCached(), getNegotiatedProt
ocol(), |
| 164 getProxyServer(), getReceivedBytesCount()); |
| 165 } |
| 166 |
| 167 // Set ReceivedBytesCount. Must not be called after request completion or ca
ncellation. |
| 168 void setReceivedBytesCount(long currentReceivedBytesCount) { |
| 169 mReceivedBytesCount.set(currentReceivedBytesCount); |
| 170 } |
| 171 } |
| 172 ; |
OLD | NEW |