Software /
code /
prosody
Comparison
net/http.lua @ 7465:f1923f00b7de
Merge 0.10->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 07 Jul 2016 23:12:25 +0100 |
parent | 6826:17a4e89a4780 |
parent | 7464:3b7de72e58a9 |
child | 7523:3c40b972260e |
comparison
equal
deleted
inserted
replaced
7462:ced4e800f42a | 7465:f1923f00b7de |
---|---|
24 local log = require "util.logger".init("http"); | 24 local log = require "util.logger".init("http"); |
25 | 25 |
26 local _ENV = nil; | 26 local _ENV = nil; |
27 | 27 |
28 local requests = {}; -- Open requests | 28 local requests = {}; -- Open requests |
29 | |
30 local function make_id(req) return (tostring(req):match("%x+$")); end | |
29 | 31 |
30 local listener = { default_port = 80, default_mode = "*a" }; | 32 local listener = { default_port = 80, default_mode = "*a" }; |
31 | 33 |
32 function listener.onconnect(conn) | 34 function listener.onconnect(conn) |
33 local req = requests[conn]; | 35 local req = requests[conn]; |
111 end | 113 end |
112 request.parser:feed(data); | 114 request.parser:feed(data); |
113 end | 115 end |
114 | 116 |
115 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end | 117 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end |
118 local function log_if_failed(id, ret, ...) | |
119 if not ret then | |
120 log("error", "Request %s: error in callback: %s", id, tostring((...))); | |
121 end | |
122 return ...; | |
123 end | |
124 | |
116 local function request(u, ex, callback) | 125 local function request(u, ex, callback) |
117 local req = url.parse(u); | 126 local req = url.parse(u); |
118 | 127 |
119 if not (req and req.host) then | 128 if not (req and req.host) then |
120 callback(nil, 0, req); | 129 callback(nil, 0, req); |
122 end | 131 end |
123 | 132 |
124 if not req.path then | 133 if not req.path then |
125 req.path = "/"; | 134 req.path = "/"; |
126 end | 135 end |
136 | |
137 req.id = ex and ex.id or make_id(req); | |
127 | 138 |
128 local method, headers, body; | 139 local method, headers, body; |
129 | 140 |
130 local host, port = req.host, req.port; | 141 local host, port = req.host, req.port; |
131 local host_header = host; | 142 local host_header = host; |
159 headers[k] = v; | 170 headers[k] = v; |
160 end | 171 end |
161 end | 172 end |
162 end | 173 end |
163 | 174 |
175 log("debug", "Making %s %s request %s to %s", req.scheme, method or "GET", req.id, (ex and ex.suppress_url and host_header) or u); | |
176 | |
164 -- Attach to request object | 177 -- Attach to request object |
165 req.method, req.headers, req.body = method, headers, body; | 178 req.method, req.headers, req.body = method, headers, body; |
166 | 179 |
167 local using_https = req.scheme == "https"; | 180 local using_https = req.scheme == "https"; |
168 if using_https and not ssl_available then | 181 if using_https and not ssl_available then |
181 return nil, conn; | 194 return nil, conn; |
182 end | 195 end |
183 req.handler, req.conn = handler, conn | 196 req.handler, req.conn = handler, conn |
184 req.write = function (...) return req.handler:write(...); end | 197 req.write = function (...) return req.handler:write(...); end |
185 | 198 |
186 req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end | 199 req.callback = function (content, code, request, response) |
200 log("debug", "request %s: Calling callback, status %s", req.id, code or "---"); | |
201 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); | |
202 end | |
187 req.reader = request_reader; | 203 req.reader = request_reader; |
188 req.state = "status"; | 204 req.state = "status"; |
189 | 205 |
190 requests[req.handler] = req; | 206 requests[req.handler] = req; |
191 return req; | 207 return req; |