Module: Syntropy::RequestInfoMethods
- Included in:
- Request
- Defined in:
- lib/syntropy/request/request_info.rb
Overview
Request information extension methods.
Constant Summary collapse
- QUERY_KV_REGEXP =
/([^=]+)(?:=(.*))?/- COOKIE_RE =
/^([^=]+)=(.*)$/.freeze
- SEMICOLON =
';'
Instance Method Summary collapse
-
#accept?(mime_type) ⇒ bool
Returns true if the accept header includes the given MIME type.
-
#accept_encoding ⇒ Object
TODO: should return encodings in client’s order of preference (and take into account q weights).
-
#auth_bearer_token ⇒ String?
Returns the bearer token.
-
#browser? ⇒ bool
Returns true if the user-agent is a browser.
-
#connection ⇒ String?
Returns the connection header value.
-
#content_type ⇒ String?
Returns the request content type.
-
#cookies ⇒ String?
Returns the parsed cookie values.
-
#forwarded_for ⇒ String?
Returns the forwarded for value.
-
#full_uri ⇒ URI::HTTP
Returns the parsed full request URI.
-
#get_form_data ⇒ Hash
Reads the request body and returns form data.
-
#host ⇒ String?
(also: #authority)
Returns the request host.
-
#method ⇒ String
Returns the HTTP method in lower case.
-
#parse_cookies(cookies) ⇒ Hash
Parses the cookie string.
-
#parse_query(query) ⇒ Hash
Converts a query string into a query hash.
-
#path ⇒ String
Returns the request path.
-
#protocol ⇒ String?
Returns the protocol.
-
#query ⇒ Hash
Returns the parsed query hash.
-
#query_string ⇒ String?
Returns the request (unparsed) query string.
-
#request_id ⇒ String?
Returns the request ID.
-
#rewrite!(src, replacement) ⇒ Syntropy::Request
Rewrites the request path by replacing the given src with the given replacement.
-
#scheme ⇒ String?
Returns the request scheme.
-
#upgrade_protocol ⇒ String?
Returns the upgrade protocol.
-
#uri ⇒ URI::Generic
Returns the parsed request URI.
-
#websocket_version ⇒ String?
Returns the websocket version.
Instance Method Details
#accept?(mime_type) ⇒ bool
Returns true if the accept header includes the given MIME type
211 212 213 214 215 216 217 |
# File 'lib/syntropy/request/request_info.rb', line 211 def accept?(mime_type) accept = headers['accept'] return nil if !accept @accept_parts ||= parse_accept_parts(accept) @accept_parts.include?(mime_type) end |
#accept_encoding ⇒ Object
TODO: should return encodings in client’s order of preference (and take into account q weights)
153 154 155 156 157 158 |
# File 'lib/syntropy/request/request_info.rb', line 153 def accept_encoding encoding = @headers['accept-encoding'] return [] unless encoding encoding.split(',').map { |i| i.strip } end |
#auth_bearer_token ⇒ String?
Returns the bearer token.
222 223 224 225 226 227 228 229 |
# File 'lib/syntropy/request/request_info.rb', line 222 def auth_bearer_token auth = headers['authorization'] if auth && (m = auth.match(/Bearer\s+([^\w]+)/)) return m[1] end nil end |
#browser? ⇒ bool
Returns true if the user-agent is a browser.
202 203 204 205 |
# File 'lib/syntropy/request/request_info.rb', line 202 def browser? user_agent = headers['user-agent'] user_agent && user_agent =~ /^Mozilla\// end |
#connection ⇒ String?
Returns the connection header value.
19 20 21 |
# File 'lib/syntropy/request/request_info.rb', line 19 def connection @headers['connection'] end |
#content_type ⇒ String?
Returns the request content type.
61 62 63 64 65 66 67 68 69 |
# File 'lib/syntropy/request/request_info.rb', line 61 def content_type ct = @headers['content-type'] return nil if !ct m = ct.match(/^([^;]+)/) return nil if !m m[1].strip end |
#cookies ⇒ String?
Returns the parsed cookie values.
163 164 165 |
# File 'lib/syntropy/request/request_info.rb', line 163 def @cookies ||= (headers['cookie']) end |
#forwarded_for ⇒ String?
Returns the forwarded for value.
147 148 149 |
# File 'lib/syntropy/request/request_info.rb', line 147 def forwarded_for @headers['x-forwarded-for'] end |
#full_uri ⇒ URI::HTTP
Returns the parsed full request URI.
97 98 99 |
# File 'lib/syntropy/request/request_info.rb', line 97 def full_uri @full_uri = "#{scheme}://#{host}#{uri}" end |
#get_form_data ⇒ Hash
Reads the request body and returns form data.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/syntropy/request/request_info.rb', line 188 def get_form_data body = read if !body || body.empty? raise Syntropy::Error.new('Missing form data', HTTP::BAD_REQUEST) end Syntropy::Request.parse_form_data(body, headers) rescue Syntropy::BadRequestError raise Syntropy::Error.new('Invalid form data', HTTP::BAD_REQUEST) end |
#host ⇒ String? Also known as:
Returns the request host.
11 12 13 |
# File 'lib/syntropy/request/request_info.rb', line 11 def host @headers['host'] || @headers[':authority'] end |
#method ⇒ String
Returns the HTTP method in lower case.
47 48 49 |
# File 'lib/syntropy/request/request_info.rb', line 47 def method @method ||= @headers[':method'].downcase end |
#parse_cookies(cookies) ⇒ Hash
Parses the cookie string.
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/syntropy/request/request_info.rb', line 174 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] = URI.decode_www_form_component(value) end end |
#parse_query(query) ⇒ Hash
Converts a query string into a query hash
130 131 132 133 134 135 |
# File 'lib/syntropy/request/request_info.rb', line 130 def parse_query(query) query.split('&').each_with_object({}) do |kv, h| k, v = kv.match(QUERY_KV_REGEXP)[1..2] h[k] = v ? URI.decode_www_form_component(v) : true end end |
#path ⇒ String
Returns the request path.
104 105 106 |
# File 'lib/syntropy/request/request_info.rb', line 104 def path @path ||= uri.path end |
#protocol ⇒ String?
Returns the protocol.
40 41 42 |
# File 'lib/syntropy/request/request_info.rb', line 40 def protocol @protocol ||= @adapter.protocol end |
#query ⇒ Hash
Returns the parsed query hash.
118 119 120 121 122 |
# File 'lib/syntropy/request/request_info.rb', line 118 def query return @query if @query @query = (q = uri.query) ? parse_query(q) : {} end |
#query_string ⇒ String?
Returns the request (unparsed) query string.
111 112 113 |
# File 'lib/syntropy/request/request_info.rb', line 111 def query_string @query_string ||= uri.query end |
#request_id ⇒ String?
Returns the request ID.
140 141 142 |
# File 'lib/syntropy/request/request_info.rb', line 140 def request_id @headers['x-request-id'] end |
#rewrite!(src, replacement) ⇒ Syntropy::Request
Rewrites the request path by replacing the given src with the given replacement.
77 78 79 80 81 82 83 84 85 |
# File 'lib/syntropy/request/request_info.rb', line 77 def rewrite!(src, replacement) @headers[':path'] = @headers[':path'] .gsub(src, replacement) .gsub('//', '/') @path = nil @uri = nil @full_uri = nil self end |
#scheme ⇒ String?
Returns the request scheme.
54 55 56 |
# File 'lib/syntropy/request/request_info.rb', line 54 def scheme @scheme ||= @headers[':scheme'] end |
#upgrade_protocol ⇒ String?
Returns the upgrade protocol.
26 27 28 |
# File 'lib/syntropy/request/request_info.rb', line 26 def upgrade_protocol connection == 'upgrade' && @headers['upgrade']&.downcase end |
#uri ⇒ URI::Generic
Returns the parsed request URI.
90 91 92 |
# File 'lib/syntropy/request/request_info.rb', line 90 def uri @uri ||= URI.parse(@headers[':path'] || '') end |
#websocket_version ⇒ String?
Returns the websocket version.
33 34 35 |
# File 'lib/syntropy/request/request_info.rb', line 33 def websocket_version headers['sec-websocket-version'].to_i end |