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.
5 package org.chromium.net;
7 import java.io.IOException;
8 import java.nio.ByteBuffer;
9 import java.nio.channels.ReadableByteChannel;
10 import java.util.List;
14 * HTTP request (GET or POST).
16 public interface HttpUrlRequest {
18 public static final int REQUEST_PRIORITY_IDLE = 0;
20 public static final int REQUEST_PRIORITY_LOWEST = 1;
22 public static final int REQUEST_PRIORITY_LOW = 2;
24 public static final int REQUEST_PRIORITY_MEDIUM = 3;
26 public static final int REQUEST_PRIORITY_HIGHEST = 4;
29 * Returns the URL associated with this request.
34 * Requests a range starting at the given offset to the end of the resource.
35 * The server may or may not honor the offset request. The client must check
36 * the HTTP status code before processing the response.
38 void setOffset(long offset);
41 * Limits the size of the download.
43 * @param limit Maximum size of the downloaded response (post gzip)
44 * @param cancelEarly If true, cancel the download as soon as the size of
45 * the response is known. If false, download {@code responseSize}
46 * bytes and then cancel.
48 void setContentLengthLimit(long limit, boolean cancelEarly);
51 * Sets data to upload as part of a POST request.
53 * @param contentType MIME type of the post content or null if this is not a
55 * @param data The content that needs to be uploaded if this is a POST
58 void setUploadData(String contentType, byte[] data);
61 * Sets a readable byte channel to upload as part of a POST request.
63 * <p>Once {@link #start()} is called, this channel is guaranteed to be
64 * closed, either when the upload completes, or when it is canceled.
66 * @param contentType MIME type of the post content or null if this is not a
68 * @param channel The channel to read to read upload data from if this is a
70 * @param contentLength The length of data to upload.
72 void setUploadChannel(String contentType, ReadableByteChannel channel,
76 * Sets the HTTP method verb to use for this request. Currently can only be
79 * <p>The default when this method is not called is "GET" if the request has
80 * no body or "POST" if it does.
82 * @param method Either "POST" or "PUT".
84 void setHttpMethod(String method);
87 * Start executing the request.
89 * If this is a streaming upload request using a ReadableByteChannel, the
90 * call will block while the request is uploaded.
95 * Cancel the request in progress.
100 * Returns {@code true} if the request has been canceled.
102 boolean isCanceled();
105 * Returns the entire response as a ByteBuffer.
107 ByteBuffer getByteBuffer();
110 * Returns the entire response as a byte array.
112 byte[] getResponseAsBytes();
115 * Returns the expected content length. It is not guaranteed to be correct
116 * and may be -1 if the content length is unknown.
118 long getContentLength();
121 * Returns the content MIME type if known or {@code null} otherwise.
123 String getContentType();
126 * Returns the HTTP status code. It may be 0 if the request has not started
127 * or failed before getting the status code from the server. If the status
128 * code is 206 (partial response) after {@link #setOffset} is called, the
129 * method returns 200.
131 int getHttpStatusCode();
134 * Returns the response header value for the given name or {@code null} if
137 String getHeader(String name);
140 * Returns an unmodifiable map of the response-header fields and values.
141 * The null key is mapped to the HTTP status line for compatibility with
144 Map<String, List<String>> getAllHeaders();
147 * Returns the exception that occurred while executing the request of null
148 * if the request was successful.
150 IOException getException();