Transport.java revision 166772be0e5cfdaea1a64b9f63e4c8dbfe48cba3
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 java.io.IOException;
20import java.io.InputStream;
21import java.io.OutputStream;
22import java.net.CacheRequest;
23
24interface Transport {
25  /**
26   * Returns an output stream where the request body can be written. The
27   * returned stream will of one of two types:
28   * <ul>
29   * <li><strong>Direct.</strong> Bytes are written to the socket and
30   * forgotten. This is most efficient, particularly for large request
31   * bodies. The returned stream may be buffered; the caller must call
32   * {@link #flushRequest} before reading the response.</li>
33   * <li><strong>Buffered.</strong> Bytes are written to an in memory
34   * buffer, and must be explicitly flushed with a call to {@link
35   * #writeRequestBody}. This allows HTTP authorization (401, 407)
36   * responses to be retransmitted transparently.</li>
37   * </ul>
38   */
39  // TODO: don't bother retransmitting the request body? It's quite a corner
40  // case and there's uncertainty whether Firefox or Chrome do this
41  OutputStream createRequestBody() throws IOException;
42
43  /** This should update the HTTP engine's sentRequestMillis field. */
44  void writeRequestHeaders() throws IOException;
45
46  /**
47   * Sends the request body returned by {@link #createRequestBody} to the
48   * remote peer.
49   */
50  void writeRequestBody(RetryableOutputStream requestBody) throws IOException;
51
52  /** Flush the request body to the underlying socket. */
53  void flushRequest() throws IOException;
54
55  /** Read response headers and update the cookie manager. */
56  ResponseHeaders readResponseHeaders() throws IOException;
57
58  // TODO: make this the content stream?
59  InputStream getTransferStream(CacheRequest cacheRequest) throws IOException;
60
61  /** Returns true if the underlying connection can be recycled. */
62  boolean makeReusable(boolean streamCanceled, OutputStream requestBodyOut,
63      InputStream responseBodyIn);
64}
65