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)


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

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)



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

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

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

#auth_bearer_tokenObject



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

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

  nil
end

#browser?Boolean

Returns:

  • (Boolean)


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

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

#connectionObject



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

def connection
  @headers['connection']
end

#content_typeObject



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

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

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

  m[1].strip
end

#cookiesObject



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

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

#forwarded_forObject



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

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

#full_uriObject



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

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

#get_form_dataHash

Reads the request body and returns form data.

Returns:

  • (Hash)

    form data



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

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



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

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

#methodObject



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

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

#parse_cookies(cookies) ⇒ Object



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

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] = URI.decode_www_form_component(value)
  end
end

#parse_query(query) ⇒ Object



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

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



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

def path
  @path ||= uri.path
end

#protocolObject



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

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

#queryObject



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

def query
  return @query if @query

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

#query_stringObject



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

def query_string
  @query_string ||= uri.query
end

#request_idObject



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

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:



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

def rewrite!(src, replacement)
  @headers[':path'] = @headers[':path']
    .gsub(src, replacement)
    .gsub('//', '/')
  @path = nil
  @uri = nil
  @full_uri = nil
  self
end

#schemeObject



32
33
34
# File 'lib/syntropy/request/request_info.rb', line 32

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

#upgrade_protocolObject



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

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

#uriObject



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

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

#websocket_versionObject



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

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