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 ⇒ HTTPHeader
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_params ⇒ Hash
parameter parsed from query string.
-
#query_string ⇒ String | nil
request query string.
-
#remote_ip ⇒ Object
Returns the value of attribute remote_ip.
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.
-
#parse_ip ⇒ String | nil
Get the real user IP from headers Modified from Rack.
-
#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
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 52 53 54 55 |
# File 'lib/midori/request.rb', line 23 def initialize @header = HTTPHeader.new @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 # Turn header into case-insensitive due to RFC 2.6 Chapter 4.2 # https://www.ietf.org/rfc/rfc2616.txt @parser.headers.each { |key, value| @header[key] = value } @remote_ip = parse_ip || @ip # Detect client real IP with RFC 7239 @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
17 18 19 |
# File 'lib/midori/request.rb', line 17 def body_parsed @body_parsed end |
#cookie ⇒ Hash
cookie hash coming from request
17 18 19 |
# File 'lib/midori/request.rb', line 17 def @cookie end |
#header ⇒ HTTPHeader
request header
17 18 19 |
# File 'lib/midori/request.rb', line 17 def header @header end |
#method ⇒ Symbol
HTTP method
17 18 19 |
# File 'lib/midori/request.rb', line 17 def method @method end |
#params ⇒ Hash
params in the url
17 18 19 |
# File 'lib/midori/request.rb', line 17 def params @params end |
#parsed ⇒ Boolean
whether the request header parsed
17 18 19 |
# File 'lib/midori/request.rb', line 17 def parsed @parsed end |
#port ⇒ Integer
client port
17 18 19 |
# File 'lib/midori/request.rb', line 17 def port @port end |
#protocol ⇒ String
protocol version of HTTP request
17 18 19 |
# File 'lib/midori/request.rb', line 17 def protocol @protocol end |
#query_params ⇒ Hash
parameter parsed from query string
17 18 19 |
# File 'lib/midori/request.rb', line 17 def query_params @query_params end |
#query_string ⇒ String | nil
request query string
17 18 19 |
# File 'lib/midori/request.rb', line 17 def query_string @query_string end |
#remote_ip ⇒ Object
Returns the value of attribute remote_ip.
18 19 20 |
# File 'lib/midori/request.rb', line 18 def remote_ip @remote_ip end |
Instance Method Details
#body_parsed? ⇒ Boolean
Syntactic sugar for whether a request body is parsed
122 123 124 |
# File 'lib/midori/request.rb', line 122 def body_parsed? @body_parsed end |
#eventsource? ⇒ Boolean
Syntactic sugar for whether a request is an eventsource request
134 135 136 |
# File 'lib/midori/request.rb', line 134 def eventsource? @is_eventsource end |
#parse(data) ⇒ nil
Init an request with String data
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/midori/request.rb', line 60 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 |
#parse_ip ⇒ String | nil
Get the real user IP from headers Modified from Rack
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/midori/request.rb', line 80 def parse_ip # Do not parse anything if not behind proxy return nil unless Midori::Configure.proxy return @header['X-Real-IP'] if Midori::Configure.trust_real_ip # Not enough infomation return nil if @header['X-Forwarded-For'].nil? forwarded_ips = @header['X-Forwarded-For'].split(', ') # Spoofing check trusted = forwarded_ips.reject do |ip| ip =~ Midori::Configure.trusted_proxies end trusted.last end |
#parsed? ⇒ Boolean
Syntactic sugar for whether a request header is parsed
116 117 118 |
# File 'lib/midori/request.rb', line 116 def parsed? @parsed end |
#pre_proceed ⇒ nil
Preproceed the request after parsed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/midori/request.rb', line 96 def pre_proceed # Deal with WebSocket upgrade = @header['Connection']&.split(', ')&.include?('Upgrade') if upgrade && @header['Upgrade'] == 'websocket' @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
128 129 130 |
# File 'lib/midori/request.rb', line 128 def websocket? @is_websocket end |