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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net.urlconnection; 5 package org.chromium.net.urlconnection;
6 6
7 import android.util.Pair;
8
7 import org.chromium.net.ExtendedResponseInfo; 9 import org.chromium.net.ExtendedResponseInfo;
8 import org.chromium.net.ResponseInfo; 10 import org.chromium.net.ResponseInfo;
9 import org.chromium.net.UrlRequest; 11 import org.chromium.net.UrlRequest;
10 import org.chromium.net.UrlRequestContext; 12 import org.chromium.net.UrlRequestContext;
11 import org.chromium.net.UrlRequestException; 13 import org.chromium.net.UrlRequestException;
12 import org.chromium.net.UrlRequestListener; 14 import org.chromium.net.UrlRequestListener;
13 15
14 import java.io.FileNotFoundException; 16 import java.io.FileNotFoundException;
15 import java.io.IOException; 17 import java.io.IOException;
16 import java.io.InputStream; 18 import java.io.InputStream;
17 import java.net.HttpURLConnection; 19 import java.net.HttpURLConnection;
18 import java.net.MalformedURLException; 20 import java.net.MalformedURLException;
19 import java.net.URL; 21 import java.net.URL;
20 import java.nio.ByteBuffer; 22 import java.nio.ByteBuffer;
23 import java.util.List;
24 import java.util.Map;
21 25
22 /** 26 /**
23 * An implementation of HttpURLConnection that uses Cronet to send requests and 27 * An implementation of HttpURLConnection that uses Cronet to send requests and
24 * receive response. This class inherits a {@code connected} field from the 28 * receive response. This class inherits a {@code connected} field from the
25 * superclass. That field indicates whether a connection has ever been 29 * superclass. That field indicates whether a connection has ever been
26 * attempted. 30 * attempted.
27 */ 31 */
28 public class CronetHttpURLConnection extends HttpURLConnection { 32 public class CronetHttpURLConnection extends HttpURLConnection {
29 private final UrlRequestContext mUrlRequestContext; 33 private final UrlRequestContext mUrlRequestContext;
30 private final MessageLoop mMessageLoop; 34 private final MessageLoop mMessageLoop;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 /** 99 /**
96 * Returns the response code returned by the remote HTTP server. 100 * Returns the response code returned by the remote HTTP server.
97 */ 101 */
98 @Override 102 @Override
99 public int getResponseCode() throws IOException { 103 public int getResponseCode() throws IOException {
100 connect(); 104 connect();
101 return mResponseInfo.getHttpStatusCode(); 105 return mResponseInfo.getHttpStatusCode();
102 } 106 }
103 107
104 /** 108 /**
109 * Returns an unmodifiable map of the response-header fields and values.
110 */
111 @Override
112 public Map<String, List<String>> getHeaderFields() {
113 try {
114 connect();
115 } catch (IOException e) {
116 // Default implementation returns an empty map on K, but returns
117 // null on L.
118 return null;
119 }
120 return mResponseInfo.getAllHeaders();
121 }
122
123 /**
124 * Returns the value of the named header field. If called on a connection
125 * that sets the same header multiple times with possibly different values,
126 * only the last value is returned.
127 */
128 @Override
129 public final String getHeaderField(String fieldName) {
130 try {
131 connect();
132 } catch (IOException e) {
133 return null;
134 }
135 Map<String, List<String>> map = mResponseInfo.getAllHeaders();
136 if (!map.containsKey(fieldName)) {
137 return null;
138 }
139 List<String> values = map.get(fieldName);
140 return values.get(values.size() - 1);
141 }
142
143 /**
144 * 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.
145 * if there are fewer than pos fields.
146 */
147 @Override
148 public final String getHeaderFieldKey(int pos) {
149 try {
150 connect();
151 } catch (IOException e) {
152 return null;
153 }
154 List<Pair<String, String>> headers =
155 mResponseInfo.getAllHeadersAsList();
156 if (pos >= headers.size()) {
157 return null;
158 }
159 return headers.get(pos).first;
160 }
161
162 /**
163 * Returns the header value at the field position pos or null if the header
164 * has fewer than pos fields.
165 */
166 @Override
167 public final String getHeaderField(int pos) {
168 try {
169 connect();
170 } catch (IOException e) {
171 return null;
172 }
173 List<Pair<String, String>> headers =
174 mResponseInfo.getAllHeadersAsList();
175 if (pos >= headers.size()) {
176 return null;
177 }
178 return headers.get(pos).second;
179 }
180
181 /**
105 * Returns an InputStream for reading data from the resource pointed by this 182 * Returns an InputStream for reading data from the resource pointed by this
106 * URLConnection. 183 * URLConnection.
107 * @throws FileNotFoundException if http response code is equal or greater 184 * @throws FileNotFoundException if http response code is equal or greater
108 * than {@link HTTP_BAD_REQUEST}. 185 * than {@link HTTP_BAD_REQUEST}.
109 * @throws IOException If the request gets a network error or HTTP error 186 * @throws IOException If the request gets a network error or HTTP error
110 * status code, or if the caller tried to read the response body 187 * status code, or if the caller tried to read the response body
111 * of a redirect when redirects are disabled. 188 * of a redirect when redirects are disabled.
112 */ 189 */
113 @Override 190 @Override
114 public InputStream getInputStream() throws IOException { 191 public InputStream getInputStream() throws IOException {
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 */ 335 */
259 private void checkHasResponse() throws IOException { 336 private void checkHasResponse() throws IOException {
260 if (mException != null) { 337 if (mException != null) {
261 throw mException; 338 throw mException;
262 } else if (mResponseInfo == null) { 339 } else if (mResponseInfo == null) {
263 throw new NullPointerException( 340 throw new NullPointerException(
264 "Response info is null when there is no exception."); 341 "Response info is null when there is no exception.");
265 } 342 }
266 } 343 }
267 } 344 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698