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 final AtomicLong mReceivedBytesCount; |
| 29 private Map<String, List<String>> mResponseHeaders; |
| 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 mReceivedBytesCount = new AtomicLong(); |
| 42 } |
| 43 |
| 44 /** |
| 45 * Returns the URL the response is for. This is the URL after following |
| 46 * redirects, so it may not be the originally requested URL. |
| 47 * @return the URL the response is for. |
| 48 */ |
| 49 public String getUrl() { |
| 50 return mResponseInfoUrlChain.get(mResponseInfoUrlChain.size() - 1); |
| 51 } |
| 52 |
| 53 /** |
| 54 * Returns the URL chain. The first entry is the origianlly requested URL; |
| 55 * the following entries are redirects followed. |
| 56 * @return the URL chain. |
| 57 */ |
| 58 public List<String> getUrlChain() { |
| 59 return mResponseInfoUrlChain; |
| 60 } |
| 61 |
| 62 /** |
| 63 * Returns the HTTP status code. When a resource is retrieved from the cache
, |
| 64 * whether it was revalidated or not, the original status code is returned. |
| 65 * @return the HTTP status code. |
| 66 */ |
| 67 public int getHttpStatusCode() { |
| 68 return mHttpStatusCode; |
| 69 } |
| 70 |
| 71 /** |
| 72 * Returns the HTTP status text of the status line. For example, if the |
| 73 * request received a "HTTP/1.1 200 OK" response, this method returns "OK". |
| 74 * @return the HTTP status text of the status line. |
| 75 */ |
| 76 public String getHttpStatusText() { |
| 77 return mHttpStatusText; |
| 78 } |
| 79 |
| 80 /** |
| 81 * Returns an unmodifiable list of response header field and value pairs. |
| 82 * The headers are in the same order they are received over the wire. |
| 83 * @return an unmodifiable list of response header field and value pairs. |
| 84 */ |
| 85 public List<Map.Entry<String, String>> getAllHeadersAsList() { |
| 86 return mAllHeadersList; |
| 87 } |
| 88 |
| 89 /** |
| 90 * Returns an unmodifiable map of the response-header fields and values. |
| 91 * Each list of values for a single header field is in the same order they |
| 92 * were received over the wire. |
| 93 * @return an unmodifiable map of the response-header fields and values. |
| 94 */ |
| 95 public Map<String, List<String>> getAllHeaders() { |
| 96 if (mResponseHeaders != null) { |
| 97 return mResponseHeaders; |
| 98 } |
| 99 Map<String, List<String>> map = |
| 100 new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER)
; |
| 101 for (Map.Entry<String, String> entry : mAllHeadersList) { |
| 102 List<String> values = new ArrayList<String>(); |
| 103 if (map.containsKey(entry.getKey())) { |
| 104 values.addAll(map.get(entry.getKey())); |
| 105 } |
| 106 values.add(entry.getValue()); |
| 107 map.put(entry.getKey(), Collections.unmodifiableList(values)); |
| 108 } |
| 109 mResponseHeaders = Collections.unmodifiableMap(map); |
| 110 return mResponseHeaders; |
| 111 } |
| 112 |
| 113 /** |
| 114 * Returns {@code true} if the response came from the cache, including |
| 115 * requests that were revalidated over the network before being retrieved |
| 116 * from the cache. |
| 117 * @return {@code true} if the response came from the cache, {@code false} |
| 118 * otherwise. |
| 119 */ |
| 120 public boolean wasCached() { |
| 121 return mWasCached; |
| 122 } |
| 123 |
| 124 /** |
| 125 * Returns the protocol (for example 'quic/1+spdy/3') negotiated with the se
rver. |
| 126 * Returns an empty string if no protocol was negotiated, the protocol is |
| 127 * not known, or when using plain HTTP or HTTPS. |
| 128 * @return the protocol negotiated with the server. |
| 129 */ |
| 130 // TODO(mef): Figure out what this returns in the cached case, both with |
| 131 // and without a revalidation request. |
| 132 public String getNegotiatedProtocol() { |
| 133 return mNegotiatedProtocol; |
| 134 } |
| 135 |
| 136 /** |
| 137 * Returns the proxy server that was used for the request. |
| 138 * @return the proxy server that was used for the request. |
| 139 */ |
| 140 public String getProxyServer() { |
| 141 return mProxyServer; |
| 142 } |
| 143 |
| 144 /** |
| 145 * Returns a minimum count of bytes received from the network to process thi
s |
| 146 * request. This count may ignore certain overheads (for example IP and TCP/
UDP framing, |
| 147 * SSL handshake and framing, proxy handling). This count is taken prior to
decompression |
| 148 * (for example GZIP and SDCH) and includes headers and data from all redire
cts. |
| 149 * |
| 150 * This value may change (even for one {@link UrlResponseInfo} instance) as
the request |
| 151 * progresses until completion, when {@link UrlRequestListener#onSucceeded}
or |
| 152 * {@link UrlRequestListener#onFailed} is called. |
| 153 */ |
| 154 public long getReceivedBytesCount() { |
| 155 return mReceivedBytesCount.get(); |
| 156 } |
| 157 |
| 158 @Override |
| 159 public String toString() { |
| 160 return String.format(Locale.ROOT, "UrlResponseInfo[%s]: urlChain = %s, " |
| 161 + "httpStatus = %d %s, headers = %s, wasCached = %b, " |
| 162 + "negotiatedProtocol = %s, proxyServer= %s, receivedByt
esCount = %d", |
| 163 getUrl(), getUrlChain().toString(), getHttpStatusCode(), getHttp
StatusText(), |
| 164 getAllHeadersAsList().toString(), wasCached(), getNegotiatedProt
ocol(), |
| 165 getProxyServer(), getReceivedBytesCount()); |
| 166 } |
| 167 |
| 168 // Sets mReceivedBytesCount. Must not be called after request completion or
cancellation. |
| 169 void setReceivedBytesCount(long currentReceivedBytesCount) { |
| 170 mReceivedBytesCount.set(currentReceivedBytesCount); |
| 171 } |
| 172 } |
OLD | NEW |