main.lua

Mon, 04 Jan 2016 17:03:19 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 Jan 2016 17:03:19 +0000
changeset 18
8050134f35b3
parent 16
d35376a53644
child 25
f018f5a759b1
permissions
-rw-r--r--

clients: Include request in the new-client event

0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 collectgarbage("setpause", 110);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 assert(arg[1], "Please supply upstream URL");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local server = require "net.server_select";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local http = require "net.http";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local logger = require "util.logger";
2
13a1d5e43a2b main.lua: Log upstream HTTP errors
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
8 local log = logger.init("main");
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 events = require "util.events".new();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 require "clients";
16
d35376a53644 main, geoip: Add GeoIP lookup support for watcher info
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
12 require "geoip";
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local function printf(source, level, fmt, ...) return print(source, level, fmt:format(...)); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 for _, level in ipairs{"debug", "info", "warn", "error"} do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 logger.add_level_sink(level, printf);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local request;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 events.add_handler("have-clients", function ()
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 request = http.request(arg[1], {success_on_chunk=true}, function (data, code, response)
2
13a1d5e43a2b main.lua: Log upstream HTTP errors
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
23 if code ~= 200 then
13a1d5e43a2b main.lua: Log upstream HTTP errors
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
24 log("error", "Error from upstream: %s", tostring(code));
13a1d5e43a2b main.lua: Log upstream HTTP errors
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
25 return;
13a1d5e43a2b main.lua: Log upstream HTTP errors
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
26 end
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local boundary = response.headers["content-type"]:match("; boundary=(.+)$").."\r\n";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if #data > #boundary and data:sub(1, #boundary) ~= boundary then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 print(string.format(("%02X "):rep(6), data:byte(1,6)));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 error("bad format");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local img_header, header_len = data:sub(#boundary-1):match("(\r\n.-\r\n)\r\n()");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if not img_header then return; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local img_size = tonumber(img_header:match("\r\nContent%-Length:%s*(%d+)\r\n"));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local start_idx, end_idx = #boundary-2+header_len, #boundary-2+header_len+img_size-1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if end_idx + 2 > #data then return; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local img = data:sub(start_idx, end_idx);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 assert(#img == img_size, "incorrect image size: actual:"..#img.." vs expected: "..img_size.." data: "..#data)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 response.body = data:sub(end_idx+3);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 events.fire_event("image-changed", { image = img });
3
8d3d5d8afbad main.lua: Log connection failures and fire an event
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
41 if not response.partial then
8d3d5d8afbad main.lua: Log connection failures and fire an event
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
42 log("warn", "connection to upstream lost");
8d3d5d8afbad main.lua: Log connection failures and fire an event
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
43 events.fire_event("connection-lost");
8d3d5d8afbad main.lua: Log connection failures and fire an event
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
44 end
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 events.add_handler("no-clients", function ()
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 request.conn:close();
4
ae185012e7ba main.lua: Set request to nil when connection closes
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
50 request = nil;
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 server.loop();

mercurial