ignore aborted response in http caching

This commit is contained in:
Gregory Terzian 2017-11-23 18:14:07 +08:00
parent 6ca651c0c8
commit 609e975c50
8 changed files with 186 additions and 12 deletions

View file

@ -12478,6 +12478,11 @@
{}
]
],
"mozilla/resources/http-cache-trickle.py": [
[
{}
]
],
"mozilla/resources/http-cache.js": [
[
{}
@ -33245,6 +33250,12 @@
{}
]
],
"mozilla/http-cache-xhr.html": [
[
"/_mozilla/mozilla/http-cache-xhr.html",
{}
]
],
"mozilla/http-cache.html": [
[
"/_mozilla/mozilla/http-cache.html",
@ -66348,6 +66359,10 @@
"592f69ee432ba5bc7a2f2649e72e083d21393496",
"testharness"
],
"mozilla/http-cache-xhr.html": [
"0f8e347991f25a08ba5f8bdff0ef99055e33e7a1",
"testharness"
],
"mozilla/http-cache.html": [
"33827dc9bdb0efcbcae4f730086693be315cfc14",
"testharness"
@ -72024,8 +72039,12 @@
"78686147f85e4146e7fc58c1f67a613f65b099a2",
"support"
],
"mozilla/resources/http-cache-trickle.py": [
"48f4e32ec2e1c1b6d47e89254045b398c1d84d40",
"support"
],
"mozilla/resources/http-cache.js": [
"c6b1ee9def26d4e12a1b93e551c225f82b4717c2",
"4bf71e1f328e778990eb756741a3be58f4f57ef6",
"support"
],
"mozilla/resources/iframe_contentDocument_inner.html": [

View file

@ -0,0 +1,2 @@
[http-cache-xhr.html]
type: testharness

View file

@ -0,0 +1,73 @@
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/http-cache.js"></script>
<script>
var test = async_test('The response from an aborted XHR request should not be cached');
test.step(function() {
var client = new XMLHttpRequest();
var requests = [
{
response_status: [200, 'OK'],
"response_headers": [
['Expires', http_date(100000)],
['Last-Modified', http_date(0)]
],
response_body: '12'
},
{
response_status: [200, 'OK'],
"response_headers": [
['Expires', http_date(100000)],
['Last-Modified', http_date(0)]
],
response_body: '12'
},
{
response_status: [200, 'OK'],
"response_headers": [
['Expires', http_date(100000)],
['Last-Modified', http_date(0)]
],
response_body: '12'
}
];
var uuid = token();
var url = make_url(uuid, requests, 0);
url += '&trickle=true';
client.onprogress = test.step_func(function() {
var client2 = new XMLHttpRequest();
client2.onloadend = test.step_func(function(event) {
server_state(uuid).then(test.step_func(function(state){
// Two server hits for two requests, no caching.
assert_equals(state.length, 2);
// The empty, aborted response.
assert_equals(client.response, "");
assert_equals(client2.response, "12");
}));
var client3 = new XMLHttpRequest();
client3.onloadend = test.step_func(function(event) {
server_state(uuid).then(test.step_func(function(state) {
// No server hits, the response from client2 was cached and re-used.
assert_equals(state.length, 0)
assert_equals(client3.response, '12');
test.done();
}));
});
client3.open("GET", url);
client3.send();
});
client.onabort = test.step_func(function() {
client2.open("GET", url);
client2.send();
});
client.abort();
});
client.open("GET", url);
client.send();
});
</script>
</head>
</html>

View file

@ -0,0 +1,64 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import time
from json import JSONEncoder, JSONDecoder
from base64 import b64decode
def main(request, response):
uuid = request.GET.first("token", None)
if "querystate" in request.GET:
response.headers.set("Content-Type", "text/plain")
return JSONEncoder().encode(request.server.stash.take(uuid))
server_state = request.server.stash.take(uuid)
if not server_state:
server_state = []
requests = JSONDecoder().decode(b64decode(request.GET.first("info", "")))
config = requests[len(server_state)]
state = dict()
state["request_method"] = request.method
state["request_headers"] = dict([[h.lower(), request.headers[h]] for h in request.headers])
server_state.append(state)
request.server.stash.put(uuid, server_state)
note_headers = ['content-type', 'access-control-allow-origin', 'last-modified', 'etag']
noted_headers = {}
for header in config.get('response_headers', []):
if header[0].lower() in ["location", "content-location"]: # magic!
header[1] = "%s&target=%s" % (request.url, header[1])
response.headers.set(header[0], header[1])
if header[0].lower() in note_headers:
noted_headers[header[0].lower()] = header[1]
if "access-control-allow-origin" not in noted_headers:
response.headers.set("Access-Control-Allow-Origin", "*")
if "content-type" not in noted_headers:
response.headers.set("Content-Type", "text/plain")
response.headers.set("Server-Request-Count", len(server_state))
code, phrase = config.get("response_status", [200, "OK"])
if request.headers.get("If-Modified-Since", False) == noted_headers.get('last-modified', True):
code, phrase = [304, "Not Modified"]
if request.headers.get("If-None-Match", False) == noted_headers.get('etag', True):
code, phrase = [304, "Not Modified"]
response.status = (code, phrase)
if request.GET.first("trickle", None):
response.write_status_headers()
for byte in config.get("response_body", uuid):
response.writer.write_content(byte)
time.sleep(1)
content = config.get("response_body", uuid)
if code in [204, 304]:
return ""
else:
return content

View file

@ -40,11 +40,11 @@ function make_url(uuid, requests, idx) {
if ("query_arg" in requests[idx]) {
arg = "&target=" + requests[idx].query_arg;
}
return "/fetch/http-cache/resources/http-cache.py?token=" + uuid + "&info=" + btoa(JSON.stringify(requests)) + arg;
return "resources/http-cache-trickle.py?token=" + uuid + "&info=" + btoa(JSON.stringify(requests)) + arg;
}
function server_state(uuid) {
return fetch("/fetch/http-cache/resources/http-cache.py?querystate&token=" + uuid)
return fetch("resources/http-cache-trickle.py?querystate&token=" + uuid)
.then(function(response) {
return response.text();
}).then(function(text) {