Commit e8ca4159 authored by mef@chromium.org's avatar mef@chromium.org

Upstream missing changes from HttpUrlRequest interface and...

Upstream missing changes from HttpUrlRequest interface and HttpUrlConnectionUrlRequest implementation.

BUG=390267

Review URL: https://codereview.chromium.org/382193002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282973 0039d316-1c4b-4281-b951-d872f2087c98
parent a3e6a38c
...@@ -14,10 +14,12 @@ import java.io.IOException; ...@@ -14,10 +14,12 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel; import java.nio.channels.WritableByteChannel;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
...@@ -55,6 +57,8 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest { ...@@ -55,6 +57,8 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest {
private int mContentLength; private int mContentLength;
private int mUploadContentLength;
private long mContentLengthLimit; private long mContentLengthLimit;
private boolean mCancelIfContentLengthOverLimit; private boolean mCancelIfContentLengthOverLimit;
...@@ -79,6 +83,8 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest { ...@@ -79,6 +83,8 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest {
private boolean mCanceled; private boolean mCanceled;
private String mMethod;
private InputStream mResponseStream; private InputStream mResponseStream;
private final Object mLock; private final Object mLock;
...@@ -162,11 +168,27 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest { ...@@ -162,11 +168,27 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest {
public void setUploadChannel(String contentType, public void setUploadChannel(String contentType,
ReadableByteChannel channel, long contentLength) { ReadableByteChannel channel, long contentLength) {
validateNotStarted(); validateNotStarted();
if (contentLength > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"Upload contentLength is too big.");
}
mUploadContentLength = (int)contentLength;
mPostContentType = contentType; mPostContentType = contentType;
mPostDataChannel = channel; mPostDataChannel = channel;
mPostData = null; mPostData = null;
} }
@Override
public void setHttpMethod(String method) {
validateNotStarted();
if (!("PUT".equals(method) || "POST".equals(method))) {
throw new IllegalArgumentException(
"Only PUT and POST are allowed.");
}
mMethod = method;
}
@Override @Override
public void start() { public void start() {
getExecutor().execute(new Runnable() { getExecutor().execute(new Runnable() {
...@@ -188,6 +210,16 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest { ...@@ -188,6 +210,16 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest {
URL url = new URL(mUrl); URL url = new URL(mUrl);
mConnection = (HttpURLConnection)url.openConnection(); mConnection = (HttpURLConnection)url.openConnection();
// If configured, use the provided http verb.
if (mMethod != null) {
try {
mConnection.setRequestMethod(mMethod);
} catch (ProtocolException e) {
// Since request hasn't started earlier, it
// must be an illegal HTTP verb.
throw new IllegalArgumentException(e);
}
}
mConnection.setConnectTimeout(CONNECT_TIMEOUT); mConnection.setConnectTimeout(CONNECT_TIMEOUT);
mConnection.setReadTimeout(READ_TIMEOUT); mConnection.setReadTimeout(READ_TIMEOUT);
mConnection.setInstanceFollowRedirects(true); mConnection.setInstanceFollowRedirects(true);
...@@ -290,7 +322,7 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest { ...@@ -290,7 +322,7 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest {
uploadStream = mConnection.getOutputStream(); uploadStream = mConnection.getOutputStream();
uploadStream.write(mPostData); uploadStream.write(mPostData);
} else { } else {
mConnection.setChunkedStreamingMode(MAX_CHUNK_SIZE); mConnection.setFixedLengthStreamingMode(mUploadContentLength);
uploadStream = mConnection.getOutputStream(); uploadStream = mConnection.getOutputStream();
byte[] bytes = new byte[MAX_CHUNK_SIZE]; byte[] bytes = new byte[MAX_CHUNK_SIZE];
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
...@@ -450,7 +482,14 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest { ...@@ -450,7 +482,14 @@ class HttpUrlConnectionUrlRequest implements HttpUrlRequest {
if (mConnection == null) { if (mConnection == null) {
throw new IllegalStateException("Response headers not available"); throw new IllegalStateException("Response headers not available");
} }
return mConnection.getHeaderField(name); Map<String, List<String>> headerFields = mConnection.getHeaderFields();
if (headerFields != null) {
List<String> headerValues = headerFields.get(name);
if (headerValues != null) {
return TextUtils.join(", ", headerValues);
}
}
return null;
} }
private void validateNotStarted() { private void validateNotStarted() {
......
...@@ -70,6 +70,17 @@ public interface HttpUrlRequest { ...@@ -70,6 +70,17 @@ public interface HttpUrlRequest {
void setUploadChannel(String contentType, ReadableByteChannel channel, void setUploadChannel(String contentType, ReadableByteChannel channel,
long contentLength); long contentLength);
/**
* Sets the HTTP method verb to use for this request. Currently can only be
* "POST" or "PUT".
*
* <p>The default when this method is not called is "GET" if the request has
* no body or "POST" if it does.
*
* @param method Either "POST" or "PUT".
*/
void setHttpMethod(String method);
/** /**
* Start executing the request. * Start executing the request.
* <p> * <p>
......
...@@ -206,7 +206,7 @@ public class CronetSampleActivity extends Activity { ...@@ -206,7 +206,7 @@ public class CronetSampleActivity extends Activity {
return null; return null;
} }
private void applyCommandLineToRequest(UrlRequest request) { private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) {
String postData = getCommandLineArg(POST_DATA_KEY); String postData = getCommandLineArg(POST_DATA_KEY);
if (postData != null) { if (postData != null) {
InputStream dataStream = new ByteArrayInputStream( InputStream dataStream = new ByteArrayInputStream(
...@@ -214,6 +214,7 @@ public class CronetSampleActivity extends Activity { ...@@ -214,6 +214,7 @@ public class CronetSampleActivity extends Activity {
ReadableByteChannel dataChannel = Channels.newChannel(dataStream); ReadableByteChannel dataChannel = Channels.newChannel(dataStream);
request.setUploadChannel("text/plain", dataChannel, request.setUploadChannel("text/plain", dataChannel,
postData.length()); postData.length());
request.setHttpMethod("POST");
} }
} }
...@@ -224,12 +225,24 @@ public class CronetSampleActivity extends Activity { ...@@ -224,12 +225,24 @@ public class CronetSampleActivity extends Activity {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
HttpUrlRequestListener listener = new SampleHttpUrlRequestListener(); HttpUrlRequestListener listener = new SampleHttpUrlRequestListener();
HttpUrlRequest request = mRequestFactory.createRequest( HttpUrlRequest request = mRequestFactory.createRequest(
url, UrlRequestPriority.MEDIUM, headers, listener); url, UrlRequestPriority.MEDIUM, headers, listener);
applyCommandLineToHttpUrlRequest(request);
request.start(); request.start();
} }
private void applyCommandLineToUrlRequest(UrlRequest request) {
String postData = getCommandLineArg(POST_DATA_KEY);
if (postData != null) {
InputStream dataStream = new ByteArrayInputStream(
postData.getBytes());
ReadableByteChannel dataChannel = Channels.newChannel(dataStream);
request.setUploadChannel("text/plain", dataChannel,
postData.length());
request.setHttpMethod("POST");
}
}
public void startWithURL_UrlRequest(String url) { public void startWithURL_UrlRequest(String url) {
Log.i(TAG, "Cronet started: " + url); Log.i(TAG, "Cronet started: " + url);
mUrl = url; mUrl = url;
...@@ -239,7 +252,7 @@ public class CronetSampleActivity extends Activity { ...@@ -239,7 +252,7 @@ public class CronetSampleActivity extends Activity {
WritableByteChannel sink = Channels.newChannel(System.out); WritableByteChannel sink = Channels.newChannel(System.out);
UrlRequest request = new SampleRequest(mRequestContext, url, UrlRequest request = new SampleRequest(mRequestContext, url,
UrlRequestPriority.MEDIUM, headers, sink); UrlRequestPriority.MEDIUM, headers, sink);
applyCommandLineToRequest(request); applyCommandLineToUrlRequest(request);
request.start(); request.start();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment