comparison net/httpclient_listener.lua @ 4352:912a49b1c4e3

net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
author Matthew Wild <mwild1@gmail.com>
date Sat, 20 Aug 2011 15:10:04 -0400
parents 692b3c6c5bd2
children 502876d94363
comparison
equal deleted inserted replaced
4351:3f414091a008 4352:912a49b1c4e3
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local log = require "util.logger".init("httpclient_listener"); 9 local log = require "util.logger".init("httpclient_listener");
10 local t_concat = table.concat;
10 11
11 local connlisteners_register = require "net.connlisteners".register; 12 local connlisteners_register = require "net.connlisteners".register;
12 13
13 local requests = {}; -- Open requests 14 local requests = {}; -- Open requests
14 local buffers = {}; -- Buffers of partial lines 15 local buffers = {}; -- Buffers of partial lines
15 16
16 local httpclient = { default_port = 80, default_mode = "*a" }; 17 local httpclient = { default_port = 80, default_mode = "*a" };
18
19 function httpclient.onconnect(conn)
20 local req = requests[conn];
21 -- Send the request
22 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
23 if req.query then
24 t_insert(request_line, 4, "?"..req.query);
25 end
26
27 conn:write(t_concat(request_line));
28 local t = { [2] = ": ", [4] = "\r\n" };
29 for k, v in pairs(req.headers) do
30 t[1], t[3] = k, v;
31 conn:write(t_concat(t));
32 end
33 conn:write("\r\n");
34
35 if body then
36 conn:write(body);
37 end
38 end
17 39
18 function httpclient.onincoming(conn, data) 40 function httpclient.onincoming(conn, data)
19 local request = requests[conn]; 41 local request = requests[conn];
20 42
21 if not request then 43 if not request then