Chromium Code Reviews| 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; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.util.Log; | |
| 8 | |
| 7 import org.apache.http.conn.ConnectTimeoutException; | 9 import org.apache.http.conn.ConnectTimeoutException; |
| 8 import org.chromium.base.CalledByNative; | 10 import org.chromium.base.CalledByNative; |
| 9 import org.chromium.base.JNINamespace; | 11 import org.chromium.base.JNINamespace; |
| 10 | 12 |
| 11 import java.io.IOException; | 13 import java.io.IOException; |
| 12 import java.net.MalformedURLException; | 14 import java.net.MalformedURLException; |
| 13 import java.net.URL; | 15 import java.net.URL; |
| 14 import java.net.UnknownHostException; | 16 import java.net.UnknownHostException; |
| 15 import java.nio.ByteBuffer; | 17 import java.nio.ByteBuffer; |
| 16 import java.nio.channels.ReadableByteChannel; | 18 import java.nio.channels.ReadableByteChannel; |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 | 396 |
| 395 private void validateHeadersAvailable() { | 397 private void validateHeadersAvailable() { |
| 396 if (!mHeadersAvailable) { | 398 if (!mHeadersAvailable) { |
| 397 throw new IllegalStateException("Response headers not available"); | 399 throw new IllegalStateException("Response headers not available"); |
| 398 } | 400 } |
| 399 } | 401 } |
| 400 | 402 |
| 401 // Private methods called by native library. | 403 // Private methods called by native library. |
| 402 | 404 |
| 403 /** | 405 /** |
| 406 * If @CalledByNative method throws an exception, request gets cancelled | |
| 407 * and exception could be retrieved from request using getException(). | |
| 408 */ | |
| 409 private void onCalledByNativeException(Exception e) { | |
| 410 mSinkException = new IOException( | |
| 411 "CalledByNative method has thrown an exception", e); | |
| 412 Log.e(ChromiumUrlRequestContext.LOG_TAG, | |
| 413 "Exception in CalledByNative method", e); | |
| 414 try { | |
| 415 cancel(); | |
| 416 } catch (Exception cancel_exception) { | |
| 417 Log.e(ChromiumUrlRequestContext.LOG_TAG, | |
| 418 "Exception trying to cancel request", cancel_exception); | |
| 419 } | |
| 420 } | |
| 421 | |
| 422 /** | |
| 404 * A callback invoked when the first chunk of the response has arrived. | 423 * A callback invoked when the first chunk of the response has arrived. |
| 405 */ | 424 */ |
| 406 @CalledByNative | 425 @CalledByNative |
| 407 private void onResponseStarted() { | 426 private void onResponseStarted() { |
| 408 mContentType = nativeGetContentType(mUrlRequestAdapter); | 427 try { |
| 409 mContentLength = nativeGetContentLength(mUrlRequestAdapter); | 428 mContentType = nativeGetContentType(mUrlRequestAdapter); |
| 410 mHeadersAvailable = true; | 429 mContentLength = nativeGetContentLength(mUrlRequestAdapter); |
| 430 mHeadersAvailable = true; | |
| 411 | 431 |
| 412 if (mContentLengthLimit > 0 && mContentLength > mContentLengthLimit | 432 if (mContentLengthLimit > 0 && |
| 413 && mCancelIfContentLengthOverLimit) { | 433 mContentLength > mContentLengthLimit && |
| 414 onContentLengthOverLimit(); | 434 mCancelIfContentLengthOverLimit) { |
| 415 return; | 435 onContentLengthOverLimit(); |
| 436 return; | |
| 437 } | |
| 438 | |
| 439 if (mBufferFullResponse && mContentLength != -1 | |
| 440 && !mContentLengthOverLimit) { | |
| 441 ((ChunkedWritableByteChannel)getSink()).setCapacity( | |
| 442 (int)mContentLength); | |
| 443 } | |
| 444 | |
| 445 if (mOffset != 0) { | |
| 446 // The server may ignore the request for a byte range, in which case | |
| 447 // status code will be 200, instead of 206. Note that we cannot call | |
| 448 // getHttpStatusCode as it rewrites 206 into 200. | |
| 449 if (nativeGetHttpStatusCode(mUrlRequestAdapter) == 200) { | |
| 450 // TODO(mef): Revisit this logic. | |
| 451 if (mContentLength != -1) { | |
| 452 mContentLength -= mOffset; | |
| 453 } | |
| 454 mSkippingToOffset = true; | |
| 455 } else { | |
| 456 mSize = mOffset; | |
| 457 } | |
| 458 } | |
| 459 mListener.onResponseStarted(this); | |
| 460 } catch (Exception e) { | |
| 461 onCalledByNativeException(e); | |
| 416 } | 462 } |
| 417 | |
| 418 if (mBufferFullResponse && mContentLength != -1 | |
| 419 && !mContentLengthOverLimit) { | |
| 420 ((ChunkedWritableByteChannel)getSink()).setCapacity( | |
| 421 (int)mContentLength); | |
| 422 } | |
| 423 | |
| 424 if (mOffset != 0) { | |
| 425 // The server may ignore the request for a byte range, in which case | |
| 426 // status code will be 200, instead of 206. Note that we cannot call | |
| 427 // getHttpStatusCode as it rewrites 206 into 200. | |
| 428 if (nativeGetHttpStatusCode(mUrlRequestAdapter) == 200) { | |
| 429 // TODO(mef): Revisit this logic. | |
| 430 if (mContentLength != -1) { | |
| 431 mContentLength -= mOffset; | |
| 432 } | |
| 433 mSkippingToOffset = true; | |
| 434 } else { | |
| 435 mSize = mOffset; | |
| 436 } | |
| 437 } | |
| 438 mListener.onResponseStarted(this); | |
| 439 } | 463 } |
| 440 | 464 |
| 441 /** | 465 /** |
| 442 * Consumes a portion of the response. | 466 * Consumes a portion of the response. |
| 443 * | 467 * |
| 444 * @param byteBuffer The ByteBuffer to append. Must be a direct buffer, and | 468 * @param byteBuffer The ByteBuffer to append. Must be a direct buffer, and |
| 445 * no references to it may be retained after the method ends, as | 469 * no references to it may be retained after the method ends, as |
| 446 * it wraps code managed on the native heap. | 470 * it wraps code managed on the native heap. |
| 447 */ | 471 */ |
| 448 @CalledByNative | 472 @CalledByNative |
| 449 private void onBytesRead(ByteBuffer buffer) { | 473 private void onBytesRead(ByteBuffer buffer) { |
| 450 if (mContentLengthOverLimit) { | 474 try { |
| 451 return; | 475 if (mContentLengthOverLimit) { |
| 452 } | 476 return; |
| 477 } | |
| 453 | 478 |
| 454 int size = buffer.remaining(); | 479 int size = buffer.remaining(); |
| 455 mSize += size; | 480 mSize += size; |
| 456 if (mSkippingToOffset) { | 481 if (mSkippingToOffset) { |
| 457 if (mSize <= mOffset) { | 482 if (mSize <= mOffset) { |
| 458 return; | 483 return; |
| 459 } else { | 484 } else { |
| 460 mSkippingToOffset = false; | 485 mSkippingToOffset = false; |
| 461 buffer.position((int)(mOffset - (mSize - size))); | 486 buffer.position((int)(mOffset - (mSize - size))); |
| 487 } | |
| 462 } | 488 } |
| 463 } | |
| 464 | 489 |
| 465 boolean contentLengthOverLimit = | 490 boolean contentLengthOverLimit = |
| 466 (mContentLengthLimit != 0 && mSize > mContentLengthLimit); | 491 (mContentLengthLimit != 0 && mSize > mContentLengthLimit); |
| 467 if (contentLengthOverLimit) { | 492 if (contentLengthOverLimit) { |
| 468 buffer.limit(size - (int)(mSize - mContentLengthLimit)); | 493 buffer.limit(size - (int)(mSize - mContentLengthLimit)); |
| 469 } | 494 } |
| 470 | 495 |
| 471 try { | |
| 472 while (buffer.hasRemaining()) { | 496 while (buffer.hasRemaining()) { |
| 473 mSink.write(buffer); | 497 mSink.write(buffer); |
| 474 } | 498 } |
| 475 } catch (IOException e) { | 499 if (contentLengthOverLimit) { |
| 476 mSinkException = e; | 500 onContentLengthOverLimit(); |
|
mef
2014/08/15 16:03:35
onContentLengthOverLimit is no longer called if ex
mmenke
2014/08/15 16:12:22
I agree
| |
| 477 cancel(); | 501 } |
| 478 } | 502 } catch (Exception e) { |
| 479 if (contentLengthOverLimit) { | 503 onCalledByNativeException(e); |
| 480 onContentLengthOverLimit(); | |
| 481 } | 504 } |
| 482 } | 505 } |
| 483 | 506 |
| 484 /** | 507 /** |
| 485 * Notifies the listener, releases native data structures. | 508 * Notifies the listener, releases native data structures. |
| 486 */ | 509 */ |
| 487 @SuppressWarnings("unused") | 510 @SuppressWarnings("unused") |
| 488 @CalledByNative | 511 @CalledByNative |
| 489 private void finish() { | 512 private void finish() { |
| 490 synchronized (mLock) { | 513 try { |
| 491 mFinished = true; | 514 synchronized (mLock) { |
| 515 mFinished = true; | |
| 492 | 516 |
| 493 if (mRecycled) { | 517 if (mRecycled) { |
| 494 return; | 518 return; |
| 519 } | |
| 520 try { | |
| 521 mSink.close(); | |
| 522 } catch (IOException e) { | |
| 523 // Ignore | |
| 524 } | |
| 525 onRequestComplete(); | |
| 526 nativeDestroyRequestAdapter(mUrlRequestAdapter); | |
| 527 mUrlRequestAdapter = 0; | |
| 528 mRecycled = true; | |
| 495 } | 529 } |
| 496 try { | 530 } catch (Exception e) { |
| 497 mSink.close(); | 531 onCalledByNativeException(e); |
| 498 } catch (IOException e) { | |
| 499 // Ignore | |
| 500 } | |
| 501 onRequestComplete(); | |
| 502 nativeDestroyRequestAdapter(mUrlRequestAdapter); | |
| 503 mUrlRequestAdapter = 0; | |
| 504 mRecycled = true; | |
| 505 } | 532 } |
| 506 } | 533 } |
| 507 | 534 |
| 508 /** | 535 /** |
| 509 * Appends header |name| with value |value| to |headersMap|. | 536 * Appends header |name| with value |value| to |headersMap|. |
| 510 */ | 537 */ |
| 511 @SuppressWarnings("unused") | 538 @SuppressWarnings("unused") |
| 512 @CalledByNative | 539 @CalledByNative |
| 513 private void onAppendResponseHeader(ResponseHeadersMap headersMap, | 540 private void onAppendResponseHeader(ResponseHeadersMap headersMap, |
| 514 String name, String value) { | 541 String name, String value) { |
| 515 if (!headersMap.containsKey(name)) { | 542 try { |
| 516 headersMap.put(name, new ArrayList<String>()); | 543 if (!headersMap.containsKey(name)) { |
| 544 headersMap.put(name, new ArrayList<String>()); | |
| 545 } | |
| 546 headersMap.get(name).add(value); | |
| 547 } catch (Exception e) { | |
| 548 onCalledByNativeException(e); | |
| 517 } | 549 } |
| 518 headersMap.get(name).add(value); | |
| 519 } | 550 } |
| 520 | 551 |
| 521 /** | 552 /** |
| 522 * Reads a sequence of bytes from upload channel into the given buffer. | 553 * Reads a sequence of bytes from upload channel into the given buffer. |
| 523 * @param dest The buffer into which bytes are to be transferred. | 554 * @param dest The buffer into which bytes are to be transferred. |
| 524 * @return Returns number of bytes read (could be 0) or -1 and closes | 555 * @return Returns number of bytes read (could be 0) or -1 and closes |
| 525 * the channel if error occured. | 556 * the channel if error occured. |
| 526 */ | 557 */ |
| 527 @SuppressWarnings("unused") | 558 @SuppressWarnings("unused") |
| 528 @CalledByNative | 559 @CalledByNative |
| 529 private int readFromUploadChannel(ByteBuffer dest) { | 560 private int readFromUploadChannel(ByteBuffer dest) { |
| 530 if (mUploadChannel == null || !mUploadChannel.isOpen()) | |
| 531 return -1; | |
| 532 try { | 561 try { |
| 562 if (mUploadChannel == null || !mUploadChannel.isOpen()) | |
| 563 return -1; | |
| 533 int result = mUploadChannel.read(dest); | 564 int result = mUploadChannel.read(dest); |
| 534 if (result < 0) { | 565 if (result < 0) { |
| 535 mUploadChannel.close(); | 566 mUploadChannel.close(); |
|
mef
2014/08/15 17:15:01
Per clm it is better to close the channel as soon
| |
| 536 return 0; | 567 return 0; |
| 537 } | 568 } |
| 538 return result; | 569 return result; |
| 539 } catch (IOException e) { | 570 } catch (Exception e) { |
| 540 mSinkException = e; | 571 onCalledByNativeException(e); |
| 541 try { | 572 try { |
| 542 mUploadChannel.close(); | 573 mUploadChannel.close(); |
|
mmenke
2014/08/15 16:12:22
Question: Why does this matter, anyways? We aren
mef
2014/08/15 17:15:01
I've discussed with clm@, and we have to close upl
| |
| 543 } catch (IOException ignored) { | 574 } catch (IOException ignored) { |
| 544 // Ignore this exception. | 575 // Ignore this exception. |
| 545 } | 576 } |
| 546 cancel(); | |
| 547 return -1; | |
| 548 } | 577 } |
| 578 return -1; | |
| 549 } | 579 } |
| 550 | 580 |
| 551 // Native methods are implemented in chromium_url_request.cc. | 581 // Native methods are implemented in chromium_url_request.cc. |
| 552 | 582 |
| 553 private native long nativeCreateRequestAdapter( | 583 private native long nativeCreateRequestAdapter( |
| 554 long ChromiumUrlRequestContextAdapter, String url, int priority); | 584 long ChromiumUrlRequestContextAdapter, String url, int priority); |
| 555 | 585 |
| 556 private native void nativeAddHeader(long urlRequestAdapter, String name, | 586 private native void nativeAddHeader(long urlRequestAdapter, String name, |
| 557 String value); | 587 String value); |
| 558 | 588 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 582 | 612 |
| 583 private native String nativeGetHeader(long urlRequestAdapter, String name); | 613 private native String nativeGetHeader(long urlRequestAdapter, String name); |
| 584 | 614 |
| 585 private native void nativeGetAllHeaders(long urlRequestAdapter, | 615 private native void nativeGetAllHeaders(long urlRequestAdapter, |
| 586 ResponseHeadersMap headers); | 616 ResponseHeadersMap headers); |
| 587 | 617 |
| 588 // Explicit class to work around JNI-generator generics confusion. | 618 // Explicit class to work around JNI-generator generics confusion. |
| 589 private class ResponseHeadersMap extends HashMap<String, List<String>> { | 619 private class ResponseHeadersMap extends HashMap<String, List<String>> { |
| 590 } | 620 } |
| 591 } | 621 } |
| OLD | NEW |