Skip to content

Commit

Permalink
Fetch: Track protocol information in Response.
Browse files Browse the repository at this point in the history
This CL plumbs three protocol related fields through the fetch API
Response internal representation:

1) The |alpn_negotiated_protocol| which is set for TLS negotiated
protocols like H2.
2) The |connection_info| enumeration which includes information on
older protocols like H1.
3) The |was_fetched_via_spdy| boolean which is set for H2.

The first two values, |alpn_negotiated_protocol| and |connection_info|,
are collectively used to populate the
`PerformanceResourceTiming.nextHopProtocol` value.  They are also both
used to populate the correct protocol information in the devtools network
panel.

The |was_fetched_via_spdy| boolean is used to populate the deprecated
`navigator.chrome.loadTimes().wasFetchedViaSpdy` property.

Without this plumbing the resulting exposed values will generally be
wrong if the page loaded via a service worker with a FetchEvent handler.
For example, `PerformanceResourceTiming.nextHopeProtocol` is always the
empty string for resources loaded via a service worker that does a
pass-through or cached load.

This CL also plumbs these values through the cache_storage layer.  While
that may seem unintuitive at first, since the data is coming from disk,
it matches the behavior when loading from http cache.  Generally the
values represent the protocol used to originally load the response and
are persisted in the cache.

I believe this also matches the cache_storage spec where Response
objects are effectively held in an array without any serialization
or deserialization where data would be lost:

https://w3c.github.io/ServiceWorker/#cache-objects

Although the spec is not currently clear if the protocol information
lives on the response object, its not clear where else it could be
stored.  This should become clearer once the Resource Timing spec is
integrated with the fetch spec in:

w3c/resource-timing#39

This CL includes an automated WPT test for the
`PerformanceResourceTiming.nextHopProtocol` value since we can verify
that its populated with "http/1.1" instead of the empty string.
Unfortunately it was not possible to writes for the other attributes
since we do not currently have the ability to test against an http2
server.  Both `wasFetchedViaSpdy` and the devtools output effectively
need an http2 server to observe differences.  These parts of the CL were
manually tested.

Bug: 1069813
Change-Id: I84819ef5a53e9246f9e383e56b6d7e5901a7a243
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2143860
Reviewed-by: Jeremy Roman <[email protected]>
Reviewed-by: Kinuko Yasuda <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/master@{#770192}
  • Loading branch information
wanderview authored and chromium-wpt-export-bot committed May 19, 2020
1 parent 883513b commit eb687b6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions service-workers/service-worker/next-hop-protocol.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: Verify nextHopProtocol is set correctly</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>

async function getNextHopProtocol(frame, url) {
let final_url = new URL(url, self.location).href;
await frame.contentWindow.fetch(final_url).then(r => r.text());
let entryList = frame.contentWindow.performance.getEntriesByName(final_url);
let entry = entryList[entryList.length - 1];
return entry.nextHopProtocol;
}

async function runTest(t, base_url, expected_protocol) {
const scope = 'resources/empty.html?next-hop-protocol';
const script = 'resources/fetch-rewrite-worker.js';
let frame;

const registration =
await service_worker_unregister_and_register(t, script, scope);
t.add_cleanup(async _ => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');
frame = await with_iframe(scope);
t.add_cleanup(_ => frame.remove());

assert_equals(await getNextHopProtocol(frame, `${base_url}?generate-png`),
'', 'nextHopProtocol is not set on synthetic response');
assert_equals(await getNextHopProtocol(frame, `${base_url}?ignore`),
expected_protocol, 'nextHopProtocol is set on fallback');
assert_equals(await getNextHopProtocol(frame, `${base_url}`),
expected_protocol, 'nextHopProtocol is set on pass-through');
assert_equals(await getNextHopProtocol(frame, `${base_url}?cache`),
expected_protocol, 'nextHopProtocol is set on cached response');
}

promise_test(async (t) => {
return runTest(t, 'resources/empty.js', 'http/1.1');
}, 'nextHopProtocol reports H1 correctly when routed via a service worker.');

// This may be expected to fail if the WPT infrastructure does not fully
// support H2 protocol testing yet.
promise_test(async (t) => {
return runTest(t, 'resources/empty.h2.js', 'h2');
}, 'nextHopProtocol reports H2 correctly when routed via a service worker.');

</script>
Empty file.

0 comments on commit eb687b6

Please sign in to comment.