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