Class: Midori::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/midori/request.rb

Overview

Request class for midori

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

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

#bodyString

request body

Returns:

  • (String)

    the current value of body



17
18
19
# File 'lib/midori/request.rb', line 17

def body
  @body
end

#body_parsedBoolean

whether the request body parsed

Returns:

  • (Boolean)

    the current value of body_parsed



17
18
19
# File 'lib/midori/request.rb', line 17

def body_parsed
  @body_parsed
end

cookie hash coming from request

Returns:

  • (Hash)

    the current value of cookie



17
18
19
# File 'lib/midori/request.rb', line 17

def cookie
  @cookie
end

#headerHTTPHeader

request header

Returns:



17
18
19
# File 'lib/midori/request.rb', line 17

def header
  @header
end

#ipString

client ip address

Returns:

  • (String)

    the current value of ip



17
18
19
# File 'lib/midori/request.rb', line 17

def ip
  @ip
end

#methodSymbol

HTTP method

Returns:

  • (Symbol)

    the current value of method



17
18
19
# File 'lib/midori/request.rb', line 17

def method
  @method
end

#paramsHash

params in the url

Returns:

  • (Hash)

    the current value of params



17
18
19
# File 'lib/midori/request.rb', line 17

def params
  @params
end

#parsedBoolean

whether the request header parsed

Returns:

  • (Boolean)

    the current value of parsed



17
18
19
# File 'lib/midori/request.rb', line 17

def parsed
  @parsed
end

#pathString

request path

Returns:

  • (String)

    the current value of path



17
18
19
# File 'lib/midori/request.rb', line 17

def path
  @path
end

#portInteger

client port

Returns:

  • (Integer)

    the current value of port



17
18
19
# File 'lib/midori/request.rb', line 17

def port
  @port
end

#protocolString

protocol version of HTTP request

Returns:

  • (String)

    the current value of protocol



17
18
19
# File 'lib/midori/request.rb', line 17

def protocol
  @protocol
end

#query_paramsHash

parameter parsed from query string

Returns:

  • (Hash)

    the current value of query_params



17
18
19
# File 'lib/midori/request.rb', line 17

def query_params
  @query_params
end

#query_stringString | nil

request query string

Returns:

  • (String | nil)

    the current value of query_string



17
18
19
# File 'lib/midori/request.rb', line 17

def query_string
  @query_string
end

#remote_ipObject

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

Returns:

  • (Boolean)

    parsed or not



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

Returns:

  • (Boolean)

    eventsource or not



134
135
136
# File 'lib/midori/request.rb', line 134

def eventsource?
  @is_eventsource
end

#parse(data) ⇒ nil

Init an request with String data

Parameters:

Returns:

  • (nil)

    nil



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_ipString | nil

Get the real user IP from headers Modified from Rack

Returns:

  • (String | nil)

    nil when not available, otherwise, return the real IP



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

Returns:

  • (Boolean)

    parsed or not



116
117
118
# File 'lib/midori/request.rb', line 116

def parsed?
  @parsed
end

#pre_proceednil

Preproceed the request after parsed

Returns:

  • (nil)

    nil



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

Returns:

  • (Boolean)

    websocket or not



128
129
130
# File 'lib/midori/request.rb', line 128

def websocket?
  @is_websocket
end