Fix caching for all of the requests. The free wifi I get at Bacchus from Angela's Bridal isn't great.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3981 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-09-14 23:18:55 +00:00
parent f983c3d987
commit 7077e69bf7

View File

@ -415,9 +415,24 @@ async function staticFileHandler(request, response, blobId, uri) {
if (uri === kStaticFiles[i].uri) { if (uri === kStaticFiles[i].uri) {
var path = kStaticFiles[i].path || uri.substring(1); var path = kStaticFiles[i].path || uri.substring(1);
var type = kStaticFiles[i].type || guessType(path); var type = kStaticFiles[i].type || guessType(path);
var data = await File.readFile("core/" + path);
response.writeHead(200, Object.assign({"Content-Type": type, "Content-Length": data.byteLength}, kStaticFiles[i].headers || {})); let stat = await File.stat('core/' + path);
response.end(data); let id = `${stat.mtime}_${stat.size}`;
if (request.headers['if-none-match'] === '"' + id + '"') {
response.writeHead(304, {});
response.end();
} else {
var data = await File.readFile('core/' + path);
response.writeHead(200, Object.assign(
{
'Content-Type': type,
'Content-Length': data.byteLength,
'etag': '"' + id + '"',
},
kStaticFiles[i].headers || {}));
response.end(data);
}
return; return;
} }
} }
@ -443,9 +458,21 @@ async function staticDirectoryHandler(request, response, directory, uri) {
} }
try { try {
var data = await File.readFile(directory + filename); let stat = await File.stat(directory + filename);
response.writeHead(200, {"Content-Type": k_mime_types[filename.split('.').pop()] || 'text/plain', "Content-Length": data.byteLength}); let id = `${stat.mtime}_${stat.size}`;
response.end(data);
if (request.headers['if-none-match'] === '"' + id + '"') {
response.writeHead(304, {});
response.end();
} else {
var data = await File.readFile(directory + filename);
response.writeHead(200, {
'Content-Type': k_mime_types[filename.split('.').pop()] || 'text/plain',
'Content-Length': data.byteLength,
'etag': '"' + id + '"',
});
response.end(data);
}
} catch { } catch {
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length}); response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length});
response.end("File not found"); response.end("File not found");
@ -513,16 +540,26 @@ function guessType(path) {
} }
async function blobHandler(request, response, blobId, uri) { async function blobHandler(request, response, blobId, uri) {
var found = false; for (var i in kStaticFiles) {
if (!found) { if (uri === kStaticFiles[i].uri && kStaticFiles[i].path) {
for (var i in kStaticFiles) { let stat = await File.stat('core/' + kStaticFiles[i].path);
if (uri === kStaticFiles[i].uri && kStaticFiles[i].path) { let id = `${stat.mtime}_${stat.size}`;
found = true;
var data = await File.readFile("core/" + kStaticFiles[i].path); if (request.headers['if-none-match'] === '"' + id + '"') {
response.writeHead(200, {"Content-Type": kStaticFiles[i].type, "Content-Length": data.byteLength}); response.writeHead(304, {});
response.end();
} else {
var data = await File.readFile('core/' + kStaticFiles[i].path);
response.writeHead(200, Object.assign(
{
'Content-Type': kStaticFiles[i].type,
'Content-Length': data.byteLength,
'etag': '"' + id + '"',
},
kStaticFiles[i].headers || {}));
response.end(data); response.end(data);
break;
} }
return;
} }
} }
@ -532,133 +569,141 @@ async function blobHandler(request, response, blobId, uri) {
return; return;
} }
if (!found) { var process;
var process; if (uri == "/view") {
if (uri == "/view") { var data;
var data; if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) { var id = await new Database(match[1]).get('path:' + match[2]);
var id = await new Database(match[1]).get('path:' + match[2]); if (id) {
if (id) { if (request.headers['if-none-match'] === '"' + id + '"') {
if (request.headers['if-none-match'] && request.headers['if-none-match'] == '"' + id + '"') { response.writeHead(304, {});
response.writeHead(304, {}); response.end();
response.end();
} else {
data = await getBlobOrContent(id);
if (match[3]) {
var appObject = JSON.parse(data);
data = appObject.files[match[3]];
}
sendData(response, data, undefined, {etag: '"' + id + '"'});
}
} else { } else {
sendData(response, data); data = await getBlobOrContent(id);
if (match[3]) {
var appObject = JSON.parse(data);
data = appObject.files[match[3]];
}
sendData(response, data, undefined, {etag: '"' + id + '"'});
} }
} else { } else {
data = await getBlobOrContent(blobId); if (request.headers['if-none-match'] === '"' + blobId + '"') {
sendData(response, data); response.writeHead(304, {});
} response.end();
} else if (uri == "/save") {
let newBlobId = await ssb.blobStore(request.body);
var match;
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
var user = match[1];
var appName = match[2];
var credentials = auth.query(request.headers);
if (credentials && credentials.session &&
(credentials.session.name == user ||
(credentials.permissions.administration && user == 'core'))) {
var database = new Database(user);
var apps = new Set();
let apps_original = database.get('apps');
try {
apps = new Set(JSON.parse(apps_original));
} catch {
}
if (!apps.has(appName)) {
apps.add(appName);
}
apps = JSON.stringify([...apps].sort());
if (apps != apps_original) {
database.set('apps', apps);
}
database.set('path:' + appName, newBlobId);
} else { } else {
response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"}); sendData(response, data, undefined, {etag: '"' + blobId + '"'});
response.end("401 Unauthorized");
return;
} }
} }
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});
response.end("/" + newBlobId);
} else if (uri == "/delete") {
let match;
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
var user = match[1];
var appName = match[2];
var credentials = auth.query(request.headers);
if (credentials && credentials.session &&
(credentials.session.name == user ||
(credentials.permissions.administration && user == 'core'))) {
var database = new Database(user);
var apps = new Set();
try {
apps = new Set(JSON.parse(database.get('apps')));
} catch {
}
if (apps.delete(appName)) {
database.set('apps', JSON.stringify([...apps]));
}
database.remove('path:' + appName);
} else {
response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"});
response.end("401 Unauthorized");
return;
}
}
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});
response.end('OK');
} else { } else {
var data; if (request.headers['if-none-match'] === '"' + blobId + '"') {
var type; response.writeHead(304, {});
var headers; response.end();
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) { } else {
var db = new Database(match[1]); data = await getBlobOrContent(blobId);
var id = await db.get('path:' + match[2]); sendData(response, data, undefined, {etag: '"' + blobId + '"'});
if (id) { }
if (request.headers['if-none-match'] && request.headers['if-none-match'] == '"' + id + '"') { }
headers = { } else if (uri == "/save") {
'Access-Control-Allow-Origin': '*', let newBlobId = await ssb.blobStore(request.body);
};
response.writeHead(304, headers); var match;
response.end(); if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
} else { var user = match[1];
data = utf8Decode(await getBlobOrContent(id)); var appName = match[2];
var appObject = JSON.parse(data); var credentials = auth.query(request.headers);
data = appObject.files[uri.substring(1)]; if (credentials && credentials.session &&
data = await getBlobOrContent(data); (credentials.session.name == user ||
type = guessType(uri); (credentials.permissions.administration && user == 'core'))) {
headers = { var database = new Database(user);
'ETag': '"' + id + '"', var apps = new Set();
'Access-Control-Allow-Origin': '*', let apps_original = database.get('apps');
}; try {
sendData(response, data, type, headers); apps = new Set(JSON.parse(apps_original));
} } catch {
}
if (!apps.has(appName)) {
apps.add(appName);
}
apps = JSON.stringify([...apps].sort());
if (apps != apps_original) {
database.set('apps', apps);
}
database.set('path:' + appName, newBlobId);
} else {
response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"});
response.end("401 Unauthorized");
return;
}
}
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});
response.end("/" + newBlobId);
} else if (uri == "/delete") {
let match;
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
var user = match[1];
var appName = match[2];
var credentials = auth.query(request.headers);
if (credentials && credentials.session &&
(credentials.session.name == user ||
(credentials.permissions.administration && user == 'core'))) {
var database = new Database(user);
var apps = new Set();
try {
apps = new Set(JSON.parse(database.get('apps')));
} catch {
}
if (apps.delete(appName)) {
database.set('apps', JSON.stringify([...apps]));
}
database.remove('path:' + appName);
} else {
response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"});
response.end("401 Unauthorized");
return;
}
}
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});
response.end('OK');
} else {
var data;
var type;
var headers;
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
var db = new Database(match[1]);
var id = await db.get('path:' + match[2]);
if (id) {
if (request.headers['if-none-match'] && request.headers['if-none-match'] == '"' + id + '"') {
headers = {
'Access-Control-Allow-Origin': '*',
};
response.writeHead(304, headers);
response.end();
} else { } else {
data = utf8Decode(await getBlobOrContent(id));
var appObject = JSON.parse(data);
data = appObject.files[uri.substring(1)];
data = await getBlobOrContent(data);
type = guessType(uri);
headers = {
'ETag': '"' + id + '"',
'Access-Control-Allow-Origin': '*',
};
sendData(response, data, type, headers); sendData(response, data, type, headers);
} }
} else { } else {
data = utf8Decode(await getBlobOrContent(blobId));
var appObject = JSON.parse(data);
data = appObject.files[uri.substring(1)];
data = await getBlobOrContent(data);
headers = {
'Access-Control-Allow-Origin': '*',
};
sendData(response, data, type, headers); sendData(response, data, type, headers);
} }
} else {
data = utf8Decode(await getBlobOrContent(blobId));
var appObject = JSON.parse(data);
data = appObject.files[uri.substring(1)];
data = await getBlobOrContent(data);
headers = {
'Access-Control-Allow-Origin': '*',
};
sendData(response, data, type, headers);
} }
} }
} }