diff -r 62aad4d94d5d -r 8f4d28ac6ae2 src/server/daemon/sessionhandler.c --- a/src/server/daemon/sessionhandler.c Wed Nov 02 17:53:15 2022 +0100 +++ b/src/server/daemon/sessionhandler.c Wed Nov 02 18:08:06 2022 +0100 @@ -143,9 +143,9 @@ void* basic_run_session(void *data) { Connection *conn = (Connection*)data; - HTTPRequest request; - http_request_init(&request); - request.connection = conn; + HTTPRequest *request = malloc(sizeof(HTTPRequest)); + http_request_init(request); + request->connection = conn; // read request netbuf *buf = malloc(sizeof(netbuf)); @@ -157,43 +157,50 @@ buf->inbuf = malloc(2048); buf->errmsg = NULL; - request.netbuf = buf; + request->netbuf = buf; - HttpParser *parser = http_parser_new(&request); + HttpParser *parser = http_parser_new(request); int state; int r; r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); - if(r <= 0) { - // TODO: error handling - fprintf(stderr, "%s\n", "Error: Cannot read from socket"); - return NULL; - } - buf->cursize += r; - while((state = http_parser_process(parser)) != 0) { - if(state == 2) { - // TODO: error handling - fprintf(stderr, "%s\n", "Error: Cannot parse http request"); - return NULL; + + if(r > 0) { + int err = 0; + buf->cursize += r; + while((state = http_parser_process(parser)) != 0) { + if(state == 2) { + log_ereport(LOG_FAILURE, "basic_run_session: invalid http request"); + err = 1; + break; + } + r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); + if(r == -1) { + log_ereport(LOG_FAILURE, "basic_run_session: IO error: %s", strerror(errno)); + err = 1; + break; + } + buf->cursize += r; } - r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); - if(r == -1) { - // TODO: error handling - fprintf(stderr, "%s\n", "Error: Cannot read from socket"); - return NULL; + + if(!err) { + if(http_parser_validate(parser)) { + // process request + r = handle_request(request, NULL, NULL); // TODO: use correct thread pool + } else { + log_ereport(LOG_FAILURE, "basic_run_session: http parser validation failed"); + fatal_error(request, 400); + } } - buf->cursize += r; - } - if(!http_parser_validate(parser)) { - log_ereport(LOG_FAILURE, "http_parser_validate failed"); - // TODO: send error 400 bad request - return NULL; + } else { + log_ereport(LOG_FAILURE, "basic_run_session: IO error: %s", strerror(errno)); } - // process request - r = handle_request(&request, NULL, NULL); // TODO: use correct thread pool + free(buf->inbuf); + free(buf); + connection_destroy(conn); + http_parser_free(parser); + http_request_cleanup(request); - // TODO: free, see evt_request_finish - return NULL; }