Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: third_party/WebKit/LayoutTests/imported/wpt/common/large.py

Issue 2015623004: Import wpt@ed94c51f3dfaa5ff4c9c311add1a560408059c51 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 def main(request, response):
2 """Code for generating large responses where the actual response data
3 isn't very important.
4
5 Two request parameters:
6 size (required): An integer number of bytes (no suffix) or kilobytes
7 ("kb" suffix) or megabytes ("Mb" suffix).
8 string (optional): The string to repeat in the response. Defaults to "a".
9
10 Example:
11 /resources/large.py?size=32Mb&string=ab
12 """
13 if not "size" in request.GET:
14 400, "Need an integer bytes parameter"
15
16 bytes_value = request.GET.first("size")
17
18 chunk_size = 1024
19
20 multipliers = {"kb": 1024,
21 "Mb": 1024*1024}
22
23 suffix = bytes_value[-2:]
24 if suffix in multipliers:
25 multiplier = multipliers[suffix]
26 bytes_value = bytes_value[:-2] * multiplier
27
28 try:
29 num_bytes = int(bytes_value)
30 except ValueError:
31 return 400, "Bytes must be an integer possibly with a kb or Mb suffix"
32
33 string = str(request.GET.first("string", "a"))
34
35 chunk = string * chunk_size
36
37 def content():
38 bytes_sent = 0
39 while bytes_sent < num_bytes:
40 if num_bytes - bytes_sent < len(chunk):
41 yield chunk[num_bytes - bytes_sent]
42 else:
43 yield chunk
44 bytes_sent += len(chunk)
45 return [("Content-Type", "text/plain")], content()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698