From 7a388dad85152a203033c14fee3c64607301865a Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Sat, 8 Jul 2023 15:06:33 -0300 Subject: libactivity: (tmp name) http request parse the parse works via callbacks, registered to a method and path. further work will be done to simplify extraction of path, and to make the request struct more private. missing the parsing and handling of reponses. Signed-off-by: Anna (navi) Figueiredo Gomes --- include/buffer.h | 19 +++++++++++++++++++ include/http.h | 41 +++++++++++++++++++++++++++++++++++++++++ include/linked_list.h | 9 +++++++++ include/server.h | 6 ++++++ include/util.h | 12 ++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 include/buffer.h create mode 100644 include/http.h create mode 100644 include/linked_list.h create mode 100644 include/server.h create mode 100644 include/util.h (limited to 'include') diff --git a/include/buffer.h b/include/buffer.h new file mode 100644 index 0000000..dd4530c --- /dev/null +++ b/include/buffer.h @@ -0,0 +1,19 @@ +#ifndef _BUFFER_H_ +#define _BUFFER_H_ + +#include + +struct buffer { + char *data; + size_t len; + size_t size; +}; + +struct buffer *buf_new(const char *init); +void buf_del(struct buffer **buf); +struct buffer *buf_dup(struct buffer *src); +char *buf_strdup(struct buffer *buf); +void buf_append(struct buffer *buf, const char *str); +void buf_printf(struct buffer *buf, char *fmt, ...); + +#endif diff --git a/include/http.h b/include/http.h new file mode 100644 index 0000000..c550b15 --- /dev/null +++ b/include/http.h @@ -0,0 +1,41 @@ +#ifndef _HTTP_H_ +#define _HTTP_H_ +#include + +#include "buffer.h" + +//static const char *supported_http_versions[] = { "1.1" }; + +struct header_item; +#define MAX_HEADERS 512 +struct header_list { + struct header_item *items[MAX_HEADERS]; +}; + +struct request { + char http_version[4]; + char *method; + char *path; + char *query; + char *body; + char *data; + struct header_list header; +}; + +struct reply { + int status; + char *body; +}; + +typedef struct reply *(request_callback)(struct request *, void *); + +struct http; + +struct http *http_init(void); +bool http_register_handler(struct http *http, char *method, char *path, + void *data, request_callback *callback); +struct reply *http_handle_request(struct http *http, struct buffer *req); +struct buffer *http_build_reply(struct reply *reply); +const char *http_get_header(struct request *req, const char *key); + +#endif diff --git a/include/linked_list.h b/include/linked_list.h new file mode 100644 index 0000000..e591e94 --- /dev/null +++ b/include/linked_list.h @@ -0,0 +1,9 @@ +#ifndef _LINKED_LIST_H_ +#define _LINKED_LIST_H_ + +struct linked_list { + void *data; + +}; + +#endif diff --git a/include/server.h b/include/server.h new file mode 100644 index 0000000..e59c38d --- /dev/null +++ b/include/server.h @@ -0,0 +1,6 @@ +#ifndef _SERVER_H_ +#define _SERVER_H_ + + + +#endif diff --git a/include/util.h b/include/util.h new file mode 100644 index 0000000..c4856db --- /dev/null +++ b/include/util.h @@ -0,0 +1,12 @@ +#ifndef _UTIL_H_ +#define _UTIL_H_ + +#include +#include + +int vasprintf(char **dest, const char *fmt, va_list ap); +int asprintf(char **dest, const char *fmt, ...); + +size_t hash(const char *str, int max_val); + +#endif -- cgit v1.2.3