Module: Qeweney::RequestInfoMethods
- Included in:
- Request
- Defined in:
- lib/qeweney/request_info.rb
Constant Summary collapse
- QUERY_KV_REGEXP =
/([^=]+)(?:=(.*))?/
- COOKIE_RE =
/^([^=]+)=(.*)$/.freeze
- SEMICOLON =
';'
Instance Method Summary collapse
-
#accept_encoding ⇒ Object
TODO: should return encodings in client’s order of preference (and take into account q weights).
- #connection ⇒ Object
- #cookies ⇒ Object
- #forwarded_for ⇒ Object
- #full_uri ⇒ Object
- #host ⇒ Object (also: #authority)
- #method ⇒ Object
- #parse_cookies(cookies) ⇒ Object
- #parse_query(query) ⇒ Object
- #path ⇒ Object
- #protocol ⇒ Object
- #query ⇒ Object
- #query_string ⇒ Object
- #request_id ⇒ Object
-
#rewrite!(src, replacement) ⇒ Qeweney::Request
Rewrites the request path by replacing the given src with the given replacement.
- #scheme ⇒ Object
- #upgrade_protocol ⇒ Object
- #uri ⇒ Object
- #websocket_version ⇒ Object
Instance Method Details
#accept_encoding ⇒ Object
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 |
#connection ⇒ Object
13 14 15 |
# File 'lib/qeweney/request_info.rb', line 13 def connection @headers['connection'] end |
#cookies ⇒ Object
101 102 103 |
# File 'lib/qeweney/request_info.rb', line 101 def @cookies ||= (headers['cookie']) end |
#forwarded_for ⇒ Object
88 89 90 |
# File 'lib/qeweney/request_info.rb', line 88 def forwarded_for @headers['x-forwarded-for'] end |
#full_uri ⇒ Object
57 58 59 |
# File 'lib/qeweney/request_info.rb', line 57 def full_uri @full_uri = "#{scheme}://#{host}#{uri}" end |
#host ⇒ Object Also known as:
8 9 10 |
# File 'lib/qeweney/request_info.rb', line 8 def host @headers['host'] || @headers[':authority'] end |
#method ⇒ Object
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 () return {} unless .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 |
#path ⇒ Object
61 62 63 |
# File 'lib/qeweney/request_info.rb', line 61 def path @path ||= uri.path end |
#protocol ⇒ Object
25 26 27 |
# File 'lib/qeweney/request_info.rb', line 25 def protocol @protocol ||= @adapter.protocol end |
#query ⇒ Object
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_string ⇒ Object
65 66 67 |
# File 'lib/qeweney/request_info.rb', line 65 def query_string @query_string ||= uri.query end |
#request_id ⇒ Object
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.
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 |
#scheme ⇒ Object
33 34 35 |
# File 'lib/qeweney/request_info.rb', line 33 def scheme @scheme ||= @headers[':scheme'] end |
#upgrade_protocol ⇒ Object
17 18 19 |
# File 'lib/qeweney/request_info.rb', line 17 def upgrade_protocol connection == 'upgrade' && @headers['upgrade']&.downcase end |
#uri ⇒ Object
53 54 55 |
# File 'lib/qeweney/request_info.rb', line 53 def uri @uri ||= URI.parse(@headers[':path'] || '') end |
#websocket_version ⇒ Object
21 22 23 |
# File 'lib/qeweney/request_info.rb', line 21 def websocket_version headers['sec-websocket-version'].to_i end |