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;
12 * HTTP request (GET or POST).
14 public interface HttpUrlRequest {
16 public static final int REQUEST_PRIORITY_IDLE = 0;
18 public static final int REQUEST_PRIORITY_LOWEST = 1;
20 public static final int REQUEST_PRIORITY_LOW = 2;
22 public static final int REQUEST_PRIORITY_MEDIUM = 3;
24 public static final int REQUEST_PRIORITY_HIGHEST = 4;
27 * Returns the URL associated with this request.
32 * Requests a range starting at the given offset to the end of the resource.
33 * The server may or may not honor the offset request. The client must check
34 * the HTTP status code before processing the response.
36 void setOffset(long offset);
39 * Limits the size of the download.
41 * @param limit Maximum size of the downloaded response (post gzip)
42 * @param cancelEarly If true, cancel the download as soon as the size of
43 * the response is known. If false, download {@code responseSize}
44 * bytes and then cancel.
46 void setContentLengthLimit(long limit, boolean cancelEarly);
49 * Sets data to upload as part of a POST request.
51 * @param contentType MIME type of the post content or null if this is not a
53 * @param data The content that needs to be uploaded if this is a POST
56 void setUploadData(String contentType, byte[] data);
59 * Sets a readable byte channel to upload as part of a POST request.
61 * @param contentType MIME type of the post content or null if this is not a
63 * @param channel The channel to read to read upload data from if this is a
66 void setUploadChannel(String contentType, ReadableByteChannel channel);
69 * Start executing the request.
71 * If this is a streaming upload request using a ReadableByteChannel, the
72 * call will block while the request is uploaded.
77 * Cancel the request in progress.
82 * Returns {@code true} if the request has been canceled.
87 * Returns the entire response as a ByteBuffer.
89 ByteBuffer getByteBuffer();
92 * Returns the entire response as a byte array.
94 byte[] getResponseAsBytes();
97 * Returns the expected content length. It is not guaranteed to be correct
98 * and may be -1 if the content length is unknown.
100 long getContentLength();
103 * Returns the content MIME type if known or {@code null} otherwise.
105 String getContentType();
108 * Returns the HTTP status code. It may be 0 if the request has not started
109 * or failed before getting the status code from the server. If the status
110 * code is 206 (partial response) after {@link #setOffset} is called, the
111 * method returns 200.
113 int getHttpStatusCode();
116 * Returns the exception that occurred while executing the request of null
117 * if the request was successful.
119 IOException getException();