28 ms·
I've done perfectly adequate HTTP parsing with this Python 4-liner: headerText, body = text.split('\r\n\r\n', 1) headerLines = headerText.split('\r\n') m
by nostrademons 10y ago
I've done perfectly adequate HTTP parsing with this Python 4-liner:
headerText, body = text.split('\r\n\r\n', 1)
headerLines = headerText.split('\r\n')
method, path, protocol = headerLines[1].split(' ')
headers = dict(line.split(':').map(str.strip) for line in lines[1:])
For production use you'd probably want something a bit faster & more robust like Mongrel's HTTP parser (itself only 166 lines of Ragel), which powers several million websites out there:
https://github.com/mongrel/mongrel/blob/master/ext/http11/http11_parser.rl https://github.com/mongrel/mongrel/blob/master/ext/http11/ht...
- niftich 10y agoThat's a perfectly adequate 4-line HTTP/1.0 parser :) But for HTTP/1.1, which must support chunked transfer coding [1], this won't work. [1] https://tools.ietf.org/html/rfc7230#section-4.1 https://tools.ietf.org/html/rfc7230#section-4.1