From e5721f6b4dd7e4084a14b6f2f38bfb3672796ac9 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 12 Jan 2025 10:25:56 -0800 Subject: http: server error handling improvements and tests We want to consume the request properly on an error, so that we can give a reasonable response. We were prematurely closing the connection for certain failure modes. We still have to fix overly long URIs and headers, but thats next! --- src/supplemental/http/http_msg.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src/supplemental/http/http_msg.c') diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 3c4ce491..b147c811 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -213,31 +213,39 @@ http_scan_line(void *vbuf, size_t n, size_t *lenp) static int http_req_parse_line(nng_http *conn, void *line) { - int rv; char *method; char *uri; char *version; method = line; if ((uri = strchr(method, ' ')) == NULL) { - return (NNG_EPROTO); + nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); + return (0); } *uri = '\0'; uri++; if ((version = strchr(uri, ' ')) == NULL) { - return (NNG_EPROTO); + nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); + return (0); } *version = '\0'; version++; - nni_http_set_method(conn, method); - if (((rv = nni_url_canonify_uri(uri)) != 0) || - ((rv = nni_http_set_uri(conn, uri, NULL)) != 0) || - ((rv = nni_http_set_version(conn, version)) != 0)) { - return (rv); + if (nni_url_canonify_uri(uri) != 0) { + nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); + return (0); } - return (0); + if (nni_http_set_version(conn, version)) { + nni_http_set_status( + conn, NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP, NULL); + return (0); + } + + nni_http_set_method(conn, method); + + // this one only can fail due to ENOMEM + return (nni_http_set_uri(conn, uri, NULL)); } static int @@ -303,12 +311,9 @@ nni_http_req_parse(nng_http *conn, void *buf, size_t n, size_t *lenp) if (req->data.parsed) { rv = http_parse_header(conn, line); - } else if ((rv = http_req_parse_line(conn, line)) == 0) { + } else { req->data.parsed = true; - } - - if (rv != 0) { - break; + rv = http_req_parse_line(conn, line); } } -- cgit v1.2.3-70-g09d2