2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# httprequest.rb -- HTTPRequest 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: httprequest.rb,v 1.64 2003/07/13 17:18:22 gotoyuzo Exp $
|
|
|
|
|
|
|
|
require 'uri'
|
|
|
|
require 'webrick/httpversion'
|
|
|
|
require 'webrick/httpstatus'
|
|
|
|
require 'webrick/httputils'
|
|
|
|
require 'webrick/cookie'
|
|
|
|
|
|
|
|
module WEBrick
|
2011-05-09 20:13:58 -04:00
|
|
|
|
|
|
|
##
|
2013-01-25 20:12:54 -05:00
|
|
|
# An HTTP request. This is consumed by service and do_* methods in
|
|
|
|
# WEBrick servlets
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class HTTPRequest
|
2011-05-09 20:13:58 -04:00
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
BODY_CONTAINABLE_METHODS = [ "POST", "PUT" ] # :nodoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
# :section: Request line
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The complete request line such as:
|
|
|
|
#
|
|
|
|
# GET / HTTP/1.1
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_reader :request_line
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The request method, GET, POST, PUT, etc.
|
|
|
|
|
|
|
|
attr_reader :request_method
|
|
|
|
|
|
|
|
##
|
|
|
|
# The unparsed URI of the request
|
|
|
|
|
|
|
|
attr_reader :unparsed_uri
|
|
|
|
|
|
|
|
##
|
|
|
|
# The HTTP version of the request
|
|
|
|
|
|
|
|
attr_reader :http_version
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
# :section: Request-URI
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The parsed URI of the request
|
|
|
|
|
|
|
|
attr_reader :request_uri
|
|
|
|
|
|
|
|
##
|
|
|
|
# The request path
|
|
|
|
|
|
|
|
attr_reader :path
|
|
|
|
|
|
|
|
##
|
|
|
|
# The script name (CGI variable)
|
|
|
|
|
|
|
|
attr_accessor :script_name
|
|
|
|
|
|
|
|
##
|
|
|
|
# The path info (CGI variable)
|
|
|
|
|
|
|
|
attr_accessor :path_info
|
|
|
|
|
|
|
|
##
|
|
|
|
# The query from the URI of the request
|
|
|
|
|
|
|
|
attr_accessor :query_string
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
# :section: Header and entity body
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The raw header of the request
|
|
|
|
|
|
|
|
attr_reader :raw_header
|
|
|
|
|
|
|
|
##
|
|
|
|
# The parsed header of the request
|
|
|
|
|
|
|
|
attr_reader :header
|
|
|
|
|
|
|
|
##
|
|
|
|
# The parsed request cookies
|
|
|
|
|
|
|
|
attr_reader :cookies
|
|
|
|
|
|
|
|
##
|
|
|
|
# The Accept header value
|
|
|
|
|
|
|
|
attr_reader :accept
|
|
|
|
|
|
|
|
##
|
|
|
|
# The Accept-Charset header value
|
|
|
|
|
|
|
|
attr_reader :accept_charset
|
|
|
|
|
|
|
|
##
|
|
|
|
# The Accept-Encoding header value
|
|
|
|
|
|
|
|
attr_reader :accept_encoding
|
|
|
|
|
|
|
|
##
|
|
|
|
# The Accept-Language header value
|
|
|
|
|
|
|
|
attr_reader :accept_language
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
# :section:
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The remote user (CGI variable)
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_accessor :user
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The socket address of the server
|
|
|
|
|
|
|
|
attr_reader :addr
|
|
|
|
|
|
|
|
##
|
|
|
|
# The socket address of the client
|
|
|
|
|
|
|
|
attr_reader :peeraddr
|
|
|
|
|
|
|
|
##
|
|
|
|
# Hash of request attributes
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_reader :attributes
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Is this a keep-alive connection?
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_reader :keep_alive
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The local time this request was received
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_reader :request_time
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Creates a new HTTP request. WEBrick::Config::HTTP is the default
|
|
|
|
# configuration.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def initialize(config)
|
|
|
|
@config = config
|
2006-05-18 09:42:52 -04:00
|
|
|
@buffer_size = @config[:InputBufferSize]
|
2003-07-23 12:51:36 -04:00
|
|
|
@logger = config[:Logger]
|
|
|
|
|
|
|
|
@request_line = @request_method =
|
|
|
|
@unparsed_uri = @http_version = nil
|
|
|
|
|
|
|
|
@request_uri = @host = @port = @path = nil
|
|
|
|
@script_name = @path_info = nil
|
|
|
|
@query_string = nil
|
|
|
|
@query = nil
|
|
|
|
@form_data = nil
|
|
|
|
|
|
|
|
@raw_header = Array.new
|
|
|
|
@header = nil
|
|
|
|
@cookies = []
|
2004-10-17 12:57:44 -04:00
|
|
|
@accept = []
|
|
|
|
@accept_charset = []
|
|
|
|
@accept_encoding = []
|
|
|
|
@accept_language = []
|
2003-07-23 12:51:36 -04:00
|
|
|
@body = ""
|
|
|
|
|
|
|
|
@addr = @peeraddr = nil
|
|
|
|
@attributes = {}
|
|
|
|
@user = nil
|
|
|
|
@keep_alive = false
|
|
|
|
@request_time = nil
|
|
|
|
|
|
|
|
@remaining_size = nil
|
|
|
|
@socket = nil
|
2008-01-09 06:37:03 -05:00
|
|
|
|
|
|
|
@forwarded_proto = @forwarded_host = @forwarded_port =
|
|
|
|
@forwarded_server = @forwarded_for = nil
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses a request from +socket+. This is called internally by
|
|
|
|
# WEBrick::HTTPServer.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def parse(socket=nil)
|
|
|
|
@socket = socket
|
|
|
|
begin
|
|
|
|
@peeraddr = socket.respond_to?(:peeraddr) ? socket.peeraddr : []
|
|
|
|
@addr = socket.respond_to?(:addr) ? socket.addr : []
|
|
|
|
rescue Errno::ENOTCONN
|
|
|
|
raise HTTPStatus::EOFError
|
|
|
|
end
|
|
|
|
|
|
|
|
read_request_line(socket)
|
|
|
|
if @http_version.major > 0
|
|
|
|
read_header(socket)
|
|
|
|
@header['cookie'].each{|cookie|
|
|
|
|
@cookies += Cookie::parse(cookie)
|
|
|
|
}
|
2004-10-12 08:26:39 -04:00
|
|
|
@accept = HTTPUtils.parse_qvalues(self['accept'])
|
|
|
|
@accept_charset = HTTPUtils.parse_qvalues(self['accept-charset'])
|
|
|
|
@accept_encoding = HTTPUtils.parse_qvalues(self['accept-encoding'])
|
|
|
|
@accept_language = HTTPUtils.parse_qvalues(self['accept-language'])
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
return if @request_method == "CONNECT"
|
|
|
|
return if @unparsed_uri == "*"
|
|
|
|
|
|
|
|
begin
|
2008-01-09 06:37:03 -05:00
|
|
|
setup_forwarded_info
|
2003-07-23 12:51:36 -04:00
|
|
|
@request_uri = parse_uri(@unparsed_uri)
|
|
|
|
@path = HTTPUtils::unescape(@request_uri.path)
|
|
|
|
@path = HTTPUtils::normalize_path(@path)
|
|
|
|
@host = @request_uri.host
|
|
|
|
@port = @request_uri.port
|
|
|
|
@query_string = @request_uri.query
|
|
|
|
@script_name = ""
|
|
|
|
@path_info = @path.dup
|
|
|
|
rescue
|
|
|
|
raise HTTPStatus::BadRequest, "bad URI `#{@unparsed_uri}'."
|
|
|
|
end
|
|
|
|
|
|
|
|
if /close/io =~ self["connection"]
|
|
|
|
@keep_alive = false
|
|
|
|
elsif /keep-alive/io =~ self["connection"]
|
|
|
|
@keep_alive = true
|
|
|
|
elsif @http_version < "1.1"
|
|
|
|
@keep_alive = false
|
|
|
|
else
|
|
|
|
@keep_alive = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
2010-09-10 06:20:35 -04:00
|
|
|
# Generate HTTP/1.1 100 continue response if the client expects it,
|
|
|
|
# otherwise does nothing.
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
def continue # :nodoc:
|
2010-09-10 06:20:35 -04:00
|
|
|
if self['expect'] == '100-continue' && @config[:HTTPVersion] >= "1.1"
|
|
|
|
@socket << "HTTP/#{@config[:HTTPVersion]} 100 continue#{CRLF}#{CRLF}"
|
|
|
|
@header.delete('expect')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Returns the request body.
|
|
|
|
|
|
|
|
def body(&block) # :yields: body_chunk
|
2003-07-23 12:51:36 -04:00
|
|
|
block ||= Proc.new{|chunk| @body << chunk }
|
|
|
|
read_body(@socket, block)
|
|
|
|
@body.empty? ? nil : @body
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Request query as a Hash
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def query
|
|
|
|
unless @query
|
|
|
|
parse_query()
|
|
|
|
end
|
|
|
|
@query
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The content-length header
|
|
|
|
|
2004-10-12 08:26:39 -04:00
|
|
|
def content_length
|
|
|
|
return Integer(self['content-length'])
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The content-type header
|
|
|
|
|
2004-10-12 08:26:39 -04:00
|
|
|
def content_type
|
|
|
|
return self['content-type']
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Retrieves +header_name+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def [](header_name)
|
|
|
|
if @header
|
|
|
|
value = @header[header_name.downcase]
|
|
|
|
value.empty? ? nil : value.join(", ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Iterates over the request headers
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def each
|
2011-07-21 04:27:08 -04:00
|
|
|
if @header
|
|
|
|
@header.each{|k, v|
|
|
|
|
value = @header[k]
|
|
|
|
yield(k, value.empty? ? nil : value.join(", "))
|
|
|
|
}
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The host this request is for
|
|
|
|
|
2008-01-09 06:37:03 -05:00
|
|
|
def host
|
|
|
|
return @forwarded_host || @host
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The port this request is for
|
|
|
|
|
2008-01-09 06:37:03 -05:00
|
|
|
def port
|
|
|
|
return @forwarded_port || @port
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The server name this request is for
|
|
|
|
|
2008-01-09 06:37:03 -05:00
|
|
|
def server_name
|
|
|
|
return @forwarded_server || @config[:ServerName]
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# The client's IP address
|
|
|
|
|
2008-01-09 06:37:03 -05:00
|
|
|
def remote_ip
|
|
|
|
return self["client-ip"] || @forwarded_for || @peeraddr[3]
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Is this an SSL request?
|
|
|
|
|
2008-01-09 06:37:03 -05:00
|
|
|
def ssl?
|
|
|
|
return @request_uri.scheme == "https"
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Should the connection this request was made on 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
|
|
|
def to_s # :nodoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
ret = @request_line.dup
|
|
|
|
@raw_header.each{|line| ret << line }
|
|
|
|
ret << CRLF
|
|
|
|
ret << body if body
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Consumes any remaining body and updates keep-alive status
|
|
|
|
|
|
|
|
def fixup() # :nodoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
begin
|
|
|
|
body{|chunk| } # read remaining body
|
|
|
|
rescue HTTPStatus::Error => ex
|
2013-05-19 10:50:47 -04:00
|
|
|
@logger.error("HTTPRequest#fixup: #{ex.class} occurred.")
|
2003-07-23 12:51:36 -04:00
|
|
|
@keep_alive = false
|
|
|
|
rescue => ex
|
|
|
|
@logger.error(ex)
|
|
|
|
@keep_alive = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
# This method provides the metavariables defined by the revision 3
|
|
|
|
# of "The WWW Common Gateway Interface Version 1.1"
|
2013-12-05 20:56:44 -05:00
|
|
|
# To browse the current document of CGI Version 1.1, see below:
|
|
|
|
# http://tools.ietf.org/html/rfc3875
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
def meta_vars
|
2003-07-23 12:51:36 -04:00
|
|
|
meta = Hash.new
|
|
|
|
|
|
|
|
cl = self["Content-Length"]
|
|
|
|
ct = self["Content-Type"]
|
|
|
|
meta["CONTENT_LENGTH"] = cl if cl.to_i > 0
|
|
|
|
meta["CONTENT_TYPE"] = ct.dup if ct
|
|
|
|
meta["GATEWAY_INTERFACE"] = "CGI/1.1"
|
2004-10-21 21:38:42 -04:00
|
|
|
meta["PATH_INFO"] = @path_info ? @path_info.dup : ""
|
2003-07-23 12:51:36 -04:00
|
|
|
#meta["PATH_TRANSLATED"] = nil # no plan to be provided
|
|
|
|
meta["QUERY_STRING"] = @query_string ? @query_string.dup : ""
|
|
|
|
meta["REMOTE_ADDR"] = @peeraddr[3]
|
|
|
|
meta["REMOTE_HOST"] = @peeraddr[2]
|
|
|
|
#meta["REMOTE_IDENT"] = nil # no plan to be provided
|
|
|
|
meta["REMOTE_USER"] = @user
|
|
|
|
meta["REQUEST_METHOD"] = @request_method.dup
|
|
|
|
meta["REQUEST_URI"] = @request_uri.to_s
|
|
|
|
meta["SCRIPT_NAME"] = @script_name.dup
|
2003-09-08 05:52:34 -04:00
|
|
|
meta["SERVER_NAME"] = @host
|
2003-09-08 16:13:15 -04:00
|
|
|
meta["SERVER_PORT"] = @port.to_s
|
2003-07-23 12:51:36 -04:00
|
|
|
meta["SERVER_PROTOCOL"] = "HTTP/" + @config[:HTTPVersion].to_s
|
|
|
|
meta["SERVER_SOFTWARE"] = @config[:ServerSoftware].dup
|
|
|
|
|
|
|
|
self.each{|key, val|
|
2003-12-20 08:01:33 -05:00
|
|
|
next if /^content-type$/i =~ key
|
|
|
|
next if /^content-length$/i =~ key
|
2003-07-23 12:51:36 -04:00
|
|
|
name = "HTTP_" + key
|
|
|
|
name.gsub!(/-/o, "_")
|
|
|
|
name.upcase!
|
|
|
|
meta[name] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
meta
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
# :stopdoc:
|
|
|
|
|
2010-10-28 00:53:57 -04:00
|
|
|
MAX_URI_LENGTH = 2083 # :nodoc:
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def read_request_line(socket)
|
2010-10-28 00:53:57 -04:00
|
|
|
@request_line = read_line(socket, MAX_URI_LENGTH) if socket
|
|
|
|
if @request_line.bytesize >= MAX_URI_LENGTH and @request_line[-1, 1] != LF
|
2007-12-17 02:03:57 -05:00
|
|
|
raise HTTPStatus::RequestURITooLarge
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
@request_time = Time.now
|
|
|
|
raise HTTPStatus::EOFError unless @request_line
|
2008-11-18 01:58:55 -05:00
|
|
|
if /^(\S+)\s+(\S++)(?:\s+HTTP\/(\d+\.\d+))?\r?\n/mo =~ @request_line
|
2003-07-23 12:51:36 -04:00
|
|
|
@request_method = $1
|
|
|
|
@unparsed_uri = $2
|
|
|
|
@http_version = HTTPVersion.new($3 ? $3 : "0.9")
|
|
|
|
else
|
|
|
|
rl = @request_line.sub(/\x0d?\x0a\z/o, '')
|
|
|
|
raise HTTPStatus::BadRequest, "bad Request-Line `#{rl}'."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_header(socket)
|
|
|
|
if socket
|
|
|
|
while line = read_line(socket)
|
|
|
|
break if /\A(#{CRLF}|#{LF})\z/om =~ line
|
|
|
|
@raw_header << line
|
|
|
|
end
|
|
|
|
end
|
2010-01-10 04:33:47 -05:00
|
|
|
@header = HTTPUtils::parse_header(@raw_header.join)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_uri(str, scheme="http")
|
|
|
|
if @config[:Escape8bitURI]
|
|
|
|
str = HTTPUtils::escape8bit(str)
|
|
|
|
end
|
2010-08-10 01:59:11 -04:00
|
|
|
str.sub!(%r{\A/+}o, '/')
|
2003-07-23 12:51:36 -04:00
|
|
|
uri = URI::parse(str)
|
|
|
|
return uri if uri.absolute?
|
2008-01-09 06:37:03 -05:00
|
|
|
if @forwarded_host
|
|
|
|
host, port = @forwarded_host, @forwarded_port
|
|
|
|
elsif self["host"]
|
2006-07-31 00:39:45 -04:00
|
|
|
pattern = /\A(#{URI::REGEXP::PATTERN::HOST})(?::(\d+))?\z/n
|
|
|
|
host, port = *self['host'].scan(pattern)[0]
|
2003-07-23 12:51:36 -04:00
|
|
|
elsif @addr.size > 0
|
|
|
|
host, port = @addr[2], @addr[1]
|
|
|
|
else
|
|
|
|
host, port = @config[:ServerName], @config[:Port]
|
|
|
|
end
|
2008-01-09 06:37:03 -05:00
|
|
|
uri.scheme = @forwarded_proto || scheme
|
2003-07-23 12:51:36 -04:00
|
|
|
uri.host = host
|
|
|
|
uri.port = port ? port.to_i : nil
|
|
|
|
return URI::parse(uri.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_body(socket, block)
|
|
|
|
return unless socket
|
|
|
|
if tc = self['transfer-encoding']
|
|
|
|
case tc
|
|
|
|
when /chunked/io then read_chunked(socket, block)
|
|
|
|
else raise HTTPStatus::NotImplemented, "Transfer-Encoding: #{tc}."
|
|
|
|
end
|
|
|
|
elsif self['content-length'] || @remaining_size
|
|
|
|
@remaining_size ||= self['content-length'].to_i
|
2009-03-05 22:56:38 -05:00
|
|
|
while @remaining_size > 0
|
2006-05-18 09:42:52 -04:00
|
|
|
sz = [@buffer_size, @remaining_size].min
|
2003-07-23 12:51:36 -04:00
|
|
|
break unless buf = read_data(socket, sz)
|
2008-11-08 04:41:24 -05:00
|
|
|
@remaining_size -= buf.bytesize
|
2003-07-23 12:51:36 -04:00
|
|
|
block.call(buf)
|
|
|
|
end
|
|
|
|
if @remaining_size > 0 && @socket.eof?
|
|
|
|
raise HTTPStatus::BadRequest, "invalid body size."
|
|
|
|
end
|
|
|
|
elsif BODY_CONTAINABLE_METHODS.member?(@request_method)
|
|
|
|
raise HTTPStatus::LengthRequired
|
|
|
|
end
|
|
|
|
return @body
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_chunk_size(socket)
|
|
|
|
line = read_line(socket)
|
|
|
|
if /^([0-9a-fA-F]+)(?:;(\S+))?/ =~ line
|
|
|
|
chunk_size = $1.hex
|
|
|
|
chunk_ext = $2
|
|
|
|
[ chunk_size, chunk_ext ]
|
|
|
|
else
|
|
|
|
raise HTTPStatus::BadRequest, "bad chunk `#{line}'."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_chunked(socket, block)
|
|
|
|
chunk_size, = read_chunk_size(socket)
|
|
|
|
while chunk_size > 0
|
|
|
|
data = read_data(socket, chunk_size) # read chunk-data
|
2008-11-08 04:41:24 -05:00
|
|
|
if data.nil? || data.bytesize != chunk_size
|
2003-07-23 12:51:36 -04:00
|
|
|
raise BadRequest, "bad chunk data size."
|
|
|
|
end
|
|
|
|
read_line(socket) # skip CRLF
|
|
|
|
block.call(data)
|
|
|
|
chunk_size, = read_chunk_size(socket)
|
|
|
|
end
|
|
|
|
read_header(socket) # trailer + CRLF
|
|
|
|
@header.delete("transfer-encoding")
|
|
|
|
@remaining_size = 0
|
|
|
|
end
|
|
|
|
|
2007-12-17 02:03:57 -05:00
|
|
|
def _read_data(io, method, *arg)
|
2003-07-23 12:51:36 -04:00
|
|
|
begin
|
2006-05-18 09:42:52 -04:00
|
|
|
WEBrick::Utils.timeout(@config[:RequestTimeout]){
|
2007-12-17 02:03:57 -05:00
|
|
|
return io.__send__(method, *arg)
|
2003-07-23 12:51:36 -04:00
|
|
|
}
|
|
|
|
rescue Errno::ECONNRESET
|
|
|
|
return nil
|
2014-10-07 16:00:09 -04:00
|
|
|
rescue Timeout::Error
|
2003-07-23 12:51:36 -04:00
|
|
|
raise HTTPStatus::RequestTimeout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-17 02:03:57 -05:00
|
|
|
def read_line(io, size=4096)
|
|
|
|
_read_data(io, :gets, LF, size)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_data(io, size)
|
|
|
|
_read_data(io, :read, size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_query()
|
|
|
|
begin
|
|
|
|
if @request_method == "GET" || @request_method == "HEAD"
|
|
|
|
@query = HTTPUtils::parse_query(@query_string)
|
|
|
|
elsif self['content-type'] =~ /^application\/x-www-form-urlencoded/
|
|
|
|
@query = HTTPUtils::parse_query(body)
|
|
|
|
elsif self['content-type'] =~ /^multipart\/form-data; boundary=(.+)/
|
|
|
|
boundary = HTTPUtils::dequote($1)
|
|
|
|
@query = HTTPUtils::parse_form_data(body, boundary)
|
|
|
|
else
|
|
|
|
@query = Hash.new
|
|
|
|
end
|
|
|
|
rescue => ex
|
|
|
|
raise HTTPStatus::BadRequest, ex.message
|
|
|
|
end
|
|
|
|
end
|
2008-01-09 06:37:03 -05:00
|
|
|
|
|
|
|
PrivateNetworkRegexp = /
|
|
|
|
^unknown$|
|
|
|
|
^((::ffff:)?127.0.0.1|::1)$|
|
|
|
|
^(::ffff:)?(10|172\.(1[6-9]|2[0-9]|3[01])|192\.168)\.
|
|
|
|
/ixo
|
|
|
|
|
2011-06-24 07:05:59 -04:00
|
|
|
# It's said that all X-Forwarded-* headers will contain more than one
|
|
|
|
# (comma-separated) value if the original request already contained one of
|
|
|
|
# these headers. Since we could use these values as Host header, we choose
|
|
|
|
# the initial(first) value. (apr_table_mergen() adds new value after the
|
|
|
|
# existing value with ", " prefix)
|
2008-01-09 06:37:03 -05:00
|
|
|
def setup_forwarded_info
|
2011-06-24 07:05:59 -04:00
|
|
|
if @forwarded_server = self["x-forwarded-server"]
|
|
|
|
@forwarded_server = @forwarded_server.split(",", 2).first
|
|
|
|
end
|
2008-01-09 06:37:03 -05:00
|
|
|
@forwarded_proto = self["x-forwarded-proto"]
|
|
|
|
if host_port = self["x-forwarded-host"]
|
2011-06-24 07:05:59 -04:00
|
|
|
host_port = host_port.split(",", 2).first
|
2008-01-09 06:37:03 -05:00
|
|
|
@forwarded_host, tmp = host_port.split(":", 2)
|
|
|
|
@forwarded_port = (tmp || (@forwarded_proto == "https" ? 443 : 80)).to_i
|
|
|
|
end
|
|
|
|
if addrs = self["x-forwarded-for"]
|
|
|
|
addrs = addrs.split(",").collect(&:strip)
|
|
|
|
addrs.reject!{|ip| PrivateNetworkRegexp =~ ip }
|
|
|
|
@forwarded_for = addrs.first
|
|
|
|
end
|
|
|
|
end
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
# :startdoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|