From 18ce121d4243358bc55a0474a529efe2580a0610 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Mon, 9 Mar 2020 18:06:10 +0300 Subject: [magi] --- include/magi/cgi.h | 18 +++--------------- include/magi/cookie.h | 13 +++++++------ include/magi/file.h | 11 +++++++++++ include/magi/param.h | 6 ++++++ include/magi/request.h | 38 +++++++++++++++++--------------------- include/magi/response.h | 29 +++++++++++++++++++++++++++-- 6 files changed, 71 insertions(+), 44 deletions(-) (limited to 'include') diff --git a/include/magi/cgi.h b/include/magi/cgi.h index 9b29a98..8a13240 100644 --- a/include/magi/cgi.h +++ b/include/magi/cgi.h @@ -5,32 +5,20 @@ * * blah-blah... */ -#include "error.h" #include "request.h" -#include "response.h" /** Analyses non-post part of request from environment. * @return 1 if ok, 0 if error. */ -int magi_request_cgi_head(magi_request *request); +int magi_cgi_head(magi_request *request); /** Complete request with post body from standard input. * @return 1 if ok, 0 if error. */ -int magi_request_cgi_body(magi_request *request); +int magi_cgi_body(magi_request *request); /** Shortcut for analysing both head and body of request. * @return 1 if ok, 0 if error. */ -int magi_request_cgi(magi_request *request); - - -/** Sends response to standard output and destroys it. - * @return 1 if ok, 0 if error. */ -int magi_response_cgi(magi_response *response); - - -/** Sends a standard response of Bad Request error. - * @return 1 if ok, 0 if error. */ -int magi_error_cgi(magi_error error); +int magi_cgi(magi_request *request); #endif diff --git a/include/magi/cookie.h b/include/magi/cookie.h index 8a92a82..8e1c6fc 100644 --- a/include/magi/cookie.h +++ b/include/magi/cookie.h @@ -25,19 +25,20 @@ typedef struct magi_cookies { magi_cookie item; /**< Cookie on top. */ } magi_cookies; + /** Add @p newitem to @p cookies. * @param[in,out] cookies to add into. * @param[in] newitem to add onto top of @p cookies. */ void magi_cookies_add(magi_cookies **cookies, magi_cookie *newitem); -/** Get data of cookie from @p cookies with @p name. +/** Get cookie from @p cookies with @p name. * @note Cookies in @p cookies are in reverse request order, and first cookie * from request is the most accurate in terms of domain and path. - * @param[in] params to search in. - * @param[in] name of needed parameter. - * @return data of the last from top of @p cookies cookie with @p name, - * null only if no such parameter. */ -char *magi_cookies_get(magi_cookies *cookies, const char *name); + * @param[in] cookies to search in. + * @param[in] name of needed cookie. + * @return the last from top of @p cookies cookie with @p name, + * null only if no such cookie. */ +magi_cookie *magi_cookies_get(magi_cookies *cookies, const char *name); /** Free memory used by @p cookies. * @param[in,out] cookies to be destructed. */ diff --git a/include/magi/file.h b/include/magi/file.h index 07baadc..1b1fb35 100644 --- a/include/magi/file.h +++ b/include/magi/file.h @@ -38,4 +38,15 @@ magi_file *magi_files_get(magi_files *files, const char *name); void magi_files_free(magi_files *files); +typedef void (*magi_file_callback_act)(void *userdata, + magi_file *file_to_add_into, + char *addon, + int addon_len); +typedef struct magi_file_callback { + magi_file_callback_act act; + void *userdata; + int addon_max; +} magi_file_callback; + + #endif diff --git a/include/magi/param.h b/include/magi/param.h index 0ad1919..b9dbde1 100644 --- a/include/magi/param.h +++ b/include/magi/param.h @@ -24,6 +24,12 @@ typedef struct magi_params { * @param[in] newitem to add onto top of @p params. */ void magi_params_add(magi_params **params, magi_param *newitem); +/** Set @p newitem in @p params. + * @param[in,out] params to add into. + * @param[in] newitem to replace item in @p params with same name + or to add, if no param with same name is in @p params. */ +void magi_params_set(magi_params **params, magi_param *newitem); + /** Get data of parameter from @p params with @p name. * @param[in] params to search in. * @param[in] name of needed parameter. diff --git a/include/magi/request.h b/include/magi/request.h index 4e04989..03e0c43 100644 --- a/include/magi/request.h +++ b/include/magi/request.h @@ -10,29 +10,26 @@ #include "file.h" #include "param.h" -typedef void (*magi_file_callback_act)(void *, magi_file *, char *, int); -typedef struct magi_file_callback { - magi_file_callback_act act; - void *userdata; - int addon_max; -} magi_file_callback; typedef struct magi_request_limits { int cookies; - int params_http; + int params_meta; int params_head; int params_body; } magi_request_limits; + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /** - * Request and response handler. + * Request handler. * - * Can be created via 'magi_request_{gateway interface name}', but will have - * nullified 'post'-related fields ('params' & 'files'). Reason is unlimited - * 'post' body size, with possible dependence of wanted limits from data of + * Can be created via 'magi_{gateway interface name}_head', but will have + * nullified 'POST'-related fields ('params' & 'files'). Reason is unlimited + * 'POST' body size, with possible dependence of wanted limits from data of * headers (e.g. session id from cookies, enabling some users to load more). - * To proceed 'post' use 'magi_request_resume_{gateway interface name}', - * specifying settings if necessary. */ + * To proceed 'post' use 'magi_{gateway interface name}_body', specifying + * settings if necessary. + * + * Or just use shortcut 'magi_{gateway interface_name}' to do both parts. */ typedef struct magi_request { magi_error error; @@ -42,12 +39,12 @@ typedef struct magi_request { magi_params *body; magi_files *files; - magi_method method; - int is_secure; - char *domain; - int port; - char *script; - char *path; + char *method; + int is_secure; + char *address; + int port; + char *script; + char *path; magi_file_callback callback; magi_request_limits limits; @@ -60,8 +57,7 @@ void magi_request_init(magi_request *r); void magi_request_free(magi_request *r); -char *magi_request_meta(magi_request *r, magi_meta code); -char *magi_request_meta_custom(magi_request *r, const char *name); +char *magi_request_meta(magi_request *r, const char *name); char *magi_request_param(magi_request *r, const char *name); char *magi_request_urlparam(magi_request *r, const char *name); diff --git a/include/magi/response.h b/include/magi/response.h index a623da1..4c6b788 100644 --- a/include/magi/response.h +++ b/include/magi/response.h @@ -12,21 +12,46 @@ * all headers should be sent before anything from the body.) */ #include "request.h" +#include -void magi_response_status(magi_request *r, magi_status code); -void magi_response_status_custom(magi_request *r, int code, const char *desc); +typedef void (*magi_response_method_head)(void *ud, magi_param *header); +typedef void (*magi_response_method_body)(void *ud, const char *data, int len); +typedef void (*magi_response_method_file)(void *ud, FILE *file); + +typedef struct magi_response_methods { + magi_response_method_head head; + magi_response_method_body body; + magi_response_method_file file; +} magi_response_methods; + +typedef struct magi_response_implementation { + magi_response_methods *methods; + void *userdata; + magi_params *head[3]; + int head_done; +} magi_response_implementation; + + +void magi_response_status(magi_request *r, int code, const char *description); void magi_response_cookie(magi_request *r, const char *name, const char *data); void magi_response_cookie_complex(magi_request *r, magi_cookie *c); void magi_response_cookie_discard(magi_request *r, const char *name); +void magi_response_header(magi_request *r, const char *name, const char *data); + void magi_response_content_length(magi_request *r, int length); void magi_response_content_type(magi_request *r, const char *type); +void magi_response_head(magi_request *r); + void magi_response(magi_request *r, const char *addon); void magi_response_format(magi_request *r, const char *format, ...); void magi_response_file(magi_request *r, FILE *file); +void magi_response_error(magi_request *r); + + #endif -- cgit v1.2.3