"Data is a precious thing and will last longer than the systems themselves."
An http parser that also includes URI parsing and retaining and rebuilding the original data
Segregate is an easy to use http parser, including object callback and the ability to rebuild the http message.
Segregate is designed so that it is not only incredibly easy to parse incoming data in any state, and uses URI to parse the request line path. There is also the ability to be able to manipulate and change the data and reform the message into data that can then be reused or forwarded.
Limitations
Currently the parser is unable to handle multiple headers with the same key.
Setup
Segregate has been tested with Ruby 1.9.2 and later. To install:
gem install segregate
Using Segregate
Require Segregate at the start of your code
require 'segregate'
Basic usage
Parsing data:
parser = Segregate.new
parser.parse data
Accessing data:
parser.request_line
parser.request_method
parser.request_url
parser.status_line
parser.status_code
parser.status_phrase
parser.http_version
parser.major_http_version
parser.minor_http_version
parser.headers
parser.body
parser.raw_data
Modifying data:
parser.request_method = "POST"
parser.path = "/new/endpoint"
parser.status_code = 404
parser.status_phrase = "Not Found"
parser.http_version = [0.2]
parser.major_http_version = 3
parser.minor_http_version = 4
parser.headers.host = "www.example.com"
parser.headers['accept'] = "application/json"
parser.body.sub! "data", "information"
Callback usage
class Callback_object
def on_message_begin parser
end
def on_request_line parser
end
def on_status_line parser
end
def on_headers_complete parser
end
def on_body chunk
end
def on_body_complete parser
end
end
parser = Segregate.new(Callback_object.new)
Segregate with event machine
module MyHttpConnection
def connection_completed
@parser = Segregate.new(self)
end
def receive_data(data)
@parser << data
end
def on_body_complete parser
puts parser.body
end
end
Rebuilding message
Forwarding raw data
socket.write parser.raw_data