Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[beacon-api] Unify python helper
set_beacon*.py
get_beacon*.py
as suggested in [3] [3]: https://crrev.com/c/3789544/10/third_party/blink/web_tests/wpt_internal/pending_beacon/resources/pending_beacon-helper.js#68 Bug: 1293679 Change-Id: I5285c1665ae8d00cf96a40ebf0f37db799764c94 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3820181 Reviewed-by: Fergal Daly <fergal@chromium.org> Commit-Queue: Ming-Ying Chung <mych@chromium.org> Cr-Commit-Position: refs/heads/main@{#1034329}
- Loading branch information
Showing
11 changed files
with
169 additions
and
138 deletions.
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
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
30 changes: 30 additions & 0 deletions
30
third_party/blink/web_tests/external/wpt/pending_beacon/resources/get_beacon.py
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,30 @@ | ||
"""An HTTP request handler for WPT that handles /get_beacon.py requests.""" | ||
|
||
import json | ||
|
||
_BEACON_ID_KEY = b"uuid" | ||
_BEACON_DATA_PATH = "beacon_data" | ||
|
||
|
||
def main(request, response): | ||
"""Retrieves the beacon data keyed by the given uuid from server storage. | ||
The response content is a JSON string in one of the following formats: | ||
- "{'data': ['abc', null, '123',...]}" | ||
- "{'data': []}" indicates that no data has been set for this uuid. | ||
""" | ||
if _BEACON_ID_KEY not in request.GET: | ||
response.status = 400 | ||
return "Must provide a UUID to store beacon data" | ||
uuid = request.GET.first(_BEACON_ID_KEY) | ||
|
||
with request.server.stash.lock: | ||
body = {'data': []} | ||
data = request.server.stash.take(key=uuid, path=_BEACON_DATA_PATH) | ||
if data: | ||
body['data'] = data | ||
# The stash is read-once/write-once, so it has to be put back after | ||
# reading if `data` is not None. | ||
request.server.stash.put( | ||
key=uuid, value=data, path=_BEACON_DATA_PATH) | ||
return [(b'Content-Type', b'text/plain')], json.dumps(body) |
15 changes: 0 additions & 15 deletions
15
third_party/blink/web_tests/external/wpt/pending_beacon/resources/get_beacon_count.py
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
third_party/blink/web_tests/external/wpt/pending_beacon/resources/get_beacon_data.py
This file was deleted.
Oops, something went wrong.
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
45 changes: 45 additions & 0 deletions
45
third_party/blink/web_tests/external/wpt/pending_beacon/resources/set_beacon.py
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,45 @@ | ||
"""An HTTP request handler for WPT that handles /set_beacon.py requests.""" | ||
|
||
_BEACON_ID_KEY = b"uuid" | ||
_BEACON_DATA_PATH = "beacon_data" | ||
_BEACON_FORM_PAYLOAD_KEY = b"payload" | ||
_BEACON_BODY_PAYLOAD_KEY = "payload=" | ||
|
||
|
||
def main(request, response): | ||
"""Stores the given beacon's data keyed by uuid in the server. | ||
For GET request, this handler assumes no data. | ||
For POST request, this handler extracts data from request body: | ||
- Content-Type=multipart/form-data: data keyed by 'payload'. | ||
- the entire request body. | ||
Multiple data can be added for the same uuid. | ||
The data is stored as UTF-8 format. | ||
""" | ||
if _BEACON_ID_KEY not in request.GET: | ||
response.status = 400 | ||
return "Must provide a UUID to store beacon data" | ||
uuid = request.GET.first(_BEACON_ID_KEY) | ||
|
||
data = None | ||
if request.method == u"POST": | ||
if b"multipart/form-data" in request.headers.get(b"Content-Type", b""): | ||
if _BEACON_FORM_PAYLOAD_KEY in request.POST: | ||
data = request.POST.first(_BEACON_FORM_PAYLOAD_KEY).decode( | ||
'utf-8') | ||
elif request.body: | ||
data = request.body.decode('utf-8') | ||
if data.startswith(_BEACON_BODY_PAYLOAD_KEY): | ||
data = data.split(_BEACON_BODY_PAYLOAD_KEY)[1] | ||
|
||
with request.server.stash.lock: | ||
saved_data = request.server.stash.take(key=uuid, path=_BEACON_DATA_PATH) | ||
if not saved_data: | ||
saved_data = [data] | ||
else: | ||
saved_data.append(data) | ||
request.server.stash.put( | ||
key=uuid, value=saved_data, path=_BEACON_DATA_PATH) | ||
return ((200, "OK"), [], "") |
Oops, something went wrong.