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