# WEBrick::HTTPRequest (Class)

In: | webrick/https.rb webrick/httprequest.rb
Parent: | Object

## Methods

[] body each fixup keep_alive? meta_vars meta_vars new parse parse parse_uri query to_s

## Constants

BODY_CONTAINABLE_METHODS | = | [ "POST", "PUT" ]
BUFSIZE | = | 1024*4

## External Aliases

parse -> orig_parse
parse_uri -> orig_parse_uri
meta_vars -> orig_meta_vars

## Attributes

addr | [R] |
attributes | [R] |
cipher | [R] |
client_cert | [R] |
cookies | [R] | Header and entity body
header | [R] | Header and entity body
host | [R] | Request-URI
http_version | [R] |
keep_alive | [R] |
path | [R] | Request-URI
path_info | [RW] |
peeraddr | [R] |
port | [R] | Request-URI
query_string | [R] | Request-URI
raw_header | [R] | Header and entity body
request_line | [R] | Request line
request_method | [R] |
request_time | [R] |
request_uri | [R] | Request-URI
script_name | [RW] |
server_cert | [R] |
unparsed_uri | [R] |
user | [RW] | Misc

## Public Class methods

new(config)

[Source]

    # File webrick/httprequest.rb, line 43
        def initialize(config)
          @config = config
          @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 = []
          @body = ""

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

          @remaining_size = nil
          @socket = nil
        end

## Public Instance methods

parse(socket=nil)

[Source]

    # File webrick/https.rb, line 23
        def parse(socket=nil)
          if socket && socket.is_a?(OpenSSL::SSL::SSLSocket)
            @server_cert = @config[:SSLCertificate]
            @client_cert = socket.peer_cert
            @client_cert_chain = socket.peer_cert_chain
            @cipher      = socket.cipher
          end
          orig_parse(socket)
        end

parse_uri(str, scheme="https")

[Source]

    # File webrick/https.rb, line 35
        def parse_uri(str, scheme="https")
          if @server_cert
            return orig_parse_uri(str, scheme)
          end
          return orig_parse_uri(str)
        end

meta_vars()

[Source]

    # File webrick/https.rb, line 44
        def meta_vars
          meta = orig_meta_vars
          if @server_cert
            meta["HTTPS"] = "on"
            meta["SSL_SERVER_CERT"] = @server_cert.to_pem
            meta["SSL_CLIENT_CERT"] = @client_cert ? @client_cert.to_pem : ""
            if @client_cert_chain
              @client_cert_chain.each_with_index{|cert, i|
                meta["SSL_CLIENT_CERT_CHAIN_#{i}"] = cert.to_pem
              }
            end
            meta["SSL_CIPHER"] = @cipher[0]
          end
          meta
        end

parse(socket=nil)

[Source]

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

          begin
            @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

body(&block)

[Source]

    # File webrick/httprequest.rb, line 114
        def body(&block)
          block ||= Proc.new{|chunk| @body << chunk }
          read_body(@socket, block)
          @body.empty? ? nil : @body
        end

query()

[Source]

    # File webrick/httprequest.rb, line 120
        def query
          unless @query
            parse_query()
          end
          @query
        end

[](header_name)

[Source]

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

each() {|k, value.empty? ? nil : value.join(", ")| ...}

[Source]

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

keep_alive?()

[Source]

    # File webrick/httprequest.rb, line 141
        def keep_alive?
          @keep_alive
        end

to_s()

[Source]

    # File webrick/httprequest.rb, line 145
        def to_s
          ret = @request_line.dup
          @raw_header.each{|line| ret << line }
          ret << CRLF
          ret << body if body
          ret
        end

fixup()

[Source]

    # File webrick/httprequest.rb, line 153
        def fixup()
          begin
            body{|chunk| }   # read remaining body

          rescue HTTPStatus::Error => ex
            @logger.error("HTTPRequest#fixup: #{ex.class} occured.")
            @keep_alive = false
          rescue => ex
            @logger.error(ex)
            @keep_alive = false
          end
        end

meta_vars()

[Source]

    # File webrick/httprequest.rb, line 165
        def meta_vars
          # This method provides the metavariables defined by the revision 3

          # of ``The WWW Common Gateway Interface Version 1.1''.

          # (http://Web.Golux.Com/coar/cgi/)

          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.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|
            name = "HTTP_" + key
            name.gsub!(/-/o, "_")
            name.upcase!
            meta[name] = val
          }

          meta
        end

[Validate]