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) { |
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 |
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); |