Class: Qeweney::Request

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

Overview

HTTP request

Constant Summary collapse

EMPTY_HEADERS =
{}.freeze

Constants included from RequestInfoClassMethods

Qeweney::RequestInfoClassMethods::MAX_PARAMETER_NAME_SIZE, Qeweney::RequestInfoClassMethods::MAX_PARAMETER_VALUE_SIZE, Qeweney::RequestInfoClassMethods::PARAMETER_RE

Constants included from ResponseMethods

Qeweney::ResponseMethods::WEBSOCKET_GUID

Constants included from RequestInfoMethods

Qeweney::RequestInfoMethods::COOKIE_RE, Qeweney::RequestInfoMethods::QUERY_KV_REGEXP, Qeweney::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, #redirect, #redirect_to_host, #redirect_to_https, #respond_with_static_file, #serve_file, #serve_io, #serve_io_deflate, #serve_io_gzip, #serve_rack, #upgrade, #upgrade_to_websocket, #validate_static_file_cache

Methods included from RoutingMethods

#default, #enter_route, #is, #leave_route, #on, #on_accept, #on_get, #on_host, #on_options, #on_plain_http, #on_post, #on_query_param, #on_root, #on_upgrade, #on_websocket_upgrade, #reject, #route, #route_found, #route_part, #route_relative_path, #stop_routing

Methods included from RequestInfoMethods

#accept_encoding, #connection, #cookies, #forwarded_for, #full_uri, #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
# File 'lib/qeweney/request.rb', line 19

def initialize(headers, adapter)
  @headers  = headers
  @adapter  = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

Instance Method Details

#buffer_body_chunk(chunk) ⇒ Object



24
25
26
27
# File 'lib/qeweney/request.rb', line 24

def buffer_body_chunk(chunk)
  @buffered_body_chunks ||= []
  @buffered_body_chunks << chunk
end

#complete?Boolean

Returns:

  • (Boolean)


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

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

#each_chunkObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/qeweney/request.rb', line 41

def each_chunk
  if @buffered_body_chunks
    while (chunk = @buffered_body_chunks.shift)
      yield chunk
    end
    @buffered_body_chunks = nil
  end
  while (chunk = @adapter.get_body_chunk(self))
    yield chunk
  end
end

#finishObject



92
93
94
95
96
# File 'lib/qeweney/request.rb', line 92

def finish
  send_headers({}) unless @headers_sent

  @adapter.finish(self)
end

#headers_sent?Boolean

Returns:

  • (Boolean)


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

def headers_sent?
  @headers_sent
end

#next_chunk(buffered_only = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/qeweney/request.rb', line 29

def next_chunk(buffered_only = false)
  if @buffered_body_chunks
    chunk = @buffered_body_chunks.shift
    @buffered_body_chunks = nil if @buffered_body_chunks.empty?
    return chunk
  elsif buffered_only
    return nil
  end

  @adapter.get_body_chunk(self, buffered_only)
end

#readObject Also known as: body



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/qeweney/request.rb', line 53

def read
  if @buffered_body_chunks
    body = @buffered_body_chunks.join
    if !complete?
      rest = @adapter.get_body(self)
      body << rest if rest
    end
    @buffered_body_chunks = nil
    return body
  end
  @adapter.get_body(self)
end

#respond(body, headers = EMPTY_HEADERS) ⇒ Object



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

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

#rx_incr(count) ⇒ Object



102
103
104
# File 'lib/qeweney/request.rb', line 102

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

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



85
86
87
88
89
# File 'lib/qeweney/request.rb', line 85

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



78
79
80
81
82
83
# File 'lib/qeweney/request.rb', line 78

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



114
115
116
# File 'lib/qeweney/request.rb', line 114

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

#transfer_countsObject



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

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

#tx_incr(count) ⇒ Object



106
107
108
# File 'lib/qeweney/request.rb', line 106

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