require "http/parser"
require "socket"

parser = Http::Parser.new

parser.on_headers_complete = proc do
    print "http version: "
    p parser.http_version
    print "http method:  "
    p parser.http_method    # for requests
    print "request url:  "
    p parser.request_url
    print "status code:  "
    p parser.status_code    # for responses
    print "headers:      "
    p parser.headers
end

parser.on_body = proc do |chunk|
    # One chunk of the body
    p chunk
end

parser.on_message_complete = proc do |env|
    # Headers and body is all parsed
    puts "Done!"
end

query = <<-END
HTTP/1.1 200 OK
Date: Sun, 13 Oct 2019 01:35:21 GMT
Server: Apache
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000
Last-Modified: Fri, 04 Oct 2019 07:39:28 GMT
ETag: "347b-59410cd5595fc"
Content-Length: 5
Vary: Accept-Encoding
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Cache-Control: public, must-revalidate
Content-Type: text/html

1234
END

# s = TCPSocket.new 'fossies.org', 80
parser << query
