Revision f2ae7641fad03150b4238a8b34dc9bdfdf58c0dd authored by Adam Rice on 20 March 2018, 12:30:43 UTC, committed by Blink WPT Bot on 20 March 2018, 12:38:49 UTC
Prior to this change, response body methods such as
response.body.arrayBuffer() would reject with a TypeError if the fetch
was aborted. This change makes them correctly reject with an AbortError
instead.

This implements stage #3 of the "Body Cancellation" section of the
design doc:

https://docs.google.com/document/d/1OuoCG2uiijbAwbCw9jaS7tHEO0LBO_4gMNio1ox0qlY/edit#heading=h.fvc7d7q07oio

Bug: 817687
Change-Id: Ifde322d9c22485a8ba9d14fd4ffca65c4fb4745a
Reviewed-on: https://chromium-review.googlesource.com/954765
Commit-Queue: Adam Rice <ricea@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: Yutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/master@{#544335}
1 parent 7cb5db5
Raw File
large.py
def main(request, response):
    """Code for generating large responses where the actual response data
    isn't very important.

    Two request parameters:
    size (required): An integer number of bytes (no suffix) or kilobytes
                     ("kb" suffix) or megabytes ("Mb" suffix).
    string (optional): The string to repeat in the response. Defaults to "a".

    Example:
        /resources/large.py?size=32Mb&string=ab
    """
    if not "size" in request.GET:
        400, "Need an integer bytes parameter"

    bytes_value = request.GET.first("size")

    chunk_size = 1024

    multipliers = {"kb": 1024,
                   "Mb": 1024*1024}

    suffix = bytes_value[-2:]
    if suffix in multipliers:
        multiplier = multipliers[suffix]
        bytes_value = bytes_value[:-2] * multiplier

    try:
        num_bytes = int(bytes_value)
    except ValueError:
        return 400, "Bytes must be an integer possibly with a kb or Mb suffix"

    string = str(request.GET.first("string", "a"))

    chunk = string * chunk_size

    def content():
        bytes_sent = 0
        while bytes_sent < num_bytes:
            if num_bytes - bytes_sent < len(chunk):
                yield chunk[num_bytes - bytes_sent]
            else:
                yield chunk
            bytes_sent += len(chunk)
    return [("Content-Type", "text/plain")], content()
back to top