diff --git a/core/core.js b/core/core.js index 28d893d6..47184078 100644 --- a/core/core.js +++ b/core/core.js @@ -27,6 +27,11 @@ const k_global_settings = { default_value: undefined, description: 'Code of conduct presented at sign-in.', }, + http_redirect: { + type: 'string', + default_value: undefined, + description: 'If connecting by HTTP and HTTPS is configured, Location header prefix (ie, "https://example.com")', + }, }; var gGlobalSettings = { diff --git a/core/httpd.js b/core/httpd.js index 6b3644d4..36272307 100644 --- a/core/httpd.js +++ b/core/httpd.js @@ -1,4 +1,5 @@ import * as sha1 from './sha1.js'; +import * as core from './core.js'; "use strict"; @@ -505,10 +506,19 @@ function handleConnection(client) { result.path, `HTTP/1.${result.minor_version}`, ]; + headers = Object.fromEntries(Object.entries(result.headers).map(x => [x[0].toLowerCase(), x[1]])); parsing_header = false; inputBuffer = inputBuffer.slice(result.bytes_parsed); + if (!client.tls && tildefriends.https_port && core.globalSettings.http_redirect) { + let requestObject = new Request(request[0], request[1], request[2], headers, body, client); + let response = new Response(requestObject, client); + response.writeHead(303, {"Location": `${core.globalSettings.http_redirect}${result.path}`, "Content-Length": "0"}); + response.end(); + return; + } + if (headers["content-length"] != undefined) { bodyToRead = parseInt(headers["content-length"]); if (bodyToRead > 16 * 1024 * 1024) {