Class: Polyphony::HTTP::Server::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/polyphony/http/server/request.rb

Overview

HTTP request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, adapter) ⇒ Request

Returns a new instance of Request.



13
14
15
16
# File 'lib/polyphony/http/server/request.rb', line 13

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

Instance Attribute Details

#__next__Object

Returns the value of attribute __next__.



11
12
13
# File 'lib/polyphony/http/server/request.rb', line 11

def __next__
  @__next__
end

#adapterObject (readonly)

Returns the value of attribute adapter.



10
11
12
# File 'lib/polyphony/http/server/request.rb', line 10

def adapter
  @adapter
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/polyphony/http/server/request.rb', line 10

def headers
  @headers
end

Instance Method Details

#buffer_body_chunk(chunk) ⇒ Object



55
56
57
58
# File 'lib/polyphony/http/server/request.rb', line 55

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

#complete!(keep_alive = nil) ⇒ Object



70
71
72
73
# File 'lib/polyphony/http/server/request.rb', line 70

def complete!(keep_alive = nil)
  @message_complete = true
  @keep_alive = keep_alive
end

#complete?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/polyphony/http/server/request.rb', line 75

def complete?
  @message_complete
end

#consumeObject



79
80
81
# File 'lib/polyphony/http/server/request.rb', line 79

def consume
  @adapter.consume_request
end

#each_chunk(&block) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/polyphony/http/server/request.rb', line 60

def each_chunk(&block)
  if @buffered_body_chunks
    @buffered_body_chunks.each(&block)
    @buffered_body_chunks = nil
  end
  while !@message_complete && (chunk = @adapter.get_body_chunk)
    yield chunk
  end
end

#finishObject



114
115
116
117
118
# File 'lib/polyphony/http/server/request.rb', line 114

def finish
  send_headers({}) unless @headers_sent

  @adapter.finish
end

#keep_alive?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/polyphony/http/server/request.rb', line 83

def keep_alive?
  @keep_alive
end

#methodObject



22
23
24
# File 'lib/polyphony/http/server/request.rb', line 22

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

#pathObject



34
35
36
# File 'lib/polyphony/http/server/request.rb', line 34

def path
  @path ||= uri.path
end

#protocolObject



18
19
20
# File 'lib/polyphony/http/server/request.rb', line 18

def protocol
  @protocol = @adapter.protocol
end

#queryObject



42
43
44
45
46
# File 'lib/polyphony/http/server/request.rb', line 42

def query
  return @query if @query

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

#query_stringObject



38
39
40
# File 'lib/polyphony/http/server/request.rb', line 38

def query_string
  @query_string ||= uri.query
end

#readObject



87
88
89
90
91
92
93
# File 'lib/polyphony/http/server/request.rb', line 87

def read
  buf = @buffered_body_chunks ? @buffered_body_chunks.join : +''
  while (chunk = @adapter.get_body_chunk)
    buf << chunk
  end
  buf
end

#respond(body, headers = {}) ⇒ Object



95
96
97
98
# File 'lib/polyphony/http/server/request.rb', line 95

def respond(body, headers = {})
  @adapter.respond(body, headers)
  @headers_sent = true
end

#schemeObject



26
27
28
# File 'lib/polyphony/http/server/request.rb', line 26

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

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



107
108
109
110
111
# File 'lib/polyphony/http/server/request.rb', line 107

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

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

#send_headers(headers = {}, empty_response = false) ⇒ Object



100
101
102
103
104
105
# File 'lib/polyphony/http/server/request.rb', line 100

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

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

#split_query_string(query) ⇒ Object



48
49
50
51
52
53
# File 'lib/polyphony/http/server/request.rb', line 48

def split_query_string(query)
  query.split('&').each_with_object({}) do |kv, h|
    k, v = kv.split('=')
    h[k.to_sym] = URI.decode_www_form_component(v)
  end
end

#uriObject



30
31
32
# File 'lib/polyphony/http/server/request.rb', line 30

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