Page MenuHomePhabricator
Paste P11584

(An Untitled Masterwork)
ActivePublic

Authored by Vgutierrez on Jun 18 2020, 11:28 AM.
Tags
None
Referenced Files
F31870386: raw.txt
Jun 18 2020, 11:28 AM
Subscribers
None
function read_config()
local configfile = ts.get_config_dir() .. "/lua/http-redirect.lua.conf"
ts.debug("Reading " .. configfile)
dofile(configfile)
assert(lua_hostname, "lua_hostname not set by " .. configfile)
ts.debug("read_config() returning " .. lua_hostname)
return lua_hostname
end
local HOSTNAME = read_config()
local ALLOWED_PLAIN_METHODS = {GET=true, HEAD=true}
function handle_plain_request()
local client_ip, client_port, client_family = ts.client_request.client_addr.get_addr()
local http_method = ts.client_request.get_method()
local response_status = "403 Insecure Request Forbidden - use HTTPS - https://lists.wikimedia.org/pipermail/mediawiki-api-announce/2016-May/000110.html"
local headers = 'Server-Timing: cache;desc="int-front"\r\n' ..
'X-Cache: ' .. HOSTNAME .. ' int\r\n' ..
'X-Cache-Status: int-front\r\n' ..
'X-Client-Ip: ' .. client_ip .. '\r\n'
if ALLOWED_PLAIN_METHODS[http_method] ~= nil then
local url = ts.client_request.get_url()
url = string.gsub(url, "^http", "https", 1)
response_status = "301 TLS Redirect"
headers = headers .. 'Location: ' .. url .. '\r\n'
end
ts.say("HTTP/1.1 " .. response_status .. "\r\n" .. headers)
end
function do_remap()
local server_port = ts.client_request.client_addr.get_incoming_port()
if server_port == 80 then
ts.http.server_intercept(handle_plain_request)
return TS_LUA_REMAP_NO_REMAP_STOP
end
return TS_LUA_REMAP_NO_REMAP
end