-
- All Implemented Interfaces:
-
com.mobwith.httpmodule.internal.http.HttpCodec
public final class Http1Codec implements HttpCodec
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-length or chunked.
- Write to and then close that sink.
- Read response headers.
- Open a source to read the response body. Either fixed-length, chunked or unknown 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 Http1Codec(MobonOkHttpClient client, StreamAllocation streamAllocation, BufferedSource source, BufferedSink sink)
-
Method Summary
Modifier and Type Method Description SinkcreateRequestBody(MobonRequest mobonRequest, long contentLength)Returns an output stream where the mobonRequest body can be streamed. voidcancel()Cancel this stream. voidwriteRequestHeaders(MobonRequest mobonRequest)Prepares the HTTP headers and sends them to the server. ResponseBodyopenResponseBody(MobonResponse mobonResponse)Returns a stream that reads the mobonResponse body. booleanisClosed()Returns true if this connection is closed. voidflushRequest()Flush the request to the underlying socket. voidfinishRequest()Flush the request to the underlying socket and signal no more bytes will be transmitted. voidwriteRequest(Headers headers, String requestLine)Returns bytes of a request header for sending on an HTTP transport. MobonResponse.BuilderreadResponseHeaders(boolean expectContinue)Parses bytes of a response header from an HTTP transport. HeadersreadHeaders()Reads headers or trailers. SinknewChunkedSink()SinknewFixedLengthSink(long contentLength)SourcenewFixedLengthSource(long length)SourcenewChunkedSource(HttpUrl url)SourcenewUnknownLengthSource()-
-
Constructor Detail
-
Http1Codec
Http1Codec(MobonOkHttpClient client, StreamAllocation streamAllocation, BufferedSource source, BufferedSink sink)
-
-
Method Detail
-
createRequestBody
Sink createRequestBody(MobonRequest mobonRequest, long contentLength)
Returns an output stream where the mobonRequest body can be streamed.
-
cancel
void cancel()
Cancel this stream. Resources held by this stream will be cleaned up, though not synchronously.That may happen later by the connection pool thread.
-
writeRequestHeaders
void writeRequestHeaders(MobonRequest mobonRequest)
Prepares the HTTP headers and sends them to the server.
For streaming requests with a body, headers must be prepared before theoutput stream has been written to. Otherwise the body would need to be buffered!
For non-streaming requests with a body, headers must be prepared after theoutput stream has been written to and closed. This ensures that the
{@code Content-Length}header field receives the proper value.
-
openResponseBody
ResponseBody openResponseBody(MobonResponse mobonResponse)
Returns a stream that reads the mobonResponse body.
-
isClosed
boolean isClosed()
Returns true if this connection is closed.
-
flushRequest
void flushRequest()
Flush the request to the underlying socket.
-
finishRequest
void finishRequest()
Flush the request to the underlying socket and signal no more bytes will be transmitted.
-
writeRequest
void writeRequest(Headers headers, String requestLine)
Returns bytes of a request header for sending on an HTTP transport.
-
readResponseHeaders
MobonResponse.Builder readResponseHeaders(boolean expectContinue)
Parses bytes of a response header from an HTTP transport.
- Parameters:
expectContinue- true to return null if this is an intermediate response with a "100"response code.
-
readHeaders
Headers readHeaders()
Reads headers or trailers.
-
newChunkedSink
Sink newChunkedSink()
-
newFixedLengthSink
Sink newFixedLengthSink(long contentLength)
-
newFixedLengthSource
Source newFixedLengthSource(long length)
-
newChunkedSource
Source newChunkedSource(HttpUrl url)
-
newUnknownLengthSource
Source newUnknownLengthSource()
-
-
-
-