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 172231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonpackage com.squareup.okhttp.internal.http; 18c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.Request; 203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.Response; 21c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.io.IOException; 22c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.net.CacheRequest; 233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport okio.Sink; 243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport okio.Source; 25c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 26c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathinterface Transport { 2754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 283c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * The timeout to use while discarding a stream of input data. Since this is 293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * used for connection reuse, this timeout should be significantly less than 303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * the time it takes to establish a new connection. 313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller int DISCARD_STREAM_TIMEOUT_MILLIS = 100; 333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** 3554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Returns an output stream where the request body can be written. The 3654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * returned stream will of one of two types: 3754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * <ul> 3854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * <li><strong>Direct.</strong> Bytes are written to the socket and 3954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * forgotten. This is most efficient, particularly for large request 4054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * bodies. The returned stream may be buffered; the caller must call 4154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * {@link #flushRequest} before reading the response.</li> 4254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * <li><strong>Buffered.</strong> Bytes are written to an in memory 4354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * buffer, and must be explicitly flushed with a call to {@link 4454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * #writeRequestBody}. This allows HTTP authorization (401, 407) 4554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * responses to be retransmitted transparently.</li> 4654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * </ul> 4754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 4854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson // TODO: don't bother retransmitting the request body? It's quite a corner 4954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson // case and there's uncertainty whether Firefox or Chrome do this 503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller Sink createRequestBody(Request request) throws IOException; 51c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 5254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** This should update the HTTP engine's sentRequestMillis field. */ 533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller void writeRequestHeaders(Request request) throws IOException; 54c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 5554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 5654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Sends the request body returned by {@link #createRequestBody} to the 5754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * remote peer. 5854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 593c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller void writeRequestBody(RetryableSink requestBody) throws IOException; 60c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 6154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** Flush the request body to the underlying socket. */ 6254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson void flushRequest() throws IOException; 63c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 6454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** Read response headers and update the cookie manager. */ 653c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller Response.Builder readResponseHeaders() throws IOException; 663c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** Notify the transport that no response body will be read. */ 683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller void emptyTransferStream() throws IOException; 69c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 7054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson // TODO: make this the content stream? 713c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller Source getTransferStream(CacheRequest cacheRequest) throws IOException; 72c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath 733c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** 743c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Configures the response body to pool or close the socket connection when 753c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * the response body is closed. 763c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 773c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller void releaseConnectionOnIdle() throws IOException; 783c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 79602d5e4cfdbd0bad91e7872837f95aff5b461595Jesse Wilson void disconnect(HttpEngine engine) throws IOException; 80602d5e4cfdbd0bad91e7872837f95aff5b461595Jesse Wilson 813c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** 823c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Returns true if the socket connection held by this transport can be reused 833c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * for a follow-up exchange. 843c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 853c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller boolean canReuseConnection(); 86c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath} 87