Mercurial > prosody-hg
comparison net/http/server.lua @ 11372:b877bd74d65e
net.http.server: Allow storing more than the parser in the session
Storing the async thread on the connection was weird.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 12 Feb 2021 14:47:27 +0100 |
| parents | 73f7acf8a61f |
| children | ad3b5384fc03 |
comparison
equal
deleted
inserted
replaced
| 11371:73f7acf8a61f | 11372:b877bd74d65e |
|---|---|
| 94 end, -1); | 94 end, -1); |
| 95 | 95 |
| 96 local runner_callbacks = {}; | 96 local runner_callbacks = {}; |
| 97 | 97 |
| 98 function runner_callbacks:ready() | 98 function runner_callbacks:ready() |
| 99 self.data:resume(); | 99 self.data.conn:resume(); |
| 100 end | 100 end |
| 101 | 101 |
| 102 function runner_callbacks:waiting() | 102 function runner_callbacks:waiting() |
| 103 self.data:pause(); | 103 self.data.conn:pause(); |
| 104 end | 104 end |
| 105 | 105 |
| 106 function runner_callbacks:error(err) | 106 function runner_callbacks:error(err) |
| 107 log("error", "Traceback[httpserver]: %s", err); | 107 log("error", "Traceback[httpserver]: %s", err); |
| 108 self.data:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = err })); | 108 self.data.conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = err })); |
| 109 self.data:close(); | 109 self.data.conn:close(); |
| 110 end | 110 end |
| 111 | 111 |
| 112 function listener.onconnect(conn) | 112 function listener.onconnect(conn) |
| 113 local session = { conn = conn }; | |
| 113 local secure = conn:ssl() and true or nil; | 114 local secure = conn:ssl() and true or nil; |
| 114 conn._thread = async.runner(function (request) | 115 session.thread = async.runner(function (request) |
| 115 local wait, done = async.waiter(); | 116 local wait, done = async.waiter(); |
| 116 handle_request(conn, request, done); wait(); | 117 handle_request(conn, request, done); wait(); |
| 117 end, runner_callbacks, conn); | 118 end, runner_callbacks, session); |
| 118 local function success_cb(request) | 119 local function success_cb(request) |
| 119 --log("debug", "success_cb: %s", request.path); | 120 --log("debug", "success_cb: %s", request.path); |
| 120 request.secure = secure; | 121 request.secure = secure; |
| 121 conn._thread:run(request); | 122 session.thread:run(request); |
| 122 end | 123 end |
| 123 local function error_cb(err) | 124 local function error_cb(err) |
| 124 log("debug", "error_cb: %s", err or "<nil>"); | 125 log("debug", "error_cb: %s", err or "<nil>"); |
| 125 -- FIXME don't close immediately, wait until we process current stuff | 126 -- FIXME don't close immediately, wait until we process current stuff |
| 126 -- FIXME if err, send off a bad-request response | 127 -- FIXME if err, send off a bad-request response |
| 127 sessions[conn] = nil; | |
| 128 conn:close(); | 128 conn:close(); |
| 129 end | 129 end |
| 130 local function options_cb() | 130 local function options_cb() |
| 131 return options; | 131 return options; |
| 132 end | 132 end |
| 133 sessions[conn] = parser_new(success_cb, error_cb, "server", options_cb); | 133 session.parser = parser_new(success_cb, error_cb, "server", options_cb); |
| 134 sessions[conn] = session; | |
| 134 end | 135 end |
| 135 | 136 |
| 136 function listener.ondisconnect(conn) | 137 function listener.ondisconnect(conn) |
| 137 local open_response = conn._http_open_response; | 138 local open_response = conn._http_open_response; |
| 138 if open_response and open_response.on_destroy then | 139 if open_response and open_response.on_destroy then |
| 147 sessions[conn] = nil; | 148 sessions[conn] = nil; |
| 148 incomplete[conn] = nil; | 149 incomplete[conn] = nil; |
| 149 end | 150 end |
| 150 | 151 |
| 151 function listener.onincoming(conn, data) | 152 function listener.onincoming(conn, data) |
| 152 sessions[conn]:feed(data); | 153 sessions[conn].parser:feed(data); |
| 153 end | 154 end |
| 154 | 155 |
| 155 function listener.ondrain(conn) | 156 function listener.ondrain(conn) |
| 156 local response = incomplete[conn]; | 157 local response = incomplete[conn]; |
| 157 if response and response._send_more then | 158 if response and response._send_more then |
