Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rtjscomp",
"version": "0.9.12",
"version": "0.9.13",
"description": "php-like server but with javascript",
"repository": {
"type": "git",
Expand Down
44 changes: 34 additions & 10 deletions rtjscomp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1526,8 +1526,25 @@ const request_handle = async (request, response, https) => {
}
}

// Generate Last-Modified header from file modification time
const mtime_ms = file_stat.mtimeMs;
const last_modified = new Date(mtime_ms).toUTCString();

// Check If-Modified-Since (time-based validation)
if ('if-modified-since' in request_headers) {
const if_modified_since = new Date(request_headers['if-modified-since']);
// Round mtime down to seconds for comparison (HTTP dates don't have millisecond precision)
const mtime_seconds = Math.floor(mtime_ms / 1000) * 1000;
if (!isNaN(if_modified_since) && mtime_seconds <= if_modified_since.getTime()) {
response.setHeader('Last-Modified', last_modified);
response.setHeader('Cache-Control', 'public, max-age=31536000');
throw 304;
}
}

if (spam_enabled) spam('static_send', [path, file_compression]);
response.setHeader('Cache-Control', 'public, max-age=600');
response.setHeader('Cache-Control', 'public, max-age=31536000');
response.setHeader('Last-Modified', last_modified);
if (compression_enabled_type) {
response.setHeader('Vary', 'Accept-Encoding');
}
Expand Down Expand Up @@ -1637,15 +1654,22 @@ const request_handle = async (request, response, https) => {
}

if (!response.headersSent) {
response.writeHead(err, {
'Content-Type': 'text/html',
'Cache-Control': 'no-cache, no-store',
});
response.end(`<!DOCTYPE html><html><body><h1>HTTP ${
err
}: ${
http.STATUS_CODES[err] || 'Error'
}</h1></body></html>`);
if (err === 304) {
// 304 Not Modified - headers already set, just send status
response.statusCode = 304;
response.end();
}
else {
response.writeHead(err, {
'Content-Type': 'text/html',
'Cache-Control': 'no-cache, no-store',
});
response.end(`<!DOCTYPE html><html><body><h1>HTTP ${
err
}: ${
http.STATUS_CODES[err] || 'Error'
}</h1></body></html>`);
}
}
if ('content-length' in request_headers) {
request.socket.destroy();
Expand Down