| 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; | 7 import android.util.Log; |
| 8 | 8 |
| 9 import org.apache.http.conn.ConnectTimeoutException; | 9 import org.apache.http.conn.ConnectTimeoutException; |
| 10 import org.chromium.base.CalledByNative; | 10 import org.chromium.base.CalledByNative; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 176 } |
| 177 return new UnknownHostException("Unknown host: " + host); | 177 return new UnknownHostException("Unknown host: " + host); |
| 178 default: | 178 default: |
| 179 throw new IllegalStateException( | 179 throw new IllegalStateException( |
| 180 "Unrecognized error code: " + errorCode); | 180 "Unrecognized error code: " + errorCode); |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 | 183 |
| 184 @Override | 184 @Override |
| 185 public ByteBuffer getByteBuffer() { | 185 public ByteBuffer getByteBuffer() { |
| 186 return ((ChunkedWritableByteChannel)getSink()).getByteBuffer(); | 186 return ((ChunkedWritableByteChannel) getSink()).getByteBuffer(); |
| 187 } | 187 } |
| 188 | 188 |
| 189 @Override | 189 @Override |
| 190 public byte[] getResponseAsBytes() { | 190 public byte[] getResponseAsBytes() { |
| 191 return ((ChunkedWritableByteChannel)getSink()).getBytes(); | 191 return ((ChunkedWritableByteChannel) getSink()).getBytes(); |
| 192 } | 192 } |
| 193 | 193 |
| 194 /** | 194 /** |
| 195 * Adds a request header. Must be done before request has started. | 195 * Adds a request header. Must be done before request has started. |
| 196 */ | 196 */ |
| 197 public void addHeader(String header, String value) { | 197 public void addHeader(String header, String value) { |
| 198 synchronized (mLock) { | 198 synchronized (mLock) { |
| 199 validateNotStarted(); | 199 validateNotStarted(); |
| 200 if (mAdditionalHeaders == null) { | 200 if (mAdditionalHeaders == null) { |
| 201 mAdditionalHeaders = new HashMap<String, String>(); | 201 mAdditionalHeaders = new HashMap<String, String>(); |
| 202 } | 202 } |
| 203 mAdditionalHeaders.put(header, value); | 203 mAdditionalHeaders.put(header, value); |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 | 206 |
| 207 /** | 207 /** |
| 208 * Sets data to upload as part of a POST or PUT request. | 208 * Sets data to upload as part of a POST or PUT request. |
| 209 * | 209 * |
| 210 * @param contentType MIME type of the upload content or null if this is not | 210 * @param contentType MIME type of the upload content or null if this is not |
| 211 * an upload. | 211 * an upload. |
| 212 * @param data The content that needs to be uploaded. | 212 * @param data The content that needs to be uploaded. |
| 213 */ | 213 */ |
| 214 @Override |
| 214 public void setUploadData(String contentType, byte[] data) { | 215 public void setUploadData(String contentType, byte[] data) { |
| 215 synchronized (mLock) { | 216 synchronized (mLock) { |
| 216 validateNotStarted(); | 217 validateNotStarted(); |
| 217 validateContentType(contentType); | 218 validateContentType(contentType); |
| 218 mUploadContentType = contentType; | 219 mUploadContentType = contentType; |
| 219 mUploadData = data; | 220 mUploadData = data; |
| 220 mUploadChannel = null; | 221 mUploadChannel = null; |
| 221 mChunkedUpload = false; | 222 mChunkedUpload = false; |
| 222 } | 223 } |
| 223 } | 224 } |
| 224 | 225 |
| 225 /** | 226 /** |
| 226 * Sets a readable byte channel to upload as part of a POST or PUT request. | 227 * Sets a readable byte channel to upload as part of a POST or PUT request. |
| 227 * | 228 * |
| 228 * @param contentType MIME type of the upload content or null if this is not | 229 * @param contentType MIME type of the upload content or null if this is not |
| 229 * an upload request. | 230 * an upload request. |
| 230 * @param channel The channel to read to read upload data from if this is an | 231 * @param channel The channel to read to read upload data from if this is an |
| 231 * upload request. | 232 * upload request. |
| 232 * @param contentLength The length of data to upload. | 233 * @param contentLength The length of data to upload. |
| 233 */ | 234 */ |
| 235 @Override |
| 234 public void setUploadChannel(String contentType, | 236 public void setUploadChannel(String contentType, |
| 235 ReadableByteChannel channel, long contentLength) { | 237 ReadableByteChannel channel, long contentLength) { |
| 236 synchronized (mLock) { | 238 synchronized (mLock) { |
| 237 validateNotStarted(); | 239 validateNotStarted(); |
| 238 validateContentType(contentType); | 240 validateContentType(contentType); |
| 239 mUploadContentType = contentType; | 241 mUploadContentType = contentType; |
| 240 mUploadChannel = channel; | 242 mUploadChannel = channel; |
| 241 mUploadContentLength = contentLength; | 243 mUploadContentLength = contentLength; |
| 242 mUploadData = null; | 244 mUploadData = null; |
| 243 mChunkedUpload = false; | 245 mChunkedUpload = false; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 @Override | 300 @Override |
| 299 public void setHttpMethod(String method) { | 301 public void setHttpMethod(String method) { |
| 300 validateNotStarted(); | 302 validateNotStarted(); |
| 301 mMethod = method; | 303 mMethod = method; |
| 302 } | 304 } |
| 303 | 305 |
| 304 public WritableByteChannel getSink() { | 306 public WritableByteChannel getSink() { |
| 305 return mSink; | 307 return mSink; |
| 306 } | 308 } |
| 307 | 309 |
| 310 @Override |
| 308 public void start() { | 311 public void start() { |
| 309 synchronized (mLock) { | 312 synchronized (mLock) { |
| 310 if (mCanceled) { | 313 if (mCanceled) { |
| 311 return; | 314 return; |
| 312 } | 315 } |
| 313 | 316 |
| 314 validateNotStarted(); | 317 validateNotStarted(); |
| 315 validateNotRecycled(); | 318 validateNotRecycled(); |
| 316 | 319 |
| 317 mStarted = true; | 320 mStarted = true; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 mUploadContentLength); | 355 mUploadContentLength); |
| 353 } else if (mChunkedUpload) { | 356 } else if (mChunkedUpload) { |
| 354 nativeEnableChunkedUpload(mUrlRequestAdapter, | 357 nativeEnableChunkedUpload(mUrlRequestAdapter, |
| 355 mUploadContentType); | 358 mUploadContentType); |
| 356 } | 359 } |
| 357 | 360 |
| 358 nativeStart(mUrlRequestAdapter); | 361 nativeStart(mUrlRequestAdapter); |
| 359 } | 362 } |
| 360 } | 363 } |
| 361 | 364 |
| 365 @Override |
| 362 public void cancel() { | 366 public void cancel() { |
| 363 synchronized (mLock) { | 367 synchronized (mLock) { |
| 364 if (mCanceled) { | 368 if (mCanceled) { |
| 365 return; | 369 return; |
| 366 } | 370 } |
| 367 | 371 |
| 368 mCanceled = true; | 372 mCanceled = true; |
| 369 | 373 |
| 370 if (!mRecycled) { | 374 if (!mRecycled) { |
| 371 nativeCancel(mUrlRequestAdapter); | 375 nativeCancel(mUrlRequestAdapter); |
| 372 } | 376 } |
| 373 } | 377 } |
| 374 } | 378 } |
| 375 | 379 |
| 380 @Override |
| 376 public boolean isCanceled() { | 381 public boolean isCanceled() { |
| 377 synchronized (mLock) { | 382 synchronized (mLock) { |
| 378 return mCanceled; | 383 return mCanceled; |
| 379 } | 384 } |
| 380 } | 385 } |
| 381 | 386 |
| 382 public boolean isRecycled() { | 387 public boolean isRecycled() { |
| 383 synchronized (mLock) { | 388 synchronized (mLock) { |
| 384 return mRecycled; | 389 return mRecycled; |
| 385 } | 390 } |
| 386 } | 391 } |
| 387 | 392 |
| 388 @Override | 393 @Override |
| 389 public String getNegotiatedProtocol() { | 394 public String getNegotiatedProtocol() { |
| 390 validateNotRecycled(); | 395 validateNotRecycled(); |
| 391 validateHeadersAvailable(); | 396 validateHeadersAvailable(); |
| 392 return nativeGetNegotiatedProtocol(mUrlRequestAdapter); | 397 return nativeGetNegotiatedProtocol(mUrlRequestAdapter); |
| 393 } | 398 } |
| 394 | 399 |
| 400 @Override |
| 395 public String getContentType() { | 401 public String getContentType() { |
| 396 return mContentType; | 402 return mContentType; |
| 397 } | 403 } |
| 398 | 404 |
| 405 @Override |
| 399 public String getHeader(String name) { | 406 public String getHeader(String name) { |
| 400 validateNotRecycled(); | 407 validateNotRecycled(); |
| 401 validateHeadersAvailable(); | 408 validateHeadersAvailable(); |
| 402 return nativeGetHeader(mUrlRequestAdapter, name); | 409 return nativeGetHeader(mUrlRequestAdapter, name); |
| 403 } | 410 } |
| 404 | 411 |
| 405 // All response headers. | 412 // All response headers. |
| 413 @Override |
| 406 public Map<String, List<String>> getAllHeaders() { | 414 public Map<String, List<String>> getAllHeaders() { |
| 407 validateNotRecycled(); | 415 validateNotRecycled(); |
| 408 validateHeadersAvailable(); | 416 validateHeadersAvailable(); |
| 409 ResponseHeadersMap result = new ResponseHeadersMap(); | 417 ResponseHeadersMap result = new ResponseHeadersMap(); |
| 410 nativeGetAllHeaders(mUrlRequestAdapter, result); | 418 nativeGetAllHeaders(mUrlRequestAdapter, result); |
| 411 return result; | 419 return result; |
| 412 } | 420 } |
| 413 | 421 |
| 422 @Override |
| 414 public String getUrl() { | 423 public String getUrl() { |
| 415 return mUrl; | 424 return mUrl; |
| 416 } | 425 } |
| 417 | 426 |
| 418 private static int convertRequestPriority(int priority) { | 427 private static int convertRequestPriority(int priority) { |
| 419 switch (priority) { | 428 switch (priority) { |
| 420 case HttpUrlRequest.REQUEST_PRIORITY_IDLE: | 429 case HttpUrlRequest.REQUEST_PRIORITY_IDLE: |
| 421 return ChromiumUrlRequestPriority.IDLE; | 430 return ChromiumUrlRequestPriority.IDLE; |
| 422 case HttpUrlRequest.REQUEST_PRIORITY_LOWEST: | 431 case HttpUrlRequest.REQUEST_PRIORITY_LOWEST: |
| 423 return ChromiumUrlRequestPriority.LOWEST; | 432 return ChromiumUrlRequestPriority.LOWEST; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 | 508 |
| 500 if (mContentLengthLimit > 0 && | 509 if (mContentLengthLimit > 0 && |
| 501 mContentLength > mContentLengthLimit && | 510 mContentLength > mContentLengthLimit && |
| 502 mCancelIfContentLengthOverLimit) { | 511 mCancelIfContentLengthOverLimit) { |
| 503 onContentLengthOverLimit(); | 512 onContentLengthOverLimit(); |
| 504 return; | 513 return; |
| 505 } | 514 } |
| 506 | 515 |
| 507 if (mBufferFullResponse && mContentLength != -1 && | 516 if (mBufferFullResponse && mContentLength != -1 && |
| 508 !mContentLengthOverLimit) { | 517 !mContentLengthOverLimit) { |
| 509 ((ChunkedWritableByteChannel)getSink()).setCapacity( | 518 ((ChunkedWritableByteChannel) getSink()).setCapacity( |
| 510 (int)mContentLength); | 519 (int) mContentLength); |
| 511 } | 520 } |
| 512 | 521 |
| 513 if (mOffset != 0) { | 522 if (mOffset != 0) { |
| 514 // The server may ignore the request for a byte range, in which | 523 // The server may ignore the request for a byte range, in which |
| 515 // case status code will be 200, instead of 206. Note that we | 524 // case status code will be 200, instead of 206. Note that we |
| 516 // cannot call getHttpStatusCode as it rewrites 206 into 200. | 525 // cannot call getHttpStatusCode as it rewrites 206 into 200. |
| 517 if (nativeGetHttpStatusCode(mUrlRequestAdapter) == 200) { | 526 if (nativeGetHttpStatusCode(mUrlRequestAdapter) == 200) { |
| 518 // TODO(mef): Revisit this logic. | 527 // TODO(mef): Revisit this logic. |
| 519 if (mContentLength != -1) { | 528 if (mContentLength != -1) { |
| 520 mContentLength -= mOffset; | 529 mContentLength -= mOffset; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 544 return; | 553 return; |
| 545 } | 554 } |
| 546 | 555 |
| 547 int size = buffer.remaining(); | 556 int size = buffer.remaining(); |
| 548 mSize += size; | 557 mSize += size; |
| 549 if (mSkippingToOffset) { | 558 if (mSkippingToOffset) { |
| 550 if (mSize <= mOffset) { | 559 if (mSize <= mOffset) { |
| 551 return; | 560 return; |
| 552 } else { | 561 } else { |
| 553 mSkippingToOffset = false; | 562 mSkippingToOffset = false; |
| 554 buffer.position((int)(mOffset - (mSize - size))); | 563 buffer.position((int) (mOffset - (mSize - size))); |
| 555 } | 564 } |
| 556 } | 565 } |
| 557 | 566 |
| 558 boolean contentLengthOverLimit = | 567 boolean contentLengthOverLimit = |
| 559 (mContentLengthLimit != 0 && mSize > mContentLengthLimit); | 568 (mContentLengthLimit != 0 && mSize > mContentLengthLimit); |
| 560 if (contentLengthOverLimit) { | 569 if (contentLengthOverLimit) { |
| 561 buffer.limit(size - (int)(mSize - mContentLengthLimit)); | 570 buffer.limit(size - (int) (mSize - mContentLengthLimit)); |
| 562 } | 571 } |
| 563 | 572 |
| 564 while (buffer.hasRemaining()) { | 573 while (buffer.hasRemaining()) { |
| 565 mSink.write(buffer); | 574 mSink.write(buffer); |
| 566 } | 575 } |
| 567 if (contentLengthOverLimit) { | 576 if (contentLengthOverLimit) { |
| 568 onContentLengthOverLimit(); | 577 onContentLengthOverLimit(); |
| 569 } | 578 } |
| 570 } catch (Exception e) { | 579 } catch (Exception e) { |
| 571 onCalledByNativeException(e); | 580 onCalledByNativeException(e); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 | 699 |
| 691 private native void nativeGetAllHeaders(long urlRequestAdapter, | 700 private native void nativeGetAllHeaders(long urlRequestAdapter, |
| 692 ResponseHeadersMap headers); | 701 ResponseHeadersMap headers); |
| 693 | 702 |
| 694 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); | 703 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); |
| 695 | 704 |
| 696 // Explicit class to work around JNI-generator generics confusion. | 705 // Explicit class to work around JNI-generator generics confusion. |
| 697 private class ResponseHeadersMap extends HashMap<String, List<String>> { | 706 private class ResponseHeadersMap extends HashMap<String, List<String>> { |
| 698 } | 707 } |
| 699 } | 708 } |
| OLD | NEW |