src/server/daemon/sessionhandler.c

changeset 152
8b85c5face66
parent 142
55298bc9ed28
child 159
9ba9f8befa80
child 161
aadda87bad1b
equal deleted inserted replaced
151:74d21dd5fd5d 152:8b85c5face66
40 #include "httplistener.h" 40 #include "httplistener.h"
41 41
42 typedef struct _event_http_io { 42 typedef struct _event_http_io {
43 HTTPRequest *request; 43 HTTPRequest *request;
44 HttpParser *parser; 44 HttpParser *parser;
45 int error;
45 } EventHttpIO; 46 } EventHttpIO;
46 47
47 48
48 int connection_read(Connection *conn, void *buf, int len) { 49 int connection_read(Connection *conn, void *buf, int len) {
49 return (int)read(conn->fd, buf, len); 50 return (int)read(conn->fd, buf, len);
210 if(io == NULL) { 211 if(io == NULL) {
211 // TODO: error handling 212 // TODO: error handling
212 } 213 }
213 io->request = request; 214 io->request = request;
214 io->parser = parser; 215 io->parser = parser;
216 io->error = 0;
215 217
216 /* 218 /*
217 * to start the request handling, we begin with a poll on the socket, 219 * to start the request handling, we begin with a poll on the socket,
218 * 220 *
219 * evt_enq_conn() --> event handler --> handle_request() 221 * evt_enq_conn() --> event handler --> handle_request()
220 */ 222 */
221 223
222 event_handler_t *ev = ((EventSessionHandler*)handler)->eventhandler; 224 event_handler_t *ev = ((EventSessionHandler*)handler)->eventhandler;
223 225
224 event_t *event = malloc(sizeof(event_t)); 226 event_t *event = malloc(sizeof(event_t));
227 ZERO(event, sizeof(event_t));
225 event->fn = evt_request_input; 228 event->fn = evt_request_input;
226 event->finish = evt_request_finish; 229 event->finish = evt_request_finish;
227 event->cookie = io; 230 event->cookie = io;
228 231
229 if(ev_pollin(ev, conn->fd, event) != 0) { 232 if(ev_pollin(ev, conn->fd, event) != 0) {
261 } 264 }
262 } 265 }
263 } 266 }
264 267
265 event->finish = evt_request_error; 268 event->finish = evt_request_error;
269 io->error = 1;
266 return 0; 270 return 0;
267 } 271 }
268 //fwrite(buf->inbuf + buf->pos, 1, r, stdout); 272 //fwrite(buf->inbuf + buf->pos, 1, r, stdout);
269 //printf("\n"); 273 //printf("\n");
270 274
272 state = http_parser_process(parser); 276 state = http_parser_process(parser);
273 if(state == 2) { 277 if(state == 2) {
274 // parse error 278 // parse error
275 fatal_error(request, 400); 279 fatal_error(request, 400);
276 event->finish = evt_request_error; 280 event->finish = evt_request_error;
281 io->error = 2;
277 return 0; 282 return 0;
278 } else if(state == 1) { 283 } else if(state == 1) {
279 /* 284 /*
280 * we need more data -> return 1 to tell the event handler to 285 * we need more data -> return 1 to tell the event handler to
281 * continue polling 286 * continue polling
292 flags = 0; 297 flags = 0;
293 } 298 }
294 if (fcntl(request->connection->fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { 299 if (fcntl(request->connection->fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
295 // just close the connection if fcntl fails 300 // just close the connection if fcntl fails
296 event->finish = evt_request_error; 301 event->finish = evt_request_error;
302 io->error = 3;
297 return 0; 303 return 0;
298 } 304 }
299 305
300 /* 306 /*
301 * process request 307 * process request
330 free(event); 336 free(event);
331 337
332 return 0; 338 return 0;
333 } 339 }
334 340
335 int evt_request_error(event_handler_t *h, event_t *event) { 341 int evt_request_error(event_handler_t *h, event_t *event) {
336 EventHttpIO *io = event->cookie; 342 EventHttpIO *io = event->cookie;
337 HttpParser *parser = io->parser; 343 HttpParser *parser = io->parser;
338 HTTPRequest *request = io->request; 344 HTTPRequest *request = io->request;
345
346 if(event->error) {
347 log_ereport(LOG_VERBOSE, "sessionhandler http io error: %d", io->error);
348 }
339 349
340 free(request->netbuf->inbuf); 350 free(request->netbuf->inbuf);
341 free(request->netbuf); 351 free(request->netbuf);
342 352
343 cfg_unref(request->connection->listener->cfg); 353 cfg_unref(request->connection->listener->cfg);

mercurial