.. |
bench |
ext |
lib |
spec |
tasks |
.gitignore |
.gitmodules |
Gemfile |
Gemfile.lock |
LICENSE-MIT |
README.md |
Rakefile |
http_parser.rb.gemspec |
A simple callback-based HTTP request/response parser for writing http servers, clients and proxies.
This gem is built on top of joyent/http-parser and its java port a2800276/http-parser.java.
This gem aims to work on all major Ruby platforms, including:
require "http/parser"
parser = Http::Parser.new
parser.on_headers_complete = proc do
p parser.http_version
p parser.http_method # for requests
p parser.request_url
p parser.status_code # for responses
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
# Feed raw data from the socket to the parser
parser << raw_data
module MyHttpConnection
def connection_completed
@parser = Http::Parser.new(self)
end
def receive_data(data)
@parser << data
end
def on_message_begin
@headers = nil
@body = ''
end
def on_headers_complete
@headers = @parser.headers
end
def on_body(chunk)
@body << chunk
end
def on_message_complete
p [@headers, @body]
end
end
parser = Http::Parser.new
parser.on_headers_complete = proc{ :stop }
offset = parser << request_data
body = request_data[offset..-1]