| 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 27 matching lines...) Expand all Loading... |
| 38 private static final String CONTENT_LENGTH = "Content-Length"; | 38 private static final String CONTENT_LENGTH = "Content-Length"; |
| 39 private final CronetEngine mCronetEngine; | 39 private final CronetEngine mCronetEngine; |
| 40 private final MessageLoop mMessageLoop; | 40 private final MessageLoop mMessageLoop; |
| 41 private UrlRequest mRequest; | 41 private UrlRequest mRequest; |
| 42 private final List<Pair<String, String>> mRequestHeaders; | 42 private final List<Pair<String, String>> mRequestHeaders; |
| 43 | 43 |
| 44 private CronetInputStream mInputStream; | 44 private CronetInputStream mInputStream; |
| 45 private CronetOutputStream mOutputStream; | 45 private CronetOutputStream mOutputStream; |
| 46 private UrlResponseInfo mResponseInfo; | 46 private UrlResponseInfo mResponseInfo; |
| 47 private CronetException mException; | 47 private CronetException mException; |
| 48 private boolean mOnRedirectCalled = false; | 48 private boolean mOnRedirectCalled; |
| 49 private boolean mHasResponse = false; | 49 // Whether response headers are received, the request is failed, or the requ
est is canceled. |
| 50 private boolean mHasResponseHeadersOrCompleted; |
| 50 private List<Map.Entry<String, String>> mResponseHeadersList; | 51 private List<Map.Entry<String, String>> mResponseHeadersList; |
| 51 private Map<String, List<String>> mResponseHeadersMap; | 52 private Map<String, List<String>> mResponseHeadersMap; |
| 52 | 53 |
| 53 public CronetHttpURLConnection(URL url, CronetEngine cronetEngine) { | 54 public CronetHttpURLConnection(URL url, CronetEngine cronetEngine) { |
| 54 super(url); | 55 super(url); |
| 55 mCronetEngine = cronetEngine; | 56 mCronetEngine = cronetEngine; |
| 56 mMessageLoop = new MessageLoop(); | 57 mMessageLoop = new MessageLoop(); |
| 57 mInputStream = new CronetInputStream(this); | 58 mInputStream = new CronetInputStream(this); |
| 58 mRequestHeaders = new ArrayList<Pair<String, String>>(); | 59 mRequestHeaders = new ArrayList<Pair<String, String>>(); |
| 59 } | 60 } |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 } | 431 } |
| 431 return -1; | 432 return -1; |
| 432 } | 433 } |
| 433 | 434 |
| 434 private class CronetUrlRequestCallback extends UrlRequest.Callback { | 435 private class CronetUrlRequestCallback extends UrlRequest.Callback { |
| 435 public CronetUrlRequestCallback() {} | 436 public CronetUrlRequestCallback() {} |
| 436 | 437 |
| 437 @Override | 438 @Override |
| 438 public void onResponseStarted(UrlRequest request, UrlResponseInfo info)
{ | 439 public void onResponseStarted(UrlRequest request, UrlResponseInfo info)
{ |
| 439 mResponseInfo = info; | 440 mResponseInfo = info; |
| 441 mHasResponseHeadersOrCompleted = true; |
| 440 // Quits the message loop since we have the headers now. | 442 // Quits the message loop since we have the headers now. |
| 441 mMessageLoop.quit(); | 443 mMessageLoop.quit(); |
| 442 } | 444 } |
| 443 | 445 |
| 444 @Override | 446 @Override |
| 445 public void onReadCompleted( | 447 public void onReadCompleted( |
| 446 UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer)
{ | 448 UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer)
{ |
| 447 mResponseInfo = info; | 449 mResponseInfo = info; |
| 448 mMessageLoop.quit(); | 450 mMessageLoop.quit(); |
| 449 } | 451 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 * @param exception if not {@code null}, it is the exception to report w
hen | 503 * @param exception if not {@code null}, it is the exception to report w
hen |
| 502 * caller tries to read more data. | 504 * caller tries to read more data. |
| 503 */ | 505 */ |
| 504 private void setResponseDataCompleted(IOException exception) { | 506 private void setResponseDataCompleted(IOException exception) { |
| 505 if (mInputStream != null) { | 507 if (mInputStream != null) { |
| 506 mInputStream.setResponseDataCompleted(exception); | 508 mInputStream.setResponseDataCompleted(exception); |
| 507 } | 509 } |
| 508 if (mOutputStream != null) { | 510 if (mOutputStream != null) { |
| 509 mOutputStream.setRequestCompleted(exception); | 511 mOutputStream.setRequestCompleted(exception); |
| 510 } | 512 } |
| 511 mHasResponse = true; | 513 mHasResponseHeadersOrCompleted = true; |
| 512 mMessageLoop.quit(); | 514 mMessageLoop.quit(); |
| 513 } | 515 } |
| 514 } | 516 } |
| 515 | 517 |
| 516 /** | 518 /** |
| 517 * Blocks until the respone headers are received. | 519 * Blocks until the respone headers are received. |
| 518 */ | 520 */ |
| 519 private void getResponse() throws IOException { | 521 private void getResponse() throws IOException { |
| 520 // Check to see if enough data has been received. | 522 // Check to see if enough data has been received. |
| 521 if (mOutputStream != null) { | 523 if (mOutputStream != null) { |
| 522 mOutputStream.checkReceivedEnoughContent(); | 524 mOutputStream.checkReceivedEnoughContent(); |
| 523 if (isChunkedUpload()) { | 525 if (isChunkedUpload()) { |
| 524 // Write last chunk. | 526 // Write last chunk. |
| 525 mOutputStream.close(); | 527 mOutputStream.close(); |
| 526 } | 528 } |
| 527 } | 529 } |
| 528 if (!mHasResponse) { | 530 if (!mHasResponseHeadersOrCompleted) { |
| 529 startRequest(); | 531 startRequest(); |
| 530 // Blocks until onResponseStarted or onFailed is called. | 532 // Blocks until onResponseStarted or onFailed is called. |
| 531 mMessageLoop.loop(); | 533 mMessageLoop.loop(); |
| 532 mHasResponse = true; | |
| 533 } | 534 } |
| 534 checkHasResponse(); | 535 checkHasResponseHeaders(); |
| 535 } | 536 } |
| 536 | 537 |
| 537 /** | 538 /** |
| 538 * Checks whether response headers are received, and throws an exception if | 539 * Checks whether response headers are received, and throws an exception if |
| 539 * an exception occurred before headers received. This method should only be | 540 * an exception occurred before headers received. This method should only be |
| 540 * called after onResponseStarted or onFailed. | 541 * called after onResponseStarted or onFailed. |
| 541 */ | 542 */ |
| 542 private void checkHasResponse() throws IOException { | 543 private void checkHasResponseHeaders() throws IOException { |
| 543 if (!mHasResponse) throw new IllegalStateException("No response."); | 544 if (!mHasResponseHeadersOrCompleted) throw new IllegalStateException("No
response."); |
| 544 if (mException != null) { | 545 if (mException != null) { |
| 545 throw mException; | 546 throw mException; |
| 546 } else if (mResponseInfo == null) { | 547 } else if (mResponseInfo == null) { |
| 547 throw new NullPointerException( | 548 throw new NullPointerException( |
| 548 "Response info is null when there is no exception."); | 549 "Response info is null when there is no exception."); |
| 549 } | 550 } |
| 550 } | 551 } |
| 551 | 552 |
| 552 /** | 553 /** |
| 553 * Helper method to return the response header field at position pos. | 554 * Helper method to return the response header field at position pos. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 // Strips Content-Encoding response header. See crbug.com/592700. | 601 // Strips Content-Encoding response header. See crbug.com/592700. |
| 601 if (!entry.getKey().equalsIgnoreCase("Content-Encoding")) { | 602 if (!entry.getKey().equalsIgnoreCase("Content-Encoding")) { |
| 602 mResponseHeadersList.add( | 603 mResponseHeadersList.add( |
| 603 new AbstractMap.SimpleImmutableEntry<String, String>(ent
ry)); | 604 new AbstractMap.SimpleImmutableEntry<String, String>(ent
ry)); |
| 604 } | 605 } |
| 605 } | 606 } |
| 606 mResponseHeadersList = Collections.unmodifiableList(mResponseHeadersList
); | 607 mResponseHeadersList = Collections.unmodifiableList(mResponseHeadersList
); |
| 607 return mResponseHeadersList; | 608 return mResponseHeadersList; |
| 608 } | 609 } |
| 609 } | 610 } |
| OLD | NEW |