chore: fix error handling

main
Sean McBride 4 years ago
parent 25d746c2e4
commit 0e892de5b0

@ -53,6 +53,7 @@ http_parser_settings_on_message_begin(http_parser *parser)
http_request->message_begin = true; http_request->message_begin = true;
http_request->last_was_value = true; /* should always start with a header */ http_request->last_was_value = true; /* should always start with a header */
sandbox->is_repeat_header = false;
return 0; 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; if (sandbox->http_request.message_end || sandbox->http_request.header_end) return 0;
// idef parser debug
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("sandbox: %lu\n", sandbox->request_arrival_timestamp); debuglog("sandbox: %lu\n", sandbox->request_arrival_timestamp);
#endif #endif
@ -104,8 +104,8 @@ http_parser_settings_on_header_field(http_parser *parser, const char *at, size_t
#endif #endif
if (!sandbox->is_repeat_header) { if (!sandbox->is_repeat_header) {
if (unlikely(http_request->header_count <= HTTP_MAX_HEADER_COUNT)) { return -1; } if (unlikely(http_request->header_count >= HTTP_MAX_HEADER_COUNT)) { return -1; }
if (unlikely(length < HTTP_MAX_HEADER_LENGTH)) { return -1; } if (unlikely(length > HTTP_MAX_HEADER_LENGTH)) { return -1; }
http_request->headers[http_request->header_count++].key = (char *)at; http_request->headers[http_request->header_count++].key = (char *)at;
http_request->last_was_value = false; http_request->last_was_value = false;
sandbox->is_repeat_header = 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. */ /* it is from the sandbox's request_response_data, should persist. */
if (!sandbox->is_repeat_header) { 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->headers[http_request->header_count - 1].value = (char *)at;
http_request->last_was_value = true; http_request->last_was_value = true;
} }

@ -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 #ifdef LOG_HTTP_PARSER
debuglog("http_parser_execute(%p, %p, %p, %lu)", &sandbox->http_parser, http_parser_settings_get(), 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], &sandbox->request_response_data[sandbox->request_response_data_length],
length_read); 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); // size_t length_parsed = sandbox_parse_http_request(sandbox, length_read);
if (length_parsed != 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); debuglog("Error parsing socket %d\n", sandbox->client_socket_descriptor);
goto err; 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); 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, * 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 * 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 */ /* Move the Sandbox's Data after the HTTP Response Data */
memmove(sandbox->request_response_data + response_cursor, memmove(sandbox->request_response_data + response_cursor,

Loading…
Cancel
Save