PiBa-NL | 0527639 | 2018-11-30 21:01:01 +0100 | [diff] [blame] | 1 | |
| 2 | local vtc_port = 0 |
| 3 | |
| 4 | core.register_service("fakeserv", "http", function(applet) |
| 5 | vtc_port = applet.headers["vtcport"][0] |
| 6 | core.Info("APPLET START") |
| 7 | local response = "OK" |
| 8 | applet:add_header("Server", "haproxy/webstats") |
| 9 | applet:add_header("Content-Length", string.len(response)) |
| 10 | applet:add_header("Content-Type", "text/html") |
| 11 | applet:start_response() |
| 12 | applet:send(response) |
| 13 | core.Info("APPLET DONE") |
| 14 | end) |
| 15 | |
| 16 | local function cron() |
| 17 | -- wait for until the correct port is set through the c0 request.. |
| 18 | while vtc_port == 0 do |
| 19 | core.msleep(1) |
| 20 | end |
| 21 | core.Debug('CRON port:' .. vtc_port) |
| 22 | |
| 23 | local socket = core.tcp() |
| 24 | local success = socket:connect("127.0.0.1", vtc_port) |
| 25 | core.Info("SOCKET MADE ".. (success or "??")) |
| 26 | if success ~= 1 then |
| 27 | core.Info("CONNECT SOCKET FAILED?") |
| 28 | return |
| 29 | end |
| 30 | local request = "GET / HTTP/1.1\r\n\r\n" |
| 31 | core.Info("SENDING REQUEST") |
| 32 | socket:send(request) |
| 33 | local result = "" |
| 34 | repeat |
| 35 | core.Info("4") |
| 36 | local d = socket:receive("*a") |
| 37 | if d ~= nil then |
| 38 | result = result .. d |
| 39 | end |
| 40 | until d == nil or d == 0 |
| 41 | core.Info("Received: "..result) |
| 42 | end |
| 43 | |
| 44 | core.register_task(cron) |