Class: Soba::Parser
- Inherits:
-
Object
- Object
- Soba::Parser
- Defined in:
- lib/soba/parser.rb
Defined Under Namespace
Classes: InvalidRequest
Constant Summary collapse
- CRLF =
"\r\n".freeze
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #body ⇒ Object
- #content_length ⇒ Object
- #headers ⇒ Object (also: #env)
-
#initialize(socket) ⇒ Parser
constructor
A new instance of Parser.
-
#request_schema ⇒ Object
http or https.
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
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
10 11 12 |
# File 'lib/soba/parser.rb', line 10 def socket @socket end |
Instance Method Details
#body ⇒ Object
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_length ⇒ Object
40 41 42 |
# File 'lib/soba/parser.rb', line 40 def content_length headers["CONTENT_LENGTH"]&.to_i end |
#headers ⇒ Object 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_schema ⇒ Object
http or https
36 37 38 |
# File 'lib/soba/parser.rb', line 36 def request_schema @request_schema ||= server_protocol.split("/").first.downcase end |