OLD | NEW |
---|---|
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; | 7 import android.util.Pair; |
8 | 8 |
9 import org.chromium.net.ExtendedResponseInfo; | 9 import org.chromium.net.ExtendedResponseInfo; |
10 import org.chromium.net.ResponseInfo; | 10 import org.chromium.net.ResponseInfo; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 /** | 107 /** |
108 * Returns the response code returned by the remote HTTP server. | 108 * Returns the response code returned by the remote HTTP server. |
109 */ | 109 */ |
110 @Override | 110 @Override |
111 public int getResponseCode() throws IOException { | 111 public int getResponseCode() throws IOException { |
112 connect(); | 112 connect(); |
113 return mResponseInfo.getHttpStatusCode(); | 113 return mResponseInfo.getHttpStatusCode(); |
114 } | 114 } |
115 | 115 |
116 /** | 116 /** |
117 * Returns an unmodifiable map of the response-header fields and values. | |
118 */ | |
119 @Override | |
120 public Map<String, List<String>> getHeaderFields() { | |
121 try { | |
122 connect(); | |
123 } catch (IOException e) { | |
124 return Collections.emptyMap(); | |
125 } | |
126 return mResponseInfo.getAllHeaders(); | |
127 } | |
128 | |
129 /** | |
130 * Returns the value of the named header field. If called on a connection | |
131 * that sets the same header multiple times with possibly different values, | |
132 * only the last value is returned. | |
133 */ | |
134 @Override | |
135 public final String getHeaderField(String fieldName) { | |
136 try { | |
137 connect(); | |
138 } catch (IOException e) { | |
139 return null; | |
140 } | |
141 Map<String, List<String>> map = mResponseInfo.getAllHeaders(); | |
mef
2015/01/23 17:08:02
Getting all headers on every call to getHeaderFiel
miloslav
2015/01/23 19:17:33
+1 for caching getAllHeaders in ResponseInfo. Also
xunjieli
2015/01/23 20:06:41
I don't think ResponseInfo instances are modified
miloslav
2015/01/23 20:21:55
Sorry, by ResponseInfo instances I was referring t
| |
142 if (!map.containsKey(fieldName)) { | |
143 return null; | |
144 } | |
145 List<String> values = map.get(fieldName); | |
146 return values.get(values.size() - 1); | |
147 } | |
148 | |
149 /** | |
150 * Returns the name of the header field at the given position pos, or null | |
151 * if there are fewer than pos fields. | |
152 */ | |
153 @Override | |
154 public final String getHeaderFieldKey(int pos) { | |
155 try { | |
156 connect(); | |
157 } catch (IOException e) { | |
158 return null; | |
159 } | |
160 List<Pair<String, String>> headers = | |
161 mResponseInfo.getAllHeadersAsList(); | |
162 if (pos >= headers.size()) { | |
163 return null; | |
164 } | |
165 return headers.get(pos).first; | |
166 } | |
167 | |
168 /** | |
169 * Returns the header value at the field position pos or null if the header | |
170 * has fewer than pos fields. | |
171 */ | |
172 @Override | |
173 public final String getHeaderField(int pos) { | |
174 try { | |
175 connect(); | |
176 } catch (IOException e) { | |
177 return null; | |
178 } | |
179 List<Pair<String, String>> headers = | |
180 mResponseInfo.getAllHeadersAsList(); | |
181 if (pos >= headers.size()) { | |
182 return null; | |
183 } | |
184 return headers.get(pos).second; | |
185 } | |
186 | |
187 /** | |
117 * Returns an InputStream for reading data from the resource pointed by this | 188 * Returns an InputStream for reading data from the resource pointed by this |
118 * URLConnection. | 189 * URLConnection. |
119 * @throws FileNotFoundException if http response code is equal or greater | 190 * @throws FileNotFoundException if http response code is equal or greater |
120 * than {@link HTTP_BAD_REQUEST}. | 191 * than {@link HTTP_BAD_REQUEST}. |
121 * @throws IOException If the request gets a network error or HTTP error | 192 * @throws IOException If the request gets a network error or HTTP error |
122 * status code, or if the caller tried to read the response body | 193 * status code, or if the caller tried to read the response body |
123 * of a redirect when redirects are disabled. | 194 * of a redirect when redirects are disabled. |
124 */ | 195 */ |
125 @Override | 196 @Override |
126 public InputStream getInputStream() throws IOException { | 197 public InputStream getInputStream() throws IOException { |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 */ | 409 */ |
339 private void checkHasResponse() throws IOException { | 410 private void checkHasResponse() throws IOException { |
340 if (mException != null) { | 411 if (mException != null) { |
341 throw mException; | 412 throw mException; |
342 } else if (mResponseInfo == null) { | 413 } else if (mResponseInfo == null) { |
343 throw new NullPointerException( | 414 throw new NullPointerException( |
344 "Response info is null when there is no exception."); | 415 "Response info is null when there is no exception."); |
345 } | 416 } |
346 } | 417 } |
347 } | 418 } |
OLD | NEW |