Class HttpConnection
- java.lang.Object
-
- com.squareup.okhttp.internal.http.HttpConnection
-
public final class HttpConnection extends Object
A socket connection that can be used to send HTTP/1.1 messages. This class strictly enforces the following lifecycle:Send request headers.- Open a sink to write the request body. Either
fixed-lengthorchunked. - Write to and then close that sink.
Read response headers.- Open a source to read the response body. Either
fixed-length,chunkedorunknown length. - Read from and close that source.
Exchanges that do not have a request body may skip creating and closing the request body. Exchanges that do not have a response body can call
newFixedLengthSource(0)and may skip reading and closing that source.
-
-
Constructor Summary
Constructors Constructor Description HttpConnection(ConnectionPool pool, Connection connection, Socket socket)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longbufferSize()Returns the number of buffered bytes immediately readable.voidcloseIfOwnedBy(Object owner)voidcloseOnIdle()Configure this connection to close itself when the HTTP response body is exhausted.voidflush()booleanisClosed()Returns true if this connection is closed.booleanisReadable()Test for a stale socket.SinknewChunkedSink()SourcenewChunkedSource(HttpEngine httpEngine)SinknewFixedLengthSink(long contentLength)SourcenewFixedLengthSource(long length)SourcenewUnknownLengthSource()voidpoolOnIdle()Configure this connection to put itself back into the connection pool when the HTTP response body is exhausted.BufferedSinkrawSink()BufferedSourcerawSource()voidreadHeaders(Headers.Builder builder)Reads headers or trailers intobuilder.Response.BuilderreadResponse()Parses bytes of a response header from an HTTP transport.voidsetTimeouts(int readTimeoutMillis, int writeTimeoutMillis)voidwriteRequest(Headers headers, String requestLine)Returns bytes of a request header for sending on an HTTP transport.voidwriteRequestBody(RetryableSink requestBody)
-
-
-
Constructor Detail
-
HttpConnection
public HttpConnection(ConnectionPool pool, Connection connection, Socket socket) throws IOException
- Throws:
IOException
-
-
Method Detail
-
setTimeouts
public void setTimeouts(int readTimeoutMillis, int writeTimeoutMillis)
-
poolOnIdle
public void poolOnIdle()
Configure this connection to put itself back into the connection pool when the HTTP response body is exhausted.
-
closeOnIdle
public void closeOnIdle() throws IOExceptionConfigure this connection to close itself when the HTTP response body is exhausted.- Throws:
IOException
-
isClosed
public boolean isClosed()
Returns true if this connection is closed.
-
closeIfOwnedBy
public void closeIfOwnedBy(Object owner) throws IOException
- Throws:
IOException
-
flush
public void flush() throws IOException- Throws:
IOException
-
bufferSize
public long bufferSize()
Returns the number of buffered bytes immediately readable.
-
isReadable
public boolean isReadable()
Test for a stale socket.
-
writeRequest
public void writeRequest(Headers headers, String requestLine) throws IOException
Returns bytes of a request header for sending on an HTTP transport.- Throws:
IOException
-
readResponse
public Response.Builder readResponse() throws IOException
Parses bytes of a response header from an HTTP transport.- Throws:
IOException
-
readHeaders
public void readHeaders(Headers.Builder builder) throws IOException
Reads headers or trailers intobuilder.- Throws:
IOException
-
newChunkedSink
public Sink newChunkedSink()
-
newFixedLengthSink
public Sink newFixedLengthSink(long contentLength)
-
writeRequestBody
public void writeRequestBody(RetryableSink requestBody) throws IOException
- Throws:
IOException
-
newFixedLengthSource
public Source newFixedLengthSource(long length) throws IOException
- Throws:
IOException
-
newChunkedSource
public Source newChunkedSource(HttpEngine httpEngine) throws IOException
- Throws:
IOException
-
newUnknownLengthSource
public Source newUnknownLengthSource() throws IOException
- Throws:
IOException
-
rawSink
public BufferedSink rawSink()
-
rawSource
public BufferedSource rawSource()
-
-