Comparison

mod_strict_https/mod_strict_https.lua @ 861:1b34c8e46ffb

mod_strict_https: New module implementing HTTP Strict Transport Security
author Kim Alvefur <zash@zash.se>
date Fri, 23 Nov 2012 19:04:10 +0100
child 863:efa9c1676d1f
comparison
equal deleted inserted replaced
860:1c886affb375 861:1b34c8e46ffb
1 -- HTTP Strict Transport Security
2 -- https://tools.ietf.org/html/rfc6797
3
4 module:set_global();
5
6 local http_server = require "net.http.server";
7
8 local hsts_header = module:get_option_string("hsts_header", "max_age=31556952"); -- This means "Don't even try to access without HTTPS for a year"
9
10 local _old_send_response;
11 local _old_fire_event;
12
13 local modules = {};
14
15 function module.load()
16 _old_send_response = http_server.send_response;
17 function http_server.send_response(response, body)
18 response.headers.strict_transport_security = hsts_header;
19 return _old_send_response(response, body);
20 end
21
22 _old_fire_event = http_server._events.fire_event;
23 function http_server._events.fire_event(event, payload)
24 local request = payload.request;
25 local host = event:match("^[A-Z]+ ([^/]+)");
26 local module = modules[host];
27 if module and not request.secure then
28 payload.response.headers.location = module:http_url(request.path);
29 return 301;
30 end
31 return _old_fire_event(event, payload);
32 end
33 end
34 function module.unload()
35 http_server.send_response = _old_send_response;
36 http_server._events.fire_event = _old_fire_event;
37 end
38 function module.add_host(module)
39 local http_host = module:get_option_string("http_host", module.host);
40 modules[http_host] = module;
41 function module.unload()
42 modules[http_host] = nil;
43 end
44 end