Class: Midori::Request
- Inherits:
-
Object
- Object
- Midori::Request
- Defined in:
- lib/midori/request.rb
Overview
Request class for midori
Instance Attribute Summary collapse
-
#body ⇒ String
request body.
-
#body_parsed ⇒ Boolean
whether the request body parsed.
-
#cookie ⇒ Hash
cookie hash coming from request.
-
#header ⇒ Hash
request header.
-
#ip ⇒ String
client ip address.
-
#method ⇒ Symbol
HTTP method.
-
#params ⇒ Hash
params in the url.
-
#parsed ⇒ Boolean
whether the request header parsed.
-
#path ⇒ String
request path.
-
#port ⇒ Integer
client port.
-
#protocol ⇒ String
protocol version of HTTP request.
-
#query_param ⇒ Hash
parameter parsed from query string.
-
#query_params ⇒ Object
Returns the value of attribute query_params.
-
#query_string ⇒ String | nil
request query string.
Instance Method Summary collapse
-
#body_parsed? ⇒ Boolean
Syntactic sugar for whether a request body is parsed.
-
#eventsource? ⇒ Boolean
Syntactic sugar for whether a request is an eventsource request.
-
#initialize ⇒ Request
constructor
Init Request.
-
#parse(data) ⇒ nil
Init an request with String data.
-
#parsed? ⇒ Boolean
Syntactic sugar for whether a request header is parsed.
-
#pre_proceed ⇒ nil
Preproceed the request after parsed.
-
#websocket? ⇒ Boolean
Syntactic sugar for whether a request is a websocket request.
Constructor Details
#initialize ⇒ Request
Init Request
22 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 |
# File 'lib/midori/request.rb', line 22 def initialize @header = {} @parsed = false @body_parsed = false @is_websocket = false @is_eventsource = false @parser = Http::Parser.new @params = {} @query_params = Hash.new(Array.new) @cookie = {} @body = '' @parser.on_headers_complete = proc do @protocol = @parser.http_version @method = @parser.http_method @path = @parser.request_url @header = @parser.headers @query_string = @path.match(/\?(.*?)$/) unless @query_string.nil? @query_string = @query_string[1] @query_params = CGI::parse(@query_string) end @cookie = CGI::Cookie.parse(@header['Cookie']) unless @header['Cookie'].nil? @path.gsub!(/\?(.*?)$/, '') @method = @method.to_sym @parsed = true :stop end end |
Instance Attribute Details
#body_parsed ⇒ Boolean
whether the request body parsed
16 17 18 |
# File 'lib/midori/request.rb', line 16 def body_parsed @body_parsed end |
#cookie ⇒ Hash
cookie hash coming from request
16 17 18 |
# File 'lib/midori/request.rb', line 16 def @cookie end |
#header ⇒ Hash
request header
16 17 18 |
# File 'lib/midori/request.rb', line 16 def header @header end |
#method ⇒ Symbol
HTTP method
16 17 18 |
# File 'lib/midori/request.rb', line 16 def method @method end |
#params ⇒ Hash
params in the url
16 17 18 |
# File 'lib/midori/request.rb', line 16 def params @params end |
#parsed ⇒ Boolean
whether the request header parsed
16 17 18 |
# File 'lib/midori/request.rb', line 16 def parsed @parsed end |
#port ⇒ Integer
client port
16 17 18 |
# File 'lib/midori/request.rb', line 16 def port @port end |
#protocol ⇒ String
protocol version of HTTP request
16 17 18 |
# File 'lib/midori/request.rb', line 16 def protocol @protocol end |
#query_param ⇒ Hash
parameter parsed from query string
16 17 18 |
# File 'lib/midori/request.rb', line 16 def query_param @query_param end |
#query_params ⇒ Object
Returns the value of attribute query_params.
17 18 19 |
# File 'lib/midori/request.rb', line 17 def query_params @query_params end |
#query_string ⇒ String | nil
request query string
16 17 18 |
# File 'lib/midori/request.rb', line 16 def query_string @query_string end |
Instance Method Details
#body_parsed? ⇒ Boolean
Syntactic sugar for whether a request body is parsed
100 101 102 |
# File 'lib/midori/request.rb', line 100 def body_parsed? @body_parsed end |
#eventsource? ⇒ Boolean
Syntactic sugar for whether a request is an eventsource request
112 113 114 |
# File 'lib/midori/request.rb', line 112 def eventsource? @is_eventsource end |
#parse(data) ⇒ nil
Init an request with String data
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/midori/request.rb', line 56 def parse(data) # Call parser if header not parsed if @parsed @body += data else offset = @parser << data @body += data[offset..-1] if @parsed end # Set body parsed if body reaches content length if @parsed && (@header['Content-Length'].to_i || 0) <= @body.bytesize @body_parsed = true pre_proceed end nil end |
#parsed? ⇒ Boolean
Syntactic sugar for whether a request header is parsed
94 95 96 |
# File 'lib/midori/request.rb', line 94 def parsed? @parsed end |
#pre_proceed ⇒ nil
Preproceed the request after parsed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/midori/request.rb', line 75 def pre_proceed # Deal with WebSocket if @header['Upgrade'] == 'websocket' && @header['Connection'] == 'Upgrade' @method = :WEBSOCKET @is_websocket = true end # Deal with EventSource if @header['Accept'] == 'text/event-stream' @method = :EVENTSOURCE @is_eventsource = true end @method = @method.to_sym nil end |
#websocket? ⇒ Boolean
Syntactic sugar for whether a request is a websocket request
106 107 108 |
# File 'lib/midori/request.rb', line 106 def websocket? @is_websocket end |