-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent opaque range request responses from entering the preload cache
ResourceLoader cancels range request responses that were not initiated with range request headers causing them to error out and be cleared from the preload cache. Other responses (200, 416, error, etc) complete successfully and would otherwise enter the preload cache, making them observable. This prevents opaque range responses of any kind from persisting in the preload cache (which would not naturally have any anyway). Bug: 1270990 Change-Id: Ife9922fe0b88e39722f3664ddd091a1516892157 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3294001 Reviewed-by: Ben Kelly <wanderview@chromium.org> Reviewed-by: Yoav Weiss <yoavweiss@chromium.org> Commit-Queue: Patrick Meenan <pmeenan@chromium.org> Cr-Commit-Position: refs/heads/main@{#946055}
- Loading branch information
1 parent
71a9256
commit 5fc81f8
Showing
4 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
This generates a partial response for a 100-byte text file. | ||
""" | ||
import re | ||
|
||
from wptserve.utils import isomorphic_decode | ||
|
||
def main(request, response): | ||
total_length = int(request.GET.first(b'length', b'100')) | ||
partial_code = int(request.GET.first(b'partial', b'206')) | ||
range_header = request.headers.get(b'Range', b'') | ||
|
||
# Send a 200 if there is no range request | ||
if not range_header: | ||
to_send = ''.zfill(total_length) | ||
response.headers.set(b"Content-Type", b"text/plain") | ||
response.headers.set(b"Cache-Control", b"no-cache") | ||
response.headers.set(b"Content-Length", total_length) | ||
response.content = to_send | ||
return | ||
|
||
# Simple range parsing, requires specifically "bytes=xxx-xxxx" | ||
range_header_match = re.search(r'^bytes=(\d*)-(\d*)$', isomorphic_decode(range_header)) | ||
start, end = range_header_match.groups() | ||
start = int(start) | ||
end = int(end) if end else total_length | ||
length = end - start | ||
|
||
# Error the request if the range goes beyond the length | ||
if length <= 0 or end > total_length: | ||
response.set_error(416, u"Range Not Satisfiable") | ||
response.write() | ||
return | ||
|
||
# Generate a partial response of the requested length | ||
to_send = ''.zfill(length) | ||
response.headers.set(b"Content-Type", b"text/plain") | ||
response.headers.set(b"Accept-Ranges", b"bytes") | ||
response.headers.set(b"Cache-Control", b"no-cache") | ||
response.status = partial_code | ||
|
||
content_range = b"bytes %d-%d/%d" % (start, end, total_length) | ||
|
||
response.headers.set(b"Content-Range", content_range) | ||
response.headers.set(b"Content-Length", length) | ||
|
||
response.content = to_send |
This file contains 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
This file contains 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
This file contains 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