Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| index 835bb28681591c837db0690fc39475741a1d708a..efb5120abdd15df0a0874dcf75e9de6c059b9d4d 100644 |
| --- a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| +++ b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| @@ -56,6 +56,8 @@ final class CronetUrlRequest implements UrlRequest { |
| private String mInitialMethod; |
| private final HeadersList mRequestHeaders = new HeadersList(); |
| + private CronetUploadDataStream mUploadDataStream; |
| + |
| private NativeResponseInfo mResponseInfo; |
| /* |
| @@ -257,29 +259,56 @@ final class CronetUrlRequest implements UrlRequest { |
| } |
| @Override |
| + public void setUploadDataProvider(UploadDataProvider uploadDataProvider, Executor executor) { |
| + if (uploadDataProvider == null) { |
| + throw new NullPointerException("Invalid UploadDataProvider."); |
| + } |
| + if (mInitialMethod == null) { |
| + mInitialMethod = "POST"; |
| + } |
| + mUploadDataStream = new CronetUploadDataStream(uploadDataProvider, executor); |
| + } |
| + |
| + @Override |
| public void start() { |
| synchronized (mUrlRequestAdapterLock) { |
| checkNotStarted(); |
| - mUrlRequestAdapter = nativeCreateRequestAdapter( |
| - mRequestContext.getUrlRequestContextAdapter(), |
| - mInitialUrl, |
| - mPriority); |
| - mRequestContext.onRequestStarted(this); |
| - if (mInitialMethod != null) { |
| - if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod)) { |
| - destroyRequestAdapter(); |
| - throw new IllegalArgumentException("Invalid http method " |
| - + mInitialMethod); |
| + |
| + try { |
| + mUrlRequestAdapter = nativeCreateRequestAdapter( |
| + mRequestContext.getUrlRequestContextAdapter(), mInitialUrl, mPriority); |
| + mRequestContext.onRequestStarted(this); |
| + if (mInitialMethod != null) { |
| + if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod)) { |
| + throw new IllegalArgumentException("Invalid http method " + mInitialMethod); |
| + } |
| } |
| - } |
| - for (Pair<String, String> header : mRequestHeaders) { |
| - if (!nativeAddHeader(mUrlRequestAdapter, header.first, |
| - header.second)) { |
| - destroyRequestAdapter(); |
| - throw new IllegalArgumentException("Invalid header " |
| - + header.first + "=" + header.second); |
| + |
| + boolean hasContentType = false; |
| + for (Pair<String, String> header : mRequestHeaders) { |
| + if (header.first == "Content-Type" && !header.second.isEmpty()) { |
|
pauljensen
2015/02/02 17:40:29
Should use equalsIgnoreCase() to compare header na
xunjieli
2015/02/02 18:25:41
Done.
|
| + hasContentType = true; |
| + } |
| + if (!nativeAddHeader(mUrlRequestAdapter, header.first, header.second)) { |
| + destroyRequestAdapter(); |
| + throw new IllegalArgumentException( |
| + "Invalid header " + header.first + "=" + header.second); |
| + } |
| + } |
| + if (mUploadDataStream != null) { |
|
mef
2015/02/02 17:45:11
Would it make sense to make Content-Type an argume
mmenke
2015/02/02 18:26:12
The problem with this is if the header is also set
|
| + if (!hasContentType) { |
| + throw new IllegalArgumentException( |
| + "Requests with body must have a Content-Type."); |
|
pauljensen
2015/02/02 17:40:29
Certain HTTP stacks (e.g. OkHttp) default to appli
xunjieli
2015/02/02 18:25:41
Thanks for the heads up! Will keep this in mind. A
|
| + } |
| + mUploadDataStream.attachToRequest(this, mUrlRequestAdapter); |
| } |
| + } catch (RuntimeException e) { |
|
mef
2015/02/02 17:45:11
should this be just Exception?
mmenke
2015/02/02 18:26:12
All other exceptions have to be declared, no? Sin
|
| + // If there's an exception, cleanup and then throw the |
| + // exception to the caller. |
| + destroyRequestAdapter(); |
| + throw e; |
| } |
| + |
| mStarted = true; |
| nativeStart(mUrlRequestAdapter); |
| } |
| @@ -415,6 +444,27 @@ final class CronetUrlRequest implements UrlRequest { |
| } |
| } |
| + /** |
|
mef
2015/02/02 17:45:11
empty comment?
xunjieli
2015/02/02 18:25:41
Done.
|
| + */ |
| + void onUploadException(Exception e) { |
| + UrlRequestException uploadError = |
| + new UrlRequestException("Exception received from UploadDataProvider", e); |
| + Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in upload method", e); |
| + // Do not call into listener if request is canceled. |
| + synchronized (mUrlRequestAdapterLock) { |
| + if (isCanceled()) { |
| + return; |
| + } |
| + cancel(); |
| + } |
| + try { |
| + mListener.onFailed(this, mResponseInfo, uploadError); |
| + } catch (Exception failException) { |
| + Log.e(CronetUrlRequestContext.LOG_TAG, "Exception notifying of failed upload", |
| + failException); |
| + } |
| + } |
| + |
| //////////////////////////////////////////////// |
| // Private methods called by the native code. |
| // Always called on network thread. |