Class: ServerSide::HTTP::Request
- Includes:
- Parsing
- Defined in:
- lib/serverside/http/request.rb
Constant Summary collapse
- HOST_PORT_RE =
/^([^:]+):(.+)$/.freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#header_count ⇒ Object
readonly
Returns the value of attribute header_count.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_version ⇒ Object
readonly
Returns the value of attribute http_version.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#persistent ⇒ Object
readonly
Returns the value of attribute persistent.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#request_line ⇒ Object
readonly
Returns the value of attribute request_line.
Instance Method Summary collapse
-
#accept?(re) ⇒ Boolean
Returns true if the accept header contains the supplied pattern.
-
#client_name ⇒ Object
Returns the client name.
- #content_type ⇒ Object
-
#encrypted? ⇒ Boolean
Returns true if the request was received on port 443.
-
#host ⇒ Object
Returns the host specified in the Host header.
-
#initialize(conn) ⇒ Request
constructor
A new instance of Request.
-
#parse_host_header ⇒ Object
Parses the Host header.
-
#port ⇒ Object
Returns the port number if specified in the Host header.
- #user_agent ⇒ Object
Constructor Details
#initialize(conn) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 16 |
# File 'lib/serverside/http/request.rb', line 11 def initialize(conn) @conn = conn @headers = {} @header_count = 0 @cookies = {} end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/serverside/http/request.rb', line 9 def body @body end |
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
8 9 10 |
# File 'lib/serverside/http/request.rb', line 8 def content_length @content_length end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
9 10 11 |
# File 'lib/serverside/http/request.rb', line 9 def @cookies end |
#header_count ⇒ Object (readonly)
Returns the value of attribute header_count.
8 9 10 |
# File 'lib/serverside/http/request.rb', line 8 def header_count @header_count end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
8 9 10 |
# File 'lib/serverside/http/request.rb', line 8 def headers @headers end |
#http_version ⇒ Object (readonly)
Returns the value of attribute http_version.
7 8 9 |
# File 'lib/serverside/http/request.rb', line 7 def http_version @http_version end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/serverside/http/request.rb', line 7 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
7 8 9 |
# File 'lib/serverside/http/request.rb', line 7 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/serverside/http/request.rb', line 7 def path @path end |
#persistent ⇒ Object (readonly)
Returns the value of attribute persistent.
8 9 10 |
# File 'lib/serverside/http/request.rb', line 8 def persistent @persistent end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
7 8 9 |
# File 'lib/serverside/http/request.rb', line 7 def query @query end |
#request_line ⇒ Object (readonly)
Returns the value of attribute request_line.
7 8 9 |
# File 'lib/serverside/http/request.rb', line 7 def request_line @request_line end |
Instance Method Details
#accept?(re) ⇒ Boolean
Returns true if the accept header contains the supplied pattern.
64 65 66 67 |
# File 'lib/serverside/http/request.rb', line 64 def accept?(re) re = Regexp.new(re) unless Regexp === re (h = @headers[:accept]) && (h =~ re) && true end |
#client_name ⇒ Object
Returns the client name. The client name is either the value of the X-Forwarded-For header, or the result of get_peername.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/serverside/http/request.rb', line 51 def client_name unless @client_name @client_name = @headers[:x_forwarded_for] unless @client_name if addr = @conn.get_peername p, @client_name = Socket.unpack_sockaddr_in(addr) end end end @client_name end |
#content_type ⇒ Object
73 74 75 76 77 |
# File 'lib/serverside/http/request.rb', line 73 def content_type if t = @headers[:content_type] t =~ /(.*);/ ? $1.strip : t end end |
#encrypted? ⇒ Boolean
Returns true if the request was received on port 443
31 32 33 |
# File 'lib/serverside/http/request.rb', line 31 def encrypted? port == 443 end |
#host ⇒ Object
Returns the host specified in the Host header.
19 20 21 22 |
# File 'lib/serverside/http/request.rb', line 19 def host parse_host_header unless @host_header_parsed @host end |
#parse_host_header ⇒ Object
Parses the Host header.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/serverside/http/request.rb', line 38 def parse_host_header h = @headers[:host] if h =~ HOST_PORT_RE @host = $1 @port = $2.to_i else @host = h end @host_header_parsed = true end |
#port ⇒ Object
Returns the port number if specified in the Host header.
25 26 27 28 |
# File 'lib/serverside/http/request.rb', line 25 def port parse_host_header unless @host_header_parsed @port end |
#user_agent ⇒ Object
69 70 71 |
# File 'lib/serverside/http/request.rb', line 69 def user_agent @headers[:user_agent] end |