diff options
author | Matthew Wild <mwild1@gmail.com> | 2011-08-20 15:10:04 -0400 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2011-08-20 15:10:04 -0400 |
commit | 3c8407228767ad8f932e8b98869e0fbab443b7ad (patch) | |
tree | 7e50423fa10ef1ca181d9d9d04d0c3fb7301c7da /net | |
parent | cbc11b51bba628e9a3dc07bdfb96a63b724e0c88 (diff) | |
download | prosody-3c8407228767ad8f932e8b98869e0fbab443b7ad.tar.gz prosody-3c8407228767ad8f932e8b98869e0fbab443b7ad.zip |
net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Diffstat (limited to 'net')
-rw-r--r-- | net/http.lua | 59 | ||||
-rw-r--r-- | net/httpclient_listener.lua | 22 |
2 files changed, 43 insertions, 38 deletions
diff --git a/net/http.lua b/net/http.lua index 355332bc..df4d9a02 100644 --- a/net/http.lua +++ b/net/http.lua @@ -97,22 +97,24 @@ function request(u, ex, callback) req.path = "/"; end - local custom_headers, body; - local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } + local method, headers, body; + headers = { + ["Host"] = req.host; + ["User-Agent"] = "Prosody XMPP Server"; + }; if req.userinfo then - default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo); + headers["Authorization"] = "Basic "..mime.b64(req.userinfo); end if ex then - custom_headers = ex.headers; req.onlystatus = ex.onlystatus; body = ex.body; if body then - req.method = "POST "; - default_headers["Content-Length"] = tostring(#body); - default_headers["Content-Type"] = "application/x-www-form-urlencoded"; + method = "POST "; + headers["Content-Length"] = tostring(#body); + headers["Content-Type"] = "application/x-www-form-urlencoded"; end if ex.method then method = ex.method; end if ex.headers then @@ -122,42 +124,23 @@ function request(u, ex, callback) end end - req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a"); - req.write = function (...) return req.handler:write(...); end - req.conn:settimeout(0); - local ok, err = req.conn:connect(req.host, req.port or 80); + -- Attach to request object + req.method, req.headers, req.body = method, headers, body; + + local using_https = req.scheme == "https"; + local port = req.port or (using_https and 443 or 80); + + -- Connect the socket, and wrap it with net.server + local conn = socket.tcp(); + conn:settimeout(10); + local ok, err = conn:connect(req.host, port); if not ok and err ~= "timeout" then callback(nil, 0, req); return nil, err; end - local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; - - if req.query then - t_insert(request_line, 4, "?"); - t_insert(request_line, 5, req.query); - end - - req.write(t_concat(request_line)); - local t = { [2] = ": ", [4] = "\r\n" }; - if custom_headers then - for k, v in pairs(custom_headers) do - t[1], t[3] = k, v; - req.write(t_concat(t)); - default_headers[k] = nil; - end - end - - for k, v in pairs(default_headers) do - t[1], t[3] = k, v; - req.write(t_concat(t)); - default_headers[k] = nil; - end - req.write("\r\n"); - - if body then - req.write(body); - end + req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", using_https and { mode = "client", protocol = "sslv23" }); + req.write = function (...) return req.handler:write(...); end req.callback = function (content, code, request) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end req.reader = request_reader; diff --git a/net/httpclient_listener.lua b/net/httpclient_listener.lua index dfa25062..df571a58 100644 --- a/net/httpclient_listener.lua +++ b/net/httpclient_listener.lua @@ -7,6 +7,7 @@ -- local log = require "util.logger".init("httpclient_listener"); +local t_concat = table.concat; local connlisteners_register = require "net.connlisteners".register; @@ -15,6 +16,27 @@ local buffers = {}; -- Buffers of partial lines local httpclient = { default_port = 80, default_mode = "*a" }; +function httpclient.onconnect(conn) + local req = requests[conn]; + -- Send the request + local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; + if req.query then + t_insert(request_line, 4, "?"..req.query); + end + + conn:write(t_concat(request_line)); + local t = { [2] = ": ", [4] = "\r\n" }; + for k, v in pairs(req.headers) do + t[1], t[3] = k, v; + conn:write(t_concat(t)); + end + conn:write("\r\n"); + + if body then + conn:write(body); + end +end + function httpclient.onincoming(conn, data) local request = requests[conn]; |