From 368478374f98b6e4a056c55d84760f8959fa6772 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 25 Sep 2020 16:33:58 -0400 Subject: [PATCH] chore: fix error handling --- runtime/src/http_parser_settings.c | 8 ++++---- runtime/src/sandbox.c | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/runtime/src/http_parser_settings.c b/runtime/src/http_parser_settings.c index ff20787..f3cff0c 100644 --- a/runtime/src/http_parser_settings.c +++ b/runtime/src/http_parser_settings.c @@ -53,6 +53,7 @@ http_parser_settings_on_message_begin(http_parser *parser) http_request->message_begin = true; http_request->last_was_value = true; /* should always start with a header */ + sandbox->is_repeat_header = false; return 0; } @@ -73,7 +74,6 @@ http_parser_settings_on_header_field(http_parser *parser, const char *at, size_t if (sandbox->http_request.message_end || sandbox->http_request.header_end) return 0; -// idef parser debug #ifdef LOG_HTTP_PARSER debuglog("sandbox: %lu\n", sandbox->request_arrival_timestamp); #endif @@ -104,8 +104,8 @@ http_parser_settings_on_header_field(http_parser *parser, const char *at, size_t #endif if (!sandbox->is_repeat_header) { - if (unlikely(http_request->header_count <= HTTP_MAX_HEADER_COUNT)) { return -1; } - if (unlikely(length < HTTP_MAX_HEADER_LENGTH)) { return -1; } + if (unlikely(http_request->header_count >= HTTP_MAX_HEADER_COUNT)) { return -1; } + if (unlikely(length > HTTP_MAX_HEADER_LENGTH)) { return -1; } http_request->headers[http_request->header_count++].key = (char *)at; http_request->last_was_value = false; sandbox->is_repeat_header = false; @@ -139,7 +139,7 @@ http_parser_settings_on_header_value(http_parser *parser, const char *at, size_t /* it is from the sandbox's request_response_data, should persist. */ if (!sandbox->is_repeat_header) { - if (unlikely(length < HTTP_MAX_HEADER_VALUE_LENGTH)) { return -1; } + if (unlikely(length >= HTTP_MAX_HEADER_VALUE_LENGTH)) return -1; http_request->headers[http_request->header_count - 1].value = (char *)at; http_request->last_was_value = true; } diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 1d1d97f..405fee2 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -85,7 +85,10 @@ sandbox_receive_and_parse_client_request(struct sandbox *sandbox) } - if (sandbox->http_request.message_end) break; + if (sandbox->http_request.message_end) { + sandbox->request_response_data_length += length_read; + break; + }; #ifdef LOG_HTTP_PARSER debuglog("http_parser_execute(%p, %p, %p, %lu)", &sandbox->http_parser, http_parser_settings_get(), @@ -104,13 +107,21 @@ sandbox_receive_and_parse_client_request(struct sandbox *sandbox) &sandbox->request_response_data[sandbox->request_response_data_length], length_read); + if (sandbox->http_request.message_end) { + sandbox->request_response_data_length += length_read; + break; + }; + // size_t length_parsed = sandbox_parse_http_request(sandbox, length_read); if (length_parsed != length_read) { + debuglog("Error: %s, Description: %s\n", http_errno_name(sandbox->http_parser.status_code), + http_errno_description(sandbox->http_parser.status_code)); + debuglog("Length Parsed %zu, Length Read %zu\n", length_parsed, length_read); debuglog("Error parsing socket %d\n", sandbox->client_socket_descriptor); goto err; } - sandbox->request_response_data_length += length_parsed; + sandbox->request_response_data_length += length_read; debuglog("After Read: %lu", sandbox->request_response_data_length); } @@ -201,7 +212,10 @@ sandbox_build_and_send_client_response(struct sandbox *sandbox) * actual data that the program appended to the HTTP Request. If proves to be a bad assumption, * we have to copy the STDOUT string to a temporary buffer before writing the header */ - assert(response_cursor < sandbox->request_length); + if (unlikely(response_cursor >= sandbox->request_length)) { + panic("Response Cursor: %zd is less that Request Length: %zd\n", response_cursor, + sandbox->request_length); + } /* Move the Sandbox's Data after the HTTP Response Data */ memmove(sandbox->request_response_data + response_cursor,