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(); |
| 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 Pair<String, String> header = getHeaderFieldPair(pos); |
| 156 if (header == null) { |
| 157 return null; |
| 158 } |
| 159 return header.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 Pair<String, String> header = getHeaderFieldPair(pos); |
| 169 if (header == null) { |
| 170 return null; |
| 171 } |
| 172 return header.second; |
| 173 } |
| 174 |
| 175 /** |
117 * Returns an InputStream for reading data from the resource pointed by this | 176 * Returns an InputStream for reading data from the resource pointed by this |
118 * URLConnection. | 177 * URLConnection. |
119 * @throws FileNotFoundException if http response code is equal or greater | 178 * @throws FileNotFoundException if http response code is equal or greater |
120 * than {@link HTTP_BAD_REQUEST}. | 179 * than {@link HTTP_BAD_REQUEST}. |
121 * @throws IOException If the request gets a network error or HTTP error | 180 * @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 | 181 * status code, or if the caller tried to read the response body |
123 * of a redirect when redirects are disabled. | 182 * of a redirect when redirects are disabled. |
124 */ | 183 */ |
125 @Override | 184 @Override |
126 public InputStream getInputStream() throws IOException { | 185 public InputStream getInputStream() throws IOException { |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 * called after onResponseStarted or onFailed. | 396 * called after onResponseStarted or onFailed. |
338 */ | 397 */ |
339 private void checkHasResponse() throws IOException { | 398 private void checkHasResponse() throws IOException { |
340 if (mException != null) { | 399 if (mException != null) { |
341 throw mException; | 400 throw mException; |
342 } else if (mResponseInfo == null) { | 401 } else if (mResponseInfo == null) { |
343 throw new NullPointerException( | 402 throw new NullPointerException( |
344 "Response info is null when there is no exception."); | 403 "Response info is null when there is no exception."); |
345 } | 404 } |
346 } | 405 } |
| 406 |
| 407 /** |
| 408 * Helper method to return the response header field at position pos. |
| 409 */ |
| 410 private Pair<String, String> getHeaderFieldPair(int pos) { |
| 411 try { |
| 412 connect(); |
| 413 } catch (IOException e) { |
| 414 return null; |
| 415 } |
| 416 List<Pair<String, String>> headers = |
| 417 mResponseInfo.getAllHeadersAsList(); |
| 418 if (pos >= headers.size()) { |
| 419 return null; |
| 420 } |
| 421 return headers.get(pos); |
| 422 } |
347 } | 423 } |
OLD | NEW |