| 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.content.Context; | 7 import android.content.Context; |
| 8 import android.text.TextUtils; | 8 import android.text.TextUtils; |
| 9 | 9 |
| 10 import org.apache.http.HttpStatus; | 10 import org.apache.http.HttpStatus; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 } | 165 } |
| 166 | 166 |
| 167 @Override | 167 @Override |
| 168 public void setUploadChannel(String contentType, | 168 public void setUploadChannel(String contentType, |
| 169 ReadableByteChannel channel, long contentLength) { | 169 ReadableByteChannel channel, long contentLength) { |
| 170 validateNotStarted(); | 170 validateNotStarted(); |
| 171 if (contentLength > Integer.MAX_VALUE) { | 171 if (contentLength > Integer.MAX_VALUE) { |
| 172 throw new IllegalArgumentException( | 172 throw new IllegalArgumentException( |
| 173 "Upload contentLength is too big."); | 173 "Upload contentLength is too big."); |
| 174 } | 174 } |
| 175 mUploadContentLength = (int)contentLength; | 175 mUploadContentLength = (int) contentLength; |
| 176 mPostContentType = contentType; | 176 mPostContentType = contentType; |
| 177 mPostDataChannel = channel; | 177 mPostDataChannel = channel; |
| 178 mPostData = null; | 178 mPostData = null; |
| 179 } | 179 } |
| 180 | 180 |
| 181 | 181 |
| 182 @Override | 182 @Override |
| 183 public void setHttpMethod(String method) { | 183 public void setHttpMethod(String method) { |
| 184 validateNotStarted(); | 184 validateNotStarted(); |
| 185 mMethod = method; | 185 mMethod = method; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 198 private void startOnExecutorThread() { | 198 private void startOnExecutorThread() { |
| 199 boolean readingResponse = false; | 199 boolean readingResponse = false; |
| 200 try { | 200 try { |
| 201 synchronized (mLock) { | 201 synchronized (mLock) { |
| 202 if (mCanceled) { | 202 if (mCanceled) { |
| 203 return; | 203 return; |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 | 206 |
| 207 URL url = new URL(mUrl); | 207 URL url = new URL(mUrl); |
| 208 mConnection = (HttpURLConnection)url.openConnection(); | 208 mConnection = (HttpURLConnection) url.openConnection(); |
| 209 // If configured, use the provided http verb. | 209 // If configured, use the provided http verb. |
| 210 if (mMethod != null) { | 210 if (mMethod != null) { |
| 211 try { | 211 try { |
| 212 mConnection.setRequestMethod(mMethod); | 212 mConnection.setRequestMethod(mMethod); |
| 213 } catch (ProtocolException e) { | 213 } catch (ProtocolException e) { |
| 214 // Since request hasn't started earlier, it | 214 // Since request hasn't started earlier, it |
| 215 // must be an illegal HTTP verb. | 215 // must be an illegal HTTP verb. |
| 216 throw new IllegalArgumentException(e); | 216 throw new IllegalArgumentException(e); |
| 217 } | 217 } |
| 218 } | 218 } |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 int size; | 374 int size; |
| 375 while (!isCanceled() && (size = mResponseStream.read(buffer)) != -1) { | 375 while (!isCanceled() && (size = mResponseStream.read(buffer)) != -1) { |
| 376 int start = 0; | 376 int start = 0; |
| 377 int count = size; | 377 int count = size; |
| 378 mSize += size; | 378 mSize += size; |
| 379 if (mSkippingToOffset) { | 379 if (mSkippingToOffset) { |
| 380 if (mSize <= mOffset) { | 380 if (mSize <= mOffset) { |
| 381 continue; | 381 continue; |
| 382 } else { | 382 } else { |
| 383 mSkippingToOffset = false; | 383 mSkippingToOffset = false; |
| 384 start = (int)(mOffset - (mSize - size)); | 384 start = (int) (mOffset - (mSize - size)); |
| 385 count -= start; | 385 count -= start; |
| 386 } | 386 } |
| 387 } | 387 } |
| 388 | 388 |
| 389 if (mContentLengthLimit != 0 && mSize > mContentLengthLimit) { | 389 if (mContentLengthLimit != 0 && mSize > mContentLengthLimit) { |
| 390 count -= (int)(mSize - mContentLengthLimit); | 390 count -= (int) (mSize - mContentLengthLimit); |
| 391 if (count > 0) { | 391 if (count > 0) { |
| 392 mSink.write(ByteBuffer.wrap(buffer, start, count)); | 392 mSink.write(ByteBuffer.wrap(buffer, start, count)); |
| 393 } | 393 } |
| 394 onContentLengthOverLimit(); | 394 onContentLengthOverLimit(); |
| 395 return; | 395 return; |
| 396 } | 396 } |
| 397 | 397 |
| 398 mSink.write(ByteBuffer.wrap(buffer, start, count)); | 398 mSink.write(ByteBuffer.wrap(buffer, start, count)); |
| 399 } | 399 } |
| 400 } | 400 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 | 452 |
| 453 private static boolean isError(int statusCode) { | 453 private static boolean isError(int statusCode) { |
| 454 return (statusCode / 100) != 2; | 454 return (statusCode / 100) != 2; |
| 455 } | 455 } |
| 456 | 456 |
| 457 /** | 457 /** |
| 458 * Returns the response as a ByteBuffer. | 458 * Returns the response as a ByteBuffer. |
| 459 */ | 459 */ |
| 460 @Override | 460 @Override |
| 461 public ByteBuffer getByteBuffer() { | 461 public ByteBuffer getByteBuffer() { |
| 462 return ((ChunkedWritableByteChannel)mSink).getByteBuffer(); | 462 return ((ChunkedWritableByteChannel) mSink).getByteBuffer(); |
| 463 } | 463 } |
| 464 | 464 |
| 465 @Override | 465 @Override |
| 466 public byte[] getResponseAsBytes() { | 466 public byte[] getResponseAsBytes() { |
| 467 return ((ChunkedWritableByteChannel)mSink).getBytes(); | 467 return ((ChunkedWritableByteChannel) mSink).getBytes(); |
| 468 } | 468 } |
| 469 | 469 |
| 470 @Override | 470 @Override |
| 471 public long getContentLength() { | 471 public long getContentLength() { |
| 472 return mContentLength; | 472 return mContentLength; |
| 473 } | 473 } |
| 474 | 474 |
| 475 @Override | 475 @Override |
| 476 public String getContentType() { | 476 public String getContentType() { |
| 477 return mContentType; | 477 return mContentType; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 499 } | 499 } |
| 500 return mConnection.getHeaderFields(); | 500 return mConnection.getHeaderFields(); |
| 501 } | 501 } |
| 502 | 502 |
| 503 private void validateNotStarted() { | 503 private void validateNotStarted() { |
| 504 if (mStarted) { | 504 if (mStarted) { |
| 505 throw new IllegalStateException("Request already started"); | 505 throw new IllegalStateException("Request already started"); |
| 506 } | 506 } |
| 507 } | 507 } |
| 508 } | 508 } |
| OLD | NEW |