Added offset and count parameters to pathsend extension #538
+26
−0
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.
Background Context
I read #469 and discussed with @gi0baro in this thread emmett-framework/granian#157 about trying to get this as part of the ASGI spec. The implementation here is just a straight copy of the discussion in the relevant thread. It didn't seem that people had too much of a disagreement of what it'd look like, and if we did we could hash it out in this PR.
Links:
offsetandcountto Path Send #469: Issue describing potential improvements to pathsend (what this PR implements)(draft)
This extends the
http.response.pathsendextension to support partial file sends by adding optional offset and count parameters, enabling HTTP Range request support.The parameters mirror the existing
http.response.zerocopysendAPI:offset: byte position to start reading from (default: beginning)count: number of bytes to send (default: to end of file)This allows ASGI frameworks to implement range requests without loading the bytes themselves, offloading the operation to the server.
Servers may advertise range support capability by setting {"ranges": True} in the extension scope dict, allowing frameworks to detect this capability.
The application remains responsible for setting appropriate headers (Content-Range, Content-Length, Accept-Ranges) in the response.
Fixes #469
Implementation
This original comment also proposed an alternative that uses
rangeinstead of offset+count. I don't really have strong opinions one way or another about which we use, but I see the trade-offs as:rangeis just one key, and its explicit about the start/end bytes. If we were to do this, we'd need to make a decision about being inclusive/exclusive with the ending byte (I think exclusive ending byte is common in most situations, but http range GETs are inclusive, so we should likely match that: docs)I'm also in the process of a branch on granian where I'm going to do a quick example implementation just to give an example. I'll likely use granian + starlette to showcase what this extended pathsend could look like
First time contributing to and submitting a PR to asgiref, so please feel free to badger me about bits that I'm missing!