diff options
| author | Garrett D'Amore <garrett@damore.org> | 2025-01-12 10:25:56 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2025-01-12 10:52:03 -0800 |
| commit | e5721f6b4dd7e4084a14b6f2f38bfb3672796ac9 (patch) | |
| tree | 71c9f6c8e4b0a260efd6209c55cc20b1f88275db /src/supplemental/http/http_msg.c | |
| parent | 169166a0ef4fad56860c40ba2eda23f27b8a4cb1 (diff) | |
| download | nng-e5721f6b4dd7e4084a14b6f2f38bfb3672796ac9.tar.gz nng-e5721f6b4dd7e4084a14b6f2f38bfb3672796ac9.tar.bz2 nng-e5721f6b4dd7e4084a14b6f2f38bfb3672796ac9.zip | |
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!
Diffstat (limited to 'src/supplemental/http/http_msg.c')
| -rw-r--r-- | src/supplemental/http/http_msg.c | 33 |
1 files changed, 19 insertions, 14 deletions
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); } } |
