Class: WEBrick::HTTPRequest
- Inherits:
-
Object
- Object
- WEBrick::HTTPRequest
- 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
-
#accept ⇒ Object
readonly
The Accept header value.
-
#accept_charset ⇒ Object
readonly
The Accept-Charset header value.
-
#accept_encoding ⇒ Object
readonly
The Accept-Encoding header value.
-
#accept_language ⇒ Object
readonly
The Accept-Language header value.
-
#addr ⇒ Object
readonly
The socket address of the server.
-
#attributes ⇒ Object
readonly
Hash of request attributes.
-
#cipher ⇒ Object
readonly
HTTP request SSL cipher.
-
#client_cert ⇒ Object
readonly
HTTP request client certificate.
-
#cookies ⇒ Object
readonly
The parsed request cookies.
-
#header ⇒ Object
readonly
The parsed header of the request.
-
#http_version ⇒ Object
readonly
The HTTP version of the request.
-
#keep_alive ⇒ Object
readonly
Is this a keep-alive connection?.
-
#path ⇒ Object
readonly
The request path.
-
#path_info ⇒ Object
The path info (CGI variable).
-
#peeraddr ⇒ Object
readonly
The socket address of the client.
-
#query_string ⇒ Object
The query from the URI of the request.
-
#raw_header ⇒ Object
readonly
The raw header of the request.
-
#request_line ⇒ Object
readonly
The complete request line such as:.
-
#request_method ⇒ Object
readonly
The request method, GET, POST, PUT, etc.
-
#request_time ⇒ Object
readonly
The local time this request was received.
-
#request_uri ⇒ Object
readonly
The parsed URI of the request.
-
#script_name ⇒ Object
The script name (CGI variable).
-
#server_cert ⇒ Object
readonly
HTTP request server certificate.
-
#unparsed_uri ⇒ Object
readonly
The unparsed URI of the request.
-
#user ⇒ Object
The remote user (CGI variable).
Instance Method Summary collapse
-
#[](header_name) ⇒ Object
Retrieves
header_name
. -
#body(&block) ⇒ Object
Returns the request body.
-
#body_reader ⇒ Object
Prepares the HTTPRequest object for use as the source for IO.copy_stream.
-
#content_length ⇒ Object
The content-length header.
-
#content_type ⇒ Object
The content-type header.
-
#continue ⇒ Object
Generate HTTP/1.1 100 continue response if the client expects it, otherwise does nothing.
-
#each ⇒ Object
Iterates over the request headers.
-
#fixup ⇒ Object
Consumes any remaining body and updates keep-alive status.
-
#host ⇒ Object
The host this request is for.
-
#initialize(config) ⇒ HTTPRequest
constructor
Creates a new HTTP request.
-
#keep_alive? ⇒ Boolean
Should the connection this request was made on be kept alive?.
-
#meta_vars ⇒ Object
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: www.rfc-editor.org/rfc/rfc3875.
- #orig_meta_vars ⇒ Object
-
#orig_parse ⇒ Object
:stopdoc:.
- #orig_parse_uri ⇒ Object
-
#parse(socket = nil) ⇒ Object
Parses a request from
socket
. -
#port ⇒ Object
The port this request is for.
-
#query ⇒ Object
Request query as a Hash.
-
#readpartial(size, buf = ''.b) ⇒ Object
for IO.copy_stream.
-
#remote_ip ⇒ Object
The client’s IP address.
-
#server_name ⇒ Object
The server name this request is for.
-
#ssl? ⇒ Boolean
Is this an SSL request?.
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(config) ⇒ HTTPRequest
Creates a new HTTP request. WEBrick::Config::HTTP is the default configuration.
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 186 187 |
# File 'lib/webrick/httprequest.rb', line 153 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
#accept ⇒ Object (readonly)
The Accept header value
100 101 102 |
# File 'lib/webrick/httprequest.rb', line 100 def accept @accept end |
#accept_charset ⇒ Object (readonly)
The Accept-Charset header value
105 106 107 |
# File 'lib/webrick/httprequest.rb', line 105 def accept_charset @accept_charset end |
#accept_encoding ⇒ Object (readonly)
The Accept-Encoding header value
110 111 112 |
# File 'lib/webrick/httprequest.rb', line 110 def accept_encoding @accept_encoding end |
#accept_language ⇒ Object (readonly)
The Accept-Language header value
115 116 117 |
# File 'lib/webrick/httprequest.rb', line 115 def accept_language @accept_language end |
#addr ⇒ Object (readonly)
The socket address of the server
127 128 129 |
# File 'lib/webrick/httprequest.rb', line 127 def addr @addr end |
#attributes ⇒ Object (readonly)
Hash of request attributes
137 138 139 |
# File 'lib/webrick/httprequest.rb', line 137 def attributes @attributes end |
#cipher ⇒ Object (readonly)
HTTP request SSL cipher
29 30 31 |
# File 'lib/webrick/https.rb', line 29 def cipher @cipher end |
#client_cert ⇒ Object (readonly)
HTTP request client certificate
39 40 41 |
# File 'lib/webrick/https.rb', line 39 def client_cert @client_cert end |
#cookies ⇒ Object (readonly)
The parsed request cookies
95 96 97 |
# File 'lib/webrick/httprequest.rb', line 95 def @cookies end |
#header ⇒ Object (readonly)
The parsed header of the request
90 91 92 |
# File 'lib/webrick/httprequest.rb', line 90 def header @header end |
#http_version ⇒ Object (readonly)
The HTTP version of the request
51 52 53 |
# File 'lib/webrick/httprequest.rb', line 51 def http_version @http_version end |
#keep_alive ⇒ Object (readonly)
Is this a keep-alive connection?
142 143 144 |
# File 'lib/webrick/httprequest.rb', line 142 def keep_alive @keep_alive end |
#path ⇒ Object (readonly)
The request path
63 64 65 |
# File 'lib/webrick/httprequest.rb', line 63 def path @path end |
#path_info ⇒ Object
The path info (CGI variable)
73 74 75 |
# File 'lib/webrick/httprequest.rb', line 73 def path_info @path_info end |
#peeraddr ⇒ Object (readonly)
The socket address of the client
132 133 134 |
# File 'lib/webrick/httprequest.rb', line 132 def peeraddr @peeraddr end |
#query_string ⇒ Object
The query from the URI of the request
78 79 80 |
# File 'lib/webrick/httprequest.rb', line 78 def query_string @query_string end |
#raw_header ⇒ Object (readonly)
The raw header of the request
85 86 87 |
# File 'lib/webrick/httprequest.rb', line 85 def raw_header @raw_header end |
#request_line ⇒ Object (readonly)
The complete request line such as:
GET / HTTP/1.1
36 37 38 |
# File 'lib/webrick/httprequest.rb', line 36 def request_line @request_line end |
#request_method ⇒ Object (readonly)
The request method, GET, POST, PUT, etc.
41 42 43 |
# File 'lib/webrick/httprequest.rb', line 41 def request_method @request_method end |
#request_time ⇒ Object (readonly)
The local time this request was received
147 148 149 |
# File 'lib/webrick/httprequest.rb', line 147 def request_time @request_time end |
#request_uri ⇒ Object (readonly)
The parsed URI of the request
58 59 60 |
# File 'lib/webrick/httprequest.rb', line 58 def request_uri @request_uri end |
#script_name ⇒ Object
The script name (CGI variable)
68 69 70 |
# File 'lib/webrick/httprequest.rb', line 68 def script_name @script_name end |
#server_cert ⇒ Object (readonly)
HTTP request server certificate
34 35 36 |
# File 'lib/webrick/https.rb', line 34 def server_cert @server_cert end |
#unparsed_uri ⇒ Object (readonly)
The unparsed URI of the request
46 47 48 |
# File 'lib/webrick/httprequest.rb', line 46 def unparsed_uri @unparsed_uri end |
#user ⇒ Object
The remote user (CGI variable)
122 123 124 |
# File 'lib/webrick/httprequest.rb', line 122 def user @user end |
Instance Method Details
#[](header_name) ⇒ Object
Retrieves header_name
318 319 320 321 322 323 |
# File 'lib/webrick/httprequest.rb', line 318 def [](header_name) if @header value = @header[header_name.downcase] value.empty? ? nil : value.join end end |
#body(&block) ⇒ Object
Returns the request body.
255 256 257 258 259 |
# File 'lib/webrick/httprequest.rb', line 255 def body(&block) # :yields: body_chunk block ||= Proc.new{|chunk| @body << chunk } read_body(@socket, block) @body.empty? ? nil : @body end |
#body_reader ⇒ Object
Prepares the HTTPRequest object for use as the source for IO.copy_stream
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/webrick/httprequest.rb', line 265 def body_reader @body_tmp = [] @body_rd = Fiber.new do body do |buf| @body_tmp << buf Fiber.yield end end @body_rd.resume # grab the first chunk and yield self end |
#content_length ⇒ Object
The content-length header
304 305 306 |
# File 'lib/webrick/httprequest.rb', line 304 def content_length return Integer(self['content-length']) end |
#content_type ⇒ Object
The content-type header
311 312 313 |
# File 'lib/webrick/httprequest.rb', line 311 def content_type return self['content-type'] end |
#continue ⇒ Object
Generate HTTP/1.1 100 continue response if the client expects it, otherwise does nothing.
245 246 247 248 249 250 |
# File 'lib/webrick/httprequest.rb', line 245 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 |
#each ⇒ Object
Iterates over the request headers
328 329 330 331 332 333 334 335 |
# File 'lib/webrick/httprequest.rb', line 328 def each if @header @header.each{|k, v| value = @header[k] yield(k, value.empty? ? nil : value.join) } end end |
#fixup ⇒ Object
Consumes any remaining body and updates keep-alive status
390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/webrick/httprequest.rb', line 390 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 |
#host ⇒ Object
The host this request is for
340 341 342 |
# File 'lib/webrick/httprequest.rb', line 340 def host return @forwarded_host || @host end |
#keep_alive? ⇒ Boolean
Should the connection this request was made on be kept alive?
375 376 377 |
# File 'lib/webrick/httprequest.rb', line 375 def keep_alive? @keep_alive end |
#meta_vars ⇒ Object
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: www.rfc-editor.org/rfc/rfc3875
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/webrick/httprequest.rb', line 407 def = if server_cert ["HTTPS"] = "on" ["SSL_SERVER_CERT"] = @server_cert.to_pem ["SSL_CLIENT_CERT"] = @client_cert ? @client_cert.to_pem : "" if @client_cert_chain @client_cert_chain.each_with_index{|cert, i| ["SSL_CLIENT_CERT_CHAIN_#{i}"] = cert.to_pem } end ["SSL_CIPHER"] = @cipher[0] ["SSL_PROTOCOL"] = @cipher[1] ["SSL_CIPHER_USEKEYSIZE"] = @cipher[2].to_s ["SSL_CIPHER_ALGKEYSIZE"] = @cipher[3].to_s end end |
#orig_meta_vars ⇒ Object
65 |
# File 'lib/webrick/https.rb', line 65 alias |
#orig_parse ⇒ Object
:stopdoc:
43 |
# File 'lib/webrick/https.rb', line 43 alias orig_parse parse |
#orig_parse_uri ⇒ Object
55 |
# File 'lib/webrick/https.rb', line 55 alias orig_parse_uri parse_uri |
#parse(socket = nil) ⇒ Object
Parses a request from socket
. This is called internally by WEBrick::HTTPServer.
193 194 195 196 197 198 199 200 201 |
# File 'lib/webrick/httprequest.rb', line 193 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 |
#port ⇒ Object
The port this request is for
347 348 349 |
# File 'lib/webrick/httprequest.rb', line 347 def port return @forwarded_port || @port end |
#query ⇒ Object
Request query as a Hash
294 295 296 297 298 299 |
# File 'lib/webrick/httprequest.rb', line 294 def query unless @query parse_query() end @query end |
#readpartial(size, buf = ''.b) ⇒ Object
for IO.copy_stream.
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/webrick/httprequest.rb', line 278 def readpartial(size, buf = ''.b) # :nodoc res = @body_tmp.shift or raise EOFError, 'end of file reached' if res.length > size @body_tmp.unshift(res[size..-1]) res = res[0..size - 1] end buf.replace(res) res.clear # get more chunks - check alive? because we can take a partial chunk @body_rd.resume if @body_rd.alive? buf end |
#remote_ip ⇒ Object
The client’s IP address
361 362 363 |
# File 'lib/webrick/httprequest.rb', line 361 def remote_ip return self["client-ip"] || @forwarded_for || @peeraddr[3] end |
#server_name ⇒ Object
The server name this request is for
354 355 356 |
# File 'lib/webrick/httprequest.rb', line 354 def server_name return @forwarded_server || @config[:ServerName] end |
#ssl? ⇒ Boolean
Is this an SSL request?
368 369 370 |
# File 'lib/webrick/httprequest.rb', line 368 def ssl? return @request_uri.scheme == "https" end |
#to_s ⇒ Object
:nodoc:
379 380 381 382 383 384 385 |
# File 'lib/webrick/httprequest.rb', line 379 def to_s # :nodoc: ret = @request_line.dup @raw_header.each{|line| ret << line } ret << CRLF ret << body if body ret end |