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.
-
#header ⇒ Hash
request header.
-
#ip ⇒ String
client ip address.
-
#method ⇒ String
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_string ⇒ String
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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/midori/request.rb', line 20 def initialize @parsed = false @body_parsed = false @is_websocket = false @is_eventsource = false @parser = Http::Parser.new @params = {} @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(/\?(.*?)$/) @query_string = @query_string[1] unless @query_string.nil? @path.gsub!(/\?(.*?)$/, '') @method = @method.to_sym @parsed = true :stop end end |
Instance Attribute Details
#body_parsed ⇒ Boolean
whether the request body parsed
14 15 16 |
# File 'lib/midori/request.rb', line 14 def body_parsed @body_parsed end |
#header ⇒ Hash
request header
14 15 16 |
# File 'lib/midori/request.rb', line 14 def header @header end |
#method ⇒ String
HTTP method
14 15 16 |
# File 'lib/midori/request.rb', line 14 def method @method end |
#params ⇒ Hash
params in the url
14 15 16 |
# File 'lib/midori/request.rb', line 14 def params @params end |
#parsed ⇒ Boolean
whether the request header parsed
14 15 16 |
# File 'lib/midori/request.rb', line 14 def parsed @parsed end |
#port ⇒ Integer
client port
14 15 16 |
# File 'lib/midori/request.rb', line 14 def port @port end |
#protocol ⇒ String
protocol version of HTTP request
14 15 16 |
# File 'lib/midori/request.rb', line 14 def protocol @protocol end |
#query_string ⇒ String
request query string
14 15 16 |
# File 'lib/midori/request.rb', line 14 def query_string @query_string end |
Instance Method Details
#body_parsed? ⇒ Boolean
Syntactic sugar for whether a request body is parsed
90 91 92 |
# File 'lib/midori/request.rb', line 90 def body_parsed? @body_parsed end |
#eventsource? ⇒ Boolean
Syntactic sugar for whether a request is an eventsource request
102 103 104 |
# File 'lib/midori/request.rb', line 102 def eventsource? @is_eventsource end |
#parse(data) ⇒ nil
Init an request with String data
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/midori/request.rb', line 46 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 (@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
84 85 86 |
# File 'lib/midori/request.rb', line 84 def parsed? @parsed end |
#pre_proceed ⇒ nil
Preproceed the request after parsed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/midori/request.rb', line 65 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
96 97 98 |
# File 'lib/midori/request.rb', line 96 def websocket? @is_websocket end |