class WEBrick::HTTPResponse
An HTTP response. This is filled in by the service or do_* methods of a WEBrick HTTP Servlet.
Attributes
Body may be:
-
a
String; -
an IO-like object that responds to
#readand#readpartial; -
a Proc-like object that responds to
#call.
In the latter case, either chunked= should be set to true, or header['content-length'] explicitly provided. Example:
server.mount_proc '/' do |req, res| res.chunked = true # or # res.header['content-length'] = 10 res.body = proc { |out| out.write(Time.now.to_s) } end
Hash[Symbol, untyped]
Configuration for this response
Filename of the static file in this response. Only used by the FileHandler servlet.
Response header
HTTP Response version
bool
Is this a keep-alive response?
Response reason phrase (“OK”)
Request HTTP version for this response
Request method for this response
Request URI for this response
Bytes sent in this response
Response status code (200)
Set the response body proc as an streaming/upgrade response.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 117 def initialize(config) @config = config @buffer_size = config[:OutputBufferSize] @logger = config[:Logger] @header = Hash.new @status = HTTPStatus::RC_OK @reason_phrase = nil @http_version = HTTPVersion::convert(@config[:HTTPVersion]) @body = +"" @keep_alive = true @cookies = [] @request_method = nil @request_uri = nil @request_http_version = @http_version # temporary @chunked = false @filename = nil @sent_size = 0 @bodytempfile = nil end
Creates a new HTTP response object. WEBrick::Config::HTTP is the default configuration.
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 155 def [](field) @header[field.downcase] end
Retrieves the response header field
(String field, _ToS value) → _ToS
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 162 def []=(field, value) @chunked = value.to_s.downcase == 'chunked' if field.downcase == 'transfer-encoding' @header[field.downcase] = value.to_s end
Sets the response header field to value
[T] (T socket, _ToS data) → T
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 115
def _write_data: [T] (T socket, _ToS data) -> T
preserved for compatibility with some 3rd-party handlers
(boolish val) → boolish
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 214 def chunked=(val) @chunked = val ? true : false end
Enables chunked transfer encoding.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 207 def chunked? @chunked end
Will this response body be returned using chunked transfer-encoding?
() → Integer?
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 170 def content_length if len = self['content-length'] return Integer(len) end end
The content-length header
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 179 def content_length=(len) self['content-length'] = len.to_s end
Sets the content-length header to len
() → String?
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 186 def content_type self['content-type'] end
The content-type header
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 193 def content_type=(type) self['content-type'] = type end
Sets the content-type header to type
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 200 def each @header.each{|field, value| yield(field, value) } end
Iterates over each header in the response
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 94
def error_body: (bool backtrace, singleton(Exception) ex, String? host, Integer? port) -> void
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 221 def keep_alive? @keep_alive end
Will this response’s connection be kept alive?
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 78
def make_body_tempfile: () -> void
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 80
def remove_body_tempfile: () -> void
(_Writer socket) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 84
def send_body: (_Writer socket) -> void
(_Writer socket) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 96
def send_body_io: (_Writer socket) -> void
(_Writer socket) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 100
def send_body_proc: (_Writer socket) -> void
(_Writer socket) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 98
def send_body_string: (_Writer socket) -> void
(_Writer socket) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 82
def send_header: (_Writer socket) -> void
(_Writer socket) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 74
def send_response: (_Writer socket) -> void
(singleton(Exception) ex, ?bool backtrace) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 405 def set_error(ex, backtrace=false) case ex when HTTPStatus::Status @keep_alive = false if HTTPStatus::error?(ex.code) self.status = ex.code else @keep_alive = false self.status = HTTPStatus::RC_INTERNAL_SERVER_ERROR end @header['content-type'] = "text/html; charset=ISO-8859-1" if respond_to?(:create_error_page) create_error_page() return end if @request_uri host, port = @request_uri.host, @request_uri.port else host, port = @config[:ServerName], @config[:Port] end error_body(backtrace, ex, host, port) end
Creates an error page for exception ex with an optional backtrace
(singleton(WEBrick::HTTPStatus::Redirect) status, URI::Generic | String url) → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 395 def set_redirect(status, url) url = URI(url).to_s @body = "<HTML><A HREF=\"#{url}\">#{url}</A>.</HTML>\n" @header['location'] = url raise status end
Redirects to url with a WEBrick::HTTPStatus::Redirect status.
Example:
res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/sig/httpresponse.rbs, line 76
def setup_header: () -> void
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 147 def status=(status) @status = status @reason_phrase = HTTPStatus::reason_phrase(status) end
Sets the response’s status to the status code
() → String
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 140 def status_line "HTTP/#@http_version #@status #@reason_phrase".rstrip << CRLF end
The response’s HTTP status line
(String protocol) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb, line 229 def upgrade!(protocol) @upgrade = protocol @keep_alive = false @chunked = false end
Sets the response to be a streaming/upgrade response. This will disable keep-alive and chunked transfer encoding.