104f4f4f447806cd92a2fb6f4b66d11f6d5003a82Dan Gohman// Copyright 2014 The Chromium Authors. All rights reserved. 2e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng// Use of this source code is governed by a BSD-style license that can be 3e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng// found in the LICENSE file. 4e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng 54ee451de366474b9c228b4e5fa573795a715216dChris Lattnerpackage org.chromium.net; 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner 7e165a78551a91d8420cd8f074d97701e8788f8b5Evan Chengimport java.io.IOException; 8e165a78551a91d8420cd8f074d97701e8788f8b5Evan Chengimport java.nio.ByteBuffer; 9e165a78551a91d8420cd8f074d97701e8788f8b5Evan Chengimport java.nio.channels.ReadableByteChannel; 10e165a78551a91d8420cd8f074d97701e8788f8b5Evan Chengimport java.util.List; 11e165a78551a91d8420cd8f074d97701e8788f8b5Evan Chengimport java.util.Map; 12e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng 13e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng/** 14e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng * HTTP request (GET or POST). 15e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng */ 16e165a78551a91d8420cd8f074d97701e8788f8b5Evan Chengpublic interface HttpUrlRequest { 17e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng 18eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey public static final int REQUEST_PRIORITY_IDLE = 0; 19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth 20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth public static final int REQUEST_PRIORITY_LOWEST = 1; 21a6fb1b6743ee1411accf2d6e636f73f2ee0a7f5bEvan Cheng 22e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng public static final int REQUEST_PRIORITY_LOW = 2; 23f662a59b8b031bd43e43e0282b58bef920f0793dWeiming Zhao 24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth public static final int REQUEST_PRIORITY_MEDIUM = 3; 25d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth 260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth public static final int REQUEST_PRIORITY_HIGHEST = 4; 270b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth 28decc2671516e6c52ee2f29f7746f8d02753845eaChris Lattner /** 29decc2671516e6c52ee2f29f7746f8d02753845eaChris Lattner * Returns the URL associated with this request. 30bbbfa99d3d18fe9f20265305e833666645ada528Chris Lattner */ 31d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth String getUrl(); 32d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth 33d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth /** 34d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth * Requests a range starting at the given offset to the end of the resource. 35e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng * The server may or may not honor the offset request. The client must check 36e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng * the HTTP status code before processing the response. 37e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng */ 38dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines void setOffset(long offset); 39dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 40cffbd2562a5e3ba435dd2b622710ec272c634da5Dan Gohman /** 41f10c973797cf79da802f9b0118543cbd50954c9cEvan Cheng * Limits the size of the download. 42a2ee2756f7f80d24312d6ac41b4f2ae548441cacEvan Cheng * 43c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng * @param limit Maximum size of the downloaded response (post gzip) 44a2ee2756f7f80d24312d6ac41b4f2ae548441cacEvan Cheng * @param cancelEarly If true, cancel the download as soon as the size of 4513ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey * the response is known. If false, download {@code responseSize} 4613ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey * bytes and then cancel. 47b8cab9227a0f6ffbdaae33e3c64268e265008a6aDan Gohman */ 4813ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey void setContentLengthLimit(long limit, boolean cancelEarly); 4913ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey 50187361b056823df4ff292561fe47468dad956872Bill Wendling /** 51187361b056823df4ff292561fe47468dad956872Bill Wendling * Sets data to upload as part of a POST request. 52187361b056823df4ff292561fe47468dad956872Bill Wendling * 53187361b056823df4ff292561fe47468dad956872Bill Wendling * @param contentType MIME type of the post content or null if this is not a 5413ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey * POST. 5515a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng * @param data The content that needs to be uploaded if this is a POST 56b11ac950d69c7a238de0a22fd23fbfcd994f57eeEvan Cheng * request. 5770017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng */ 5870017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng void setUploadData(String contentType, byte[] data); 5915a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng 6015a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng /** 6170017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng * Sets a readable byte channel to upload as part of a POST request. 6270017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng * 6370017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng * <p>Once {@link #start()} is called, this channel is guaranteed to be 6470017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng * closed, either when the upload completes, or when it is canceled. 6570017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng * 6670017e44cdba1946cc478ce1856a3e855a767e28Evan Cheng * @param contentType MIME type of the post content or null if this is not a 67c8bfd1d78ff9a307d1d4cb57cce4549b538e60f4Andrew Trick * POST. 68d1dace8aea073716daf0055ad07fde1164b2a472Andrew Trick * @param channel The channel to read to read upload data from if this is a 69c8bfd1d78ff9a307d1d4cb57cce4549b538e60f4Andrew Trick * POST request. 702da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick * @param contentLength The length of data to upload. 71e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick */ 7212f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick void setUploadChannel(String contentType, ReadableByteChannel channel, 73e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick long contentLength); 74e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick 75e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick /** 76e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * Sets the HTTP method verb to use for this request. 773c6e49504e9a57a4818750fd2520967f84634eacAndrew Trick * 78e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * <p>The default when this method is not called is "GET" if the request has 7954699765064842fd08d1466adc93453660bc2a85Andrew Trick * no body or "POST" if it does. 8054699765064842fd08d1466adc93453660bc2a85Andrew Trick * 8154699765064842fd08d1466adc93453660bc2a85Andrew Trick * @param method "GET", "POST", etc. Must be all uppercase. 8212f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick */ 8312f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick void setHttpMethod(String method); 8412f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick 85e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick /** 863c6e49504e9a57a4818750fd2520967f84634eacAndrew Trick * Start executing the request. 87e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * <p> 88e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * If this is a streaming upload request using a ReadableByteChannel, the 89e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * call will block while the request is uploaded. 90e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick */ 91e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick void start(); 92e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick 93e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick /** 94623a7e146bd86747dc46a6f8bb9993fc217d6b78Evan Cheng * Cancel the request in progress. 95623a7e146bd86747dc46a6f8bb9993fc217d6b78Evan Cheng */ 96623a7e146bd86747dc46a6f8bb9993fc217d6b78Evan Cheng void cancel(); 97e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick 98e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick /** 99e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * Returns {@code true} if the request has been canceled. 100e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick */ 101e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick boolean isCanceled(); 102e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick 103e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick /** 104e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * Returns protocol (e.g. "quic/1+spdy/3") negotiated with server. Returns 105e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * empty string if no protocol was negotiated, or the protocol is not known. 106e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * Returns empty when using plain http or https. Must be called after 107e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng * onResponseStarted but before request is recycled. 108e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng */ 109e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng String getNegotiatedProtocol(); 110e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng 111e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng /** 1126726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky * Returns the entire response as a ByteBuffer. 113e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng */ 11415a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng ByteBuffer getByteBuffer(); 11515a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng 11615a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng /** 11715a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng * Returns the entire response as a byte array. 118e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng */ 119e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng byte[] getResponseAsBytes(); 120e165a78551a91d8420cd8f074d97701e8788f8b5Evan Cheng 1212da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick /** 1222da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick * Returns the expected content length. It is not guaranteed to be correct 1232da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick * and may be -1 if the content length is unknown. 1242da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick */ 1252da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick long getContentLength(); 1262da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick 1272da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick /** 1282da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick * Returns the content MIME type if known or {@code null} otherwise. 1292da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick */ 1302902736a507df1c6fdc0daa4e8f0e385bb5f7820Andrew Trick String getContentType(); 1312902736a507df1c6fdc0daa4e8f0e385bb5f7820Andrew Trick 1322902736a507df1c6fdc0daa4e8f0e385bb5f7820Andrew Trick /** 1332da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick * Returns the HTTP status code. It may be 0 if the request has not started 1342da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick * or failed before getting the status code from the server. If the status 1352da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick * code is 206 (partial response) after {@link #setOffset} is called, the 136e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick * method returns 200. 137e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick */ 138e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick int getHttpStatusCode(); 139e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick 140086ec9976ff6cee083de618429c78473491d5713Dan Gohman /** 141a6fb1b6743ee1411accf2d6e636f73f2ee0a7f5bEvan Cheng * Returns the response header value for the given name or {@code null} if 142a6fb1b6743ee1411accf2d6e636f73f2ee0a7f5bEvan Cheng * not found. 143086ec9976ff6cee083de618429c78473491d5713Dan Gohman */ 144a6fb1b6743ee1411accf2d6e636f73f2ee0a7f5bEvan Cheng String getHeader(String name); 1453d420cb2fee925d0888cd3a60a222a19e75cd890Andrew Trick 146a6fb1b6743ee1411accf2d6e636f73f2ee0a7f5bEvan Cheng /** 147029f4fd2ff539ed143b83c140349df2c064965d2Andrew Trick * Returns an unmodifiable map of the response-header fields and values. 148029f4fd2ff539ed143b83c140349df2c064965d2Andrew Trick * The null key is mapped to the HTTP status line for compatibility with 149029f4fd2ff539ed143b83c140349df2c064965d2Andrew Trick * HttpUrlConnection. 150029f4fd2ff539ed143b83c140349df2c064965d2Andrew Trick */ 151029f4fd2ff539ed143b83c140349df2c064965d2Andrew Trick Map<String, List<String>> getAllHeaders(); 152029f4fd2ff539ed143b83c140349df2c064965d2Andrew Trick 15321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman /** 15421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman * Returns the exception that occurred while executing the request of null 15521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman * if the request was successful. 15621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman */ 1570e6307f6423a7ee39b80f8dc3caef00ad11e0266Eli Friedman IOException getException(); 1580e6307f6423a7ee39b80f8dc3caef00ad11e0266Eli Friedman} 1590e6307f6423a7ee39b80f8dc3caef00ad11e0266Eli Friedman