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.base.Log; | 9 import org.chromium.base.Log; |
10 import org.chromium.net.CronetEngine; | 10 import org.chromium.net.CronetEngine; |
11 import org.chromium.net.ExtendedResponseInfo; | |
12 import org.chromium.net.ResponseInfo; | |
13 import org.chromium.net.UrlRequest; | 11 import org.chromium.net.UrlRequest; |
14 import org.chromium.net.UrlRequestException; | 12 import org.chromium.net.UrlRequestException; |
15 import org.chromium.net.UrlRequestListener; | 13 import org.chromium.net.UrlRequestListener; |
| 14 import org.chromium.net.UrlResponseInfo; |
16 | 15 |
17 import java.io.FileNotFoundException; | 16 import java.io.FileNotFoundException; |
18 import java.io.IOException; | 17 import java.io.IOException; |
19 import java.io.InputStream; | 18 import java.io.InputStream; |
20 import java.io.OutputStream; | 19 import java.io.OutputStream; |
21 import java.net.HttpURLConnection; | 20 import java.net.HttpURLConnection; |
22 import java.net.MalformedURLException; | 21 import java.net.MalformedURLException; |
23 import java.net.ProtocolException; | 22 import java.net.ProtocolException; |
24 import java.net.URL; | 23 import java.net.URL; |
25 import java.nio.ByteBuffer; | 24 import java.nio.ByteBuffer; |
(...skipping 11 matching lines...) Expand all Loading... |
37 public class CronetHttpURLConnection extends HttpURLConnection { | 36 public class CronetHttpURLConnection extends HttpURLConnection { |
38 private static final String TAG = "cr.CronetHttpURLConn"; | 37 private static final String TAG = "cr.CronetHttpURLConn"; |
39 private static final String CONTENT_LENGTH = "Content-Length"; | 38 private static final String CONTENT_LENGTH = "Content-Length"; |
40 private final CronetEngine mCronetEngine; | 39 private final CronetEngine mCronetEngine; |
41 private final MessageLoop mMessageLoop; | 40 private final MessageLoop mMessageLoop; |
42 private UrlRequest mRequest; | 41 private UrlRequest mRequest; |
43 private final List<Pair<String, String>> mRequestHeaders; | 42 private final List<Pair<String, String>> mRequestHeaders; |
44 | 43 |
45 private CronetInputStream mInputStream; | 44 private CronetInputStream mInputStream; |
46 private CronetOutputStream mOutputStream; | 45 private CronetOutputStream mOutputStream; |
47 private ResponseInfo mResponseInfo; | 46 private UrlResponseInfo mResponseInfo; |
48 private UrlRequestException mException; | 47 private UrlRequestException mException; |
49 private boolean mOnRedirectCalled = false; | 48 private boolean mOnRedirectCalled = false; |
50 private boolean mHasResponse = false; | 49 private boolean mHasResponse = false; |
51 | 50 |
52 public CronetHttpURLConnection(URL url, CronetEngine cronetEngine) { | 51 public CronetHttpURLConnection(URL url, CronetEngine cronetEngine) { |
53 super(url); | 52 super(url); |
54 mCronetEngine = cronetEngine; | 53 mCronetEngine = cronetEngine; |
55 mMessageLoop = new MessageLoop(); | 54 mMessageLoop = new MessageLoop(); |
56 mInputStream = new CronetInputStream(this); | 55 mInputStream = new CronetInputStream(this); |
57 mRequestHeaders = new ArrayList<Pair<String, String>>(); | 56 mRequestHeaders = new ArrayList<Pair<String, String>>(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 List<String> values = map.get(fieldName); | 134 List<String> values = map.get(fieldName); |
136 return values.get(values.size() - 1); | 135 return values.get(values.size() - 1); |
137 } | 136 } |
138 | 137 |
139 /** | 138 /** |
140 * Returns the name of the header field at the given position {@code pos}, o
r {@code null} | 139 * Returns the name of the header field at the given position {@code pos}, o
r {@code null} |
141 * if there are fewer than {@code pos} fields. | 140 * if there are fewer than {@code pos} fields. |
142 */ | 141 */ |
143 @Override | 142 @Override |
144 public final String getHeaderFieldKey(int pos) { | 143 public final String getHeaderFieldKey(int pos) { |
145 Pair<String, String> header = getHeaderFieldPair(pos); | 144 Map.Entry<String, String> header = getHeaderFieldEntry(pos); |
146 if (header == null) { | 145 if (header == null) { |
147 return null; | 146 return null; |
148 } | 147 } |
149 return header.first; | 148 return header.getKey(); |
150 } | 149 } |
151 | 150 |
152 /** | 151 /** |
153 * Returns the header value at the field position {@code pos} or {@code null
} if the header | 152 * Returns the header value at the field position {@code pos} or {@code null
} if the header |
154 * has fewer than {@code pos} fields. | 153 * has fewer than {@code pos} fields. |
155 */ | 154 */ |
156 @Override | 155 @Override |
157 public final String getHeaderField(int pos) { | 156 public final String getHeaderField(int pos) { |
158 Pair<String, String> header = getHeaderFieldPair(pos); | 157 Map.Entry<String, String> header = getHeaderFieldEntry(pos); |
159 if (header == null) { | 158 if (header == null) { |
160 return null; | 159 return null; |
161 } | 160 } |
162 return header.second; | 161 return header.getValue(); |
163 } | 162 } |
164 | 163 |
165 /** | 164 /** |
166 * Returns an InputStream for reading data from the resource pointed by this | 165 * Returns an InputStream for reading data from the resource pointed by this |
167 * {@link java.net.URLConnection}. | 166 * {@link java.net.URLConnection}. |
168 * @throws FileNotFoundException if http response code is equal or greater | 167 * @throws FileNotFoundException if http response code is equal or greater |
169 * than {@link HTTP_BAD_REQUEST}. | 168 * than {@link HTTP_BAD_REQUEST}. |
170 * @throws IOException If the request gets a network error or HTTP error | 169 * @throws IOException If the request gets a network error or HTTP error |
171 * status code, or if the caller tried to read the response body | 170 * status code, or if the caller tried to read the response body |
172 * of a redirect when redirects are disabled. | 171 * of a redirect when redirects are disabled. |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 } | 415 } |
417 } | 416 } |
418 return -1; | 417 return -1; |
419 } | 418 } |
420 | 419 |
421 private class CronetUrlRequestListener extends UrlRequestListener { | 420 private class CronetUrlRequestListener extends UrlRequestListener { |
422 public CronetUrlRequestListener() { | 421 public CronetUrlRequestListener() { |
423 } | 422 } |
424 | 423 |
425 @Override | 424 @Override |
426 public void onResponseStarted(UrlRequest request, ResponseInfo info) { | 425 public void onResponseStarted(UrlRequest request, UrlResponseInfo info)
{ |
427 mResponseInfo = info; | 426 mResponseInfo = info; |
428 // Quits the message loop since we have the headers now. | 427 // Quits the message loop since we have the headers now. |
429 mMessageLoop.quit(); | 428 mMessageLoop.quit(); |
430 } | 429 } |
431 | 430 |
432 @Override | 431 @Override |
433 public void onReadCompleted(UrlRequest request, ResponseInfo info, | 432 public void onReadCompleted( |
434 ByteBuffer byteBuffer) { | 433 UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer)
{ |
435 mResponseInfo = info; | 434 mResponseInfo = info; |
436 mMessageLoop.quit(); | 435 mMessageLoop.quit(); |
437 } | 436 } |
438 | 437 |
439 @Override | 438 @Override |
440 public void onReceivedRedirect(UrlRequest request, ResponseInfo info, | 439 public void onReceivedRedirect( |
441 String newLocationUrl) { | 440 UrlRequest request, UrlResponseInfo info, String newLocationUrl)
{ |
442 mOnRedirectCalled = true; | 441 mOnRedirectCalled = true; |
443 if (instanceFollowRedirects) { | 442 if (instanceFollowRedirects) { |
444 try { | 443 try { |
445 url = new URL(newLocationUrl); | 444 url = new URL(newLocationUrl); |
446 } catch (MalformedURLException e) { | 445 } catch (MalformedURLException e) { |
447 // Ignored. | 446 // Ignored. |
448 } | 447 } |
449 mRequest.followRedirect(); | 448 mRequest.followRedirect(); |
450 } else { | 449 } else { |
451 mResponseInfo = info; | 450 mResponseInfo = info; |
452 mRequest.cancel(); | 451 mRequest.cancel(); |
453 setResponseDataCompleted(); | 452 setResponseDataCompleted(); |
454 } | 453 } |
455 } | 454 } |
456 | 455 |
457 @Override | 456 @Override |
458 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { | 457 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
459 mResponseInfo = info.getResponseInfo(); | 458 mResponseInfo = info; |
460 setResponseDataCompleted(); | 459 setResponseDataCompleted(); |
461 } | 460 } |
462 | 461 |
463 @Override | 462 @Override |
464 public void onFailed(UrlRequest request, ResponseInfo info, | 463 public void onFailed( |
465 UrlRequestException exception) { | 464 UrlRequest request, UrlResponseInfo info, UrlRequestException ex
ception) { |
466 if (exception == null) { | 465 if (exception == null) { |
467 throw new IllegalStateException( | 466 throw new IllegalStateException( |
468 "Exception cannot be null in onFailed."); | 467 "Exception cannot be null in onFailed."); |
469 } | 468 } |
470 mResponseInfo = info; | 469 mResponseInfo = info; |
471 mException = exception; | 470 mException = exception; |
472 setResponseDataCompleted(); | 471 setResponseDataCompleted(); |
473 } | 472 } |
474 | 473 |
475 /** | 474 /** |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 throw mException; | 515 throw mException; |
517 } else if (mResponseInfo == null) { | 516 } else if (mResponseInfo == null) { |
518 throw new NullPointerException( | 517 throw new NullPointerException( |
519 "Response info is null when there is no exception."); | 518 "Response info is null when there is no exception."); |
520 } | 519 } |
521 } | 520 } |
522 | 521 |
523 /** | 522 /** |
524 * Helper method to return the response header field at position pos. | 523 * Helper method to return the response header field at position pos. |
525 */ | 524 */ |
526 private Pair<String, String> getHeaderFieldPair(int pos) { | 525 private Map.Entry<String, String> getHeaderFieldEntry(int pos) { |
527 try { | 526 try { |
528 getResponse(); | 527 getResponse(); |
529 } catch (IOException e) { | 528 } catch (IOException e) { |
530 return null; | 529 return null; |
531 } | 530 } |
532 List<Pair<String, String>> headers = | 531 List<Map.Entry<String, String>> headers = mResponseInfo.getAllHeadersAsL
ist(); |
533 mResponseInfo.getAllHeadersAsList(); | |
534 if (pos >= headers.size()) { | 532 if (pos >= headers.size()) { |
535 return null; | 533 return null; |
536 } | 534 } |
537 return headers.get(pos); | 535 return headers.get(pos); |
538 } | 536 } |
539 | 537 |
540 /** | 538 /** |
541 * Returns whether the client has used {@link #setChunkedStreamingMode} to | 539 * Returns whether the client has used {@link #setChunkedStreamingMode} to |
542 * set chunked encoding for upload. | 540 * set chunked encoding for upload. |
543 */ | 541 */ |
544 private boolean isChunkedUpload() { | 542 private boolean isChunkedUpload() { |
545 return chunkLength > 0; | 543 return chunkLength > 0; |
546 } | 544 } |
547 } | 545 } |
OLD | NEW |