In Files

  • webrick/httprequest.rb
  • webrick/https.rb

Class/Module Index [+]

Quicksearch

WEBrick::HTTPRequest

An HTTP request. This is consumed by service and do_* methods in WEBrick servlets

Attributes

addr[R]

The socket address of the server

attributes[R]

Hash of request attributes

cipher[R]

HTTP request SSL cipher

client_cert[R]

HTTP request client certificate

keep_alive[R]

Is this a keep-alive connection?

peeraddr[R]

The socket address of the client

request_time[R]

The local time this request was received

server_cert[R]

HTTP request server certificate

user[RW]

The remote user (CGI variable)

Public Class Methods

new(config) click to toggle source

Creates a new HTTP request. WEBrick::Config::HTTP is the default configuration.

 
               # File webrick/httprequest.rb, line 152
def initialize(config)
  @config = config
  @buffer_size = @config[:InputBufferSize]
  @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 = []
  @accept = []
  @accept_charset = []
  @accept_encoding = []
  @accept_language = []
  @body = ""

  @addr = @peeraddr = nil
  @attributes = {}
  @user = nil
  @keep_alive = false
  @request_time = nil

  @remaining_size = nil
  @socket = nil

  @forwarded_proto = @forwarded_host = @forwarded_port =
    @forwarded_server = @forwarded_for = nil
end
            

Public Instance Methods

[](header_name) click to toggle source

Retrieves header_name

 
               # File webrick/httprequest.rb, line 313
def [](header_name)
  if @header
    value = @header[header_name.downcase]
    value.empty? ? nil : value.join(", ")
  end
end
            
body() click to toggle source

Returns the request body.

 
               # File webrick/httprequest.rb, line 254
def body(&block) # :yields: body_chunk
  block ||= Proc.new{|chunk| @body << chunk }
  read_body(@socket, block)
  @body.empty? ? nil : @body
end
            
body_reader() click to toggle source

Prepares the HTTPRequest object for use as the source for IO.copy_stream

 
               # File webrick/httprequest.rb, line 264
def body_reader
  @body_tmp = []
  @body_rd = Fiber.new do
    body do |buf|
      @body_tmp << buf
      Fiber.yield
    end
  end
  @body_rd.resume # grab the first chunk and yield
  self
end
            
content_length() click to toggle source

The content-length header

 
               # File webrick/httprequest.rb, line 299
def content_length
  return Integer(self['content-length'])
end
            
content_type() click to toggle source

The content-type header

 
               # File webrick/httprequest.rb, line 306
def content_type
  return self['content-type']
end
            
each() click to toggle source

Iterates over the request headers

 
               # File webrick/httprequest.rb, line 323
def each
  if @header
    @header.each{|k, v|
      value = @header[k]
      yield(k, value.empty? ? nil : value.join(", "))
    }
  end
end
            
host() click to toggle source

The host this request is for

 
               # File webrick/httprequest.rb, line 335
def host
  return @forwarded_host || @host
end
            
keep_alive?() click to toggle source

Should the connection this request was made on be kept alive?

 
               # File webrick/httprequest.rb, line 370
def keep_alive?
  @keep_alive
end
            
meta_vars() click to toggle source

This method provides the metavariables defined by the revision 3 of “The WWW Common Gateway Interface Version 1.1” To browse the current document of CGI Version 1.1, see below: tools.ietf.org/html/rfc3875

 
               # File webrick/httprequest.rb, line 402
def meta_vars
  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"
  meta["PATH_INFO"]         = @path_info ? @path_info.dup : ""
 #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
  meta["SERVER_NAME"]       = @host
  meta["SERVER_PORT"]       = @port.to_s
  meta["SERVER_PROTOCOL"]   = "HTTP/" + @config[:HTTPVersion].to_s
  meta["SERVER_SOFTWARE"]   = @config[:ServerSoftware].dup

  self.each{|key, val|
    next if /^content-type$/i =~ key
    next if /^content-length$/i =~ key
    name = "HTTP_" + key
    name.gsub!(/-/o, "_")
    name.upcase!
    meta[name] = val
  }

  meta
end
            
parse(socket=nil) click to toggle source

Parses a request from socket. This is called internally by WEBrick::HTTPServer.

 
               # File webrick/httprequest.rb, line 192
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)
    }
    @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'])
  end
  return if @request_method == "CONNECT"
  return if @unparsed_uri == "*"

  begin
    setup_forwarded_info
    @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 /\Aclose\z/io =~ self["connection"]
    @keep_alive = false
  elsif /\Akeep-alive\z/io =~ self["connection"]
    @keep_alive = true
  elsif @http_version < "1.1"
    @keep_alive = false
  else
    @keep_alive = true
  end
end
            
port() click to toggle source

The port this request is for

 
               # File webrick/httprequest.rb, line 342
def port
  return @forwarded_port || @port
end
            
query() click to toggle source

Request query as a Hash

 
               # File webrick/httprequest.rb, line 289
def query
  unless @query
    parse_query()
  end
  @query
end
            
readpartial(size, buf = ''.b) click to toggle source

for IO.copy_stream. Note: we may return a larger string than size here; but IO.copy_stream does not care.

 
               # File webrick/httprequest.rb, line 278
def readpartial(size, buf = ''.b) # :nodoc
  res = @body_tmp.shift or raise EOFError, 'end of file reached'
  buf.replace(res)
  res.clear
  @body_rd.resume # get more chunks
  buf
end
            
remote_ip() click to toggle source

The client’s IP address

 
               # File webrick/httprequest.rb, line 356
def remote_ip
  return self["client-ip"] || @forwarded_for || @peeraddr[3]
end
            
server_name() click to toggle source

The server name this request is for

 
               # File webrick/httprequest.rb, line 349
def server_name
  return @forwarded_server || @config[:ServerName]
end
            
ssl?() click to toggle source

Is this an SSL request?

 
               # File webrick/httprequest.rb, line 363
def ssl?
  return @request_uri.scheme == "https"
end
            

Header and entity body ↑ top

Attributes

accept[R]

The Accept header value

accept_charset[R]

The Accept-Charset header value

accept_encoding[R]

The Accept-Encoding header value

accept_language[R]

The Accept-Language header value

cookies[R]

The parsed request cookies

header[R]

The parsed header of the request

raw_header[R]

The raw header of the request

Request line ↑ top

Attributes

http_version[R]

The HTTP version of the request

request_line[R]

The complete request line such as:

GET / HTTP/1.1
request_method[R]

The request method, GET, POST, PUT, etc.

unparsed_uri[R]

The unparsed URI of the request

Request-URI ↑ top

Attributes

path[R]

The request path

path_info[RW]

The path info (CGI variable)

query_string[RW]

The query from the URI of the request

request_uri[R]

The parsed URI of the request

script_name[RW]

The script name (CGI variable)