Class: RDKit::HTTPParser

Inherits:
Object show all
Defined in:
lib/rdkit/http_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeHTTPParser

Returns a new instance of HTTPParser.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rdkit/http_parser.rb', line 5

def initialize
  @parser = HTTP::Parser.new

  @responses = []

  # https://github.com/tmm1/http_parser.rb

  @body = nil

  @parser.on_message_begin = proc do
  end

  @parser.on_headers_complete = proc do
    # p @parser.http_method
    # p @parser.request_url
    # p @parser.headers

    if (length = @parser.headers['Content-Length']) && length.to_i > 0
      @body = ''
    else
      @responses << [@parser.http_method.downcase + "|" + @parser.request_url, nil]
    end
  end

  @parser.on_body = proc do |chunk|
    @body << chunk
  end

  @parser.on_message_complete = proc do
    # Headers and body is all parsed
    if @body
      @responses << [@parser.http_method.downcase + "|" + @parser.request_url, @body]

      @body = nil
    end
  end
end

Instance Method Details

#feed(data) ⇒ Object



43
44
45
# File 'lib/rdkit/http_parser.rb', line 43

def feed(data)
  @parser << data
end

#getsObject



47
48
49
50
51
52
53
54
# File 'lib/rdkit/http_parser.rb', line 47

def gets
  if result = @responses.shift

    result
  else
    false
  end
end