Software /
code /
prosody
Comparison
net/http.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 |
parent | 4351:3f414091a008 |
child | 4355:9e2a841739b5 |
comparison
equal
deleted
inserted
replaced
4351:3f414091a008 | 4352:912a49b1c4e3 |
---|---|
95 | 95 |
96 if not req.path then | 96 if not req.path then |
97 req.path = "/"; | 97 req.path = "/"; |
98 end | 98 end |
99 | 99 |
100 local custom_headers, body; | 100 local method, headers, body; |
101 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } | |
102 | 101 |
102 headers = { | |
103 ["Host"] = req.host; | |
104 ["User-Agent"] = "Prosody XMPP Server"; | |
105 }; | |
103 | 106 |
104 if req.userinfo then | 107 if req.userinfo then |
105 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo); | 108 headers["Authorization"] = "Basic "..mime.b64(req.userinfo); |
106 end | 109 end |
107 | 110 |
108 if ex then | 111 if ex then |
109 custom_headers = ex.headers; | |
110 req.onlystatus = ex.onlystatus; | 112 req.onlystatus = ex.onlystatus; |
111 body = ex.body; | 113 body = ex.body; |
112 if body then | 114 if body then |
113 req.method = "POST "; | 115 method = "POST "; |
114 default_headers["Content-Length"] = tostring(#body); | 116 headers["Content-Length"] = tostring(#body); |
115 default_headers["Content-Type"] = "application/x-www-form-urlencoded"; | 117 headers["Content-Type"] = "application/x-www-form-urlencoded"; |
116 end | 118 end |
117 if ex.method then method = ex.method; end | 119 if ex.method then method = ex.method; end |
118 if ex.headers then | 120 if ex.headers then |
119 for k, v in pairs(ex.headers) do | 121 for k, v in pairs(ex.headers) do |
120 headers[k] = v; | 122 headers[k] = v; |
121 end | 123 end |
122 end | 124 end |
123 end | 125 end |
124 | 126 |
125 req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a"); | 127 -- Attach to request object |
126 req.write = function (...) return req.handler:write(...); end | 128 req.method, req.headers, req.body = method, headers, body; |
127 req.conn:settimeout(0); | 129 |
128 local ok, err = req.conn:connect(req.host, req.port or 80); | 130 local using_https = req.scheme == "https"; |
131 local port = req.port or (using_https and 443 or 80); | |
132 | |
133 -- Connect the socket, and wrap it with net.server | |
134 local conn = socket.tcp(); | |
135 conn:settimeout(10); | |
136 local ok, err = conn:connect(req.host, port); | |
129 if not ok and err ~= "timeout" then | 137 if not ok and err ~= "timeout" then |
130 callback(nil, 0, req); | 138 callback(nil, 0, req); |
131 return nil, err; | 139 return nil, err; |
132 end | 140 end |
133 | 141 |
134 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; | 142 req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", using_https and { mode = "client", protocol = "sslv23" }); |
135 | 143 req.write = function (...) return req.handler:write(...); end |
136 if req.query then | |
137 t_insert(request_line, 4, "?"); | |
138 t_insert(request_line, 5, req.query); | |
139 end | |
140 | |
141 req.write(t_concat(request_line)); | |
142 local t = { [2] = ": ", [4] = "\r\n" }; | |
143 if custom_headers then | |
144 for k, v in pairs(custom_headers) do | |
145 t[1], t[3] = k, v; | |
146 req.write(t_concat(t)); | |
147 default_headers[k] = nil; | |
148 end | |
149 end | |
150 | |
151 for k, v in pairs(default_headers) do | |
152 t[1], t[3] = k, v; | |
153 req.write(t_concat(t)); | |
154 default_headers[k] = nil; | |
155 end | |
156 req.write("\r\n"); | |
157 | |
158 if body then | |
159 req.write(body); | |
160 end | |
161 | 144 |
162 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 | 145 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 |
163 req.reader = request_reader; | 146 req.reader = request_reader; |
164 req.state = "status"; | 147 req.state = "status"; |
165 | 148 |