Class: Knod::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/knod/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Request

Returns a new instance of Request.



5
6
7
8
9
# File 'lib/knod/request.rb', line 5

def initialize(socket)
  @socket = socket
  @request_line = socket.gets
  parse_request
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/knod/request.rb', line 3

def headers
  @headers
end

#request_lineObject (readonly)

Returns the value of attribute request_line.



3
4
5
# File 'lib/knod/request.rb', line 3

def request_line
  @request_line
end

#socketObject (readonly)

Returns the value of attribute socket.



3
4
5
# File 'lib/knod/request.rb', line 3

def socket
  @socket
end

Instance Method Details

#bodyObject



38
39
40
# File 'lib/knod/request.rb', line 38

def body
  @body ||= socket.read(content_length)
end

#content_lengthObject



22
23
24
# File 'lib/knod/request.rb', line 22

def content_length
  headers['Content-Length'].to_i
end

#content_typeObject



26
27
28
# File 'lib/knod/request.rb', line 26

def content_type
  headers['Content-Type']
end

#methodObject



34
35
36
# File 'lib/knod/request.rb', line 34

def method
  @verb ||= request_line.split.first.upcase
end

#parse_requestObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/knod/request.rb', line 11

def parse_request
  headers = {}
  loop do
    line = socket.gets
    break if line == "\r\n"
    name, value = line.strip.split(': ')
    headers[name] = value
  end
  @headers = headers
end

#uriObject



30
31
32
# File 'lib/knod/request.rb', line 30

def uri
  @uri ||= request_line.split[1]
end