Module: Syntropy::RequestInfoMethods

Included in:
Request
Defined in:
lib/syntropy/request/request_info.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#accept?(mime_type) ⇒ bool

Returns true if the accept header includes the given MIME type

Parameters:

  • mime_type (String)

    MIME type

Returns:

  • (bool)


152
153
154
155
156
157
158
# File 'lib/syntropy/request/request_info.rb', line 152

def accept?(mime_type)
  accept = headers['accept']
  return nil if !accept

  @accept_parts ||= parse_accept_parts(accept)
  @accept_parts.include?(mime_type)
end

#accept_encodingObject

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



104
105
106
107
108
109
# File 'lib/syntropy/request/request_info.rb', line 104

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

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

#auth_bearer_tokenObject



160
161
162
163
164
165
166
167
# File 'lib/syntropy/request/request_info.rb', line 160

def auth_bearer_token
  auth = headers['authorization']
  if (m = auth.match(/Bearer\s+([^\w]+)/))
    return m[1]
  end

  nil
end

#browser?Boolean

Returns:

  • (Boolean)


143
144
145
146
# File 'lib/syntropy/request/request_info.rb', line 143

def browser?
  user_agent = headers['user-agent']
  user_agent && user_agent =~ /^Mozilla\//
end

#connectionObject



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

def connection
  @headers['connection']
end

#content_typeObject



37
38
39
40
41
42
43
44
45
# File 'lib/syntropy/request/request_info.rb', line 37

def content_type
  ct = @headers['content-type']
  return nil if !ct

  m = ct.match(/^([^;]+)/)
  return nil if !m

  m[1].strip
end

#cookiesObject



111
112
113
# File 'lib/syntropy/request/request_info.rb', line 111

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

#forwarded_forObject



98
99
100
# File 'lib/syntropy/request/request_info.rb', line 98

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

#full_uriObject



67
68
69
# File 'lib/syntropy/request/request_info.rb', line 67

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

#get_form_dataHash

Reads the request body and returns form data.

Returns:

  • (Hash)

    form data



132
133
134
135
136
137
138
139
140
141
# File 'lib/syntropy/request/request_info.rb', line 132

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

#hostObject Also known as: authority



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

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

#methodObject



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

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

#parse_cookies(cookies) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/syntropy/request/request_info.rb', line 118

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



87
88
89
90
91
92
# File 'lib/syntropy/request/request_info.rb', line 87

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

#pathObject



71
72
73
# File 'lib/syntropy/request/request_info.rb', line 71

def path
  @path ||= uri.path
end

#protocolObject



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

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

#queryObject



79
80
81
82
83
# File 'lib/syntropy/request/request_info.rb', line 79

def query
  return @query if @query

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

#query_stringObject



75
76
77
# File 'lib/syntropy/request/request_info.rb', line 75

def query_string
  @query_string ||= uri.query
end

#request_idObject



94
95
96
# File 'lib/syntropy/request/request_info.rb', line 94

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.

Parameters:

  • src (String, Regexp)

    src pattern

  • replacement (String)

    replacement

Returns:



53
54
55
56
57
58
59
60
61
# File 'lib/syntropy/request/request_info.rb', line 53

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/syntropy/request/request_info.rb', line 33

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

#upgrade_protocolObject



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

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

#uriObject



63
64
65
# File 'lib/syntropy/request/request_info.rb', line 63

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

#websocket_versionObject



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

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