Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(420)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java

Issue 790273002: [Cronet] Handle redirects in CronetHttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 org.chromium.net.ExtendedResponseInfo; 7 import org.chromium.net.ExtendedResponseInfo;
8 import org.chromium.net.ResponseInfo; 8 import org.chromium.net.ResponseInfo;
9 import org.chromium.net.UrlRequest; 9 import org.chromium.net.UrlRequest;
10 import org.chromium.net.UrlRequestContext; 10 import org.chromium.net.UrlRequestContext;
11 import org.chromium.net.UrlRequestException; 11 import org.chromium.net.UrlRequestException;
12 import org.chromium.net.UrlRequestListener; 12 import org.chromium.net.UrlRequestListener;
13 13
14 import java.io.IOException; 14 import java.io.IOException;
15 import java.io.InputStream; 15 import java.io.InputStream;
16 import java.net.HttpURLConnection; 16 import java.net.HttpURLConnection;
17 import java.net.MalformedURLException;
17 import java.net.URL; 18 import java.net.URL;
18 import java.nio.ByteBuffer; 19 import java.nio.ByteBuffer;
19 20
20 /** 21 /**
21 * An implementation of HttpURLConnection that uses Cronet to send requests and 22 * An implementation of HttpURLConnection that uses Cronet to send requests and
22 * receive response. This class inherits a {@code connected} field from the 23 * receive response. This class inherits a {@code connected} field from the
23 * superclass. That field indicates whether a connection has ever been 24 * superclass. That field indicates whether a connection has ever been
24 * attempted. 25 * attempted.
25 */ 26 */
26 public class CronetHttpURLConnection extends HttpURLConnection { 27 public class CronetHttpURLConnection extends HttpURLConnection {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 return mResponseInfo.getHttpStatusCode(); 101 return mResponseInfo.getHttpStatusCode();
101 } 102 }
102 103
103 /** 104 /**
104 * Returns an InputStream for reading data from the resource pointed by this 105 * Returns an InputStream for reading data from the resource pointed by this
105 * URLConnection. 106 * URLConnection.
106 */ 107 */
107 @Override 108 @Override
108 public InputStream getInputStream() throws IOException { 109 public InputStream getInputStream() throws IOException {
109 connect(); 110 connect();
111 if (mResponseInfo == null) {
112 throw new IOException();
113 }
114 if (!instanceFollowRedirects
115 && mResponseInfo.getHttpStatusCode() == 302) {
mmenke 2014/12/10 21:44:52 301, 303, 307, and 308 may also be redirects (308s
xunjieli 2014/12/12 20:58:02 Done. Smart idea! thanks
116 throw new IOException(
117 "Cannot read response body of a 302 response");
118 }
110 return mInputStream; 119 return mInputStream;
111 } 120 }
112 121
113 /** 122 /**
114 * Adds the given property to the request header. 123 * Adds the given property to the request header.
115 */ 124 */
116 @Override 125 @Override
117 public final void addRequestProperty(String key, String value) { 126 public final void addRequestProperty(String key, String value) {
118 // Note that Cronet right now does not allow setting multiple headers 127 // Note that Cronet right now does not allow setting multiple headers
119 // of the same key, see crbug.com/432719 for more details. 128 // of the same key, see crbug.com/432719 for more details.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 mResponseInfo = info; 178 mResponseInfo = info;
170 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity()); 179 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity());
171 mResponseByteBuffer.put(byteBuffer); 180 mResponseByteBuffer.put(byteBuffer);
172 mResponseByteBuffer.flip(); 181 mResponseByteBuffer.flip();
173 mMessageLoop.postQuitTask(); 182 mMessageLoop.postQuitTask();
174 } 183 }
175 184
176 @Override 185 @Override
177 public void onRedirect(UrlRequest request, ResponseInfo info, 186 public void onRedirect(UrlRequest request, ResponseInfo info,
178 String newLocationUrl) { 187 String newLocationUrl) {
179 // TODO(xunjieli): Handle redirect and test it. 188 if (instanceFollowRedirects) {
189 try {
190 url = new URL(newLocationUrl);
191 } catch (MalformedURLException e) {
xunjieli 2014/12/10 20:27:17 Not sure if we need to handle malformed url, I gue
mef 2014/12/11 16:30:56 I agree.
xunjieli 2014/12/12 20:58:02 Acknowledged. I will leave it unhandled, unless Ma
192 // Ignored.
193 }
194 } else {
195 mResponseInfo = info;
196 mRequest.cancel();
197 setResponseDataCompleted();
mef 2014/12/11 16:30:56 Per Matt's suggestion maybe set a flag indicating
xunjieli 2014/12/12 20:58:01 Done.
198 }
180 } 199 }
181 200
182 @Override 201 @Override
183 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { 202 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) {
184 mResponseInfo = info.getResponseInfo(); 203 mResponseInfo = info.getResponseInfo();
185 setResponseDataCompleted(); 204 setResponseDataCompleted();
186 } 205 }
187 206
188 @Override 207 @Override
189 public void onFailed(UrlRequest request, ResponseInfo info, 208 public void onFailed(UrlRequest request, ResponseInfo info,
190 UrlRequestException exception) { 209 UrlRequestException exception) {
191 // TODO(xunjieli): Handle failure. 210 // TODO(xunjieli): Handle failure.
192 setResponseDataCompleted(); 211 setResponseDataCompleted();
193 } 212 }
194 213
195 /** 214 /**
196 * Notifies {@link #mInputStream} that transferring of response data has 215 * Notifies {@link #mInputStream} that transferring of response data has
197 * completed. 216 * completed.
198 */ 217 */
199 private void setResponseDataCompleted() { 218 private void setResponseDataCompleted() {
200 if (mInputStream != null) { 219 if (mInputStream != null) {
201 mInputStream.setResponseDataCompleted(); 220 mInputStream.setResponseDataCompleted();
202 } 221 }
203 mMessageLoop.postQuitTask(); 222 mMessageLoop.postQuitTask();
204 } 223 }
205 } 224 }
206 } 225 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698