Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Unified Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java

Issue 966743003: [Cronet] Implement getOutputStream in CronetHttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chunked_support
Patch Set: Address Paul's and Misha's comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..2e15151f3ce28f43024bb4b10afde91549dd6f64 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,20 @@ 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;
- protected CronetHttpURLConnection(URL url,
+ public CronetHttpURLConnection(URL url,
UrlRequestContext urlRequestContext) {
super(url);
mUrlRequestContext = urlRequestContext;
@@ -63,20 +69,11 @@ 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();
+ if (!connected) {
+ startRequest();
+ // Blocks until onResponseStarted or onFailed is called.
+ mMessageLoop.loop();
}
- mRequest.start();
- // Blocks until onResponseStarted or onFailed is called.
- mMessageLoop.loop();
checkHasResponse();
}
@@ -198,6 +195,80 @@ 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.");
+ }
+ // Use reflection to see whether fixedContentLengthLong (only added
+ // in API 19) is inherited.
mmenke 2015/03/13 14:51:12 I don't suppose we can just make our own implement
xunjieli 2015/03/13 19:26:40 I adopted this approach earlier, but changed after
+ long superFixedContentLengthLong = -1;
+ try {
+ Class parent = this.getClass();
+ superFixedContentLengthLong =
+ (long) (parent.getField("fixedContentLengthLong").get(this));
mmenke 2015/03/13 14:51:11 Instead of the cast, there's a getLong method.
xunjieli 2015/03/13 19:26:40 Done.
+ } catch (Exception e) {
+ // Ignored.
+ }
+ if (fixedContentLength != -1) {
+ mOutputStream = new CronetOutputStream(this, fixedContentLength);
+ // Start the request now since all headers can be sent.
+ startRequest();
+ } else if (superFixedContentLengthLong != -1) {
+ mOutputStream = new CronetOutputStream(this, superFixedContentLengthLong);
+ // Start the request now since all headers can be sent.
+ startRequest();
mmenke 2015/03/13 14:51:11 This seems overly complicated. Why not: long con
xunjieli 2015/03/13 19:26:40 Done. Added check before overriding. Looking at th
+ } else {
+ // For the buffered case, start the request only when
+ // content-length bytes are received, or when an
+ // connect action is initiated by the consumer.
+ Log.d(TAG, "Outputstream is being buffered in memory.");
+ String length = getRequestProperty("Content-Length");
+ if (length == null) {
+ mOutputStream = new CronetBufferedOutputStream(this);
+ } else {
+ long lengthParsed = Long.parseLong(length);
+ mOutputStream = new CronetBufferedOutputStream(this, lengthParsed);
mmenke 2015/03/13 14:51:11 Should we do something if we can't parse the lengt
xunjieli 2015/03/13 19:26:40 If there's a parse error, an runtime exception (Nu
mmenke 2015/03/25 18:34:57 As long as things will fail, I'm fine with the cur
xunjieli 2015/03/27 17:46:44 Acknowledged.
+ }
+ }
+ }
+ return mOutputStream;
+ }
+
+ /**
+ * Starts the request if {@code connected} is false.
+ */
+ private void startRequest() throws IOException {
+ if (connected) {
+ 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");
+ }
+ if (getRequestProperty("Content-Length") == null) {
+ addRequestProperty("Content-Length",
+ Long.toString(((UploadDataProvider) mOutputStream).getLength()));
+ }
+ 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();
+ }
+
/**
* 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 +372,15 @@ 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");
+ }
+
+ /**
* 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 +392,23 @@ public class CronetHttpURLConnection extends HttpURLConnection {
}
/**
+ * Used by {@link CronetOutputStream} to wait for data to be read by the
+ * network stack before letting the consumer write more data.
+ */
+ void waitForRead() throws IOException {
+ mMessageLoop.loop();
+ }
+
+ /**
+ * Used by {@link CronetOutputStream} to execute read after consumer writes
+ * more data.
+ */
+ void postponeRead(Runnable readTask) {
+ mMessageLoop.postQuitTask();
+ mMessageLoop.execute(readTask);
+ }
+
+ /**
* Returns the index of request header in {@link #mRequestHeaders} or
* -1 if not found.
*/

Powered by Google App Engine
This is Rietveld 408576698