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(); |
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. |
(...skipping 101 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 // Use reflection to see whether fixedContentLengthLong (only added | |
206 // 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
| |
207 long superFixedContentLengthLong = -1; | |
208 try { | |
209 Class parent = this.getClass(); | |
210 superFixedContentLengthLong = | |
211 (long) (parent.getField("fixedContentLengthLong").get(th is)); | |
mmenke
2015/03/13 14:51:11
Instead of the cast, there's a getLong method.
xunjieli
2015/03/13 19:26:40
Done.
| |
212 } catch (Exception e) { | |
213 // Ignored. | |
214 } | |
215 if (fixedContentLength != -1) { | |
216 mOutputStream = new CronetOutputStream(this, fixedContentLength) ; | |
217 // Start the request now since all headers can be sent. | |
218 startRequest(); | |
219 } else if (superFixedContentLengthLong != -1) { | |
220 mOutputStream = new CronetOutputStream(this, superFixedContentLe ngthLong); | |
221 // Start the request now since all headers can be sent. | |
222 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
| |
223 } else { | |
224 // For the buffered case, start the request only when | |
225 // content-length bytes are received, or when an | |
226 // connect action is initiated by the consumer. | |
227 Log.d(TAG, "Outputstream is being buffered in memory."); | |
228 String length = getRequestProperty("Content-Length"); | |
229 if (length == null) { | |
230 mOutputStream = new CronetBufferedOutputStream(this); | |
231 } else { | |
232 long lengthParsed = Long.parseLong(length); | |
233 mOutputStream = new CronetBufferedOutputStream(this, lengthP arsed); | |
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.
| |
234 } | |
235 } | |
236 } | |
237 return mOutputStream; | |
238 } | |
239 | |
240 /** | |
241 * Starts the request if {@code connected} is false. | |
242 */ | |
243 private void startRequest() throws IOException { | |
244 if (connected) { | |
245 return; | |
246 } | |
247 if (mOutputStream != null) { | |
248 // Default Content-Type to application/x-www-form-urlencoded | |
249 if (getRequestProperty("Content-Type") == null) { | |
250 addRequestProperty("Content-Type", | |
251 "application/x-www-form-urlencoded"); | |
252 } | |
253 if (getRequestProperty("Content-Length") == null) { | |
254 addRequestProperty("Content-Length", | |
255 Long.toString(((UploadDataProvider) mOutputStream).getLe ngth())); | |
256 } | |
257 mRequest.setUploadDataProvider((UploadDataProvider) mOutputStream, | |
258 mMessageLoop); | |
259 } | |
260 connected = true; | |
261 // Start the request. Note that connect() is not used since | |
262 // connect() blocks until headers are received. | |
263 for (Pair<String, String> requestHeader : mRequestHeaders) { | |
264 mRequest.addHeader(requestHeader.first, requestHeader.second); | |
265 } | |
266 if (!getUseCaches()) { | |
267 mRequest.disableCache(); | |
268 } | |
269 mRequest.start(); | |
270 } | |
271 | |
201 /** | 272 /** |
202 * Returns an input stream from the server in the case of an error such as | 273 * 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. | 274 * the requested file has not been found on the remote server. |
204 */ | 275 */ |
205 @Override | 276 @Override |
206 public InputStream getErrorStream() { | 277 public InputStream getErrorStream() { |
207 try { | 278 try { |
208 connect(); | 279 connect(); |
209 } catch (IOException e) { | 280 } catch (IOException e) { |
210 return null; | 281 return null; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 /** | 365 /** |
295 * Returns whether this connection uses a proxy server. | 366 * Returns whether this connection uses a proxy server. |
296 */ | 367 */ |
297 @Override | 368 @Override |
298 public boolean usingProxy() { | 369 public boolean usingProxy() { |
299 // TODO(xunjieli): implement this. | 370 // TODO(xunjieli): implement this. |
300 return false; | 371 return false; |
301 } | 372 } |
302 | 373 |
303 /** | 374 /** |
375 * Sets chunked streaming mode. | |
376 */ | |
377 @Override | |
378 public void setChunkedStreamingMode(int chunklen) { | |
379 // TODO(xunjieli): implement this. | |
380 throw new UnsupportedOperationException("Chunked mode not supported yet" ); | |
381 } | |
382 | |
383 /** | |
304 * Used by {@link CronetInputStream} to get more data from the network | 384 * Used by {@link CronetInputStream} to get more data from the network |
305 * stack. This should only be called after the request has started. Note | 385 * 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. | 386 * that this call might block if there isn't any more data to be read. |
307 */ | 387 */ |
308 ByteBuffer getMoreData() throws IOException { | 388 ByteBuffer getMoreData() throws IOException { |
309 mResponseByteBuffer = null; | 389 mResponseByteBuffer = null; |
310 mMessageLoop.loop(); | 390 mMessageLoop.loop(); |
311 return mResponseByteBuffer; | 391 return mResponseByteBuffer; |
312 } | 392 } |
313 | 393 |
314 /** | 394 /** |
395 * Used by {@link CronetOutputStream} to wait for data to be read by the | |
396 * network stack before letting the consumer write more data. | |
397 */ | |
398 void waitForRead() throws IOException { | |
399 mMessageLoop.loop(); | |
400 } | |
401 | |
402 /** | |
403 * Used by {@link CronetOutputStream} to execute read after consumer writes | |
404 * more data. | |
405 */ | |
406 void postponeRead(Runnable readTask) { | |
407 mMessageLoop.postQuitTask(); | |
408 mMessageLoop.execute(readTask); | |
409 } | |
410 | |
411 /** | |
315 * Returns the index of request header in {@link #mRequestHeaders} or | 412 * Returns the index of request header in {@link #mRequestHeaders} or |
316 * -1 if not found. | 413 * -1 if not found. |
317 */ | 414 */ |
318 private int findRequestProperty(String key) { | 415 private int findRequestProperty(String key) { |
319 for (int i = 0; i < mRequestHeaders.size(); i++) { | 416 for (int i = 0; i < mRequestHeaders.size(); i++) { |
320 Pair<String, String> entry = mRequestHeaders.get(i); | 417 Pair<String, String> entry = mRequestHeaders.get(i); |
321 if (entry.first.equalsIgnoreCase(key)) { | 418 if (entry.first.equalsIgnoreCase(key)) { |
322 return i; | 419 return i; |
323 } | 420 } |
324 } | 421 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
417 return null; | 514 return null; |
418 } | 515 } |
419 List<Pair<String, String>> headers = | 516 List<Pair<String, String>> headers = |
420 mResponseInfo.getAllHeadersAsList(); | 517 mResponseInfo.getAllHeadersAsList(); |
421 if (pos >= headers.size()) { | 518 if (pos >= headers.size()) { |
422 return null; | 519 return null; |
423 } | 520 } |
424 return headers.get(pos); | 521 return headers.get(pos); |
425 } | 522 } |
426 } | 523 } |
OLD | NEW |