Class: Soba::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/soba/parser.rb

Defined Under Namespace

Classes: InvalidRequest

Constant Summary collapse

CRLF =
"\r\n".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
16
# File 'lib/soba/parser.rb', line 12

def initialize(socket)
  @socket = socket
  @headers = nil
  @body = nil
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



10
11
12
# File 'lib/soba/parser.rb', line 10

def socket
  @socket
end

Instance Method Details

#bodyObject



44
45
46
47
48
# File 'lib/soba/parser.rb', line 44

def body
  @body ||= begin
    StringIO.new(content_length ? socket.read(content_length) : '').binmode
  end
end

#content_lengthObject



40
41
42
# File 'lib/soba/parser.rb', line 40

def content_length
  headers["CONTENT_LENGTH"]&.to_i
end

#headersObject Also known as: env



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/soba/parser.rb', line 18

def headers
  @headers ||= begin
    request_body = ""
    while (line = socket.readline) != CRLF
      request_body << line
    end
    request_body << CRLF
    debug request_body
    ret = PicoHTTPParser.parse_http_request(request_body, @headers ||= {})
    debug ret
    raise InvalidRequest, "ret: #{ret}" if ret < 0
    @headers
  end
end

#request_schemaObject

http or https



36
37
38
# File 'lib/soba/parser.rb', line 36

def request_schema
  @request_schema ||= server_protocol.split("/").first.downcase
end