Commit b69e43a0 authored by Yoav Weiss's avatar Yoav Weiss Committed by Commit Bot

Fix processing of double Content-DPR values and add Content-DPR WPT tests

When processing multiple Content-DPR headers, the current logic parses
all of them as a single float, and understandably, fails in that task.
That means that if a server is adding multiple headers (due to
configuration error), none of them will be effective, contrary to the RFC.

This PR fixes that, copies over relevant tests from internal layout tests
to WPT, and adds a test for that issue specifically.

BUG=895245

Change-Id: I72b570ad4b1e4db9a2bb03be9d8fb1e3799b902b
Reviewed-on: https://chromium-review.googlesource.com/c/1280266Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Commit-Queue: Yoav Weiss <yoav@yoav.ws>
Cr-Commit-Position: refs/heads/master@{#599678}
parent 392e5ae6
<!DOCTYPE html>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<img id="int_dpr" src="resources/dpr.py?name=square.png&mimeType=image/png&dpr=4.0">
<img id="fractional_dpr" src="resources/dpr.py?name=square.png&mimeType=image/png&dpr=2.5">
<img id="smaller_than_one" src="resources/dpr.py?name=square.png&mimeType=image/png&dpr=0.5">
<img id="srcset" srcset="resources/square.png 4x">
<img id="invalid" src="resources/dpr.py?name=square.png&mimeType=image/png&dpr=xx">
<img id="header_and_srcset_invalid" srcset="resources/dpr.py?name=square.png&mimeType=image/png&dpr=xx 4x">
<img id="header_and_srcset_valid" srcset="resources/dpr.py?name=square.png&mimeType=image/png&dpr=4.0 2x">
<img id="explicit_width" src="resources/dpr.py?name=square.png&mimeType=image/png&dpr=4.0" width="100" height="100">
<img id="double_dpr" src="resources/dpr.py?name=square.png&mimeType=image/png&dpr=4.0&double=4.0">
<img id="double_dpr_different_values" src="resources/dpr.py?name=square.png&mimeType=image/png&dpr=4.0&double=2.5">
<script>
let run_test = () => {
test(() => {
assert_equals(document.getElementById("int_dpr").naturalWidth, 25, "Integer Content-DPR header value")
assert_equals(document.getElementById("fractional_dpr").naturalWidth, 40, "Fractional Content-DPR header value")
assert_equals(document.getElementById("smaller_than_one").naturalWidth, 200, "Smaller than one Content-DPR header value")
assert_equals(document.getElementById("srcset").naturalWidth, 25, "srcset")
assert_equals(document.getElementById("invalid").naturalWidth, 100, "Invalid Content-DPR header value")
assert_equals(document.getElementById("header_and_srcset_invalid").naturalWidth, 25, "Invalid Content-DPR header value with valid srcset")
assert_equals(document.getElementById("header_and_srcset_valid").naturalWidth, 25, "Value Content-DPR header value and srcset")
assert_equals(document.getElementById("explicit_width").naturalWidth, 25, "Explicit width attribute")
assert_equals(document.getElementById("double_dpr").naturalWidth, 25, "Double Content-DPR header")
assert_equals(document.getElementById("double_dpr_different_values").naturalWidth, 40, "Double Content-DPR header different values")
}, "Test the image dimensions of different DPR sizes");
}
window.addEventListener("load", run_test);
</script>
</body>
</html>
def main(request, response):
"""
Simple handler that sets a response header based on which client hint
request headers were received.
"""
response.headers.append("Access-Control-Allow-Origin", "*")
values = request.GET
name = values.first('name')
type = values.first('mimeType')
dpr = values.first('dpr')
double = None
if 'double' in values:
double = values.first('double')
image_path = request.doc_root + "/".join(request.url_parts[2].split("/")[:-1]) + "/" + name
f = open(image_path, "rb")
buff = f.read()
f.close()
response.headers.set("Content-Type", type)
response.headers.set("Content-DPR", dpr)
if double:
response.headers.append("Content-DPR", double)
response.headers.set("Content-Length", len(buff))
response.content = buff
......@@ -292,10 +292,14 @@ void ImageResourceContent::NotifyObservers(
}
scoped_refptr<Image> ImageResourceContent::CreateImage(bool is_multipart) {
String content_dpr_value =
info_->GetResponse().HttpHeaderField(HTTPNames::Content_DPR);
size_t comma = content_dpr_value.ReverseFind(',');
if (comma != kNotFound && comma < content_dpr_value.length() - 1) {
content_dpr_value = content_dpr_value.Substring(comma + 1);
}
device_pixel_ratio_header_value_ =
info_->GetResponse()
.HttpHeaderField(HTTPNames::Content_DPR)
.ToFloat(&has_device_pixel_ratio_header_value_);
content_dpr_value.ToFloat(&has_device_pixel_ratio_header_value_);
if (!has_device_pixel_ratio_header_value_ ||
device_pixel_ratio_header_value_ <= 0.0) {
device_pixel_ratio_header_value_ = 1.0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment