chore: decompose http

main
Sean McBride 5 years ago
parent f471011776
commit 5e937fc204

@ -0,0 +1,30 @@
#ifndef SFRT_HTTP_REQUEST_H
#define SFRT_HTTP_REQUEST_H
#include <types.h>
/* 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 */

@ -1,5 +1,5 @@
#ifndef SFRT_HTTP_H
#define SFRT_HTTP_H
#ifndef SFRT_HTTP_RESPONSE_H
#define SFRT_HTTP_RESPONSE_H
#include <http_parser.h>
#include <types.h>
@ -10,25 +10,6 @@
#include <uv.h>
#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 */

@ -1,8 +1,8 @@
#ifndef SRFT_HTTP_PARSER_SETTINGS_H
#define SRFT_HTTP_PARSER_SETTINGS_H
#include <http.h>
#include <http.h>
#include <http/http_request.h>
#include <http/http_response.h>
#include <sandbox.h>
#include <uv.h>

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

@ -8,7 +8,8 @@
#include <ucontext.h>
#include <uv.h>
#include "deque.h"
#include <http.h>
#include <http/http_request.h>
#include <http/http_response.h>
struct io_handle {
int file_descriptor;

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

@ -0,0 +1,19 @@
#include <http/http_request.h>
/***************************************************
* 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;
}

@ -0,0 +1,110 @@
#include <http/http_response.h>
#ifdef USE_HTTP_UVIO
#include <uv.h>
#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;
}

@ -1,7 +1,7 @@
#include <runtime.h>
#include <sandbox.h>
#include <uv.h>
#include <http.h>
#include <http/http_request.h>
// What should we tell the child program its UID and GID are?
#define UID 0xFF

@ -7,7 +7,6 @@
#include <module.h>
#include <util.h>
#include <jsmn.h>
#include <http.h>
#define UTIL_MOD_LINE_MAX 1024

Loading…
Cancel
Save