refactor: Abstract sandbox from http-parser

master
Sean McBride 3 years ago
parent ee24d1f5b0
commit c7758bbb00

@ -19,3 +19,14 @@ struct http_session {
struct vec_u8 request; struct vec_u8 request;
struct vec_u8 response; struct vec_u8 response;
}; };
static inline void
http_session_init_parser(struct http_session *session)
{
assert(session != NULL);
http_parser_init(&session->http_parser, HTTP_REQUEST);
/* Set the session as the data the http-parser has access to */
session->http_parser.data = &session->http_request;
}

@ -79,10 +79,7 @@ sandbox_open_http(struct sandbox *sandbox)
{ {
assert(sandbox != NULL); assert(sandbox != NULL);
http_parser_init(&sandbox->http->http_parser, HTTP_REQUEST); http_session_init_parser(sandbox->http);
/* Set the sandbox as the data the http-parser has access to */
sandbox->http->http_parser.data = sandbox;
/* Freshly allocated sandbox going runnable for first time, so register client socket with epoll */ /* Freshly allocated sandbox going runnable for first time, so register client socket with epoll */
struct epoll_event accept_evt; struct epoll_event accept_evt;

@ -80,7 +80,7 @@ sandbox_receive_request(struct sandbox *sandbox)
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("Sandbox: %lu http_parser_execute(%p, %p, %p, %zu\n)", sandbox->id, parser, settings, debuglog("Sandbox: %lu http_parser_execute(%p, %p, %p, %zu\n)", sandbox->id, parser, settings,
&sandbox->request.buffer[sandbox->request.length], bytes_received); &sandbox->http->request.buffer[sandbox->http->request.length], bytes_received);
#endif #endif
size_t bytes_parsed = http_parser_execute(parser, settings, size_t bytes_parsed = http_parser_execute(parser, settings,
(const char *)&request->buffer[request_length], (const char *)&request->buffer[request_length],

@ -2,7 +2,7 @@
#include "http.h" #include "http.h"
#include "http_request.h" #include "http_request.h"
#include "http_parser_settings.h" #include "http_parser_settings.h"
#include "sandbox_types.h" #include "likely.h"
http_parser_settings runtime_http_parser_settings; http_parser_settings runtime_http_parser_settings;
@ -21,14 +21,13 @@ http_parser_settings runtime_http_parser_settings;
int int
http_parser_settings_on_url(http_parser *parser, const char *at, size_t length) http_parser_settings_on_url(http_parser *parser, const char *at, size_t length)
{ {
struct sandbox *sandbox = (struct sandbox *)parser->data; struct http_request *http_request = (struct http_request *)parser->data;
struct http_request *http_request = &sandbox->http->http_request;
assert(!http_request->message_end); assert(!http_request->message_end);
assert(!http_request->header_end); assert(!http_request->header_end);
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("sandbox: %lu, length: %zu, Content \"%.*s\"\n", sandbox->id, length, (int)length, at); debuglog("parser: %p, length: %zu, Content \"%.*s\"\n", parser, length, (int)length, at);
#endif #endif
char *query_params = memchr(at, '?', length); char *query_params = memchr(at, '?', length);
@ -72,14 +71,13 @@ http_parser_settings_on_url(http_parser *parser, const char *at, size_t length)
int int
http_parser_settings_on_message_begin(http_parser *parser) http_parser_settings_on_message_begin(http_parser *parser)
{ {
struct sandbox *sandbox = (struct sandbox *)parser->data; struct http_request *http_request = (struct http_request *)parser->data;
struct http_request *http_request = &sandbox->http->http_request;
assert(!http_request->message_end); assert(!http_request->message_end);
assert(!http_request->header_end); assert(!http_request->header_end);
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("sandbox: %lu\n", sandbox->id); debuglog("parser: %p\n", parser);
#endif #endif
http_request->message_begin = true; http_request->message_begin = true;
@ -100,11 +98,10 @@ http_parser_settings_on_message_begin(http_parser *parser)
int int
http_parser_settings_on_header_field(http_parser *parser, const char *at, size_t length) http_parser_settings_on_header_field(http_parser *parser, const char *at, size_t length)
{ {
struct sandbox *sandbox = (struct sandbox *)parser->data; struct http_request *http_request = (struct http_request *)parser->data;
struct http_request *http_request = &sandbox->http->http_request;
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("sandbox: %lu, length: %zu, Content \"%.*s\"\n", sandbox->id, length, (int)length, at); debuglog("parser: %p, length: %zu, Content \"%.*s\"\n", parser, length, (int)length, at);
#endif #endif
assert(!http_request->message_end); assert(!http_request->message_end);
@ -143,12 +140,11 @@ http_parser_settings_on_header_field(http_parser *parser, const char *at, size_t
int int
http_parser_settings_on_header_value(http_parser *parser, const char *at, size_t length) http_parser_settings_on_header_value(http_parser *parser, const char *at, size_t length)
{ {
struct sandbox *sandbox = (struct sandbox *)parser->data; struct http_request *http_request = (struct http_request *)parser->data;
struct http_request *http_request = &sandbox->http->http_request;
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("sandbox: %lu, length: %zu, Content \"%.*s\"\n", sandbox->id, length, (int)length, at); debuglog("parser: %p, length: %zu, Content \"%.*s\"\n", parser, length, (int)length, at);
#endif #endif
assert(!http_request->message_end); assert(!http_request->message_end);
@ -175,14 +171,13 @@ http_parser_settings_on_header_value(http_parser *parser, const char *at, size_t
int int
http_parser_settings_on_header_end(http_parser *parser) http_parser_settings_on_header_end(http_parser *parser)
{ {
struct sandbox *sandbox = (struct sandbox *)parser->data; struct http_request *http_request = (struct http_request *)parser->data;
struct http_request *http_request = &sandbox->http->http_request;
assert(!http_request->message_end); assert(!http_request->message_end);
assert(!http_request->header_end); assert(!http_request->header_end);
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("sandbox: %lu\n", sandbox->id); debuglog("parser: %p\n", parser);
#endif #endif
http_request->header_end = true; http_request->header_end = true;
@ -194,7 +189,7 @@ const char *http_methods[http_methods_len] = { "OPTIONS", "GET", "HEAD", "POST"
/** /**
* http-parser callback called for HTTP Bodies * http-parser callback called for HTTP Bodies
* Assigns the parsed data to the http_request body of the sandbox struct * Assigns the parsed data to the http_request struct
* Presumably, this might only be part of the body * Presumably, this might only be part of the body
* @param parser * @param parser
* @param at - start address of body * @param at - start address of body
@ -204,17 +199,11 @@ const char *http_methods[http_methods_len] = { "OPTIONS", "GET", "HEAD", "POST"
int int
http_parser_settings_on_body(http_parser *parser, const char *at, size_t length) http_parser_settings_on_body(http_parser *parser, const char *at, size_t length)
{ {
struct sandbox *sandbox = (struct sandbox *)parser->data; struct http_request *http_request = (struct http_request *)parser->data;
struct http_request *http_request = &sandbox->http->http_request;
assert(http_request->header_end); assert(http_request->header_end);
assert(!http_request->message_end); assert(!http_request->message_end);
/* Assumption: We should never exceed the buffer we're reusing */
assert(http_request->body_length + length <= sandbox->module->max_request_size);
if (!http_request->body) { if (!http_request->body) {
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("Setting start of body!\n"); debuglog("Setting start of body!\n");
@ -231,8 +220,7 @@ http_parser_settings_on_body(http_parser *parser, const char *at, size_t length)
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
int capped_len = length > 1000 ? 1000 : length; int capped_len = length > 1000 ? 1000 : length;
debuglog("sandbox: %lu, length: %zu, Content(up to 1000 chars) \"%.*s\"\n", sandbox->id, length, debuglog("parser: %p, length: %zu, Content(up to 1000 chars) \"%.*s\"\n", parser, length, (int)capped_len, at);
(int)capped_len, at);
#endif #endif
return 0; return 0;
@ -246,14 +234,13 @@ http_parser_settings_on_body(http_parser *parser, const char *at, size_t length)
int int
http_parser_settings_on_msg_end(http_parser *parser) http_parser_settings_on_msg_end(http_parser *parser)
{ {
struct sandbox *sandbox = (struct sandbox *)parser->data; struct http_request *http_request = (struct http_request *)parser->data;
struct http_request *http_request = &sandbox->http->http_request;
assert(http_request->header_end); assert(http_request->header_end);
assert(!http_request->message_end); assert(!http_request->message_end);
#ifdef LOG_HTTP_PARSER #ifdef LOG_HTTP_PARSER
debuglog("sandbox: %lu\n", sandbox->id); debuglog("parser: %p\n", parser);
#endif #endif
http_request->message_end = true; http_request->message_end = true;

Loading…
Cancel
Save