219 handler->sh.create_iostream = create_connection_iostream; |
220 handler->sh.create_iostream = create_connection_iostream; |
220 return (SessionHandler*)handler; |
221 return (SessionHandler*)handler; |
221 } |
222 } |
222 |
223 |
223 void evt_enq_conn(SessionHandler *handler, Connection *conn) { |
224 void evt_enq_conn(SessionHandler *handler, Connection *conn) { |
224 HTTPRequest *request = malloc(sizeof(HTTPRequest)); |
|
225 http_request_init(request); |
|
226 request->connection = conn; |
|
227 conn->session_handler = handler; |
|
228 |
|
229 // set socket non blocking |
225 // set socket non blocking |
230 int flags; |
226 int flags; |
231 if ((flags = fcntl(conn->fd, F_GETFL, 0)) == -1) { |
227 if ((flags = fcntl(conn->fd, F_GETFL, 0)) == -1) { |
232 flags = 0; |
228 flags = 0; |
233 } |
229 } |
234 if (fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
230 if (fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
235 perror("Error: start_event_session: fcntl"); |
231 log_ereport(LOG_FAILURE, "sessionhandler: fcntl failed: %s", strerror(errno)); |
236 // TODO: error |
232 connection_destroy(conn); |
237 } |
233 return; |
|
234 } |
|
235 |
|
236 HTTPRequest *request = malloc(sizeof(HTTPRequest)); |
|
237 if(!request) { |
|
238 connection_destroy(conn); |
|
239 return; |
|
240 } |
|
241 http_request_init(request); |
|
242 request->connection = conn; |
|
243 conn->session_handler = handler; |
238 |
244 |
239 // TODO: remove code redundancy (basic_run_session) |
245 // TODO: remove code redundancy (basic_run_session) |
240 |
246 |
241 // read request |
247 // read request |
242 netbuf *buf = malloc(sizeof(netbuf)); |
248 netbuf *buf = malloc(sizeof(netbuf)); |
|
249 if(!buf) { |
|
250 connection_destroy(conn); |
|
251 http_request_cleanup(request); |
|
252 return; |
|
253 } |
243 buf->rdtimeout = 120; |
254 buf->rdtimeout = 120; |
244 buf->pos = 0; |
255 buf->pos = 0; |
245 buf->cursize = 0; |
256 buf->cursize = 0; |
246 buf->maxsize = 2048; |
257 buf->maxsize = 2048; |
247 buf->sd = &conn->fd; |
258 buf->sd = &conn->fd; |
|
259 buf->errmsg = NULL; |
248 buf->inbuf = malloc(2048); |
260 buf->inbuf = malloc(2048); |
249 buf->errmsg = NULL; |
261 if(!buf->inbuf) { |
|
262 connection_destroy(conn); |
|
263 http_request_cleanup(request); |
|
264 free(buf); |
|
265 return; |
|
266 } |
250 |
267 |
251 request->netbuf = buf; |
268 request->netbuf = buf; |
252 |
269 |
253 HttpParser *parser = http_parser_new(request); |
270 HttpParser *parser = http_parser_new(request); |
|
271 if(!parser) { |
|
272 connection_destroy(conn); |
|
273 http_request_cleanup(request); |
|
274 free(buf->inbuf); |
|
275 free(buf); |
|
276 return; |
|
277 } |
254 |
278 |
255 EventHttpIO *io = malloc(sizeof(EventHttpIO)); |
279 EventHttpIO *io = malloc(sizeof(EventHttpIO)); |
256 if(io == NULL) { |
280 if(io == NULL) { |
257 // TODO: error handling |
281 connection_destroy(conn); |
|
282 http_request_cleanup(request); |
|
283 free(buf->inbuf); |
|
284 free(buf); |
|
285 http_parser_free(parser); |
|
286 return; |
258 } |
287 } |
259 io->request = request; |
288 io->request = request; |
260 io->parser = parser; |
289 io->parser = parser; |
261 io->error = 0; |
290 io->error = 0; |
262 |
291 |
275 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler); |
304 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler); |
276 |
305 |
277 if(ev_pollin(ev, conn->fd, event) != 0) { |
306 if(ev_pollin(ev, conn->fd, event) != 0) { |
278 // TODO: ev_pollin should log, intercept some errors here |
307 // TODO: ev_pollin should log, intercept some errors here |
279 log_ereport(LOG_FAILURE, "Cannot enqueue connection"); |
308 log_ereport(LOG_FAILURE, "Cannot enqueue connection"); |
280 connection_destroy(conn); |
309 evt_request_error(ev, event); |
281 // TODO: free stuff |
|
282 } |
310 } |
283 } |
311 } |
284 |
312 |
285 int evt_request_ssl_accept(EventHandler *handler, Event *event) { |
313 int evt_request_ssl_accept(EventHandler *handler, Event *event) { |
286 EventHttpIO *io = event->cookie; |
314 EventHttpIO *io = event->cookie; |