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,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;
|
||||||
|
}
|
Loading…
Reference in new issue