Skip to content

Commit e59e343

Browse files
Added offset and count parameters to pathsend extension
This extends the http.response.pathsend extension to support partial file sends by adding optional offset and count parameters, enabling HTTP Range request support (RFC 7233) and efficient seeking in large media files. The parameters mirror the existing http.response.zerocopysend API: - 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 handling file descriptors directly, offloading the operation to the server while maintaining pathsend's simpler API. Capability Signaling: Servers that support offset and count parameters MUST advertise this by setting {"ranges": True} in the pathsend extension scope dict. Applications should check for this flag before using these parameters. This provides a clear upgrade path from basic pathsend to range-capable pathsend without ambiguity. The application remains responsible for: - Checking the "ranges" capability flag - Parsing Range request headers - Setting appropriate response headers (Content-Range, Content-Length, Accept-Ranges, status 206) Fixes #469 Related: emmett-framework/granian#157
1 parent b7b15b2 commit e59e343

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

asgiref/typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class HTTPResponseTrailersEvent(TypedDict):
140140
class HTTPResponsePathsendEvent(TypedDict):
141141
type: Literal["http.response.pathsend"]
142142
path: str
143+
offset: NotRequired[int]
144+
count: NotRequired[int]
143145

144146

145147
class HTTPServerPushEvent(TypedDict):

docs/extensions.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ ASGI servers that implement this extension will provide
146146
},
147147
}
148148

149+
Servers that support the ``offset`` and ``count`` parameters for range
150+
requests must advertise this capability via::
151+
152+
"scope": {
153+
...
154+
"extensions": {
155+
"http.response.pathsend": {
156+
"ranges": True,
157+
},
158+
},
159+
}
160+
161+
Applications should check for the presence of ``ranges`` before using
162+
``offset`` and ``count`` parameters. Servers that advertise
163+
``"ranges": True`` must handle these parameters correctly.
164+
149165
The ASGI framework can initiate a path-send by sending a message with
150166
the following keys. This message can be sent at any time after the
151167
*Response Start* message, and cannot be mixed with ``http.response.body``.
@@ -160,6 +176,14 @@ Keys:
160176
* ``path`` (*Unicode string*): The string representation of the absolute
161177
file path to be sent by the server, platform specific.
162178

179+
* ``offset`` (*int*): Optional. If this value exists, it will specify
180+
the offset at which the server starts to read data from ``path``.
181+
Otherwise, it will be read from the beginning of the file.
182+
183+
* ``count`` (*int*): Optional. ``count`` is the number of bytes to
184+
copy from the file. If omitted, the file will be read from the
185+
offset (or beginning) until its end.
186+
163187
The ASGI application itself is responsible to send the relevant headers
164188
in the *Response Start* message, like the ``Content-Type`` and
165189
``Content-Length`` headers for the file to be sent.

0 commit comments

Comments
 (0)