Comparison

net/http/server.lua @ 4691:a164fc7057ae

net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
author Matthew Wild <mwild1@gmail.com>
date Wed, 25 Apr 2012 23:08:33 +0100
parent 4689:e8c357259993
child 4694:f9a091b6dfd5
comparison
equal deleted inserted replaced
4690:55f690fdc915 4691:a164fc7057ae
115 end 115 end
116 sessions[conn] = parser_new(success_cb, error_cb); 116 sessions[conn] = parser_new(success_cb, error_cb);
117 end 117 end
118 118
119 function listener.ondisconnect(conn) 119 function listener.ondisconnect(conn)
120 local open_response = conn._http_open_response;
121 if open_response and open_response.on_destroy then
122 open_response.finished = true;
123 open_response:on_destroy();
124 end
120 sessions[conn] = nil; 125 sessions[conn] = nil;
121 end 126 end
122 127
123 function listener.onincoming(conn, data) 128 function listener.onincoming(conn, data)
124 sessions[conn]:feed(data); 129 sessions[conn]:feed(data);
152 headers = { date = date_header, connection = (keep_alive and "Keep-Alive" or "close") }; 157 headers = { date = date_header, connection = (keep_alive and "Keep-Alive" or "close") };
153 conn = conn; 158 conn = conn;
154 send = _M.send_response; 159 send = _M.send_response;
155 finish_cb = finish_cb; 160 finish_cb = finish_cb;
156 }; 161 };
162 conn._http_open_response = response;
157 163
158 if not request.headers.host then 164 if not request.headers.host then
159 response.status_code = 400; 165 response.status_code = 400;
160 response.headers.content_type = "text/html"; 166 response.headers.content_type = "text/html";
161 response:send("<html><head>400 Bad Request</head><body>400 Bad Request: No Host header.</body></html>"); 167 response:send("<html><head>400 Bad Request</head><body>400 Bad Request: No Host header.</body></html>");
193 response.headers.content_type = "text/html"; 199 response.headers.content_type = "text/html";
194 response:send("<html><head><title>404 Not Found</title></head><body>404 Not Found: No such page.</body></html>"); 200 response:send("<html><head><title>404 Not Found</title></head><body>404 Not Found: No such page.</body></html>");
195 end 201 end
196 end 202 end
197 function _M.send_response(response, body) 203 function _M.send_response(response, body)
204 if response.finished then return; end
205 response.finished = true;
206 response.conn._http_open_response = nil;
207
198 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); 208 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]);
199 local headers = response.headers; 209 local headers = response.headers;
200 body = body or ""; 210 body = body or "";
201 headers.content_length = #body; 211 headers.content_length = #body;
202 212
206 end 216 end
207 t_insert(output, "\r\n\r\n"); 217 t_insert(output, "\r\n\r\n");
208 t_insert(output, body); 218 t_insert(output, body);
209 219
210 response.conn:write(t_concat(output)); 220 response.conn:write(t_concat(output));
221 if response.on_destroy then
222 response:on_destroy();
223 response.on_destroy = nil;
224 end
211 if headers.connection == "Keep-Alive" then 225 if headers.connection == "Keep-Alive" then
212 response:finish_cb(); 226 response:finish_cb();
213 else 227 else
214 response.conn:close(); 228 response.conn:close();
215 end 229 end