java.lang.Object
org.miaixz.bus.http.metric.http.Http1Codec
- All Implemented Interfaces:
HttpCodec
A socket connection that can be used to send HTTP/1.1 messages. This class strictly enforces the following lifecycle:
Exchanges that do not have a request body can 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.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields inherited from interface org.miaixz.bus.http.metric.http.HttpCodec
DISCARD_STREAM_TIMEOUT_MILLIS -
Constructor Summary
ConstructorsConstructorDescriptionHttp1Codec(Httpd httpd, RealConnection realConnection, org.miaixz.bus.core.io.source.BufferSource source, org.miaixz.bus.core.io.sink.BufferSink sink) -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancels this stream.Returns the connection that carries this codec.org.miaixz.bus.core.io.sink.SinkcreateRequestBody(Request request, long contentLength) Returns an output stream that can stream the request body.voidFlushes the request to the underlying socket, indicating that no more bytes will be transmitted.voidFlushes the request to the underlying socket.booleanisClosed()Returns true if this connection is closed.org.miaixz.bus.core.io.source.SourceopenResponseBodySource(Response response) Opens a source to read the response body.readResponseHeaders(boolean expectContinue) Parses the response headers from the HTTP transport.longreportedContentLength(Response response) Returns the reported content length of the response.voidskipConnectBody(Response response) The response body from a CONNECT should be empty, but if it is not then we should consume it before proceeding.trailers()Returns the trailer headers after the HTTP response.voidwriteRequest(Headers headers, String requestLine) Returns bytes of a request header for sending on an HTTP transport.voidwriteRequestHeaders(Request request) Prepares the HTTP headers and sends them to the server.
-
Constructor Details
-
Http1Codec
public Http1Codec(Httpd httpd, RealConnection realConnection, org.miaixz.bus.core.io.source.BufferSource source, org.miaixz.bus.core.io.sink.BufferSink sink)
-
-
Method Details
-
connection
Description copied from interface:HttpCodecReturns the connection that carries this codec.- Specified by:
connectionin interfaceHttpCodec- Returns:
- The real connection.
-
createRequestBody
public org.miaixz.bus.core.io.sink.Sink createRequestBody(Request request, long contentLength) throws IOException Description copied from interface:HttpCodecReturns an output stream that can stream the request body.- Specified by:
createRequestBodyin interfaceHttpCodec- Parameters:
request- The network request.contentLength- The content length of the request body.- Returns:
- A sink for the request body.
- Throws:
IOException- if an I/O error occurs.
-
cancel
public void cancel()Description copied from interface:HttpCodecCancels this stream. Resources held by this stream will be cleaned up, though not necessarily synchronously. This may happen after the connection pool thread. -
writeRequestHeaders
Prepares the HTTP headers and sends them to the server. For streaming requests with a body, headers must be prepared before the output 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 the output stream has been written to and closed. This ensures that the
Content-Lengthheader field receives the proper value.- Specified by:
writeRequestHeadersin interfaceHttpCodec- Parameters:
request- The network request.- Throws:
IOException- if an I/O error occurs.
-
reportedContentLength
Description copied from interface:HttpCodecReturns the reported content length of the response.- Specified by:
reportedContentLengthin interfaceHttpCodec- Parameters:
response- The response.- Returns:
- The reported content length.
-
openResponseBodySource
Description copied from interface:HttpCodecOpens a source to read the response body.- Specified by:
openResponseBodySourcein interfaceHttpCodec- Parameters:
response- The response.- Returns:
- A source for the response body.
-
trailers
Description copied from interface:HttpCodecReturns the trailer headers after the HTTP response. -
isClosed
public boolean isClosed()Returns true if this connection is closed.- Returns:
- true if this connection is closed.
-
flushRequest
Description copied from interface:HttpCodecFlushes the request to the underlying socket.- Specified by:
flushRequestin interfaceHttpCodec- Throws:
IOException- if an I/O error occurs.
-
finishRequest
Description copied from interface:HttpCodecFlushes the request to the underlying socket, indicating that no more bytes will be transmitted.- Specified by:
finishRequestin interfaceHttpCodec- Throws:
IOException- if an I/O error occurs.
-
writeRequest
Returns bytes of a request header for sending on an HTTP transport.- Parameters:
headers- The headers to write.requestLine- The request line to write.- Throws:
IOException- if an I/O error occurs.
-
readResponseHeaders
Description copied from interface:HttpCodecParses the response headers from the HTTP transport.- Specified by:
readResponseHeadersin interfaceHttpCodec- Parameters:
expectContinue- If this is an intermediate response with a "100" response code, this returns null. Otherwise, this method will never return null.- Returns:
- A response builder.
- Throws:
IOException- if an I/O error occurs.
-
skipConnectBody
The response body from a CONNECT should be empty, but if it is not then we should consume it before proceeding.- Parameters:
response- The response from the CONNECT request.- Throws:
IOException- if an I/O error occurs.
-