Module: Qeweney::RequestInfoMethods

Included in:
Request
Defined in:
lib/qeweney/request_info.rb

Constant Summary collapse

QUERY_KV_REGEXP =
/([^=]+)(?:=(.*))?/
/^([^=]+)=(.*)$/.freeze
SEMICOLON =
';'

Instance Method Summary collapse

Instance Method Details

#accept_encodingObject

TODO: should return encodings in client’s order of preference (and take into account q weights)



78
79
80
81
82
83
# File 'lib/qeweney/request_info.rb', line 78

def accept_encoding
  encoding = @headers['accept-encoding']
  return [] unless encoding

  encoding.split(',').map { |i| i.strip }
end

#connectionObject



13
14
15
# File 'lib/qeweney/request_info.rb', line 13

def connection
  @headers['connection']
end

#cookiesObject



85
86
87
# File 'lib/qeweney/request_info.rb', line 85

def cookies
  @cookies ||= parse_cookies(headers['cookie'])
end

#forwarded_forObject



72
73
74
# File 'lib/qeweney/request_info.rb', line 72

def forwarded_for
  @headers['x-forwarded-for']
end

#full_uriObject



41
42
43
# File 'lib/qeweney/request_info.rb', line 41

def full_uri
  @full_uri = "#{scheme}://#{host}#{uri}"
end

#hostObject Also known as: authority



8
9
10
# File 'lib/qeweney/request_info.rb', line 8

def host
  @headers['host'] || @headers[':authority']
end

#methodObject



29
30
31
# File 'lib/qeweney/request_info.rb', line 29

def method
  @method ||= @headers[':method'].downcase
end

#parse_cookies(cookies) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/qeweney/request_info.rb', line 92

def parse_cookies(cookies)
  return {} unless cookies

  cookies.split(SEMICOLON).each_with_object({}) do |c, h|
    raise BadRequestError, 'Invalid cookie format' unless c.strip =~ COOKIE_RE

    key, value = Regexp.last_match[1..2]
    h[key] = EscapeUtils.unescape_uri(value)
  end
end

#parse_query(query) ⇒ Object



61
62
63
64
65
66
# File 'lib/qeweney/request_info.rb', line 61

def parse_query(query)
  query.split('&').each_with_object({}) do |kv, h|
    k, v = kv.match(QUERY_KV_REGEXP)[1..2]
    h[k.to_sym] = v ? URI.decode_www_form_component(v) : true
  end
end

#pathObject



45
46
47
# File 'lib/qeweney/request_info.rb', line 45

def path
  @path ||= uri.path
end

#protocolObject



25
26
27
# File 'lib/qeweney/request_info.rb', line 25

def protocol
  @protocol ||= @adapter.protocol
end

#queryObject



53
54
55
56
57
# File 'lib/qeweney/request_info.rb', line 53

def query
  return @query if @query

  @query = (q = uri.query) ? parse_query(q) : {}
end

#query_stringObject



49
50
51
# File 'lib/qeweney/request_info.rb', line 49

def query_string
  @query_string ||= uri.query
end

#request_idObject



68
69
70
# File 'lib/qeweney/request_info.rb', line 68

def request_id
  @headers['x-request-id']
end

#schemeObject



33
34
35
# File 'lib/qeweney/request_info.rb', line 33

def scheme
  @scheme ||= @headers[':scheme']
end

#upgrade_protocolObject



17
18
19
# File 'lib/qeweney/request_info.rb', line 17

def upgrade_protocol
  connection == 'upgrade' && @headers['upgrade']&.downcase
end

#uriObject



37
38
39
# File 'lib/qeweney/request_info.rb', line 37

def uri
  @uri ||= URI.parse(@headers[':path'] || '')
end

#websocket_versionObject



21
22
23
# File 'lib/qeweney/request_info.rb', line 21

def websocket_version
  headers['sec-websocket-version'].to_i
end