From f4710117760c72a334d1fb3f320b00cab1d12f2f Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 16 Mar 2020 19:54:00 -0400 Subject: [PATCH] chore: cleanup http --- runtime/include/http.h | 20 ++++- runtime/include/http_api.h | 96 ------------------------ runtime/include/libuv_callbacks.h | 2 +- runtime/include/sandbox.h | 73 ++++++++++++++++++ runtime/src/http.c | 121 ++++++++++++------------------ runtime/src/runtime.c | 1 - runtime/src/sandbox.c | 19 ++++- 7 files changed, 157 insertions(+), 175 deletions(-) delete mode 100644 runtime/include/http_api.h diff --git a/runtime/include/http.h b/runtime/include/http.h index 22d016a..eda9171 100644 --- a/runtime/include/http.h +++ b/runtime/include/http.h @@ -3,9 +3,13 @@ #include #include -#include #include +// Conditionally load libuv +#ifdef USE_HTTP_UVIO +#include +#endif + /* all in-memory ptrs.. don't mess around with that! */ struct http_header { char *key; @@ -44,4 +48,18 @@ struct http_response { #endif }; +/*************************************************** + * General HTTP Request Functions * + **************************************************/ +int http_request__get_body(struct http_request *http_request, char **body); + + +/*************************************************** + * General HTTP Response Functions * + **************************************************/ +int http_response__encode_as_vector(struct http_response *http_response); +int http_response__set_body(struct http_response *http_response, char *body, int length); +int http_response__set_header(struct http_response *http_response, char *h, int length); +int http_response__set_status(struct http_response *http_response, char *status, int length); + #endif /* SFRT_HTTP_H */ diff --git a/runtime/include/http_api.h b/runtime/include/http_api.h deleted file mode 100644 index d387137..0000000 --- a/runtime/include/http_api.h +++ /dev/null @@ -1,96 +0,0 @@ -#ifndef SRFT_HTTP_API_H -#define SRFT_HTTP_API_H - -#include - -/*************************************************** - * General HTTP Request Functions * - **************************************************/ -int sandbox__get_http_request_body(struct sandbox *sandbox, char **body); - -/** - * Gets the HTTP Request body from the current sandbox - * @param body pointer that we'll assign to the http_request body - * @returns the length of the http_request's body - **/ -static inline int -current_sandbox__get_http_request_body(char **body) -{ - return sandbox__get_http_request_body(current_sandbox__get(), body); -} - -/*************************************************** - * General HTTP Response Functions * - **************************************************/ -int sandbox__set_http_response_header(struct sandbox *sandbox, char *h, int length); -int sandbox__set_http_response_body(struct sandbox *sandbox, char *body, int length); -int sandbox__set_http_response_status(struct sandbox *sandbox, char *status, int length); -int sandbox__vectorize_http_response(struct sandbox *sandbox); - -/** - * Set an HTTP Response Header on the current sandbox - * @param header string of the header that we want to set - * @param length the length of the header string - * @returns 0 (abends program in case of error) - **/ -static inline int -current_sandbox__set_http_response_header(char *header, int length) -{ - return sandbox__set_http_response_header(current_sandbox__get(), header, length); -} - -/** - * Set an HTTP Response Body on the current sandbox - * @param body string of the body that we want to set - * @param length the length of the body string - * @returns 0 (abends program in case of error) - **/ -static inline int -current_sandbox__set_http_response_body(char *body, int length) -{ - return sandbox__set_http_response_body(current_sandbox__get(), body, length); -} - -/** - * Set an HTTP Response Status on the current sandbox - * @param status string of the status we want to set - * @param length the length of the status - * @returns 0 (abends program in case of error) - **/ -static inline int -current_sandbox__set_http_response_status(char *status, int length) -{ - return sandbox__set_http_response_status(current_sandbox__get(), status, length); -} - -/** - * Encode the current sandbox's HTTP Response as an array of buffers - * @returns the number of buffers used to store the HTTP Response - **/ -static inline int -current_sandbox__vectorize_http_response(void) -{ - return sandbox__vectorize_http_response(current_sandbox__get()); -} - -/*********************************************************************** - * http-parser Setup and Excute * - **********************************************************************/ -// void global__http_parser_settings__initialize_and_register_callbacks(void); - -extern http_parser_settings global__http_parser_settings; - -int sandbox__parse_http_request(struct sandbox *sandbox, size_t l); - -/** - * Parse the current sandbox's request_response_data up to length - * @param length - * @returns 0 - **/ -static inline int -current_sandbox__parse_http_request(size_t length) -{ - return sandbox__parse_http_request(current_sandbox__get(), length); -} - -#endif /* SRFT_HTTP_API_H */ diff --git a/runtime/include/libuv_callbacks.h b/runtime/include/libuv_callbacks.h index 6041c93..4507eec 100644 --- a/runtime/include/libuv_callbacks.h +++ b/runtime/include/libuv_callbacks.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include /** * TODO: is there some weird edge case where a UNICODE character might be split between reads? Do we care? diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index 662d5ff..54e3b0e 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -285,6 +285,20 @@ current_sandbox__initialize_io_handle_and_set_file_descriptor(int file_descripto return sandbox__initialize_io_handle_and_set_file_descriptor(sandbox, file_descriptor); } +extern http_parser_settings global__http_parser_settings; +int sandbox__parse_http_request(struct sandbox *sandbox, size_t l); + +/** + * Parse the current sandbox's request_response_data up to length + * @param length + * @returns 0 + **/ +static inline int +current_sandbox__parse_http_request(size_t length) +{ + return sandbox__parse_http_request(current_sandbox__get(), length); +} + /** * Sets the file descriptor of the sandbox's ith io_handle * Returns error condition if the file_descriptor to set does not contain sandbox preopen magin @@ -334,4 +348,63 @@ current_sandbox__get_libuv_handle(int handle_index) return sandbox__get_libuv_handle(sandbox, handle_index); } +/** + * Gets the HTTP Request body from the current sandbox + * @param body pointer that we'll assign to the http_request body + * @returns the length of the http_request's body + **/ +static inline int +current_sandbox__get_http_request_body(char **body) +{ + return http_request__get_body(¤t_sandbox__get()->http_request, body); +} + + +/** + * Set an HTTP Response Header on the current sandbox + * @param header string of the header that we want to set + * @param length the length of the header string + * @returns 0 (abends program in case of error) + **/ +static inline int +current_sandbox__set_http_response_header(char *header, int length) +{ + return http_response__set_header(¤t_sandbox__get()->http_response, header, length); +} + +/** + * Set an HTTP Response Body on the current sandbox + * @param body string of the body that we want to set + * @param length the length of the body string + * @returns 0 (abends program in case of error) + **/ +static inline int +current_sandbox__set_http_response_body(char *body, int length) +{ + return http_response__set_body(¤t_sandbox__get()->http_response, body, length); +} + +/** + * Set an HTTP Response Status on the current sandbox + * @param status string of the status we want to set + * @param length the length of the status + * @returns 0 (abends program in case of error) + **/ +static inline int +current_sandbox__set_http_response_status(char *status, int length) +{ + return http_response__set_status(¤t_sandbox__get()->http_response, status, length); +} + +/** + * Encode the current sandbox's HTTP Response as an array of buffers + * @returns the number of buffers used to store the HTTP Response + **/ +static inline int +current_sandbox__vectorize_http_response(void) +{ + return http_response__encode_as_vector(¤t_sandbox__get()->http_response); +} + + #endif /* SFRT_SANDBOX_H */ diff --git a/runtime/src/http.c b/runtime/src/http.c index e91dfa8..56fa1f5 100644 --- a/runtime/src/http.c +++ b/runtime/src/http.c @@ -1,7 +1,5 @@ #include -#include #include -#include /*************************************************** * General HTTP Request Functions * @@ -14,10 +12,8 @@ * @returns the length of the http_request's body **/ int -sandbox__get_http_request_body(struct sandbox *sandbox, char **body) +http_request__get_body(struct http_request *http_request, char **body) { - struct http_request *http_request = &sandbox->http_request; - *body = http_request->body; return http_request->body_length; } @@ -26,74 +22,15 @@ sandbox__get_http_request_body(struct sandbox *sandbox, char **body) * General HTTP Response Functions * **************************************************/ -/** - * Append a header to the HTTP Response of a Sandbox - * @param sandbox the sandbox we want to set the request header on - * @param header string containing the header that we want to append - * @param length the length of the header string - * @returns 0 (abends program in case of error) - **/ -int -sandbox__set_http_response_header(struct sandbox *sandbox, char *header, int length) -{ - // by now, request_response_data should only be containing response! - struct http_response *http_response = &sandbox->http_response; - - assert(http_response->header_count < HTTP_HEADERS_MAX); - http_response->header_count++; - http_response->headers[http_response->header_count - 1].header = header; - http_response->headers[http_response->header_count - 1].length = length; - - return 0; -} - -/** - * Set an HTTP Response Body on a Sandbox - * @param sandbox the sandbox we want to set the request header on - * @param body string of the body that we want to set - * @param length the length of the header string - * @returns 0 (abends program in case of error) - **/ -int -sandbox__set_http_response_body(struct sandbox *sandbox, char *body, int length) -{ - struct http_response *http_response = &sandbox->http_response; - - assert(length <= sandbox->module->max_response_size); - http_response->body = body; - http_response->body_length = length; - - return 0; -} - -/** - * Set an HTTP Response Status on a Sandbox - * @param sandbox the sandbox we want to set the request status on - * @param status string of the status we want to set - * @param length the length of the status - * @returns 0 (abends program in case of error) - **/ -int -sandbox__set_http_response_status(struct sandbox *sandbox, char *status, int length) -{ - struct http_response *http_response = &sandbox->http_response; - - http_response->status = status; - http_response->status_length = length; - - return 0; -} - /** * Encodes a sandbox's HTTP Response as an array of buffers * @param sandbox the sandbox containing the HTTP response we want to encode as buffers * @returns the number of buffers used to store the HTTP Response **/ int -sandbox__vectorize_http_response(struct sandbox *sandbox) +http_response__encode_as_vector(struct http_response *http_response) { int buffer_count = 0; - struct http_response *http_response = &sandbox->http_response; #ifdef USE_HTTP_UVIO @@ -136,18 +73,52 @@ sandbox__vectorize_http_response(struct sandbox *sandbox) } /** - * Run the http-parser on the sandbox's request_response_data using the configured settings global - * @param sandbox the sandbox containing the req_resp data that we want to parse - * @param length The size of the request_response_data that we want to parse - * @returns 0 - * - * Globals: global__http_parser_settings + * Set an HTTP Response Body on a Sandbox + * @param sandbox the sandbox we want to set the request header on + * @param body string of the body that we want to set + * @param length the length of the header string + * @returns 0 (abends program in case of error) **/ int -sandbox__parse_http_request(struct sandbox *sandbox, size_t length) +http_response__set_body(struct http_response *http_response, char *body, int length) { - // Why is our start address sandbox->request_response_data + sandbox->request_response_data_length? - // it's like a cursor to keep track of what we've read so far - http_parser_execute(&sandbox->http_parser, &global__http_parser_settings, sandbox->request_response_data + sandbox->request_response_data_length, length); + // assert(length <= sandbox->module->max_response_size); + http_response->body = body; + http_response->body_length = length; + + return 0; +} + +/** + * Append a header to the HTTP Response of a Sandbox + * @param sandbox the sandbox we want to set the request header on + * @param header string containing the header that we want to append + * @param length the length of the header string + * @returns 0 (abends program in case of error) + **/ +int +http_response__set_header(struct http_response *http_response, char *header, int length) +{ + assert(http_response->header_count < HTTP_HEADERS_MAX); + http_response->header_count++; + http_response->headers[http_response->header_count - 1].header = header; + http_response->headers[http_response->header_count - 1].length = length; + + return 0; +} + +/** + * Set an HTTP Response Status on a Sandbox + * @param sandbox the sandbox we want to set the request status on + * @param status string of the status we want to set + * @param length the length of the status + * @returns 0 (abends program in case of error) + **/ +int +http_response__set_status(struct http_response *http_response, char *status, int length) +{ + http_response->status = status; + http_response->status_length = length; + return 0; } diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index 38ce033..3a4f875 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include "http_parser_settings.h" diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index ae0b825..6a52815 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -41,6 +40,24 @@ current_sandbox__setup_arguments(i32 argument_count) stub_init(string_off); } +/** + * Run the http-parser on the sandbox's request_response_data using the configured settings global + * @param sandbox the sandbox containing the req_resp data that we want to parse + * @param length The size of the request_response_data that we want to parse + * @returns 0 + * + * Globals: global__http_parser_settings + **/ +int +sandbox__parse_http_request(struct sandbox *sandbox, size_t length) +{ + // Why is our start address sandbox->request_response_data + sandbox->request_response_data_length? + // it's like a cursor to keep track of what we've read so far + http_parser_execute(&sandbox->http_parser, &global__http_parser_settings, sandbox->request_response_data + sandbox->request_response_data_length, length); + return 0; +} + + /** * Receive and Parse the Request for the current sandbox * @return 1 on success, 0 if no context, < 0 on failure.