An HTTP response. This is filled in by the service or do_* methods of a WEBrick HTTP Servlet.
Body may be:
-
a
String; -
an IO-like object that responds to read and 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
Configuration for this response
Response cookies
Filename of the static file in this response. Only used by the FileHandler servlet.
Response header
HTTP Response version
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)
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 112
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.
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 150
def [](field)
@header[field.downcase]
end
Retrieves the response header field
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 157
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
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 410
def check_header(header_value)
header_value = header_value.to_s
if /[\r\n]/ =~ header_value
raise InvalidHeader
else
header_value
end
end
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 209
def chunked=(val)
@chunked = val ? true : false
end
Enables chunked transfer encoding.
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 202
def chunked?
@chunked
end
Will this response body be returned using chunked transfer-encoding?
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 165
def content_length
if len = self['content-length']
return Integer(len)
end
end
The content-length header
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 174
def content_length=(len)
self['content-length'] = len.to_s
end
Sets the content-length header to len
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 181
def content_type
self['content-type']
end
The content-type header
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 188
def content_type=(type)
self['content-type'] = type
end
Sets the content-type header to type
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 195
def each
@header.each{|field, value| yield(field, value) }
end
Iterates over each header in the response
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 216
def keep_alive?
@keep_alive
end
Will this response’s connection be kept alive?
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 383
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
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 373
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
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 142
def status=(status)
@status = status
@reason_phrase = HTTPStatus::reason_phrase(status)
end
Sets the response’s status to the status code
# File tmp/rubies/ruby-2.7.6/lib/webrick/httpresponse.rb, line 135
def status_line
"HTTP/#@http_version #@status #@reason_phrase".rstrip << CRLF
end
The response’s HTTP status line