Transport.java revision c3f6f16bd4a2338e88275641b9f2f56e816ca377
1c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath/* 2c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Copyright (C) 2012 The Android Open Source Project 3c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * 4c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Licensed under the Apache License, Version 2.0 (the "License"); 5c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * you may not use this file except in compliance with the License. 6c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * You may obtain a copy of the License at 7c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * 8c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * http://www.apache.org/licenses/LICENSE-2.0 9c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * 10c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Unless required by applicable law or agreed to in writing, software 11c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * distributed under the License is distributed on an "AS IS" BASIS, 12c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * See the License for the specific language governing permissions and 14c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * limitations under the License. 15c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */ 16c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 17c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathpackage libcore.net.http; 18c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 19c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.io.IOException; 20c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.io.InputStream; 21c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.io.OutputStream; 22c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.net.CacheRequest; 23c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 24c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathinterface Transport { 25c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath /** 26c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Returns an output stream where the request body can be written. The 27c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * returned stream will of one of two types: 28c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * <ul> 29c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * <li><strong>Direct.</strong> Bytes are written to the socket and 30c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * forgotten. This is most efficient, particularly for large request 31c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * bodies. The returned stream may be buffered; the caller must call 32c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * {@link #flushRequest} before reading the response.</li> 33c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * <li><strong>Buffered.</strong> Bytes are written to an in memory 34c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * buffer, and must be explicitly flushed with a call to {@link 35c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * #writeRequestBody}. This allows HTTP authorization (401, 407) 36c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * responses to be retransmitted transparently.</li> 37c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * </ul> 38c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */ 39c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath // TODO: don't bother retransmitting the request body? It's quite a corner 40c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath // case and there's uncertainty whether Firefox or Chrome do this 41c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath OutputStream createRequestBody() throws IOException; 42c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 43c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath /** 44c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * This should update the HTTP engine's sentRequestMillis field. 45c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */ 46c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath void writeRequestHeaders() throws IOException; 47c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 48c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath /** 49c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Sends the request body returned by {@link #createRequestBody} to the 50c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * remote peer. 51c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */ 52c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath void writeRequestBody(RetryableOutputStream requestBody) throws IOException; 53c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 54c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath /** 55c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Flush the request body to the underlying socket. 56c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */ 57c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath void flushRequest() throws IOException; 58c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 59c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath /** 60c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Read response headers and update the cookie manager. 61c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */ 62c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath ResponseHeaders readResponseHeaders() throws IOException; 63c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 64c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath // TODO: make this the content stream? 65c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath InputStream getTransferStream(CacheRequest cacheRequest) throws IOException; 66c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 67c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath /** 68c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Returns true if the underlying connection can be recycled. 69c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */ 70c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath boolean makeReusable(OutputStream requestBodyOut, InputStream responseBodyIn); 71c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath} 72