Class: WEBrick::HTTPRequest

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

Overview

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

Constant Summary collapse

BODY_CONTAINABLE_METHODS =

:nodoc:

[ "POST", "PUT" ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HTTPRequest

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/webrick/httprequest.rb', line 151

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)

The Accept header value



98
99
100
# File 'lib/webrick/httprequest.rb', line 98

def accept
  @accept
end

#accept_charsetObject (readonly)

The Accept-Charset header value



103
104
105
# File 'lib/webrick/httprequest.rb', line 103

def accept_charset
  @accept_charset
end

#accept_encodingObject (readonly)

The Accept-Encoding header value



108
109
110
# File 'lib/webrick/httprequest.rb', line 108

def accept_encoding
  @accept_encoding
end

#accept_languageObject (readonly)

The Accept-Language header value



113
114
115
# File 'lib/webrick/httprequest.rb', line 113

def accept_language
  @accept_language
end

#addrObject (readonly)

The socket address of the server



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

def addr
  @addr
end

#attributesObject (readonly)

Hash of request attributes



135
136
137
# File 'lib/webrick/httprequest.rb', line 135

def attributes
  @attributes
end

#cipherObject (readonly)

HTTP request SSL cipher



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

def cipher
  @cipher
end

#client_certObject (readonly)

HTTP request client certificate



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

def client_cert
  @client_cert
end

#cookiesObject (readonly)

The parsed request cookies



93
94
95
# File 'lib/webrick/httprequest.rb', line 93

def cookies
  @cookies
end

#headerObject (readonly)

The parsed header of the request



88
89
90
# File 'lib/webrick/httprequest.rb', line 88

def header
  @header
end

#http_versionObject (readonly)

The HTTP version of the request



49
50
51
# File 'lib/webrick/httprequest.rb', line 49

def http_version
  @http_version
end

#keep_aliveObject (readonly)

Is this a keep-alive connection?



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

def keep_alive
  @keep_alive
end

#pathObject (readonly)

The request path



61
62
63
# File 'lib/webrick/httprequest.rb', line 61

def path
  @path
end

#path_infoObject

The path info (CGI variable)



71
72
73
# File 'lib/webrick/httprequest.rb', line 71

def path_info
  @path_info
end

#peeraddrObject (readonly)

The socket address of the client



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

def peeraddr
  @peeraddr
end

#query_stringObject

The query from the URI of the request



76
77
78
# File 'lib/webrick/httprequest.rb', line 76

def query_string
  @query_string
end

#raw_headerObject (readonly)

The raw header of the request



83
84
85
# File 'lib/webrick/httprequest.rb', line 83

def raw_header
  @raw_header
end

#request_lineObject (readonly)

The complete request line such as:

GET / HTTP/1.1


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

def request_line
  @request_line
end

#request_methodObject (readonly)

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



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

def request_method
  @request_method
end

#request_timeObject (readonly)

The local time this request was received



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

def request_time
  @request_time
end

#request_uriObject (readonly)

The parsed URI of the request



56
57
58
# File 'lib/webrick/httprequest.rb', line 56

def request_uri
  @request_uri
end

#script_nameObject

The script name (CGI variable)



66
67
68
# File 'lib/webrick/httprequest.rb', line 66

def script_name
  @script_name
end

#server_certObject (readonly)

HTTP request server certificate



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

def server_cert
  @server_cert
end

#unparsed_uriObject (readonly)

The unparsed URI of the request



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

def unparsed_uri
  @unparsed_uri
end

#userObject

The remote user (CGI variable)



120
121
122
# File 'lib/webrick/httprequest.rb', line 120

def user
  @user
end

Instance Method Details

#[](header_name) ⇒ Object

Retrieves header_name



286
287
288
289
290
291
# File 'lib/webrick/httprequest.rb', line 286

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

#body(&block) ⇒ Object

Returns the request body.



253
254
255
256
257
# File 'lib/webrick/httprequest.rb', line 253

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

#content_lengthObject

The content-length header



272
273
274
# File 'lib/webrick/httprequest.rb', line 272

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

#content_typeObject

The content-type header



279
280
281
# File 'lib/webrick/httprequest.rb', line 279

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

#continueObject

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



243
244
245
246
247
248
# File 'lib/webrick/httprequest.rb', line 243

def continue # :nodoc:
  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



296
297
298
299
300
301
302
303
# File 'lib/webrick/httprequest.rb', line 296

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

#fixupObject

Consumes any remaining body and updates keep-alive status



358
359
360
361
362
363
364
365
366
367
368
# File 'lib/webrick/httprequest.rb', line 358

def fixup() # :nodoc:
  begin
    body{|chunk| }   # read remaining body
  rescue HTTPStatus::Error => ex
    @logger.error("HTTPRequest#fixup: #{ex.class} occurred.")
    @keep_alive = false
  rescue => ex
    @logger.error(ex)
    @keep_alive = false
  end
end

#hostObject

The host this request is for



308
309
310
# File 'lib/webrick/httprequest.rb', line 308

def host
  return @forwarded_host || @host
end

#keep_alive?Boolean

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

Returns:

  • (Boolean)


343
344
345
# File 'lib/webrick/httprequest.rb', line 343

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” To browse the current document of CGI Version 1.1, see below: tools.ietf.org/html/rfc3875



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/webrick/httprequest.rb', line 375

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



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

alias orig_meta_vars meta_vars

#orig_parseObject

:stopdoc:



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

alias orig_parse parse

#orig_parse_uriObject



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

alias orig_parse_uri parse_uri

#parse(socket = nil) ⇒ Object

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



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

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



315
316
317
# File 'lib/webrick/httprequest.rb', line 315

def port
  return @forwarded_port || @port
end

#queryObject

Request query as a Hash



262
263
264
265
266
267
# File 'lib/webrick/httprequest.rb', line 262

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

#remote_ipObject

The client's IP address



329
330
331
# File 'lib/webrick/httprequest.rb', line 329

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

#server_nameObject

The server name this request is for



322
323
324
# File 'lib/webrick/httprequest.rb', line 322

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

#ssl?Boolean

Is this an SSL request?

Returns:

  • (Boolean)


336
337
338
# File 'lib/webrick/httprequest.rb', line 336

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

#to_sObject

:nodoc:



347
348
349
350
351
352
353
# File 'lib/webrick/httprequest.rb', line 347

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