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.Executor; | |
| 20 | |
| 21 /** | |
| 22 * An implementation of HttpURLConnection that uses Cronet to send requests and | |
| 23 * receive response. This class inherits a {@code connected} field from the | |
| 24 * superclass. That field indicates whether a connection has ever been | |
| 25 * attempted. | |
| 26 */ | |
| 27 public class CronetHttpURLConnection extends HttpURLConnection { | |
| 28 private final UrlRequestContext mUrlRequestContext; | |
| 29 private final MessageLoop mMessageLoop; | |
| 30 private final UrlRequest mRequest; | |
| 31 | |
| 32 private CronetInputStream mInputStream; | |
| 33 private ResponseInfo mResponseInfo; | |
| 34 private ByteBuffer mResponseByteBuffer; | |
| 35 private InterruptedException mInterruptedException; | |
| 36 | |
| 37 protected CronetHttpURLConnection(URL url, | |
| 38 UrlRequestContext urlRequestContext) { | |
| 39 super(url); | |
| 40 mUrlRequestContext = urlRequestContext; | |
| 41 mMessageLoop = new MessageLoop(); | |
| 42 mRequest = mUrlRequestContext.createRequest(url.toString(), | |
| 43 new CronetUrlRequestListener(), new MessageLoopExecutor()); | |
| 44 mInputStream = new CronetInputStream(this); | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Wrapper executor class which posts tasks to message loop. | |
| 49 */ | |
| 50 private class MessageLoopExecutor implements Executor { | |
| 51 public MessageLoopExecutor() { | |
| 52 } | |
| 53 | |
| 54 @Override | |
| 55 public void execute(Runnable command) { | |
| 56 try { | |
| 57 mMessageLoop.postTask(command); | |
| 58 } catch (InterruptedException e) { | |
| 59 mInterruptedException = e; | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Opens a connection to the resource. If the connect method is called when | |
| 66 * the connection has already been opened (indicated by the connected field | |
| 67 * having the value true), the call is ignored. | |
| 68 */ | |
| 69 @Override | |
| 70 public void connect() throws IOException { | |
| 71 if (connected) { | |
| 72 return; | |
| 73 } | |
| 74 connected = true; | |
| 75 mRequest.start(); | |
| 76 // Blocks until onResponseStarted or onFailed is called. | |
| 77 startMessageLoop(); | |
| 78 if (mInterruptedException != null) { | |
| 79 throw new IOException(mInterruptedException); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * Releases this connection so that its resources may be either reused or | |
| 85 * closed. | |
| 86 */ | |
| 87 @Override | |
| 88 public void disconnect() { | |
| 89 // Disconnect before connection is made should have no effect. | |
| 90 if (connected) { | |
| 91 try { | |
| 92 mInputStream.close(); | |
| 93 } catch (IOException e) { | |
| 94 e.printStackTrace(); | |
| 95 } | |
| 96 mInputStream = null; | |
| 97 mRequest.cancel(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Returns the response message returned by the remote HTTP server. This | |
| 103 * method throws a {@code NullPointerException} if the request fails without | |
| 104 * getting headers. | |
| 105 */ | |
| 106 @Override | |
| 107 public String getResponseMessage() throws IOException { | |
| 108 connect(); | |
| 109 return mResponseInfo.getHttpStatusText(); | |
|
xunjieli
2014/11/26 18:41:25
I should probably add a null check here, and only
| |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * Returns the response code returned by the remote HTTP server. This method | |
| 114 * throws a {@code NullPointerException} if the request fails without | |
| 115 * getting headers. | |
| 116 */ | |
| 117 @Override | |
| 118 public int getResponseCode() throws IOException { | |
| 119 connect(); | |
| 120 return mResponseInfo.getHttpStatusCode(); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Returns an InputStream for reading data from the resource pointed by this | |
| 125 * URLConnection. | |
| 126 */ | |
| 127 @Override | |
| 128 public InputStream getInputStream() throws IOException { | |
| 129 connect(); | |
| 130 return mInputStream; | |
| 131 } | |
| 132 | |
| 133 /** | |
| 134 * Adds the given property to the request header. | |
| 135 */ | |
| 136 @Override | |
| 137 public final void addRequestProperty(String key, String value) { | |
| 138 // Note that Cronet right now does not allow setting multiple headers | |
| 139 // of the same key, see crbug.com/432719 for more details. | |
| 140 setRequestProperty(key, value); | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Sets the value of the specified request header field. | |
| 145 */ | |
| 146 @Override | |
| 147 public final void setRequestProperty(String key, String value) { | |
| 148 if (connected) { | |
| 149 throw new IllegalStateException( | |
| 150 "Cannot set request property after connection is made"); | |
| 151 } | |
| 152 mRequest.addHeader(key, value); | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * Returns whether this connection uses a proxy server or not. | |
| 157 */ | |
| 158 @Override | |
| 159 public boolean usingProxy() { | |
| 160 // TODO(xunjieli): implement this. | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 /** | |
| 165 * Used by {@link CronetInputStream} to get more data from the network | |
| 166 * stack. This should only be called after the request has started. Note | |
| 167 * that this call might block if there isn't any more data to be read. | |
| 168 */ | |
| 169 ByteBuffer getMoreData() { | |
| 170 mResponseByteBuffer = null; | |
| 171 startMessageLoop(); | |
| 172 return mResponseByteBuffer; | |
| 173 } | |
| 174 | |
| 175 private class CronetUrlRequestListener implements UrlRequestListener { | |
| 176 public CronetUrlRequestListener() { | |
| 177 } | |
| 178 | |
| 179 @Override | |
| 180 public void onResponseStarted(UrlRequest request, ResponseInfo info) { | |
| 181 mResponseInfo = info; | |
| 182 // Quits the message loop since we have the headers now. | |
| 183 quitMessageLoop(); | |
| 184 } | |
| 185 | |
| 186 @Override | |
| 187 public void onDataReceived(UrlRequest request, ResponseInfo info, | |
| 188 ByteBuffer byteBuffer) { | |
| 189 mResponseInfo = info; | |
| 190 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity()); | |
| 191 mResponseByteBuffer.put(byteBuffer); | |
| 192 mResponseByteBuffer.flip(); | |
| 193 quitMessageLoop(); | |
| 194 } | |
| 195 | |
| 196 @Override | |
| 197 public void onRedirect(UrlRequest request, ResponseInfo info, | |
| 198 String newLocationUrl) { | |
| 199 // TODO(xunjieli): Handle redirect and test it. | |
| 200 } | |
| 201 | |
| 202 @Override | |
| 203 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { | |
| 204 mResponseInfo = info.getResponseInfo(); | |
| 205 setResponseDataCompleted(); | |
| 206 } | |
| 207 | |
| 208 @Override | |
| 209 public void onFailed(UrlRequest request, ResponseInfo info, | |
| 210 UrlRequestException exception) { | |
| 211 // TODO(xunjieli): Handle failure. | |
| 212 setResponseDataCompleted(); | |
| 213 } | |
| 214 | |
| 215 /** | |
| 216 * Notifies {@link #mInputStream} that transferring of response data has | |
| 217 * completed. | |
| 218 */ | |
| 219 private void setResponseDataCompleted() { | |
| 220 if (mInputStream != null) { | |
| 221 mInputStream.setResponseDataCompleted(); | |
| 222 } | |
| 223 quitMessageLoop(); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 * Posts a quit task to the message loop. | |
| 229 */ | |
| 230 private void quitMessageLoop() { | |
| 231 try { | |
| 232 mMessageLoop.postQuitTask(); | |
| 233 } catch (InterruptedException e) { | |
| 234 mInterruptedException = e; | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 /** | |
| 239 * Starts running the message Loop. | |
| 240 */ | |
| 241 private void startMessageLoop() { | |
| 242 try { | |
| 243 mMessageLoop.loop(); | |
| 244 } catch (InterruptedException e) { | |
| 245 mInterruptedException = e; | |
| 246 } | |
| 247 } | |
| 248 } | |
| OLD | NEW |