Chromium Code Reviews| 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.urlconnection; | 5 package org.chromium.net.urlconnection; |
| 6 | 6 |
| 7 import android.util.Log; | |
| 7 import android.util.Pair; | 8 import android.util.Pair; |
| 8 | 9 |
| 9 import org.chromium.net.ExtendedResponseInfo; | 10 import org.chromium.net.ExtendedResponseInfo; |
| 10 import org.chromium.net.ResponseInfo; | 11 import org.chromium.net.ResponseInfo; |
| 12 import org.chromium.net.UploadDataProvider; | |
| 11 import org.chromium.net.UrlRequest; | 13 import org.chromium.net.UrlRequest; |
| 12 import org.chromium.net.UrlRequestContext; | 14 import org.chromium.net.UrlRequestContext; |
| 13 import org.chromium.net.UrlRequestException; | 15 import org.chromium.net.UrlRequestException; |
| 14 import org.chromium.net.UrlRequestListener; | 16 import org.chromium.net.UrlRequestListener; |
| 15 | 17 |
| 16 import java.io.FileNotFoundException; | 18 import java.io.FileNotFoundException; |
| 17 import java.io.IOException; | 19 import java.io.IOException; |
| 18 import java.io.InputStream; | 20 import java.io.InputStream; |
| 21 import java.io.OutputStream; | |
| 19 import java.net.HttpURLConnection; | 22 import java.net.HttpURLConnection; |
| 20 import java.net.MalformedURLException; | 23 import java.net.MalformedURLException; |
| 24 import java.net.ProtocolException; | |
| 21 import java.net.URL; | 25 import java.net.URL; |
| 22 import java.nio.ByteBuffer; | 26 import java.nio.ByteBuffer; |
| 23 import java.util.ArrayList; | 27 import java.util.ArrayList; |
| 24 import java.util.Collections; | 28 import java.util.Collections; |
| 25 import java.util.List; | 29 import java.util.List; |
| 26 import java.util.Map; | 30 import java.util.Map; |
| 27 import java.util.TreeMap; | 31 import java.util.TreeMap; |
| 28 | 32 |
| 29 /** | 33 /** |
| 30 * An implementation of HttpURLConnection that uses Cronet to send requests and | 34 * An implementation of HttpURLConnection that uses Cronet to send requests and |
| 31 * receive response. This class inherits a {@code connected} field from the | 35 * receive response. This class inherits a {@code connected} field from the |
| 32 * superclass. That field indicates whether a connection has ever been | 36 * superclass. That field indicates whether a connection has ever been |
| 33 * attempted. | 37 * attempted. |
| 34 */ | 38 */ |
| 35 public class CronetHttpURLConnection extends HttpURLConnection { | 39 public class CronetHttpURLConnection extends HttpURLConnection { |
| 40 private static final String TAG = "CronetHttpURLConnection"; | |
| 36 private final UrlRequestContext mUrlRequestContext; | 41 private final UrlRequestContext mUrlRequestContext; |
| 37 private final MessageLoop mMessageLoop; | 42 private final MessageLoop mMessageLoop; |
| 38 private final UrlRequest mRequest; | 43 private final UrlRequest mRequest; |
| 39 private final List<Pair<String, String>> mRequestHeaders; | 44 private final List<Pair<String, String>> mRequestHeaders; |
| 40 | 45 |
| 41 private CronetInputStream mInputStream; | 46 private CronetInputStream mInputStream; |
| 47 private OutputStream mOutputStream; | |
| 42 private ResponseInfo mResponseInfo; | 48 private ResponseInfo mResponseInfo; |
| 43 private UrlRequestException mException; | 49 private UrlRequestException mException; |
| 44 private ByteBuffer mResponseByteBuffer; | 50 private ByteBuffer mResponseByteBuffer; |
| 45 private boolean mOnRedirectCalled = false; | 51 private boolean mOnRedirectCalled = false; |
| 46 | 52 |
| 47 protected CronetHttpURLConnection(URL url, | 53 public CronetHttpURLConnection(URL url, |
| 48 UrlRequestContext urlRequestContext) { | 54 UrlRequestContext urlRequestContext) { |
| 49 super(url); | 55 super(url); |
| 50 mUrlRequestContext = urlRequestContext; | 56 mUrlRequestContext = urlRequestContext; |
| 51 mMessageLoop = new MessageLoop(); | 57 mMessageLoop = new MessageLoop(); |
| 52 mRequest = mUrlRequestContext.createRequest(url.toString(), | 58 mRequest = mUrlRequestContext.createRequest(url.toString(), |
| 53 new CronetUrlRequestListener(), mMessageLoop); | 59 new CronetUrlRequestListener(), mMessageLoop); |
| 54 mInputStream = new CronetInputStream(this); | 60 mInputStream = new CronetInputStream(this); |
| 55 mRequestHeaders = new ArrayList<Pair<String, String>>(); | 61 mRequestHeaders = new ArrayList<Pair<String, String>>(); |
| 56 } | 62 } |
| 57 | 63 |
| 58 /** | 64 /** |
| 59 * Opens a connection to the resource. If the connect method is called when | 65 * Opens a connection to the resource. If the connect method is called when |
| 60 * the connection has already been opened (indicated by the connected field | 66 * the connection has already been opened (indicated by the connected field |
| 61 * having the value true), the call is ignored unless an exception is thrown | 67 * having the value true), the call is ignored unless an exception is thrown |
| 62 * previously, in which case, the exception will be rethrown. | 68 * previously, in which case, the exception will be rethrown. |
| 63 */ | 69 */ |
| 64 @Override | 70 @Override |
| 65 public void connect() throws IOException { | 71 public void connect() throws IOException { |
| 66 if (connected) { | 72 if (!connected) { |
| 67 checkHasResponse(); | 73 startRequest(); |
| 68 return; | 74 // Blocks until onResponseStarted or onFailed is called. |
| 75 mMessageLoop.loop(); | |
| 69 } | 76 } |
| 70 connected = true; | |
| 71 for (Pair<String, String> requestHeader : mRequestHeaders) { | |
| 72 mRequest.addHeader(requestHeader.first, requestHeader.second); | |
| 73 } | |
| 74 if (!getUseCaches()) { | |
| 75 mRequest.disableCache(); | |
| 76 } | |
| 77 mRequest.start(); | |
| 78 // Blocks until onResponseStarted or onFailed is called. | |
| 79 mMessageLoop.loop(); | |
| 80 checkHasResponse(); | 77 checkHasResponse(); |
|
mmenke
2015/03/25 18:34:57
I'm still hazy on this API. If you're uploading,
xunjieli
2015/03/27 17:46:45
Looking at Okhttp's implementation, connect() does
| |
| 81 } | 78 } |
| 82 | 79 |
| 83 /** | 80 /** |
| 84 * Releases this connection so that its resources may be either reused or | 81 * Releases this connection so that its resources may be either reused or |
| 85 * closed. | 82 * closed. |
| 86 */ | 83 */ |
| 87 @Override | 84 @Override |
| 88 public void disconnect() { | 85 public void disconnect() { |
| 89 // Disconnect before connection is made should have no effect. | 86 // Disconnect before connection is made should have no effect. |
| 90 if (connected) { | 87 if (connected) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 throw new IOException("Cannot read response body of a redirect."); | 188 throw new IOException("Cannot read response body of a redirect."); |
| 192 } | 189 } |
| 193 // Emulate default implementation's behavior to throw | 190 // Emulate default implementation's behavior to throw |
| 194 // FileNotFoundException when we get a 400 and above. | 191 // FileNotFoundException when we get a 400 and above. |
| 195 if (mResponseInfo.getHttpStatusCode() >= HTTP_BAD_REQUEST) { | 192 if (mResponseInfo.getHttpStatusCode() >= HTTP_BAD_REQUEST) { |
| 196 throw new FileNotFoundException(url.toString()); | 193 throw new FileNotFoundException(url.toString()); |
| 197 } | 194 } |
| 198 return mInputStream; | 195 return mInputStream; |
| 199 } | 196 } |
| 200 | 197 |
| 198 @Override | |
| 199 public OutputStream getOutputStream() throws IOException { | |
| 200 if (mOutputStream == null) { | |
| 201 if (connected) { | |
| 202 throw new ProtocolException( | |
| 203 "Cannot write to OutputStream after receiving response." ); | |
| 204 } | |
| 205 long fixedStreamingModeContentLength = getStreamingModeContentLength (); | |
| 206 if (fixedStreamingModeContentLength != -1) { | |
| 207 mOutputStream = new CronetOutputStream(this, | |
| 208 fixedStreamingModeContentLength); | |
| 209 // Start the request now since all headers can be sent. | |
| 210 startRequest(); | |
| 211 } else { | |
| 212 // For the buffered case, start the request only when | |
| 213 // content-length bytes are received, or when an | |
|
mmenke
2015/03/25 18:34:57
nit: an -> a
xunjieli
2015/03/27 17:46:45
Done.
| |
| 214 // connect action is initiated by the consumer. | |
| 215 Log.d(TAG, "Outputstream is being buffered in memory."); | |
| 216 String length = getRequestProperty("Content-Length"); | |
| 217 if (length == null) { | |
| 218 mOutputStream = new CronetBufferedOutputStream(this); | |
| 219 } else { | |
| 220 long lengthParsed = Long.parseLong(length); | |
| 221 mOutputStream = new CronetBufferedOutputStream(this, lengthP arsed); | |
| 222 } | |
| 223 } | |
| 224 } | |
| 225 return mOutputStream; | |
| 226 } | |
| 227 | |
| 228 /** | |
| 229 * Helper method to get content length passed in by | |
| 230 * {@link #setFixedLengthStreamingMode} | |
| 231 */ | |
| 232 private long getStreamingModeContentLength() { | |
| 233 long contentLength = fixedContentLength; | |
| 234 // Use reflection to see whether fixedContentLengthLong (only added | |
| 235 // in API 19) is inherited. | |
| 236 try { | |
| 237 Class<?> parent = this.getClass(); | |
| 238 long superFixedContentLengthLong = | |
| 239 parent.getField("fixedContentLengthLong").getLong(this); | |
| 240 if (superFixedContentLengthLong != -1) { | |
| 241 contentLength = superFixedContentLengthLong; | |
| 242 } | |
| 243 } catch (Exception e) { | |
| 244 // Ignored. | |
| 245 } | |
| 246 return contentLength; | |
| 247 } | |
| 248 | |
| 249 /** | |
| 250 * Starts the request if {@code connected} is false. | |
| 251 */ | |
| 252 private void startRequest() throws IOException { | |
| 253 if (connected) { | |
| 254 return; | |
| 255 } | |
| 256 if (doOutput) { | |
| 257 long fixedStreamingModeContentLength = getStreamingModeContentLength (); | |
| 258 if (fixedStreamingModeContentLength != -1) { | |
| 259 // Do not use UploadDataProvider.getLength() because if | |
| 260 // mOutputStream is null, the header won't be set. | |
| 261 addRequestProperty("Content-Length", | |
| 262 Long.toString(fixedStreamingModeContentLength)); | |
| 263 } | |
| 264 if (mOutputStream != null) { | |
| 265 mRequest.setUploadDataProvider( | |
| 266 (UploadDataProvider) mOutputStream, mMessageLoop); | |
| 267 } | |
| 268 // Default Content-Type to application/x-www-form-urlencoded | |
| 269 if (getRequestProperty("Content-Type") == null) { | |
| 270 addRequestProperty("Content-Type", | |
| 271 "application/x-www-form-urlencoded"); | |
| 272 } | |
| 273 } | |
| 274 connected = true; | |
| 275 // Start the request. Note that connect() is not used since | |
| 276 // connect() blocks until headers are received. | |
|
mmenke
2015/03/25 18:34:57
This comment about connect is a little weird, sinc
xunjieli
2015/03/27 17:46:45
Done. Ah.. sorry should have removed it.
| |
| 277 for (Pair<String, String> requestHeader : mRequestHeaders) { | |
| 278 mRequest.addHeader(requestHeader.first, requestHeader.second); | |
| 279 } | |
| 280 if (!getUseCaches()) { | |
| 281 mRequest.disableCache(); | |
| 282 } | |
| 283 mRequest.start(); | |
| 284 } | |
| 285 | |
| 201 /** | 286 /** |
| 202 * Returns an input stream from the server in the case of an error such as | 287 * Returns an input stream from the server in the case of an error such as |
| 203 * the requested file has not been found on the remote server. | 288 * the requested file has not been found on the remote server. |
| 204 */ | 289 */ |
| 205 @Override | 290 @Override |
| 206 public InputStream getErrorStream() { | 291 public InputStream getErrorStream() { |
| 207 try { | 292 try { |
| 208 connect(); | 293 connect(); |
| 209 } catch (IOException e) { | 294 } catch (IOException e) { |
| 210 return null; | 295 return null; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 /** | 379 /** |
| 295 * Returns whether this connection uses a proxy server. | 380 * Returns whether this connection uses a proxy server. |
| 296 */ | 381 */ |
| 297 @Override | 382 @Override |
| 298 public boolean usingProxy() { | 383 public boolean usingProxy() { |
| 299 // TODO(xunjieli): implement this. | 384 // TODO(xunjieli): implement this. |
| 300 return false; | 385 return false; |
| 301 } | 386 } |
| 302 | 387 |
| 303 /** | 388 /** |
| 389 * Sets chunked streaming mode. | |
| 390 */ | |
| 391 @Override | |
| 392 public void setChunkedStreamingMode(int chunklen) { | |
| 393 // TODO(xunjieli): implement this. | |
| 394 throw new UnsupportedOperationException("Chunked mode not supported yet" ); | |
| 395 } | |
| 396 | |
| 397 /** | |
| 304 * Used by {@link CronetInputStream} to get more data from the network | 398 * Used by {@link CronetInputStream} to get more data from the network |
| 305 * stack. This should only be called after the request has started. Note | 399 * stack. This should only be called after the request has started. Note |
| 306 * that this call might block if there isn't any more data to be read. | 400 * that this call might block if there isn't any more data to be read. |
| 307 */ | 401 */ |
| 308 ByteBuffer getMoreData() throws IOException { | 402 ByteBuffer getMoreData() throws IOException { |
| 309 mResponseByteBuffer = null; | 403 mResponseByteBuffer = null; |
| 310 mMessageLoop.loop(); | 404 mMessageLoop.loop(); |
| 311 return mResponseByteBuffer; | 405 return mResponseByteBuffer; |
| 312 } | 406 } |
| 313 | 407 |
| 314 /** | 408 /** |
| 409 * Used by {@link CronetOutputStream} to wait for data to be read by the | |
| 410 * network stack before letting the consumer write more data. | |
| 411 */ | |
| 412 void waitForRead() throws IOException { | |
| 413 mMessageLoop.loop(); | |
| 414 } | |
| 415 | |
| 416 /** | |
| 417 * Used by {@link CronetOutputStream} to execute read after consumer writes | |
| 418 * more data. | |
| 419 */ | |
| 420 void postponeRead(Runnable readTask) { | |
| 421 mMessageLoop.postQuitTask(); | |
| 422 mMessageLoop.execute(readTask); | |
| 423 } | |
| 424 | |
| 425 /** | |
| 315 * Returns the index of request header in {@link #mRequestHeaders} or | 426 * Returns the index of request header in {@link #mRequestHeaders} or |
| 316 * -1 if not found. | 427 * -1 if not found. |
| 317 */ | 428 */ |
| 318 private int findRequestProperty(String key) { | 429 private int findRequestProperty(String key) { |
| 319 for (int i = 0; i < mRequestHeaders.size(); i++) { | 430 for (int i = 0; i < mRequestHeaders.size(); i++) { |
| 320 Pair<String, String> entry = mRequestHeaders.get(i); | 431 Pair<String, String> entry = mRequestHeaders.get(i); |
| 321 if (entry.first.equalsIgnoreCase(key)) { | 432 if (entry.first.equalsIgnoreCase(key)) { |
| 322 return i; | 433 return i; |
| 323 } | 434 } |
| 324 } | 435 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 return null; | 528 return null; |
| 418 } | 529 } |
| 419 List<Pair<String, String>> headers = | 530 List<Pair<String, String>> headers = |
| 420 mResponseInfo.getAllHeadersAsList(); | 531 mResponseInfo.getAllHeadersAsList(); |
| 421 if (pos >= headers.size()) { | 532 if (pos >= headers.size()) { |
| 422 return null; | 533 return null; |
| 423 } | 534 } |
| 424 return headers.get(pos); | 535 return headers.get(pos); |
| 425 } | 536 } |
| 426 } | 537 } |
| OLD | NEW |