Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net.urlconnection; | |
| 6 | |
| 7 import org.chromium.net.ExtendedResponseInfo; | |
| 8 import org.chromium.net.ResponseInfo; | |
| 9 import org.chromium.net.UrlRequest; | |
| 10 import org.chromium.net.UrlRequestContext; | |
| 11 import org.chromium.net.UrlRequestException; | |
| 12 import org.chromium.net.UrlRequestListener; | |
| 13 | |
| 14 import java.io.IOException; | |
| 15 import java.io.InputStream; | |
| 16 import java.net.HttpURLConnection; | |
| 17 import java.net.URL; | |
| 18 import java.nio.ByteBuffer; | |
| 19 import java.util.concurrent.BlockingQueue; | |
| 20 import java.util.concurrent.Executor; | |
| 21 import java.util.concurrent.LinkedBlockingQueue; | |
| 22 | |
| 23 /** | |
| 24 * An implementation of HttpURLConnection that uses Cronet to send requests and | |
| 25 * receive response. This class inherits a {@code connected} field from the | |
| 26 * superclass. That field indicates whether a connection has ever been | |
| 27 * attempted. | |
| 28 */ | |
| 29 public class CronetHttpURLConnection extends HttpURLConnection { | |
| 30 private final UrlRequestContext mUrlRequestContext; | |
| 31 private final MessageLoop mMessageLoop; | |
| 32 private final BlockingQueue<MessageLoop.Message> mMessageQueue; | |
| 33 | |
| 34 private final UrlRequest mRequest; | |
|
mmenke
2014/11/21 16:53:07
optional: Maybe another line break here, to empha
xunjieli
2014/11/21 20:30:55
Done.
| |
| 35 private ResponseInfo mResponseInfo; | |
| 36 private CronetInputStream mInputStream; | |
| 37 private ByteBuffer mResponseByteBuffer; | |
| 38 | |
| 39 protected CronetHttpURLConnection(URL url, | |
| 40 UrlRequestContext urlRequestContext) { | |
| 41 super(url); | |
| 42 mUrlRequestContext = urlRequestContext; | |
| 43 mMessageQueue = new LinkedBlockingQueue<MessageLoop.Message>(); | |
| 44 mMessageLoop = new MessageLoop(mMessageQueue); | |
| 45 mRequest = mUrlRequestContext.createRequest(url.toString(), | |
| 46 new CronetUrlRequestListener(), new HandlerThreadExecutor()); | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Wrapper executor class which posts tasks to current looper. | |
| 51 */ | |
| 52 private class HandlerThreadExecutor implements Executor { | |
| 53 public HandlerThreadExecutor() { | |
| 54 } | |
| 55 | |
| 56 @Override | |
| 57 public void execute(Runnable command) { | |
| 58 try { | |
| 59 mMessageQueue.put(new MessageLoop.Message(command)); | |
| 60 } catch (InterruptedException e) { | |
| 61 e.printStackTrace(); | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 /** | |
| 67 * Opens a connection to the resource. If the connect method is called when | |
| 68 * the connection has already been opened (indicated by the connected field | |
| 69 * having the value true), the call is ignored. | |
| 70 */ | |
| 71 @Override | |
| 72 public void connect() throws IOException { | |
| 73 maybeStartRequest(); | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * Releases this connection so that its resources may be either reused or | |
| 78 * closed. | |
| 79 */ | |
| 80 @Override | |
| 81 public void disconnect() { | |
| 82 if (mInputStream != null) { | |
| 83 try { | |
| 84 mInputStream.close(); | |
|
mmenke
2014/11/21 16:53:07
mInputStream = null, so we handle multiple calls?
xunjieli
2014/11/21 20:30:55
OkHttp does nothing if it is called multiple times
mmenke
2014/11/21 21:26:12
Hrm...ok, so looks like we're copying their behavi
| |
| 85 } catch (IOException e) { | |
| 86 e.printStackTrace(); | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Returns the response message returned by the remote HTTP server. | |
| 93 */ | |
| 94 @Override | |
| 95 public String getResponseMessage() { | |
|
mmenke
2014/11/21 16:53:07
We don't have any tests exercise this.
xunjieli
2014/11/21 20:30:55
Done.
| |
| 96 maybeStartRequest(); | |
|
mmenke
2014/11/21 16:53:07
It's a pain, but we should probably test all these
xunjieli
2014/11/21 20:30:56
Acknowledged. Will keep this in mind and handle it
| |
| 97 return mResponseInfo.getHttpStatusText(); | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * Returns the response code returned by the remote HTTP server. | |
| 102 */ | |
| 103 @Override | |
| 104 public int getResponseCode() { | |
| 105 maybeStartRequest(); | |
| 106 return mResponseInfo.getHttpStatusCode(); | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * Returns an InputStream for reading data from the resource pointed by this | |
| 111 * URLConnection. | |
| 112 */ | |
| 113 @Override | |
| 114 public InputStream getInputStream() { | |
| 115 if (mInputStream == null) { | |
| 116 maybeStartRequest(); | |
| 117 mInputStream = new CronetInputStream(this); | |
| 118 } | |
| 119 return mInputStream; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Adds the given property to the request header. | |
| 124 */ | |
| 125 @Override | |
| 126 public final void addRequestProperty(String key, String value) { | |
| 127 setRequestProperty(key, value); | |
| 128 } | |
| 129 | |
| 130 /** | |
| 131 * Sets the value of the specified request header field. | |
| 132 */ | |
| 133 @Override | |
| 134 public final void setRequestProperty(String key, String value) { | |
| 135 if (connected) { | |
| 136 throw new IllegalStateException( | |
| 137 "Cannot set request property after connection is made"); | |
| 138 } | |
| 139 mRequest.addHeader(key, value); | |
| 140 } | |
| 141 | |
| 142 /** | |
| 143 * Returns whether this connection uses a proxy server or not. | |
| 144 */ | |
| 145 @Override | |
| 146 public boolean usingProxy() { | |
| 147 // TODO(xunjieli): implement this. | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 /** | |
| 152 * Used by {@link CronetInputStream} to request more data from the network | |
| 153 * stack. Note that this call might block if there isn't any more data to be | |
| 154 * read. | |
|
mmenke
2014/11/21 16:53:07
Worth noting that this may only be called after th
xunjieli
2014/11/21 20:30:56
Done.
| |
| 155 */ | |
| 156 ByteBuffer requestData() { | |
|
mmenke
2014/11/21 16:53:06
maybe requestMoreData? ("Request data" could be a
xunjieli
2014/11/21 20:30:56
Done.
| |
| 157 mMessageLoop.loop(); | |
| 158 return mResponseByteBuffer; | |
| 159 } | |
| 160 | |
| 161 private class CronetUrlRequestListener implements UrlRequestListener { | |
| 162 public CronetUrlRequestListener() { | |
| 163 } | |
| 164 | |
| 165 @Override | |
| 166 public void onResponseStarted(UrlRequest request, ResponseInfo info) { | |
| 167 mResponseInfo = info; | |
| 168 // Quits the message loop since we have the headers now. | |
| 169 mMessageLoop.quit(); | |
| 170 } | |
| 171 | |
| 172 @Override | |
| 173 public void onDataReceived(UrlRequest request, ResponseInfo info, | |
| 174 ByteBuffer byteBuffer) { | |
| 175 mResponseInfo = info; | |
| 176 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity()); | |
| 177 mResponseByteBuffer.put(byteBuffer); | |
| 178 mResponseByteBuffer.rewind(); | |
| 179 mMessageLoop.quit(); | |
| 180 } | |
| 181 | |
| 182 @Override | |
| 183 public void onRedirect(UrlRequest request, ResponseInfo info, | |
| 184 String newLocationUrl) { | |
| 185 // TODO(xunjieli): Handle redirect. | |
| 186 mResponseInfo = info; | |
| 187 } | |
| 188 | |
| 189 @Override | |
| 190 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { | |
| 191 setResponseDataCompleted(); | |
| 192 } | |
| 193 | |
| 194 @Override | |
| 195 public void onFailed(UrlRequest request, ResponseInfo info, | |
| 196 UrlRequestException exception) { | |
| 197 // TODO(xunjieli): Handle failure. | |
| 198 setResponseDataCompleted(); | |
| 199 } | |
| 200 | |
| 201 /** | |
| 202 * Notifies {@link #mInputStream} that transferring of response data has | |
| 203 * completed. | |
| 204 */ | |
| 205 private void setResponseDataCompleted() { | |
| 206 if (mInputStream != null) { | |
| 207 mInputStream.setResponseDataCompleted(); | |
| 208 } | |
| 209 mMessageLoop.quit(); | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 /** | |
| 214 * On first call, creates and starts UrlRequest, and waits until response | |
| 215 * headers are received. Does nothing on subsequent calls. | |
| 216 */ | |
| 217 private void maybeStartRequest() { | |
| 218 if (connected) { | |
| 219 return; | |
| 220 } | |
| 221 connected = true; | |
| 222 mRequest.start(); | |
| 223 // Blocks until onResponseStarted or onFailed is called. | |
| 224 mMessageLoop.loop(); | |
| 225 } | |
| 226 } | |
| OLD | NEW |