From 5e937fc20496b639a995b624b9de5ce25e6fa124 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 16 Mar 2020 20:07:04 -0400 Subject: [PATCH] chore: decompose http --- runtime/include/http/http_request.h | 30 +++++ .../include/{http.h => http/http_response.h} | 31 +---- runtime/include/http_parser_settings.h | 4 +- runtime/include/libuv_callbacks.h | 2 +- runtime/include/sandbox.h | 3 +- runtime/src/http.c | 124 ------------------ runtime/src/http/http_request.c | 19 +++ runtime/src/http/http_response.c | 110 ++++++++++++++++ runtime/src/libc/uvio.c | 2 +- runtime/src/util.c | 1 - 10 files changed, 168 insertions(+), 158 deletions(-) create mode 100644 runtime/include/http/http_request.h rename runtime/include/{http.h => http/http_response.h} (53%) create mode 100644 runtime/src/http/http_request.c create mode 100644 runtime/src/http/http_response.c diff --git a/runtime/include/http/http_request.h b/runtime/include/http/http_request.h new file mode 100644 index 0000000..4343850 --- /dev/null +++ b/runtime/include/http/http_request.h @@ -0,0 +1,30 @@ +#ifndef SFRT_HTTP_REQUEST_H +#define SFRT_HTTP_REQUEST_H + +#include + +/* all in-memory ptrs.. don't mess around with that! */ +struct http_header { + char *key; + char *value; +}; + +struct http_request { + struct http_header headers[HTTP_HEADERS_MAX]; + int header_count; + char * body; + int body_length; + int body_read_length; // How far we've read + // additional for http-parser + int last_was_value; // http-parser flag used to help the http-parser callbacks differentiate between header fields and values to know when to allocate a new header + int header_end; // boolean flag set when header processing is complete + int message_begin; // boolean flag set when body processing begins + int message_end; // boolean flag set when body processing is complete +}; + +/*************************************************** + * General HTTP Request Functions * + **************************************************/ +int http_request__get_body(struct http_request *http_request, char **body); + +#endif /* SFRT_HTTP_HEADER_H */ diff --git a/runtime/include/http.h b/runtime/include/http/http_response.h similarity index 53% rename from runtime/include/http.h rename to runtime/include/http/http_response.h index eda9171..72e8ddb 100644 --- a/runtime/include/http.h +++ b/runtime/include/http/http_response.h @@ -1,5 +1,5 @@ -#ifndef SFRT_HTTP_H -#define SFRT_HTTP_H +#ifndef SFRT_HTTP_RESPONSE_H +#define SFRT_HTTP_RESPONSE_H #include #include @@ -10,25 +10,6 @@ #include #endif -/* all in-memory ptrs.. don't mess around with that! */ -struct http_header { - char *key; - char *value; -}; - -struct http_request { - struct http_header headers[HTTP_HEADERS_MAX]; - int header_count; - char * body; - int body_length; - int body_read_length; // How far we've read - // additional for http-parser - int last_was_value; // http-parser flag used to help the http-parser callbacks differentiate between header fields and values to know when to allocate a new header - int header_end; // boolean flag set when header processing is complete - int message_begin; // boolean flag set when body processing begins - int message_end; // boolean flag set when body processing is complete -}; - struct http_response_header { char *header; int length; @@ -48,12 +29,6 @@ struct http_response { #endif }; -/*************************************************** - * General HTTP Request Functions * - **************************************************/ -int http_request__get_body(struct http_request *http_request, char **body); - - /*************************************************** * General HTTP Response Functions * **************************************************/ @@ -62,4 +37,4 @@ int http_response__set_body(struct http_response *http_response, char *body, int 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 */ +#endif /* SFRT_HTTP_RESPONSE_H */ diff --git a/runtime/include/http_parser_settings.h b/runtime/include/http_parser_settings.h index 22c0b60..150d0a4 100644 --- a/runtime/include/http_parser_settings.h +++ b/runtime/include/http_parser_settings.h @@ -1,8 +1,8 @@ #ifndef SRFT_HTTP_PARSER_SETTINGS_H #define SRFT_HTTP_PARSER_SETTINGS_H -#include -#include +#include +#include #include #include diff --git a/runtime/include/libuv_callbacks.h b/runtime/include/libuv_callbacks.h index 4507eec..6338b74 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 54e3b0e..f5521dc 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -8,7 +8,8 @@ #include #include #include "deque.h" -#include +#include +#include struct io_handle { int file_descriptor; diff --git a/runtime/src/http.c b/runtime/src/http.c index 56fa1f5..e69de29 100644 --- a/runtime/src/http.c +++ b/runtime/src/http.c @@ -1,124 +0,0 @@ -#include -#include - -/*************************************************** - * General HTTP Request Functions * - **************************************************/ - -/** - * Gets the HTTP Request body from a Sandbox - * @param sandbox the sandbox we want the body from - * @param body pointer that we'll assign to the http_request body - * @returns the length of the http_request's body - **/ -int -http_request__get_body(struct http_request *http_request, char **body) -{ - *body = http_request->body; - return http_request->body_length; -} - -/*************************************************** - * General HTTP Response Functions * - **************************************************/ - -/** - * 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 -http_response__encode_as_vector(struct http_response *http_response) -{ - int buffer_count = 0; - -#ifdef USE_HTTP_UVIO - - http_response->bufs[buffer_count] = uv_buf_init(http_response->status, http_response->status_length); - buffer_count++; - for (int i = 0; i < http_response->header_count; i++) { - http_response->bufs[buffer_count] = uv_buf_init(http_response->headers[i].header, http_response->headers[i].length); - buffer_count++; - } - - if (http_response->body) { - http_response->bufs[buffer_count] = uv_buf_init(http_response->body, http_response->body_length); - buffer_count++; - http_response->bufs[buffer_count] = uv_buf_init(http_response->status + http_response->status_length - 2, 2); // for crlf - buffer_count++; - } -#else - http_response->bufs[buffer_count].iov_base = http_response->status; - http_response->bufs[buffer_count].iov_len = http_response->status_length; - buffer_count++; - - for (int i = 0; i < http_response->header_count; i++) { - http_response->bufs[buffer_count].iov_base = http_response->headers[i].hdr; - http_response->bufs[buffer_count].iov_len = http_response->headers[i].len; - buffer_count++; - } - - if (http_response->body) { - http_response->bufs[buffer_count].iov_base = http_response->body; - http_response->bufs[buffer_count].iov_len = http_response->body_length; - buffer_count++; - - http_response->bufs[buffer_count].iov_base = http_response->status + http_response->status_length - 2; - http_response->bufs[buffer_count].iov_len = 2; - buffer_count++; - } -#endif - - return buffer_count; -} - -/** - * 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 -http_response__set_body(struct http_response *http_response, char *body, int 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/http/http_request.c b/runtime/src/http/http_request.c new file mode 100644 index 0000000..ecf5653 --- /dev/null +++ b/runtime/src/http/http_request.c @@ -0,0 +1,19 @@ +#include + +/*************************************************** + * General HTTP Request Functions * + **************************************************/ + +/** + * Gets the HTTP Request body from a Sandbox + * @param sandbox the sandbox we want the body from + * @param body pointer that we'll assign to the http_request body + * @returns the length of the http_request's body + **/ +int +http_request__get_body(struct http_request *http_request, char **body) +{ + *body = http_request->body; + return http_request->body_length; +} + diff --git a/runtime/src/http/http_response.c b/runtime/src/http/http_response.c new file mode 100644 index 0000000..f2b9c65 --- /dev/null +++ b/runtime/src/http/http_response.c @@ -0,0 +1,110 @@ +#include + +#ifdef USE_HTTP_UVIO +#include +#endif + +/*************************************************** + * General HTTP Response Functions * + **************************************************/ + +/** + * 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 +http_response__encode_as_vector(struct http_response *http_response) +{ + int buffer_count = 0; + +#ifdef USE_HTTP_UVIO + + http_response->bufs[buffer_count] = uv_buf_init(http_response->status, http_response->status_length); + buffer_count++; + for (int i = 0; i < http_response->header_count; i++) { + http_response->bufs[buffer_count] = uv_buf_init(http_response->headers[i].header, http_response->headers[i].length); + buffer_count++; + } + + if (http_response->body) { + http_response->bufs[buffer_count] = uv_buf_init(http_response->body, http_response->body_length); + buffer_count++; + http_response->bufs[buffer_count] = uv_buf_init(http_response->status + http_response->status_length - 2, 2); // for crlf + buffer_count++; + } +#else + http_response->bufs[buffer_count].iov_base = http_response->status; + http_response->bufs[buffer_count].iov_len = http_response->status_length; + buffer_count++; + + for (int i = 0; i < http_response->header_count; i++) { + http_response->bufs[buffer_count].iov_base = http_response->headers[i].header; + http_response->bufs[buffer_count].iov_len = http_response->headers[i].length; + buffer_count++; + } + + if (http_response->body) { + http_response->bufs[buffer_count].iov_base = http_response->body; + http_response->bufs[buffer_count].iov_len = http_response->body_length; + buffer_count++; + + http_response->bufs[buffer_count].iov_base = http_response->status + http_response->status_length - 2; + http_response->bufs[buffer_count].iov_len = 2; + buffer_count++; + } +#endif + + return buffer_count; +} + +/** + * 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 +http_response__set_body(struct http_response *http_response, char *body, int 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/libc/uvio.c b/runtime/src/libc/uvio.c index ffdec32..c57360b 100644 --- a/runtime/src/libc/uvio.c +++ b/runtime/src/libc/uvio.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include // What should we tell the child program its UID and GID are? #define UID 0xFF diff --git a/runtime/src/util.c b/runtime/src/util.c index 0dc1b0b..338baaf 100644 --- a/runtime/src/util.c +++ b/runtime/src/util.c @@ -7,7 +7,6 @@ #include #include #include -#include #define UTIL_MOD_LINE_MAX 1024