An HTTP response. This is filled in by the service or do_* methods of a WEBrick
HTTP Servlet.
Creates a new HTTP response object. WEBrick::Config::HTTP is the default configuration.
# File webrick/httpresponse.rb, line 96 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 end
Retrieves the response header field
# File webrick/httpresponse.rb, line 133 def [](field) @header[field.downcase] end
Sets the response header field
to value
# File webrick/httpresponse.rb, line 140 def []=(field, value) @header[field.downcase] = value.to_s end
Enables chunked transfer encoding.
# File webrick/httpresponse.rb, line 191 def chunked=(val) @chunked = val ? true : false end
Will this response body be returned using chunked transfer-encoding?
# File webrick/httpresponse.rb, line 184 def chunked? @chunked end
The content-length header
# File webrick/httpresponse.rb, line 147 def content_length if len = self['content-length'] return Integer(len) end end
Sets the content-length header to len
# File webrick/httpresponse.rb, line 156 def content_length=(len) self['content-length'] = len.to_s end
The content-type header
# File webrick/httpresponse.rb, line 163 def content_type self['content-type'] end
Sets the content-type header to type
# File webrick/httpresponse.rb, line 170 def content_type=(type) self['content-type'] = type end
Iterates over each header in the response
# File webrick/httpresponse.rb, line 177 def each @header.each{|field, value| yield(field, value) } end
Will this response's connection be kept alive?
# File webrick/httpresponse.rb, line 198 def keep_alive? @keep_alive end
Creates an error page for exception ex
with an optional backtrace
# File webrick/httpresponse.rb, line 335 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
Redirects to url
with a WEBrick::HTTPStatus::Redirect
status
.
Example:
res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect
# File webrick/httpresponse.rb, line 326 def set_redirect(status, url) @body = "<HTML><A HREF=\"#{url}\">#{url}</A>.</HTML>\n" @header['location'] = url.to_s raise status end