Repository navigation
sans-io parsers - #139
Draft
lovelydinosaur wants to merge 18 commits into
Draft
sans-io parsers#139lovelydinosaur wants to merge 18 commits into
lovelydinosaur wants to merge 18 commits into
Conversation
Member
Author
|
@encode/hip - This is starting to be first-pass reviewable... Here's what I'm planning on adding next...
I think that's the essential functionality covered at that point? After that, the Lower priority there's also the following bits of HTTP/1.1 edginess...
|
cgl
reviewed
Nov 19, 2026
lovelydinosaur
commented
Nov 19, 2026
lovelydinosaur
commented
Nov 19, 2026
inanpy
reviewed
Nov 21, 2026
| if self._client != SEND_START: | ||
| raise RuntimeError("Called 'recv_request_line' out of sequence.") | ||
|
|
||
| if (data := self._parser.read_until(b"\r\n", MAX_RESPONSE_LINE_SIZE)) is None: |
There was a problem hiding this comment.
Should this use MAX_REQUEST_LINE_SIZE instead of MAX_RESPONSE_LINE_SIZE?
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Parsers
Hip provides a little Sans I/O HTTP client parser, inspired by
h11.Usage
The parser has a fairly simple little API.
There are 3 methods for sending parts of the request, and 3 methods for receiving the response. Each set needs to be called in order, although sending and receiving operations can be interleaved.
The parser class does not perform any I/O itself. Instead the send methods return data that needs to be sent on the wire, while the recieve methods explicitly signal if more data needs to be fed into the parser.
Sending the request.
.send_request_line(method, target, protocol)method - A bytes instance representing the request method, such as `GET`. headers - A bytes instance representing the request URI, such as `/`. protocol - The bytes instance b'HTTP/1.1'. return - The bytes representation of the data that needs to be sent on the wire..send_headers(headers)headers - A list of `(byte, byte)` tuples representing the request headers. return - The bytes representation of the data that needs to be sent on the wire..send_body(headers)body - A bytes instance of data to be sent in the request body. To finalise the request, send b''. return - The bytes representation of the data that needs to be sent on the wire.Receiving the response.
.recv_response_line()return - A three tuple of `(bytes, int, bytes)` representing the status line of the response. These are the protocol string (eg b'HTTP/1.1'), status code (eg 200), and reason phrase (eg b'OK'). Returns `None` if the parser does not have enough data yet..recv_headers()return - A list of `(byte, byte)` tuples representing the response headers. Returns `None` if the parser does not have enough data yet..recv_body()body - A bytes instance of data representing part in the request body. Returns b'' to finalise the response. Returns `None` if the parser does not have enough data yet.Feeding data to the parser.
.feed(data)data - A bytes instance of data representing incoming data to be fed to the parser. The b'' byte string is used to indicate EOF.Example
The following demonstrates a little example of using standard python sockets to send a single request, and parse the response. This uses the standard library, plus the
truststorepackage for dealing with SSL verification.