From 4d4cc9215024ad455c57345b3ccf9942e1c36d2c Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 11 Jan 2023 23:39:42 +0000 Subject: [PATCH] Optionally enforce an HTTP => HTTPS redirect. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4116 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/core.js | 5 +++++ core/httpd.js | 10 ++++++++++ 2 files changed, 15 insertions(+) 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) {