Class HttpEngine

java.lang.Object
com.squareup.okhttp.internal.http.HttpEngine
Direct Known Subclasses:
HttpsEngine

public class HttpEngine
extends Object
Handles a single HTTP request/response pair. Each HTTP engine follows this lifecycle:
  1. It is created.
  2. The HTTP request message is sent with sendRequest(). Once the request is sent it is an error to modify the request headers. After sendRequest() has been called the request body can be written to if it exists.
  3. The HTTP response message is read with readResponse(). After the response has been read the response headers and body can be read. All responses have a response body input stream, though in some instances this stream is empty.

The request and response may be served by the HTTP response cache, by the network, or by both in the event of a conditional GET.

This class may hold a socket connection that needs to be released or recycled. By default, this socket connection is held when the last byte of the response is consumed. To release the connection when it is no longer required, use automaticallyReleaseConnectionToPool().

  • Field Details

  • Constructor Details

    • HttpEngine

      public HttpEngine​(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders, Connection connection, com.squareup.okhttp.internal.http.RetryableOutputStream requestBodyOut) throws IOException
      Parameters:
      requestHeaders - the client's supplied request headers. This class creates a private copy that it can mutate.
      connection - the connection used for an intermediate response immediately prior to this request/response pair, such as a same-host redirect. This engine assumes ownership of the connection and must release it when it is unneeded.
      Throws:
      IOException
  • Method Details

    • getUri

      public URI getUri()
    • sendRequest

      public final void sendRequest() throws IOException
      Figures out what the response source will be, and opens a socket to that source if necessary. Prepares the request headers and gets ready to start writing the request body if it exists.
      Throws:
      IOException
    • connect

      protected final void connect() throws IOException
      Connect to the origin server either directly or via a proxy.
      Throws:
      IOException
    • connected

      protected void connected​(Connection connection)
      Called after a socket connection has been created or retrieved from the pool. Subclasses use this hook to get a reference to the TLS data.
    • writingRequestHeaders

      public void writingRequestHeaders()
      Called immediately before the transport transmits HTTP request headers. This is used to observe the sent time should the request be cached.
    • getRequestBody

      public final OutputStream getRequestBody()
      Returns the request body or null if this request doesn't have a body.
    • hasResponse

      public final boolean hasResponse()
    • getRequestHeaders

      public final RequestHeaders getRequestHeaders()
    • getResponseHeaders

      public final ResponseHeaders getResponseHeaders()
    • getResponseCode

      public final int getResponseCode()
    • getResponseBody

      public final InputStream getResponseBody()
    • getCacheResponse

      public final CacheResponse getCacheResponse()
    • getConnection

      public final Connection getConnection()
    • acceptCacheResponseType

      protected boolean acceptCacheResponseType​(CacheResponse cacheResponse)
      Returns true if cacheResponse is of the right type. This condition is necessary but not sufficient for the cached response to be used.
    • automaticallyReleaseConnectionToPool

      public final void automaticallyReleaseConnectionToPool()
      Cause the socket connection to be released to the connection pool when it is no longer needed. If it is already unneeded, it will be pooled immediately. Otherwise the connection is held so that redirects can be handled by the same connection.
    • release

      public final void release​(boolean streamCancelled)
      Releases this engine so that its resources may be either reused or closed. Also call automaticallyReleaseConnectionToPool unless the connection will be used to follow a redirect.
    • hasResponseBody

      public final boolean hasResponseBody()
      Returns true if the response must have a (possibly 0-length) body. See RFC 2616 section 4.3.
    • requestPath

      public static String requestPath​(URL url)
      Returns the path to request, like the '/' in 'GET / HTTP/1.1'. Never empty, even if the request URL is. Includes the query component if it exists.
    • includeAuthorityInRequestLine

      protected boolean includeAuthorityInRequestLine()
      Returns true if the request line should contain the full URL with host and port (like "GET http://android.com/foo HTTP/1.1") or only the path (like "GET /foo HTTP/1.1").

      This is non-final because for HTTPS it's never necessary to supply the full URL, even if a proxy is in use.

    • getDefaultUserAgent

      public static String getDefaultUserAgent()
    • getOriginAddress

      public static String getOriginAddress​(URL url)
    • readResponse

      public final void readResponse() throws IOException
      Flushes the remaining request header and body, parses the HTTP response headers and starts reading the HTTP response body if it exists.
      Throws:
      IOException
    • getTunnelConfig

      protected TunnelRequest getTunnelConfig()
    • receiveHeaders

      public void receiveHeaders​(RawHeaders headers) throws IOException
      Throws:
      IOException