chore: cleanup http

main
Sean McBride 5 years ago
parent b0ae9ba266
commit f471011776

@ -3,9 +3,13 @@
#include <http_parser.h>
#include <types.h>
#include <uv.h>
#include <sys/uio.h>
// Conditionally load libuv
#ifdef USE_HTTP_UVIO
#include <uv.h>
#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 */

@ -1,96 +0,0 @@
#ifndef SRFT_HTTP_API_H
#define SRFT_HTTP_API_H
#include <http.h>
/***************************************************
* 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 */

@ -9,7 +9,7 @@
#include <pthread.h>
#include <signal.h>
#include <uv.h>
#include <http_api.h>
#include <http.h>
/**
* TODO: is there some weird edge case where a UNICODE character might be split between reads? Do we care?

@ -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(&current_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(&current_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(&current_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(&current_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(&current_sandbox__get()->http_response);
}
#endif /* SFRT_SANDBOX_H */

@ -1,7 +1,5 @@
#include <http.h>
#include <sandbox.h>
#include <uv.h>
#include <http_api.h>
/***************************************************
* 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;
}

@ -9,7 +9,6 @@
#include <sched.h>
#include <softint.h>
#include <uv.h>
#include <http_api.h>
#include <util.h>
#include "http_parser_settings.h"

@ -5,7 +5,6 @@
#include <pthread.h>
#include <signal.h>
#include <uv.h>
#include <http_api.h>
#include <libuv_callbacks.h>
#include <util.h>
@ -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.

Loading…
Cancel
Save