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; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 startRequest(); | 66 startRequest(); |
67 } | 67 } |
68 | 68 |
69 /** | 69 /** |
70 * Releases this connection so that its resources may be either reused or | 70 * Releases this connection so that its resources may be either reused or |
71 * closed. | 71 * closed. |
72 */ | 72 */ |
73 @Override | 73 @Override |
74 public void disconnect() { | 74 public void disconnect() { |
75 // Disconnect before connection is made should have no effect. | 75 // Disconnect before connection is made should have no effect. |
76 if (connected && mInputStream != null) { | 76 if (connected) { |
77 try { | |
78 mInputStream.close(); | |
79 } catch (IOException e) { | |
80 e.printStackTrace(); | |
81 } | |
82 mInputStream = null; | |
83 mRequest.cancel(); | 77 mRequest.cancel(); |
84 } | 78 } |
85 } | 79 } |
86 | 80 |
87 /** | 81 /** |
88 * Returns the response message returned by the remote HTTP server. | 82 * Returns the response message returned by the remote HTTP server. |
89 */ | 83 */ |
90 @Override | 84 @Override |
91 public String getResponseMessage() throws IOException { | 85 public String getResponseMessage() throws IOException { |
92 getResponse(); | 86 getResponse(); |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 } | 445 } |
452 if (instanceFollowRedirects && sameProtocol) { | 446 if (instanceFollowRedirects && sameProtocol) { |
453 mRequest.followRedirect(); | 447 mRequest.followRedirect(); |
454 return; | 448 return; |
455 } | 449 } |
456 } catch (MalformedURLException e) { | 450 } catch (MalformedURLException e) { |
457 // Ignored. Just cancel the request and not follow the redirect. | 451 // Ignored. Just cancel the request and not follow the redirect. |
458 } | 452 } |
459 mResponseInfo = info; | 453 mResponseInfo = info; |
460 mRequest.cancel(); | 454 mRequest.cancel(); |
461 setResponseDataCompleted(); | 455 setResponseDataCompleted(null); |
462 } | 456 } |
463 | 457 |
464 @Override | 458 @Override |
465 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 459 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
466 mResponseInfo = info; | 460 mResponseInfo = info; |
467 setResponseDataCompleted(); | 461 setResponseDataCompleted(null); |
468 } | 462 } |
469 | 463 |
470 @Override | 464 @Override |
471 public void onFailed( | 465 public void onFailed( |
472 UrlRequest request, UrlResponseInfo info, UrlRequestException ex
ception) { | 466 UrlRequest request, UrlResponseInfo info, UrlRequestException ex
ception) { |
473 if (exception == null) { | 467 if (exception == null) { |
474 throw new IllegalStateException( | 468 throw new IllegalStateException( |
475 "Exception cannot be null in onFailed."); | 469 "Exception cannot be null in onFailed."); |
476 } | 470 } |
477 mResponseInfo = info; | 471 mResponseInfo = info; |
478 mException = exception; | 472 mException = exception; |
479 setResponseDataCompleted(); | 473 setResponseDataCompleted(mException); |
| 474 } |
| 475 |
| 476 @Override |
| 477 public void onCanceled(UrlRequest request, UrlResponseInfo info) { |
| 478 mResponseInfo = info; |
| 479 setResponseDataCompleted(new IOException("stream closed")); |
480 } | 480 } |
481 | 481 |
482 /** | 482 /** |
483 * Notifies {@link #mInputStream} that transferring of response data has | 483 * Notifies {@link #mInputStream} that transferring of response data has |
484 * completed. | 484 * completed. |
| 485 * @param exception if not {@code null}, it is the exception to report w
hen |
| 486 * caller tries to read more data. |
485 */ | 487 */ |
486 private void setResponseDataCompleted() { | 488 private void setResponseDataCompleted(IOException exception) { |
487 if (mInputStream != null) { | 489 if (mInputStream != null) { |
488 mInputStream.setResponseDataCompleted(); | 490 mInputStream.setResponseDataCompleted(exception); |
489 } | 491 } |
490 mMessageLoop.quit(); | 492 mMessageLoop.quit(); |
491 } | 493 } |
492 } | 494 } |
493 | 495 |
494 /** | 496 /** |
495 * Blocks until the respone headers are received. | 497 * Blocks until the respone headers are received. |
496 */ | 498 */ |
497 private void getResponse() throws IOException { | 499 private void getResponse() throws IOException { |
498 // Check to see if enough data has been received. | 500 // Check to see if enough data has been received. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 } | 546 } |
545 | 547 |
546 /** | 548 /** |
547 * Returns whether the client has used {@link #setChunkedStreamingMode} to | 549 * Returns whether the client has used {@link #setChunkedStreamingMode} to |
548 * set chunked encoding for upload. | 550 * set chunked encoding for upload. |
549 */ | 551 */ |
550 private boolean isChunkedUpload() { | 552 private boolean isChunkedUpload() { |
551 return chunkLength > 0; | 553 return chunkLength > 0; |
552 } | 554 } |
553 } | 555 } |
OLD | NEW |