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(); |
pauljensen
2015/11/18 13:15:05
This seems racy. We cancel on this thread but the
xunjieli
2015/11/18 21:14:07
This is probably unavoidable. This HttpURLConnecti
pauljensen
2015/11/19 04:51:08
Can we make disconnect() call mMessageLoop.loop()
xunjieli
2015/12/02 15:20:03
I assume you meant mMessageLoop.quit(). I don't th
pauljensen
2015/12/02 16:21:52
No, I meant loop(). We know onCanceled() or some
xunjieli
2015/12/02 23:49:27
I don't think we should call loop here. That might
pauljensen
2015/12/03 04:04:27
I really think we need to do something here. This
| |
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(); |
93 return mResponseInfo.getHttpStatusText(); | 87 return mResponseInfo.getHttpStatusText(); |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 } | 442 } |
449 if (instanceFollowRedirects && sameProtocol) { | 443 if (instanceFollowRedirects && sameProtocol) { |
450 mRequest.followRedirect(); | 444 mRequest.followRedirect(); |
451 return; | 445 return; |
452 } | 446 } |
453 } catch (MalformedURLException e) { | 447 } catch (MalformedURLException e) { |
454 // Ignored. Just cancel the request and not follow the redirect. | 448 // Ignored. Just cancel the request and not follow the redirect. |
455 } | 449 } |
456 mResponseInfo = info; | 450 mResponseInfo = info; |
457 mRequest.cancel(); | 451 mRequest.cancel(); |
458 setResponseDataCompleted(); | 452 setResponseDataCompleted(null); |
459 } | 453 } |
460 | 454 |
461 @Override | 455 @Override |
462 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 456 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
463 mResponseInfo = info; | 457 mResponseInfo = info; |
464 setResponseDataCompleted(); | 458 setResponseDataCompleted(null); |
465 } | 459 } |
466 | 460 |
467 @Override | 461 @Override |
468 public void onFailed( | 462 public void onFailed( |
469 UrlRequest request, UrlResponseInfo info, UrlRequestException ex ception) { | 463 UrlRequest request, UrlResponseInfo info, UrlRequestException ex ception) { |
470 if (exception == null) { | 464 if (exception == null) { |
471 throw new IllegalStateException( | 465 throw new IllegalStateException( |
472 "Exception cannot be null in onFailed."); | 466 "Exception cannot be null in onFailed."); |
473 } | 467 } |
474 mResponseInfo = info; | 468 mResponseInfo = info; |
475 mException = exception; | 469 mException = exception; |
476 setResponseDataCompleted(); | 470 setResponseDataCompleted(mException); |
471 } | |
472 | |
473 @Override | |
474 public void onCanceled(UrlRequest request, UrlResponseInfo info) { | |
475 mResponseInfo = info; | |
476 setResponseDataCompleted(new IOException("stream closed")); | |
477 } | 477 } |
478 | 478 |
479 /** | 479 /** |
480 * Notifies {@link #mInputStream} that transferring of response data has | 480 * Notifies {@link #mInputStream} that transferring of response data has |
481 * completed. | 481 * completed. |
482 * @param exception if not {@code null}, it is the exception to report w hen | |
483 * caller tries to read more data. | |
482 */ | 484 */ |
483 private void setResponseDataCompleted() { | 485 private void setResponseDataCompleted(IOException exception) { |
484 if (mInputStream != null) { | 486 if (mInputStream != null) { |
485 mInputStream.setResponseDataCompleted(); | 487 mInputStream.setResponseDataCompleted(exception); |
486 } | 488 } |
487 mMessageLoop.quit(); | 489 mMessageLoop.quit(); |
488 } | 490 } |
489 } | 491 } |
490 | 492 |
491 /** | 493 /** |
492 * Blocks until the respone headers are received. | 494 * Blocks until the respone headers are received. |
493 */ | 495 */ |
494 private void getResponse() throws IOException { | 496 private void getResponse() throws IOException { |
495 // Check to see if enough data has been received. | 497 // Check to see if enough data has been received. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
541 } | 543 } |
542 | 544 |
543 /** | 545 /** |
544 * Returns whether the client has used {@link #setChunkedStreamingMode} to | 546 * Returns whether the client has used {@link #setChunkedStreamingMode} to |
545 * set chunked encoding for upload. | 547 * set chunked encoding for upload. |
546 */ | 548 */ |
547 private boolean isChunkedUpload() { | 549 private boolean isChunkedUpload() { |
548 return chunkLength > 0; | 550 return chunkLength > 0; |
549 } | 551 } |
550 } | 552 } |
OLD | NEW |