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:
- It is created.
- 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.
- 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 Summary
Fields Modifier and Type Field Description protected OkHttpClientclientprotected Connectionconnectionstatic intHTTP_CONTINUEprotected Stringmethodprotected Policypolicyprotected RouteSelectorrouteSelector -
Constructor Summary
Constructors Constructor Description HttpEngine(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders, Connection connection, com.squareup.okhttp.internal.http.RetryableOutputStream requestBodyOut) -
Method Summary
Modifier and Type Method Description protected booleanacceptCacheResponseType(CacheResponse cacheResponse)Returns true ifcacheResponseis of the right type.voidautomaticallyReleaseConnectionToPool()Cause the socket connection to be released to the connection pool when it is no longer needed.protected voidconnect()Connect to the origin server either directly or via a proxy.protected voidconnected(Connection connection)Called after a socket connection has been created or retrieved from the pool.CacheResponsegetCacheResponse()ConnectiongetConnection()static StringgetDefaultUserAgent()static StringgetOriginAddress(URL url)OutputStreamgetRequestBody()Returns the request body or null if this request doesn't have a body.RequestHeadersgetRequestHeaders()InputStreamgetResponseBody()intgetResponseCode()ResponseHeadersgetResponseHeaders()protected TunnelRequestgetTunnelConfig()URIgetUri()booleanhasResponse()booleanhasResponseBody()Returns true if the response must have a (possibly 0-length) body.protected booleanincludeAuthorityInRequestLine()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").voidreadResponse()Flushes the remaining request header and body, parses the HTTP response headers and starts reading the HTTP response body if it exists.voidreceiveHeaders(RawHeaders headers)voidrelease(boolean streamCancelled)Releases this engine so that its resources may be either reused or closed.static StringrequestPath(URL url)Returns the path to request, like the '/' in 'GET / HTTP/1.1'.voidsendRequest()Figures out what the response source will be, and opens a socket to that source if necessary.voidwritingRequestHeaders()Called immediately before the transport transmits HTTP request headers.
-
Field Details
-
HTTP_CONTINUE
public static final int HTTP_CONTINUE- See Also:
- Constant Field Values
-
policy
-
client
-
method
-
connection
-
routeSelector
-
-
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
-
sendRequest
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
Connect to the origin server either directly or via a proxy.- Throws:
IOException
-
connected
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
Returns the request body or null if this request doesn't have a body. -
hasResponse
public final boolean hasResponse() -
getRequestHeaders
-
getResponseHeaders
-
getResponseCode
public final int getResponseCode() -
getResponseBody
-
getCacheResponse
-
getConnection
-
acceptCacheResponseType
Returns true ifcacheResponseis 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 callautomaticallyReleaseConnectionToPoolunless 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
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
-
getOriginAddress
-
readResponse
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
-
receiveHeaders
- Throws:
IOException
-