Class: WEBrick::HTTPRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/webrick/https.rb,
lib/webrick/httprequest.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HTTPRequest



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/webrick/httprequest.rb', line 45

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 = []
  @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
end

Instance Attribute Details

#acceptObject (readonly)

Returns the value of attribute accept.



35
36
37
# File 'lib/webrick/httprequest.rb', line 35

def accept
  @accept
end

#accept_charsetObject (readonly)

Returns the value of attribute accept_charset.



35
36
37
# File 'lib/webrick/httprequest.rb', line 35

def accept_charset
  @accept_charset
end

#accept_encodingObject (readonly)

Returns the value of attribute accept_encoding.



36
37
38
# File 'lib/webrick/httprequest.rb', line 36

def accept_encoding
  @accept_encoding
end

#accept_languageObject (readonly)

Returns the value of attribute accept_language.



36
37
38
# File 'lib/webrick/httprequest.rb', line 36

def accept_language
  @accept_language
end

#addrObject (readonly)

Returns the value of attribute addr.



40
41
42
# File 'lib/webrick/httprequest.rb', line 40

def addr
  @addr
end

#attributesObject (readonly)

Returns the value of attribute attributes.



41
42
43
# File 'lib/webrick/httprequest.rb', line 41

def attributes
  @attributes
end

#cipherObject (readonly)

Returns the value of attribute cipher.



19
20
21
# File 'lib/webrick/https.rb', line 19

def cipher
  @cipher
end

#client_certObject (readonly)

Returns the value of attribute client_cert.



19
20
21
# File 'lib/webrick/https.rb', line 19

def client_cert
  @client_cert
end

#cookiesObject (readonly)

Header and entity body



34
35
36
# File 'lib/webrick/httprequest.rb', line 34

def cookies
  @cookies
end

#headerObject (readonly)

Header and entity body



34
35
36
# File 'lib/webrick/httprequest.rb', line 34

def header
  @header
end

#hostObject (readonly)

Request-URI



30
31
32
# File 'lib/webrick/httprequest.rb', line 30

def host
  @host
end

#http_versionObject (readonly)

Returns the value of attribute http_version.



27
28
29
# File 'lib/webrick/httprequest.rb', line 27

def http_version
  @http_version
end

#keep_aliveObject (readonly)

Returns the value of attribute keep_alive.



42
43
44
# File 'lib/webrick/httprequest.rb', line 42

def keep_alive
  @keep_alive
end

#pathObject (readonly)

Request-URI



30
31
32
# File 'lib/webrick/httprequest.rb', line 30

def path
  @path
end

#path_infoObject

Returns the value of attribute path_info.



31
32
33
# File 'lib/webrick/httprequest.rb', line 31

def path_info
  @path_info
end

#peeraddrObject (readonly)

Returns the value of attribute peeraddr.



40
41
42
# File 'lib/webrick/httprequest.rb', line 40

def peeraddr
  @peeraddr
end

#portObject (readonly)

Request-URI



30
31
32
# File 'lib/webrick/httprequest.rb', line 30

def port
  @port
end

#query_stringObject

Returns the value of attribute query_string.



31
32
33
# File 'lib/webrick/httprequest.rb', line 31

def query_string
  @query_string
end

#raw_headerObject (readonly)

Header and entity body



34
35
36
# File 'lib/webrick/httprequest.rb', line 34

def raw_header
  @raw_header
end

#request_lineObject (readonly)

Request line



26
27
28
# File 'lib/webrick/httprequest.rb', line 26

def request_line
  @request_line
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



27
28
29
# File 'lib/webrick/httprequest.rb', line 27

def request_method
  @request_method
end

#request_timeObject (readonly)

Returns the value of attribute request_time.



43
44
45
# File 'lib/webrick/httprequest.rb', line 43

def request_time
  @request_time
end

#request_uriObject (readonly)

Request-URI



30
31
32
# File 'lib/webrick/httprequest.rb', line 30

def request_uri
  @request_uri
end

#script_nameObject

Returns the value of attribute script_name.



31
32
33
# File 'lib/webrick/httprequest.rb', line 31

def script_name
  @script_name
end

#server_certObject (readonly)

Returns the value of attribute server_cert.



19
20
21
# File 'lib/webrick/https.rb', line 19

def server_cert
  @server_cert
end

#unparsed_uriObject (readonly)

Returns the value of attribute unparsed_uri.



27
28
29
# File 'lib/webrick/httprequest.rb', line 27

def unparsed_uri
  @unparsed_uri
end

#userObject

Misc



39
40
41
# File 'lib/webrick/httprequest.rb', line 39

def user
  @user
end

Instance Method Details

#[](header_name) ⇒ Object



145
146
147
148
149
150
# File 'lib/webrick/httprequest.rb', line 145

def [](header_name)
  if @header
    value = @header[header_name.downcase]
    value.empty? ? nil : value.join(", ")
  end
end

#body(&block) ⇒ Object



124
125
126
127
128
# File 'lib/webrick/httprequest.rb', line 124

def body(&block)
  block ||= Proc.new{|chunk| @body << chunk }
  read_body(@socket, block)
  @body.empty? ? nil : @body
end

#content_lengthObject



137
138
139
# File 'lib/webrick/httprequest.rb', line 137

def content_length
  return Integer(self['content-length'])
end

#content_typeObject



141
142
143
# File 'lib/webrick/httprequest.rb', line 141

def content_type
  return self['content-type']
end

#eachObject



152
153
154
155
156
157
# File 'lib/webrick/httprequest.rb', line 152

def each
  @header.each{|k, v|
    value = @header[k]
    yield(k, value.empty? ? nil : value.join(", "))
  }
end

#fixupObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/webrick/httprequest.rb', line 171

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

#keep_alive?Boolean



159
160
161
# File 'lib/webrick/httprequest.rb', line 159

def keep_alive?
  @keep_alive
end

#meta_varsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/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]
    meta["SSL_PROTOCOL"] = @cipher[1]
    meta["SSL_CIPHER_USEKEYSIZE"] = @cipher[2].to_s
    meta["SSL_CIPHER_ALGKEYSIZE"] = @cipher[3].to_s
  end
  meta
end

#orig_meta_varsObject



42
# File 'lib/webrick/https.rb', line 42

alias orig_meta_vars meta_vars

#orig_parseObject



21
# File 'lib/webrick/https.rb', line 21

alias orig_parse parse

#orig_parse_uriObject



33
# File 'lib/webrick/https.rb', line 33

alias orig_parse_uri parse_uri

#parse(socket = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/webrick/https.rb', line 23

def parse(socket=nil)
  if socket.respond_to?(:cert)
    @server_cert = socket.cert || @config[:SSLCertificate]
    @client_cert = socket.peer_cert
    @client_cert_chain = socket.peer_cert_chain
    @cipher      = socket.cipher
  end
  orig_parse(socket)
end

#queryObject



130
131
132
133
134
135
# File 'lib/webrick/httprequest.rb', line 130

def query
  unless @query
    parse_query()
  end
  @query
end

#to_sObject



163
164
165
166
167
168
169
# File 'lib/webrick/httprequest.rb', line 163

def to_s
  ret = @request_line.dup
  @raw_header.each{|line| ret << line }
  ret << CRLF
  ret << body if body
  ret
end