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