Class: Syntropy::Request

Inherits:
Object
  • Object
show all
Extended by:
RequestInfoClassMethods
Includes:
RequestInfoMethods, RequestValidationMethods, ResponseMethods
Defined in:
lib/syntropy/test.rb,
lib/syntropy/request.rb

Constant Summary collapse

EMPTY_HEADERS =
{}.freeze

Constants included from RequestInfoClassMethods

Syntropy::RequestInfoClassMethods::MAX_PARAMETER_NAME_SIZE, Syntropy::RequestInfoClassMethods::MAX_PARAMETER_VALUE_SIZE, Syntropy::RequestInfoClassMethods::PARAMETER_RE

Constants included from ResponseMethods

Syntropy::ResponseMethods::WEBSOCKET_GUID

Constants included from RequestInfoMethods

Syntropy::RequestInfoMethods::COOKIE_RE, Syntropy::RequestInfoMethods::QUERY_KV_REGEXP, Syntropy::RequestInfoMethods::SEMICOLON

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestInfoClassMethods

parse_form_data, parse_multipart_form_data, parse_multipart_form_data_part, parse_multipart_form_data_part_headers, parse_urlencoded_form_data

Methods included from ResponseMethods

#file_full_path, #json_pretty_response, #redirect, #redirect_to_host, #redirect_to_https, #respond_by_http_method, #respond_html, #respond_json, #respond_on_get, #respond_on_post, #respond_with_static_file, #serve_file, #serve_io, #set_cookie, #set_response_headers, #upgrade, #upgrade_to_websocket, #validate_static_file_cache

Methods included from RequestValidationMethods

#validate, #validate_cache, #validate_content_type, #validate_http_method, #validate_param

Methods included from RequestInfoMethods

#accept?, #accept_encoding, #auth_bearer_token, #browser?, #connection, #content_type, #cookies, #forwarded_for, #full_uri, #get_form_data, #host, #method, #parse_cookies, #parse_query, #path, #protocol, #query, #query_string, #request_id, #rewrite!, #scheme, #upgrade_protocol, #uri, #websocket_version

Constructor Details

#initialize(headers, adapter) ⇒ Request

Returns a new instance of Request.



19
20
21
22
23
24
25
26
# File 'lib/syntropy/request.rb', line 19

def initialize(headers, adapter)
  @headers  = headers
  @adapter  = adapter
  @start_stamp = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  @route = nil
  @route_params = {}
  @ctx = nil
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#routeObject

Returns the value of attribute route.



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

def route
  @route
end

#route_paramsObject (readonly)

Returns the value of attribute route_params.



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

def route_params
  @route_params
end

#start_stampObject (readonly)

Returns the value of attribute start_stamp.



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

def start_stamp
  @start_stamp
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/syntropy/request.rb', line 48

def complete?
  @adapter.complete?(self)
end

#ctxObject

Returns the request context



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

def ctx
  @ctx ||= {}
end

#each_chunkObject



37
38
39
40
41
# File 'lib/syntropy/request.rb', line 37

def each_chunk
  while (chunk = @adapter.get_body_chunk(self))
    yield chunk
  end
end

#finishObject



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

def finish
  send_headers({}) unless @headers_sent

  @adapter.finish(self)
end

#headers_sent?Boolean

Returns:

  • (Boolean)


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

def headers_sent?
  @headers_sent
end

#next_chunkObject



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

def next_chunk
  @adapter.get_body_chunk(self)
end

#readObject Also known as: body



43
44
45
# File 'lib/syntropy/request.rb', line 43

def read
  @adapter.get_body(self)
end

#respond(body, headers = EMPTY_HEADERS) ⇒ Object



54
55
56
57
# File 'lib/syntropy/request.rb', line 54

def respond(body, headers = EMPTY_HEADERS)
  @adapter.respond(self, body, headers)
  @headers_sent = true
end

#response_bodyObject



36
37
38
# File 'lib/syntropy/test.rb', line 36

def response_body
  adapter.response_body
end

#response_content_typeObject



45
46
47
48
49
50
51
52
53
# File 'lib/syntropy/test.rb', line 45

def response_content_type
  ct = response_headers['Content-Type']
  return nil if !ct

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

  m[1]
end


55
56
57
58
59
60
61
62
63
# File 'lib/syntropy/test.rb', line 55

def response_cookie(name)
  sc = response_headers['Set-Cookie']
  return nil if !sc

  m = sc.match(/#{name}=([^\s]+)$/)
  return nil if !m

  m[1]
end

#response_headersObject



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

def response_headers
  adapter.response_headers
end

#response_jsonObject



40
41
42
43
# File 'lib/syntropy/test.rb', line 40

def response_json
  raise if response_content_type != 'application/json'
  JSON.parse(response_body)
end

#response_statusObject



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

def response_status
  adapter.status
end

#rx_incr(count) ⇒ Object



83
84
85
# File 'lib/syntropy/request.rb', line 83

def rx_incr(count)
  headers[':rx'] ? headers[':rx'] += count : headers[':rx'] = count
end

#send_chunk(body, done: false) ⇒ Object Also known as: <<



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

def send_chunk(body, done: false)
  send_headers({}) unless @headers_sent

  @adapter.send_chunk(self, body, done: done)
end

#send_headers(headers = EMPTY_HEADERS, empty_response = false) ⇒ Object



59
60
61
62
63
64
# File 'lib/syntropy/request.rb', line 59

def send_headers(headers = EMPTY_HEADERS, empty_response = false)
  return if @headers_sent

  @headers_sent = true
  @adapter.send_headers(self, headers, empty_response: empty_response)
end

#total_transferObject



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

def total_transfer
  (headers[':rx'] || 0) + (headers[':tx'] || 0)
end

#transfer_countsObject



91
92
93
# File 'lib/syntropy/request.rb', line 91

def transfer_counts
  [headers[':rx'], headers[':tx']]
end

#tx_incr(count) ⇒ Object



87
88
89
# File 'lib/syntropy/request.rb', line 87

def tx_incr(count)
  headers[':tx'] ? headers[':tx'] += count : headers[':tx'] = count
end