class HTTP::Server::Response
inherits IO
¶
The response to configure and write to in an HTTP::Server handler.
The response status and headers must be configured before writing
the response body. Once response output is written, changing the status
and headers properties has no effect.
The HTTP::Server::Response is a write-only IO, so all IO methods are available
in it.
A response can be upgraded with the upgrade method. Once invoked, headers
are written and the connection IO (a socket) is yielded to the given block.
This is useful to implement protocol upgrades, such as websockets.
Methods¶
#close
¶
Closes this response, writing headers and body if not done yet. This method must be implemented if wrapping the response output.
#content_length=(content_length : Int)
¶
(content_length : Int)
Convenience method to set the Content-Length header.
#content_type=(content_type : String)
¶
(content_type : String)
Convenience method to set the Content-Type header.
#flush
¶
Flushes the output. This method must be implemented if wrapping the response output.
#headers : HTTP::Headers
¶
: HTTP::Headers
The response headers (HTTP::Headers). These must be set before writing to the response.
#output : IO
¶
: IO
The IO to which output is written. This can be changed/wrapped to filter
the response body (for example to compress the output).
#output=(output : IO)
¶
(output : IO)
The IO to which output is written. This can be changed/wrapped to filter
the response body (for example to compress the output).
#respond_with_status(status : HTTP::Status, message : String? = nil)
¶
(status : HTTP::Status, message : String? = nil)
Sends status and message as response.
This method calls #reset to remove any previous settings and writes the
given status and message to the response IO. Finally, it closes the
response.
If message is nil, the default message for status is used provided
by HTTP::Status#description.
Raises IO::Error if the response is closed or headers were already
sent.
#respond_with_status(status : Int, message : String? = nil)
¶
(status : Int, message : String? = nil)
Sends status and message as response.
This method calls #reset to remove any previous settings and writes the
given status and message to the response IO. Finally, it closes the
response.
If message is nil, the default message for status is used provided
by HTTP::Status#description.
Raises IO::Error if the response is closed or headers were already
sent.
#status : HTTP::Status
¶
: HTTP::Status
The status code of this response, which must be set before writing the response body. If not set, the default value is 200 (OK).
#status=(status : HTTP::Status)
¶
(status : HTTP::Status)
The status code of this response, which must be set before writing the response body. If not set, the default value is 200 (OK).
#upgrade(&block : IO -> )
¶
(&block : IO -> )
Upgrades this response, writing headers and yielding the connection IO (a socket) to the given block.
This is useful to implement protocol upgrades, such as websockets.