Class: WEBrick::HTTPRequest

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

Constant Summary

BODY_CONTAINABLE_METHODS =
[ "POST", "PUT" ]
PrivateNetworkRegexp =
/
  ^unknown$|
  ^((::ffff:)?127.0.0.1|::1)$|
  ^(::ffff:)?(10|172\.(1[6-9]|2[0-9]|3[01])|192\.168)\.
/ixo

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HTTPRequest) initialize(config)

A new instance of HTTPRequest



41
42
43
44
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 41

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

- (Object) accept (readonly)

Returns the value of attribute accept



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

def accept
  @accept
end

- (Object) accept_charset (readonly)

Returns the value of attribute accept_charset



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

def accept_charset
  @accept_charset
end

- (Object) accept_encoding (readonly)

Returns the value of attribute accept_encoding



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

def accept_encoding
  @accept_encoding
end

- (Object) accept_language (readonly)

Returns the value of attribute accept_language



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

def accept_language
  @accept_language
end

- (Object) addr (readonly)

Returns the value of attribute addr



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

def addr
  @addr
end

- (Object) attributes (readonly)

Returns the value of attribute attributes



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

def attributes
  @attributes
end

- (Object) cipher (readonly)

Returns the value of attribute cipher



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

def cipher
  @cipher
end

- (Object) client_cert (readonly)

Returns the value of attribute client_cert



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

def client_cert
  @client_cert
end

- (Object) cookies (readonly)

Header and entity body



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

def cookies
  @cookies
end

- (Object) header (readonly)

Header and entity body



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

def header
  @header
end

- (Object) http_version (readonly)

Returns the value of attribute http_version



23
24
25
# File 'lib/webrick/httprequest.rb', line 23

def http_version
  @http_version
end

- (Object) keep_alive (readonly)

Returns the value of attribute keep_alive



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

def keep_alive
  @keep_alive
end

- (Object) path (readonly)

Request-URI



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

def path
  @path
end

- (Object) path_info

Returns the value of attribute path_info



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

def path_info
  @path_info
end

- (Object) peeraddr (readonly)

Returns the value of attribute peeraddr



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

def peeraddr
  @peeraddr
end

- (Object) query_string

Returns the value of attribute query_string



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

def query_string
  @query_string
end

- (Object) raw_header (readonly)

Header and entity body



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

def raw_header
  @raw_header
end

- (Object) request_line (readonly)

Request line



22
23
24
# File 'lib/webrick/httprequest.rb', line 22

def request_line
  @request_line
end

- (Object) request_method (readonly)

Returns the value of attribute request_method



23
24
25
# File 'lib/webrick/httprequest.rb', line 23

def request_method
  @request_method
end

- (Object) request_time (readonly)

Returns the value of attribute request_time



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

def request_time
  @request_time
end

- (Object) request_uri (readonly)

Request-URI



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

def request_uri
  @request_uri
end

- (Object) script_name

Returns the value of attribute script_name



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

def script_name
  @script_name
end

- (Object) server_cert (readonly)

Returns the value of attribute server_cert



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

def server_cert
  @server_cert
end

- (Object) unparsed_uri (readonly)

Returns the value of attribute unparsed_uri



23
24
25
# File 'lib/webrick/httprequest.rb', line 23

def unparsed_uri
  @unparsed_uri
end

- (Object) user

Misc



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

def user
  @user
end

Instance Method Details

- (Object) [](header_name)



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

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

- (Object) body(&block)



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

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

- (Object) content_length



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

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

- (Object) content_type



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

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

- (Object) each



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

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

- (Object) fixup



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/webrick/httprequest.rb', line 192

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

- (Object) host



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

def host
  return @forwarded_host || @host
end

- (Boolean) keep_alive?

Returns:

  • (Boolean)


180
181
182
# File 'lib/webrick/httprequest.rb', line 180

def keep_alive?
  @keep_alive
end

- (Object) meta_vars



44
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
80
81
# File 'lib/webrick/https.rb', line 44

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

- (Object) orig_meta_vars



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

alias orig_meta_vars meta_vars

- (Object) orig_parse



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

alias orig_parse parse

- (Object) orig_parse_uri



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

alias orig_parse_uri parse_uri

- (Object) parse(socket = nil)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/webrick/https.rb', line 23

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 /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

- (Object) port



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

def port
  return @forwarded_port || @port
end

- (Object) query



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

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

- (Object) remote_ip



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

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

- (Object) server_name



168
169
170
# File 'lib/webrick/httprequest.rb', line 168

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

- (Boolean) ssl?

Returns:

  • (Boolean)


176
177
178
# File 'lib/webrick/httprequest.rb', line 176

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

- (Object) to_s



184
185
186
187
188
189
190
# File 'lib/webrick/httprequest.rb', line 184

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