Module bus.http

Class Http1Codec

java.lang.Object
org.miaixz.bus.http.metric.http.Http1Codec
All Implemented Interfaces:
HttpCodec

public class Http1Codec extends Object implements 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
  • 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

      public RealConnection connection()
      Description copied from interface: HttpCodec
      Returns the connection that carries this codec.
      Specified by:
      connection in interface HttpCodec
      Returns:
      The real connection.
    • createRequestBody

      public org.miaixz.bus.core.io.sink.Sink createRequestBody(Request request, long contentLength) throws IOException
      Description copied from interface: HttpCodec
      Returns an output stream that can stream the request body.
      Specified by:
      createRequestBody in interface HttpCodec
      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: HttpCodec
      Cancels this stream. Resources held by this stream will be cleaned up, though not necessarily synchronously. This may happen after the connection pool thread.
      Specified by:
      cancel in interface HttpCodec
    • writeRequestHeaders

      public void writeRequestHeaders(Request request) throws IOException
      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-Length header field receives the proper value.

      Specified by:
      writeRequestHeaders in interface HttpCodec
      Parameters:
      request - The network request.
      Throws:
      IOException - if an I/O error occurs.
    • reportedContentLength

      public long reportedContentLength(Response response)
      Description copied from interface: HttpCodec
      Returns the reported content length of the response.
      Specified by:
      reportedContentLength in interface HttpCodec
      Parameters:
      response - The response.
      Returns:
      The reported content length.
    • openResponseBodySource

      public org.miaixz.bus.core.io.source.Source openResponseBodySource(Response response)
      Description copied from interface: HttpCodec
      Opens a source to read the response body.
      Specified by:
      openResponseBodySource in interface HttpCodec
      Parameters:
      response - The response.
      Returns:
      A source for the response body.
    • trailers

      public Headers trailers()
      Description copied from interface: HttpCodec
      Returns the trailer headers after the HTTP response.
      Specified by:
      trailers in interface HttpCodec
      Returns:
      The trailer headers.
    • isClosed

      public boolean isClosed()
      Returns true if this connection is closed.
      Returns:
      true if this connection is closed.
    • flushRequest

      public void flushRequest() throws IOException
      Description copied from interface: HttpCodec
      Flushes the request to the underlying socket.
      Specified by:
      flushRequest in interface HttpCodec
      Throws:
      IOException - if an I/O error occurs.
    • finishRequest

      public void finishRequest() throws IOException
      Description copied from interface: HttpCodec
      Flushes the request to the underlying socket, indicating that no more bytes will be transmitted.
      Specified by:
      finishRequest in interface HttpCodec
      Throws:
      IOException - if an I/O error occurs.
    • writeRequest

      public void writeRequest(Headers headers, String requestLine) throws IOException
      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

      public Response.Builder readResponseHeaders(boolean expectContinue) throws IOException
      Description copied from interface: HttpCodec
      Parses the response headers from the HTTP transport.
      Specified by:
      readResponseHeaders in interface HttpCodec
      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

      public void skipConnectBody(Response response) throws IOException
      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.