3 assert(arg[1], "Please supply upstream URL"); |
3 assert(arg[1], "Please supply upstream URL"); |
4 |
4 |
5 local server = require "net.server_select"; |
5 local server = require "net.server_select"; |
6 local http = require "net.http"; |
6 local http = require "net.http"; |
7 local logger = require "util.logger"; |
7 local logger = require "util.logger"; |
|
8 local log = logger.init("main"); |
8 events = require "util.events".new(); |
9 events = require "util.events".new(); |
9 |
10 |
10 require "clients"; |
11 require "clients"; |
11 |
12 |
12 local function printf(source, level, fmt, ...) return print(source, level, fmt:format(...)); end |
13 local function printf(source, level, fmt, ...) return print(source, level, fmt:format(...)); end |
16 |
17 |
17 local request; |
18 local request; |
18 |
19 |
19 events.add_handler("have-clients", function () |
20 events.add_handler("have-clients", function () |
20 request = http.request(arg[1], {success_on_chunk=true}, function (data, code, response) |
21 request = http.request(arg[1], {success_on_chunk=true}, function (data, code, response) |
|
22 if code ~= 200 then |
|
23 log("error", "Error from upstream: %s", tostring(code)); |
|
24 return; |
|
25 end |
21 local boundary = response.headers["content-type"]:match("; boundary=(.+)$").."\r\n"; |
26 local boundary = response.headers["content-type"]:match("; boundary=(.+)$").."\r\n"; |
22 if #data > #boundary and data:sub(1, #boundary) ~= boundary then |
27 if #data > #boundary and data:sub(1, #boundary) ~= boundary then |
23 print(string.format(("%02X "):rep(6), data:byte(1,6))); |
28 print(string.format(("%02X "):rep(6), data:byte(1,6))); |
24 error("bad format"); |
29 error("bad format"); |
25 end |
30 end |