Thu, 31 Jul 2025 20:53:08 +0200
improve error handling/logging in nsapi_start_request
| src/server/daemon/httprequest.c | file | annotate | diff | comparison | revisions | |
| src/server/daemon/httprequest.h | file | annotate | diff | comparison | revisions |
--- a/src/server/daemon/httprequest.c Mon Jul 21 22:22:46 2025 +0200 +++ b/src/server/daemon/httprequest.c Thu Jul 31 20:53:08 2025 +0200 @@ -140,6 +140,9 @@ int ret = nsapi_start_request(request, thrpool, ev, pool); if(ret) { + if(request->status != 0) { + // TODO: send error response + } pool_destroy(pool); } return ret; @@ -151,13 +154,15 @@ // create nsapi data structures NSAPISession *sn = nsapisession_create(pool); if(sn == NULL) { - /* TODO: error */ + log_ereport(LOG_FAILURE, "cannot start request: OOM"); + request->status = 503; return 1; } NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); if(rq == NULL) { - /* TODO: error */ + log_ereport(LOG_FAILURE, "cannot start request: OOM"); + request->status = 503; return 1; } ZERO(rq, sizeof(NSAPIRequest)); @@ -167,7 +172,8 @@ // fill session structure IOStream *io = NULL; if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) { - // TODO: error + log_ereport(LOG_FAILURE, "cannot start request: OOM"); + request->status = 503; return 1; } @@ -205,6 +211,7 @@ // init NSAPI request structure if(request_initialize(pool, request, rq) != 0) { log_ereport(LOG_FAILURE, "Cannot initialize request structure"); + request->status = 503; return 1; } @@ -251,6 +258,7 @@ "invalid protocol version: %.*s", (int)request->httpv.length, request->httpv.ptr); + request->status = 505; return 1; } @@ -259,8 +267,8 @@ */ // TODO: check for '#' #72 cxmutstr absPath = http_request_get_abspath(request); - if(!absPath.ptr) { - // TODO: error msg + if(absPath.length == 0) { + log_ereport(LOG_FAILURE, "request uri length is zero"); return 1; } else if(absPath.ptr[0] == '*') { // TODO: implement global OPTIONS #71 @@ -316,7 +324,7 @@ "invalid request path: {%.*s}", (int)orig_path.length, orig_path.ptr); - // TODO: 400 bad request + request->status = 400; return 1; } @@ -330,12 +338,13 @@ rq->rq.reqpb); } else { log_ereport( - LOG_WARN, + LOG_FAILURE, "uri unescape failed: {%.*s}", (int)absPath.length, absPath.ptr); - // TODO: 400 bad request - pblock_kvinsert(pb_key_uri, "/", 1, rq->rq.reqpb); + request->status = 400; + return 1; + //pblock_kvinsert(pb_key_uri, "/", 1, rq->rq.reqpb); } // pass http header to the NSAPI request structure @@ -450,7 +459,7 @@ // a separate buffer is required for reading chunked transfer enc sn->buffer = pool_malloc(pool, nb->maxsize); if(!sn->buffer) { - // TODO: error 500 + request->status = 503; return 1; } @@ -467,7 +476,7 @@ nb->cursize = 0; if(httpstream_enable_chunked_read(sn->sn.csd, sn->buffer, nb->maxsize, &sn->cursize, &sn->pos)) { - // TODO: error 500 + request->status = 500; // should not happen return 1; } } // else: TODO: unknown transfer encoding error
--- a/src/server/daemon/httprequest.h Mon Jul 21 22:22:46 2025 +0200 +++ b/src/server/daemon/httprequest.h Thu Jul 31 20:53:08 2025 +0200 @@ -47,13 +47,14 @@ struct _http_request { Connection *connection; - cxmutstr request_line; - cxmutstr method; - cxmutstr uri; - cxmutstr httpv; + cxmutstr request_line; + cxmutstr method; + cxmutstr uri; + cxmutstr httpv; HeaderArray *headers; netbuf *netbuf; time_t req_start; + short status; }; struct _header {