Class: WEBrick::HTTPRequest

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

Overview

An HTTP request.

Constant Summary collapse

BODY_CONTAINABLE_METHODS =
[ "POST", "PUT" ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HTTPRequest

Returns a new instance of 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
76
77
78
79
# File 'lib/webrick/httprequest.rb', line 45

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

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)

:section: Header and entity body



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

def cookies
  @cookies
end

#headerObject (readonly)

:section: Header and entity body



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

def header
  @header
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)

:section: 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

#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)

:section: Header and entity body



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

def raw_header
  @raw_header
end

#request_lineObject (readonly)

:section: 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)

:section: 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

:section:



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

def user
  @user
end

Instance Method Details

#[](header_name) ⇒ Object

Retrieves header_name



171
172
173
174
175
176
# File 'lib/webrick/httprequest.rb', line 171

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

#body(&block) ⇒ Object



138
139
140
141
142
# File 'lib/webrick/httprequest.rb', line 138

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

#content_lengthObject

The content-length header



157
158
159
# File 'lib/webrick/httprequest.rb', line 157

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

#content_typeObject

The content-type header



164
165
166
# File 'lib/webrick/httprequest.rb', line 164

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

#continueObject

Generate HTTP/1.1 100 continue response if the client expects it, otherwise does nothing.



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

def continue
  if self['expect'] == '100-continue' && @config[:HTTPVersion] >= "1.1"
    @socket << "HTTP/#{@config[:HTTPVersion]} 100 continue#{CRLF}#{CRLF}"
    @header.delete('expect')
  end
end

#eachObject

Iterates over the request headers



181
182
183
184
185
186
187
188
# File 'lib/webrick/httprequest.rb', line 181

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

#fixupObject



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/webrick/httprequest.rb', line 240

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

#hostObject

The host this request is for



193
194
195
# File 'lib/webrick/httprequest.rb', line 193

def host
  return @forwarded_host || @host
end

#keep_alive?Boolean

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

Returns:

  • (Boolean)


228
229
230
# File 'lib/webrick/httprequest.rb', line 228

def keep_alive?
  @keep_alive
end

#meta_varsObject

This method provides the metavariables defined by the revision 3 of “The WWW Common Gateway Interface Version 1.1” Web.Golux.Com/coar/cgi/



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/webrick/httprequest.rb', line 256

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



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

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

#portObject

The port this request is for



200
201
202
# File 'lib/webrick/httprequest.rb', line 200

def port
  return @forwarded_port || @port
end

#queryObject

Request query as a Hash



147
148
149
150
151
152
# File 'lib/webrick/httprequest.rb', line 147

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

#remote_ipObject

The client’s IP address



214
215
216
# File 'lib/webrick/httprequest.rb', line 214

def remote_ip
  return self["client-ip"] || @forwarded_for || @peeraddr[3]
end

#server_nameObject

The server name this request is for



207
208
209
# File 'lib/webrick/httprequest.rb', line 207

def server_name
  return @forwarded_server || @config[:ServerName]
end

#ssl?Boolean

Is this an SSL request?

Returns:

  • (Boolean)


221
222
223
# File 'lib/webrick/httprequest.rb', line 221

def ssl?
  return @request_uri.scheme == "https"
end

#to_sObject

:nodoc:



232
233
234
235
236
237
238
# File 'lib/webrick/httprequest.rb', line 232

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