Index: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
index 585b6e6af7c75855eac1b29dae12a5e015f4fae0..ce96d2946bf358302de1f5a280fd4d6a14274fe8 100644 |
--- a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
+++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
@@ -4,10 +4,12 @@ |
package org.chromium.net.urlconnection; |
+import android.util.Log; |
import android.util.Pair; |
import org.chromium.net.ExtendedResponseInfo; |
import org.chromium.net.ResponseInfo; |
+import org.chromium.net.UploadDataProvider; |
import org.chromium.net.UrlRequest; |
import org.chromium.net.UrlRequestContext; |
import org.chromium.net.UrlRequestException; |
@@ -16,8 +18,10 @@ import org.chromium.net.UrlRequestListener; |
import java.io.FileNotFoundException; |
import java.io.IOException; |
import java.io.InputStream; |
+import java.io.OutputStream; |
import java.net.HttpURLConnection; |
import java.net.MalformedURLException; |
+import java.net.ProtocolException; |
import java.net.URL; |
import java.nio.ByteBuffer; |
import java.util.ArrayList; |
@@ -33,18 +37,22 @@ import java.util.TreeMap; |
* attempted. |
*/ |
public class CronetHttpURLConnection extends HttpURLConnection { |
+ private static final String TAG = "CronetHttpURLConnection"; |
private final UrlRequestContext mUrlRequestContext; |
private final MessageLoop mMessageLoop; |
private final UrlRequest mRequest; |
private final List<Pair<String, String>> mRequestHeaders; |
private CronetInputStream mInputStream; |
+ private OutputStream mOutputStream; |
private ResponseInfo mResponseInfo; |
private UrlRequestException mException; |
private ByteBuffer mResponseByteBuffer; |
private boolean mOnRedirectCalled = false; |
+ // A long version of the superclass field of the same name. |
+ private long mFixedContentLength = -1; |
pauljensen
2015/03/04 16:44:11
Can this just be replaced with super.fixedContentL
xunjieli
2015/03/05 17:43:11
Done. Didn't realize this field exists in 1.7. Was
xunjieli
2015/03/05 18:28:22
Looks like this long version is in API level 19 +.
pauljensen
2015/03/06 13:07:43
:( ya...
|
- protected CronetHttpURLConnection(URL url, |
+ public CronetHttpURLConnection(URL url, |
UrlRequestContext urlRequestContext) { |
super(url); |
mUrlRequestContext = urlRequestContext; |
@@ -63,21 +71,7 @@ public class CronetHttpURLConnection extends HttpURLConnection { |
*/ |
@Override |
public void connect() throws IOException { |
- if (connected) { |
- checkHasResponse(); |
- return; |
- } |
- connected = true; |
- for (Pair<String, String> requestHeader : mRequestHeaders) { |
- mRequest.addHeader(requestHeader.first, requestHeader.second); |
- } |
- if (!getUseCaches()) { |
- mRequest.disableCache(); |
- } |
- mRequest.start(); |
- // Blocks until onResponseStarted or onFailed is called. |
- mMessageLoop.loop(); |
- checkHasResponse(); |
+ startRequest(true); |
} |
/** |
@@ -198,6 +192,72 @@ public class CronetHttpURLConnection extends HttpURLConnection { |
return mInputStream; |
} |
+ @Override |
+ public OutputStream getOutputStream() throws IOException { |
+ if (mOutputStream == null) { |
+ if (connected) { |
+ throw new ProtocolException( |
+ "Cannot write to OutputStream after receiving response."); |
+ } |
+ boolean bufferRequestBody = false; |
pauljensen
2015/03/04 16:44:10
dead?
xunjieli
2015/03/05 17:43:10
Done.
|
+ if (mFixedContentLength != -1) { |
+ addRequestProperty("Content-Length", |
+ Long.toString(mFixedContentLength)); |
+ mOutputStream = new CronetOutputStream(this, mFixedContentLength); |
+ } else { |
+ bufferRequestBody = true; |
pauljensen
2015/03/04 16:44:10
dead?
xunjieli
2015/03/05 17:43:11
Done.
|
+ Log.d(TAG, "Outputstream is being buffered in memory."); |
+ String length = getRequestProperty("Content-Length"); |
+ if (length == null) { |
+ mOutputStream = new CronetBufferedOutputStream(this, -1); |
+ } else { |
+ long lengthParsed = Long.parseLong(length); |
+ mOutputStream = new CronetBufferedOutputStream(this, lengthParsed); |
+ } |
+ return mOutputStream; |
+ } |
+ } |
+ startRequest(false); |
+ return mOutputStream; |
+ } |
+ |
+ /** |
+ * Starts the request if {@code connected} is false. If {@code readResponse} |
+ * is true, block until the response is received. |
+ */ |
+ private void startRequest(boolean readResponse) throws IOException { |
+ if (connected) { |
+ if (readResponse) { |
+ checkHasResponse(); |
+ } |
+ return; |
+ } |
+ if (mOutputStream != null) { |
+ // Default Content-Type to application/x-www-form-urlencoded |
+ if (getRequestProperty("Content-Type") == null) { |
+ addRequestProperty("Content-Type", |
+ "application/x-www-form-urlencoded"); |
+ } |
+ mRequest.setUploadDataProvider((UploadDataProvider) mOutputStream, |
+ mMessageLoop); |
+ } |
+ connected = true; |
+ // Start the request. Note that connect() is not used since |
+ // connect() blocks until headers are received. |
+ for (Pair<String, String> requestHeader : mRequestHeaders) { |
+ mRequest.addHeader(requestHeader.first, requestHeader.second); |
+ } |
+ if (!getUseCaches()) { |
+ mRequest.disableCache(); |
+ } |
+ mRequest.start(); |
+ if (readResponse) { |
+ // Blocks until onResponseStarted or onFailed is called. |
+ mMessageLoop.loop(); |
+ checkHasResponse(); |
+ } |
+ } |
+ |
/** |
* Returns an input stream from the server in the case of an error such as |
* the requested file has not been found on the remote server. |
@@ -301,6 +361,35 @@ public class CronetHttpURLConnection extends HttpURLConnection { |
} |
/** |
+ * Sets chunked streaming mode. |
+ */ |
+ @Override |
+ public void setChunkedStreamingMode(int chunklen) { |
+ // TODO(xunjieli): implement this. |
+ throw new UnsupportedOperationException("Chunked mode not supported yet"); |
+ } |
+ |
+ @Override |
+ public void setFixedLengthStreamingMode(int contentLength) { |
+ setFixedLengthStreamingMode((long) contentLength); |
+ } |
+ |
+ @Override |
+ public void setFixedLengthStreamingMode(long contentLength) { |
+ if (connected) { |
+ throw new IllegalStateException("Already connected"); |
+ } |
+ if (chunkLength > 0) { |
+ throw new IllegalStateException("Already in chunked mode"); |
+ } |
+ if (contentLength < 0) { |
+ throw new IllegalArgumentException("contentLength < 0"); |
+ } |
+ mFixedContentLength = contentLength; |
+ super.fixedContentLength = (int) Math.min(contentLength, Integer.MAX_VALUE); |
pauljensen
2015/03/04 16:44:11
Should this set super.fixedContentLengthLong? or m
xunjieli
2015/03/05 17:43:10
Done. this long version exists only since Jdk 1.7.
|
+ } |
+ |
+ /** |
* Used by {@link CronetInputStream} to get more data from the network |
* stack. This should only be called after the request has started. Note |
* that this call might block if there isn't any more data to be read. |
@@ -312,6 +401,22 @@ public class CronetHttpURLConnection extends HttpURLConnection { |
} |
/** |
+ * Used by {@link CronetOutputStream} to wait for data consumed by the |
+ * network stack before writing more data. |
+ */ |
+ void waitForPostDataConsumed() throws IOException { |
+ mMessageLoop.loop(); |
+ } |
+ |
+ /** |
+ * Used by {@link CronetOutputStream.CronetUploadDataProvider} to wait for |
pauljensen
2015/03/04 16:44:11
What is CronetOutputStream.CronetUploadDataProvide
xunjieli
2015/03/05 17:43:10
Done. Sorry, forgot to update the comment.
|
+ * embedder to provide more data. |
+ */ |
+ void waitForPostData() { |
+ mMessageLoop.postQuitTask(); |
+ } |
+ |
+ /** |
* Returns the index of request header in {@link #mRequestHeaders} or |
* -1 if not found. |
*/ |