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)



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

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



101
102
103
# File 'lib/qeweney/request_info.rb', line 101

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

#forwarded_forObject



88
89
90
# File 'lib/qeweney/request_info.rb', line 88

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

#full_uriObject



57
58
59
# File 'lib/qeweney/request_info.rb', line 57

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/qeweney/request_info.rb', line 108

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



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

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



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

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



69
70
71
72
73
# File 'lib/qeweney/request_info.rb', line 69

def query
  return @query if @query

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

#query_stringObject



65
66
67
# File 'lib/qeweney/request_info.rb', line 65

def query_string
  @query_string ||= uri.query
end

#request_idObject



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

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

#rewrite!(src, replacement) ⇒ Qeweney::Request

Rewrites the request path by replacing the given src with the given replacement.

Parameters:

  • src (String, Regexp)

    src pattern

  • replacement (String)

    replacement

Returns:



43
44
45
46
47
48
49
50
51
# File 'lib/qeweney/request_info.rb', line 43

def rewrite!(src, replacement)
  @headers[':path'] = @headers[':path']
    .gsub(src, replacement)
    .gsub('//', '/')
  @path = nil
  @uri = nil
  @full_uri = nil
  self
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



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

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