2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# httpresponse.rb -- HTTPResponse Class
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: httpresponse.rb,v 1.45 2003/07/11 11:02:25 gotoyuzo Exp $
|
|
|
|
|
|
|
|
require 'time'
|
|
|
|
require 'webrick/httpversion'
|
|
|
|
require 'webrick/htmlutils'
|
|
|
|
require 'webrick/httputils'
|
|
|
|
require 'webrick/httpstatus'
|
|
|
|
|
|
|
|
module WEBrick
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# An HTTP response.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class HTTPResponse
|
|
|
|
attr_reader :http_version, :status, :header
|
|
|
|
attr_reader :cookies
|
|
|
|
attr_accessor :reason_phrase
|
2011-05-09 20:13:58 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Body may be a String or IO subclass.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_accessor :body
|
|
|
|
|
|
|
|
attr_accessor :request_method, :request_uri, :request_http_version
|
|
|
|
attr_accessor :filename
|
2003-11-25 11:02:45 -05:00
|
|
|
attr_accessor :keep_alive
|
|
|
|
attr_reader :config, :sent_size
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Creates a new HTTP response object
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def initialize(config)
|
|
|
|
@config = config
|
2006-05-18 09:42:52 -04:00
|
|
|
@buffer_size = config[:OutputBufferSize]
|
2003-07-23 12:51:36 -04:00
|
|
|
@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
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The response's HTTP status line
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def status_line
|
|
|
|
"HTTP/#@http_version #@status #@reason_phrase #{CRLF}"
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sets the response's status to the +status+ code
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def status=(status)
|
|
|
|
@status = status
|
|
|
|
@reason_phrase = HTTPStatus::reason_phrase(status)
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Retrieves the response header +field+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def [](field)
|
|
|
|
@header[field.downcase]
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sets the response header +field+ to +value+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def []=(field, value)
|
|
|
|
@header[field.downcase] = value.to_s
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The content-length header
|
|
|
|
|
2004-10-12 08:26:39 -04:00
|
|
|
def content_length
|
|
|
|
if len = self['content-length']
|
|
|
|
return Integer(len)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sets the content-length header to +len+
|
|
|
|
|
2004-10-12 08:26:39 -04:00
|
|
|
def content_length=(len)
|
|
|
|
self['content-length'] = len.to_s
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The content-type header
|
|
|
|
|
2004-10-12 08:26:39 -04:00
|
|
|
def content_type
|
|
|
|
self['content-type']
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sets the content-type header to +type+
|
|
|
|
|
2004-10-12 08:26:39 -04:00
|
|
|
def content_type=(type)
|
|
|
|
self['content-type'] = type
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Iterates over each header in the resopnse
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def each
|
2011-05-09 20:13:58 -04:00
|
|
|
@header.each{|field, value| yield(field, value) }
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Will this response body be returned using chunked transfer-encoding?
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def chunked?
|
|
|
|
@chunked
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Enables chunked transfer encoding.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def chunked=(val)
|
|
|
|
@chunked = val ? true : false
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Will this response's connection be kept alive?
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def keep_alive?
|
|
|
|
@keep_alive
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sends the response on +socket+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def send_response(socket)
|
|
|
|
begin
|
|
|
|
setup_header()
|
|
|
|
send_header(socket)
|
|
|
|
send_body(socket)
|
2003-12-03 19:12:14 -05:00
|
|
|
rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ENOTCONN => ex
|
|
|
|
@logger.debug(ex)
|
2003-07-23 12:51:36 -04:00
|
|
|
@keep_alive = false
|
2003-12-03 19:12:14 -05:00
|
|
|
rescue Exception => ex
|
2003-07-23 12:51:36 -04:00
|
|
|
@logger.error(ex)
|
|
|
|
@keep_alive = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sets up the headers for sending
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def setup_header()
|
|
|
|
@reason_phrase ||= HTTPStatus::reason_phrase(@status)
|
|
|
|
@header['server'] ||= @config[:ServerSoftware]
|
|
|
|
@header['date'] ||= Time.now.httpdate
|
|
|
|
|
|
|
|
# HTTP/0.9 features
|
|
|
|
if @request_http_version < "1.0"
|
|
|
|
@http_version = HTTPVersion.new("0.9")
|
|
|
|
@keep_alive = false
|
|
|
|
end
|
|
|
|
|
|
|
|
# HTTP/1.0 features
|
|
|
|
if @request_http_version < "1.1"
|
|
|
|
if chunked?
|
|
|
|
@chunked = false
|
|
|
|
ver = @request_http_version.to_s
|
|
|
|
msg = "chunked is set for an HTTP/#{ver} request. (ignored)"
|
|
|
|
@logger.warn(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-04 05:37:38 -04:00
|
|
|
# Determine the message length (RFC2616 -- 4.4 Message Length)
|
2003-07-23 12:51:36 -04:00
|
|
|
if @status == 304 || @status == 204 || HTTPStatus::info?(@status)
|
|
|
|
@header.delete('content-length')
|
|
|
|
@body = ""
|
|
|
|
elsif chunked?
|
|
|
|
@header["transfer-encoding"] = "chunked"
|
|
|
|
@header.delete('content-length')
|
|
|
|
elsif %r{^multipart/byteranges} =~ @header['content-type']
|
|
|
|
@header.delete('content-length')
|
|
|
|
elsif @header['content-length'].nil?
|
|
|
|
unless @body.is_a?(IO)
|
2008-11-08 04:41:24 -05:00
|
|
|
@header['content-length'] = @body ? @body.bytesize : 0
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Keep-Alive connection.
|
|
|
|
if @header['connection'] == "close"
|
|
|
|
@keep_alive = false
|
2003-12-22 16:13:06 -05:00
|
|
|
elsif keep_alive?
|
2003-07-23 12:51:36 -04:00
|
|
|
if chunked? || @header['content-length']
|
|
|
|
@header['connection'] = "Keep-Alive"
|
2011-06-21 08:58:37 -04:00
|
|
|
else
|
|
|
|
msg = "Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true"
|
|
|
|
@logger.warn(msg)
|
|
|
|
@header['connection'] = "close"
|
|
|
|
@keep_alive = false
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
2003-12-22 16:13:06 -05:00
|
|
|
else
|
|
|
|
@header['connection'] = "close"
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Location is a single absoluteURI.
|
|
|
|
if location = @header['location']
|
|
|
|
if @request_uri
|
|
|
|
@header['location'] = @request_uri.merge(location)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sends the headers on +socket+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def send_header(socket)
|
|
|
|
if @http_version.major > 0
|
|
|
|
data = status_line()
|
|
|
|
@header.each{|key, value|
|
* ext/json/lib/json/pure/generator.rb,
ext/json/lib/json/pure/parser.rb, ext/openssl/lib/openssl/x509.rb,
ext/win32ole/sample/olegen.rb, lib/date/format.rb, lib/irb/context.rb,
lib/irb/workspace.rb, lib/net/http.rb, lib/net/imap.rb,
lib/rdoc/generator.rb, lib/rdoc/markup/to_html.rb,
lib/rdoc/markup/to_latex.rb, lib/rdoc/parsers/parse_c.rb,
lib/rdoc/ri/formatter.rb, lib/rexml/parsers/baseparser.rb,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rss/parser.rb,
lib/uri/common.rb, lib/uri/generic.rb, lib/webrick/httpresponse.rb,
lib/webrick/httpservlet/filehandler.rb, lib/yaml/baseemitter.rb,
lib/yaml/encoding.rb: performance tuning arround String#gsub.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-02-12 01:18:06 -05:00
|
|
|
tmp = key.gsub(/\bwww|^te$|\b\w/){ $&.upcase }
|
2003-07-23 12:51:36 -04:00
|
|
|
data << "#{tmp}: #{value}" << CRLF
|
|
|
|
}
|
|
|
|
@cookies.each{|cookie|
|
|
|
|
data << "Set-Cookie: " << cookie.to_s << CRLF
|
|
|
|
}
|
|
|
|
data << CRLF
|
|
|
|
_write_data(socket, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Sends the body on +socket+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def send_body(socket)
|
|
|
|
case @body
|
|
|
|
when IO then send_body_io(socket)
|
|
|
|
else send_body_string(socket)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
def to_s # :nodoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
ret = ""
|
|
|
|
send_response(ret)
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Redirects to +url+ with a WEBrick::HTTPStatus::Redirect +status+.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
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
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Creates an error page for exception +ex+ with an optional +backtrace+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def set_error(ex, backtrace=false)
|
|
|
|
case ex
|
2009-03-05 22:56:38 -05:00
|
|
|
when HTTPStatus::Status
|
2003-07-23 12:51:36 -04:00
|
|
|
@keep_alive = false if HTTPStatus::error?(ex.code)
|
|
|
|
self.status = ex.code
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2003-07-23 12:51:36 -04:00
|
|
|
@keep_alive = false
|
|
|
|
self.status = HTTPStatus::RC_INTERNAL_SERVER_ERROR
|
|
|
|
end
|
2010-08-15 23:41:12 -04:00
|
|
|
@header['content-type'] = "text/html; charset=ISO-8859-1"
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
@body = ''
|
|
|
|
@body << <<-_end_of_html_
|
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
|
|
|
<HTML>
|
|
|
|
<HEAD><TITLE>#{HTMLUtils::escape(@reason_phrase)}</TITLE></HEAD>
|
|
|
|
<BODY>
|
|
|
|
<H1>#{HTMLUtils::escape(@reason_phrase)}</H1>
|
|
|
|
#{HTMLUtils::escape(ex.message)}
|
|
|
|
<HR>
|
|
|
|
_end_of_html_
|
|
|
|
|
|
|
|
if backtrace && $DEBUG
|
|
|
|
@body << "backtrace of `#{HTMLUtils::escape(ex.class.to_s)}' "
|
|
|
|
@body << "#{HTMLUtils::escape(ex.message)}"
|
|
|
|
@body << "<PRE>"
|
|
|
|
ex.backtrace.each{|line| @body << "\t#{line}\n"}
|
|
|
|
@body << "</PRE><HR>"
|
|
|
|
end
|
|
|
|
|
|
|
|
@body << <<-_end_of_html_
|
|
|
|
<ADDRESS>
|
|
|
|
#{HTMLUtils::escape(@config[:ServerSoftware])} at
|
|
|
|
#{host}:#{port}
|
|
|
|
</ADDRESS>
|
|
|
|
</BODY>
|
|
|
|
</HTML>
|
|
|
|
_end_of_html_
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def send_body_io(socket)
|
2004-10-21 06:10:52 -04:00
|
|
|
begin
|
|
|
|
if @request_method == "HEAD"
|
|
|
|
# do nothing
|
|
|
|
elsif chunked?
|
2006-05-18 09:42:52 -04:00
|
|
|
while buf = @body.read(@buffer_size)
|
2004-10-21 06:10:52 -04:00
|
|
|
next if buf.empty?
|
|
|
|
data = ""
|
2008-11-08 04:41:24 -05:00
|
|
|
data << format("%x", buf.bytesize) << CRLF
|
2004-10-21 06:10:52 -04:00
|
|
|
data << buf << CRLF
|
|
|
|
_write_data(socket, data)
|
2008-11-08 04:41:24 -05:00
|
|
|
@sent_size += buf.bytesize
|
2004-10-21 06:10:52 -04:00
|
|
|
end
|
|
|
|
_write_data(socket, "0#{CRLF}#{CRLF}")
|
|
|
|
else
|
|
|
|
size = @header['content-length'].to_i
|
|
|
|
_send_file(socket, @body, 0, size)
|
|
|
|
@sent_size = size
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
2004-10-21 06:10:52 -04:00
|
|
|
ensure
|
|
|
|
@body.close
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_body_string(socket)
|
|
|
|
if @request_method == "HEAD"
|
|
|
|
# do nothing
|
|
|
|
elsif chunked?
|
2010-11-08 15:59:01 -05:00
|
|
|
body ? @body.bytesize : 0
|
2006-05-18 09:42:52 -04:00
|
|
|
while buf = @body[@sent_size, @buffer_size]
|
2003-07-23 12:51:36 -04:00
|
|
|
break if buf.empty?
|
|
|
|
data = ""
|
2008-11-08 04:41:24 -05:00
|
|
|
data << format("%x", buf.bytesize) << CRLF
|
2003-07-23 12:51:36 -04:00
|
|
|
data << buf << CRLF
|
|
|
|
_write_data(socket, data)
|
2008-11-08 04:41:24 -05:00
|
|
|
@sent_size += buf.bytesize
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
_write_data(socket, "0#{CRLF}#{CRLF}")
|
|
|
|
else
|
2008-11-08 04:41:24 -05:00
|
|
|
if @body && @body.bytesize > 0
|
2003-07-23 12:51:36 -04:00
|
|
|
_write_data(socket, @body)
|
2008-11-08 04:41:24 -05:00
|
|
|
@sent_size = @body.bytesize
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _send_file(output, input, offset, size)
|
|
|
|
while offset > 0
|
2006-05-18 09:42:52 -04:00
|
|
|
sz = @buffer_size < size ? @buffer_size : size
|
2003-07-23 12:51:36 -04:00
|
|
|
buf = input.read(sz)
|
2008-11-08 04:41:24 -05:00
|
|
|
offset -= buf.bytesize
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if size == 0
|
2006-05-18 09:42:52 -04:00
|
|
|
while buf = input.read(@buffer_size)
|
2003-07-23 12:51:36 -04:00
|
|
|
_write_data(output, buf)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
while size > 0
|
2006-05-18 09:42:52 -04:00
|
|
|
sz = @buffer_size < size ? @buffer_size : size
|
2003-07-23 12:51:36 -04:00
|
|
|
buf = input.read(sz)
|
|
|
|
_write_data(output, buf)
|
2008-11-08 04:41:24 -05:00
|
|
|
size -= buf.bytesize
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _write_data(socket, data)
|
|
|
|
socket << data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|