1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.squareup.okhttp.internal.http;
18
19import com.squareup.okhttp.Request;
20import com.squareup.okhttp.Response;
21import java.io.IOException;
22import java.net.CacheRequest;
23import okio.Sink;
24import okio.Source;
25
26interface Transport {
27  /**
28   * The timeout to use while discarding a stream of input data. Since this is
29   * used for connection reuse, this timeout should be significantly less than
30   * the time it takes to establish a new connection.
31   */
32  int DISCARD_STREAM_TIMEOUT_MILLIS = 100;
33
34  /**
35   * Returns an output stream where the request body can be written. The
36   * returned stream will of one of two types:
37   * <ul>
38   * <li><strong>Direct.</strong> Bytes are written to the socket and
39   * forgotten. This is most efficient, particularly for large request
40   * bodies. The returned stream may be buffered; the caller must call
41   * {@link #flushRequest} before reading the response.</li>
42   * <li><strong>Buffered.</strong> Bytes are written to an in memory
43   * buffer, and must be explicitly flushed with a call to {@link
44   * #writeRequestBody}. This allows HTTP authorization (401, 407)
45   * responses to be retransmitted transparently.</li>
46   * </ul>
47   */
48  // TODO: don't bother retransmitting the request body? It's quite a corner
49  // case and there's uncertainty whether Firefox or Chrome do this
50  Sink createRequestBody(Request request) throws IOException;
51
52  /** This should update the HTTP engine's sentRequestMillis field. */
53  void writeRequestHeaders(Request request) throws IOException;
54
55  /**
56   * Sends the request body returned by {@link #createRequestBody} to the
57   * remote peer.
58   */
59  void writeRequestBody(RetryableSink requestBody) throws IOException;
60
61  /** Flush the request body to the underlying socket. */
62  void flushRequest() throws IOException;
63
64  /** Read response headers and update the cookie manager. */
65  Response.Builder readResponseHeaders() throws IOException;
66
67  /** Notify the transport that no response body will be read. */
68  void emptyTransferStream() throws IOException;
69
70  // TODO: make this the content stream?
71  Source getTransferStream(CacheRequest cacheRequest) throws IOException;
72
73  /**
74   * Configures the response body to pool or close the socket connection when
75   * the response body is closed.
76   */
77  void releaseConnectionOnIdle() throws IOException;
78
79  void disconnect(HttpEngine engine) throws IOException;
80
81  /**
82   * Returns true if the socket connection held by this transport can be reused
83   * for a follow-up exchange.
84   */
85  boolean canReuseConnection();
86}
87