summaryrefslogtreecommitdiff
path: root/lib/webrick/httpresponse.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/webrick/httpresponse.rb')
-rw-r--r--lib/webrick/httpresponse.rb72
1 files changed, 70 insertions, 2 deletions
diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb
index 0c292be..b7c61a2 100644
--- a/lib/webrick/httpresponse.rb
+++ b/lib/webrick/httpresponse.rb
@@ -15,10 +15,17 @@ require 'webrick/httputils'
require 'webrick/httpstatus'
module WEBrick
+ ##
+ # An HTTP response.
+
class HTTPResponse
attr_reader :http_version, :status, :header
attr_reader :cookies
attr_accessor :reason_phrase
+
+ ##
+ # Body may be a String or IO subclass.
+
attr_accessor :body
attr_accessor :request_method, :request_uri, :request_http_version
@@ -26,6 +33,9 @@ module WEBrick
attr_accessor :keep_alive
attr_reader :config, :sent_size
+ ##
+ # Creates a new HTTP response object
+
def initialize(config)
@config = config
@buffer_size = config[:OutputBufferSize]
@@ -45,57 +55,96 @@ module WEBrick
@sent_size = 0
end
+ ##
+ # The response's HTTP status line
+
def status_line
"HTTP/#@http_version #@status #@reason_phrase #{CRLF}"
end
+ ##
+ # Sets the response's status to the +status+ code
+
def status=(status)
@status = status
@reason_phrase = HTTPStatus::reason_phrase(status)
end
+ ##
+ # Retrieves the response header +field+
+
def [](field)
@header[field.downcase]
end
+ ##
+ # Sets the response header +field+ to +value+
+
def []=(field, value)
@header[field.downcase] = value.to_s
end
+ ##
+ # The content-length header
+
def content_length
if len = self['content-length']
return Integer(len)
end
end
+ ##
+ # Sets the content-length header to +len+
+
def content_length=(len)
self['content-length'] = len.to_s
end
+ ##
+ # The content-type header
+
def content_type
self['content-type']
end
+ ##
+ # Sets the content-type header to +type+
+
def content_type=(type)
self['content-type'] = type
end
+ ##
+ # Iterates over each header in the resopnse
+
def each
- @header.each{|k, v| yield(k, v) }
+ @header.each{|field, value| yield(field, value) }
end
+ ##
+ # Will this response body be returned using chunked transfer-encoding?
+
def chunked?
@chunked
end
+ ##
+ # Enables chunked transfer encoding.
+
def chunked=(val)
@chunked = val ? true : false
end
+ ##
+ # Will this response's connection be kept alive?
+
def keep_alive?
@keep_alive
end
+ ##
+ # Sends the response on +socket+
+
def send_response(socket)
begin
setup_header()
@@ -110,6 +159,9 @@ module WEBrick
end
end
+ ##
+ # Sets up the headers for sending
+
def setup_header()
@reason_phrase ||= HTTPStatus::reason_phrase(@status)
@header['server'] ||= @config[:ServerSoftware]
@@ -165,6 +217,9 @@ module WEBrick
end
end
+ ##
+ # Sends the headers on +socket+
+
def send_header(socket)
if @http_version.major > 0
data = status_line()
@@ -180,6 +235,9 @@ module WEBrick
end
end
+ ##
+ # Sends the body on +socket+
+
def send_body(socket)
case @body
when IO then send_body_io(socket)
@@ -187,18 +245,28 @@ module WEBrick
end
end
- def to_s
+ def to_s # :nodoc:
ret = ""
send_response(ret)
ret
end
+ ##
+ # Redirects to +url+ with a WEBrick::HTTPStatus::Redirect +status+.
+ #
+ # Example:
+ #
+ # res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect
+
def set_redirect(status, url)
@body = "<HTML><A HREF=\"#{url.to_s}\">#{url.to_s}</A>.</HTML>\n"
@header['location'] = url.to_s
raise status
end
+ ##
+ # Creates an error page for exception +ex+ with an optional +backtrace+
+
def set_error(ex, backtrace=false)
case ex
when HTTPStatus::Status