From 4d77e4bb69a47daf23ad4a902b300bf1aa7d5597 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Mon, 13 Jul 2020 10:21:47 +0300 Subject: typedef struct removed to support C++98. --- examples/append.c | 6 +-- examples/cookie.c | 12 +++--- examples/echo.c | 22 +++++----- examples/upload.c | 14 +++--- include/magi/cookie.h | 19 ++++---- include/magi/error.h | 8 ++-- include/magi/file.h | 40 +++++++++-------- include/magi/loadfiles.h | 25 +++++------ include/magi/param.h | 21 +++++---- include/magi/parse.h | 9 ++-- include/magi/request.h | 43 +++++++++--------- include/magi/response.h | 38 +++++++++------- man/magi_param.3 | 8 ++++ src/cookie.c | 11 ++--- src/cookies.c | 82 +++++++++++++++++------------------ src/cookies.h | 2 +- src/error.c | 4 +- src/file.c | 9 ++-- src/loadfiles.c | 27 ++++++------ src/multipart.c | 110 +++++++++++++++++++++++------------------------ src/multipart.h | 8 ++-- src/param.c | 10 ++--- src/parse.c | 24 +++++------ src/request.c | 26 +++++------ src/response.c | 51 ++++++++++++---------- src/urlencoded.c | 41 +++++++++--------- src/urlencoded.h | 3 +- 27 files changed, 356 insertions(+), 317 deletions(-) diff --git a/examples/append.c b/examples/append.c index f1b8f96..65662b5 100644 --- a/examples/append.c +++ b/examples/append.c @@ -5,7 +5,7 @@ #include -void append(magi_request *r) +void append(struct magi_request *r) { char *data = magi_request_param(r, "addon"); if (data) { /* If field "addon" was in request: */ @@ -16,7 +16,7 @@ void append(magi_request *r) } } -void response(magi_request *r) +void response(struct magi_request *r) { magi_response_default(); /* Pass default headers and send body: */ printf(" -void list_cookies(magi_request *r) +void list_cookies(struct magi_request *r) { - magi_cookies *current; + struct magi_cookies *current; printf("Cookies:"); /* Iterate through all cookies in request to show them in body: */ for (current = r->cookies; current; current = current->next) { - magi_cookie *c = ¤t->item; + struct magi_cookie *c = ¤t->item; printf("
[%s]=[%s]", c->name, c->data); } } -void response(magi_request *r) +void response(struct magi_request *r) { - magi_response head; + struct magi_response head; magi_response_init(&head); /* Setting defaults. */ /* Set cookie "cookie" with value "monster" on clientside: */ magi_response_cookie(&head, "cookie", "monster"); @@ -36,7 +36,7 @@ void response(magi_request *r) int main() { - magi_request request; + struct magi_request request; magi_request_init(&request); /* Setting defaults. */ if (magi_parse(&request)) { /* If parsing was done successful */ response(&request); /* we need to response the request. */ diff --git a/examples/echo.c b/examples/echo.c index 3598986..c93a897 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -4,13 +4,13 @@ #include -void list_cookies(magi_request *r) +void list_cookies(struct magi_request *r) { - magi_cookies *current = r->cookies; + struct magi_cookies *current = r->cookies; printf("

"); /* Iterate through all cookies in request to show them in body: */ for (current = r->cookies; current; current = current->next) { - magi_cookie *c = ¤t->item; + struct magi_cookie *c = ¤t->item; printf("Cookie with name [%s", c->name); if (c->data) { printf("] is [%s", c->data); @@ -26,30 +26,30 @@ void list_cookies(magi_request *r) printf("

"); } -void list_params(magi_params *current) +void list_params(struct magi_params *current) { printf("

"); /* Iterate through specified params to show them in body: */ for (; current; current = current->next) { - magi_param *p = ¤t->item; + struct magi_param *p = ¤t->item; printf("[%s] is [%s]
", p->name, p->data); } printf("

"); } -void list_files(magi_request *r) +void list_files(struct magi_request *r) { - magi_files *current; + struct magi_files *current; printf("

"); /* Iterate through all field files in request to show them in body: */ for (current = r->files; current; current = current->next) { - magi_file *f = ¤t->item; + struct magi_file *f = ¤t->item; printf("[%s] was [%s] on clientside
", f->field, f->filename); } printf("

"); } -void show_meta(magi_request *r) +void show_meta(struct magi_request *r) { printf("

I was called "); if (r->is_secure) { @@ -68,7 +68,7 @@ void show_meta(magi_request *r) printf("]

"); } -void response(magi_request *r) +void response(struct magi_request *r) { magi_response_default(); /* Pass default headers and send body: */ printf(" -void upload(magi_request *r) +void upload(struct magi_request *r) { - char *name = magi_request_param(r, "name"); - const magi_file *data = magi_request_file(r, "data"); + char *name = magi_request_param(r, "name"); + const struct magi_file *data = magi_request_file(r, "data"); if (name && data) { /* If file to load and its name are in the request: */ rename("data", name); /* Rename loaded file to designated name. */ printf("

Uploaded!

"); /* And display success message. */ } } -void response(magi_request *r) +void response(struct magi_request *r) { magi_response_default(); /* Pass default headers and send body: */ printf(""); } -void get(magi_request *r) +void get(struct magi_request *r) { - magi_loadfiles rules = { 0, 0 }; + struct magi_loadfiles rules = { 0, 0 }; /* Setup callback to load file from "data" field into file "data": */ magi_loadfiles_add(&rules, "data", "data", 0); magi_loadfiles_set(r, &rules); /* Setup request to use the callback. */ @@ -46,7 +46,7 @@ void get(magi_request *r) int main() { - magi_request request; + struct magi_request request; magi_request_init(&request); get(&request); /* Parse request. */ if (request.error) { /* If error occurred display error message: */ diff --git a/include/magi/cookie.h b/include/magi/cookie.h index 7573747..68cbe3f 100644 --- a/include/magi/cookie.h +++ b/include/magi/cookie.h @@ -5,32 +5,33 @@ */ -typedef struct magi_cookie { +struct magi_cookie { char *name; /* Cookie name. */ char *data; /* Cookie value. */ char *path; /* Path on which cookie is set. Without '/' at the end. */ char *domain; /* Domain in wich cookie is set. * With dot at the begining. */ char *max_age; /* In seconds until discard (response only). */ -} magi_cookie; +}; /* HTTP cookies collection, implemented as a linked list. */ -typedef struct magi_cookies { +struct magi_cookies { struct magi_cookies *next; /* Pointer to next cookies. */ - magi_cookie item; /* Cookie on top. */ -} magi_cookies; + struct magi_cookie item; /* Cookie on top. */ +}; /* Free memory used by cookies. */ -void magi_cookies_free(magi_cookies *cookies); +void magi_cookies_free(struct magi_cookies *cookies); /* Add newitem onto top of cookies. */ -void magi_cookies_add(magi_cookies **cookies, magi_cookie *newitem); +void magi_cookies_add(struct magi_cookies **cookies, + struct magi_cookie *newitem); /* Get first cookie with given name, null if no such cookie. * First cookie is the most accurate in terms of domain and path. */ -const magi_cookie *magi_cookies_get(const magi_cookies *cookies, - const char *name); +const struct magi_cookie *magi_cookies_get(const struct magi_cookies *cookies, + const char *name); #endif diff --git a/include/magi/error.h b/include/magi/error.h index 6f64228..c11ed51 100644 --- a/include/magi/error.h +++ b/include/magi/error.h @@ -6,7 +6,7 @@ /* Magi error codes. */ -typedef enum magi_error { +enum magi_error { magi_error_none = 0, /* No error, all is ok. */ magi_error_nobound, /* No boundary provided for multipart/form-data. */ magi_error_unknown, /* Unknown Content Type. */ @@ -16,14 +16,14 @@ typedef enum magi_error { magi_error_urlenc, /* Wrong url encoding. */ magi_error_multipart, /* Malformed multipart/form-data. */ magi_error_limit /* One of specified limits reached. */ -} magi_error; +}; /* Get description message of given error. */ -const char *magi_error_message(magi_error error); +const char *magi_error_message(enum magi_error error); /* Response default error page for given error. */ -void magi_error_response(magi_error error); +void magi_error_response(enum magi_error error); #endif diff --git a/include/magi/file.h b/include/magi/file.h index d976194..615d8b7 100644 --- a/include/magi/file.h +++ b/include/magi/file.h @@ -5,46 +5,48 @@ #include "param.h" -typedef struct magi_file { - char *field; /* Name of form field. */ - char *filename; /* File name on user's computer. */ - magi_params *params; /* Multipart params (e.g. type). */ -} magi_file; +struct magi_file { + char *field; /* Name of form field. */ + char *filename; /* File name on user's computer. */ + struct magi_params *params; /* Multipart params (e.g. type). */ +}; /* Form files collection, implemented as a linked list. */ -typedef struct magi_files { +struct magi_files { struct magi_files *next; /* Pointer to next files. */ - magi_file item; /* File on top. */ -} magi_files; + struct magi_file item; /* File on top. */ +}; /* Free memory used by files. */ -void magi_files_free(magi_files *files); +void magi_files_free(struct magi_files *files); /* Add newitem onto top of files. */ -void magi_files_add(magi_files **files, magi_file *newitem); +void magi_files_add(struct magi_files **files, + struct magi_file *newitem); /* Get first from top of files file with name, null if no such file. */ -const magi_file *magi_files_get(const magi_files *files, const char *name); +const struct magi_file *magi_files_get(const struct magi_files *files, + const char *name); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Callback to load files while analysing request. * addon_len is not null if something to add is in addon - * and null if file_to_add_into is ended. + * and null if file is ended. * newfile flag is setted up in the beginning of new file. * Files are passed sequentialy, one by one. */ -typedef void (*magi_file_callback_act)(void *userdata, - int newfile, - magi_file *file_to_add_into, - char *addon, - int addon_len); +typedef void (*magi_file_callback_act)(void *userdata, + int newfile, + struct magi_file *file, + char *addon, + int addon_len); -typedef struct magi_file_callback { +struct magi_file_callback { magi_file_callback_act act; void *userdata; int addon_max; -} magi_file_callback; +}; #endif diff --git a/include/magi/loadfiles.h b/include/magi/loadfiles.h index d6cdaeb..9d7978f 100644 --- a/include/magi/loadfiles.h +++ b/include/magi/loadfiles.h @@ -12,35 +12,36 @@ /* Rule of loading single file. * There is no need to form or edit it directly. */ -typedef struct magi_loadfile { +struct magi_loadfile { const char *name; /* Form field to load file from. */ const char *path; /* Path to load file in. */ int max; /* Limit in bytes. Null means unlimited. */ -} magi_loadfile; +}; /* Table of rules for loading files. * Set count and files as null to initialize. */ -typedef struct magi_loadfiles { - int count; /* Size of files array.*/ - magi_loadfile *files; /* Dynamic array of rules to load files. */ -} magi_loadfiles; +struct magi_loadfiles { + int count; /* Size of files array.*/ + struct magi_loadfile *files; /* Dynamic array of rules to load files. */ +}; /* Free memory used by table. Request using table will become invalid. */ -void magi_loadfiles_free(magi_loadfiles *table); +void magi_loadfiles_free(struct magi_loadfiles *table); /* Add entity into table. * Specify form field to load file from with name, * wnated loaction to load file with path, * and file size limit in bytes with max (pass null to unlimit). */ -void magi_loadfiles_add(magi_loadfiles *table, - const char *name, - const char *path, - int max); +void magi_loadfiles_add(struct magi_loadfiles *table, + const char *name, + const char *path, + int max); /* Setup request to use loadfiles callback with table. */ -void magi_loadfiles_set(magi_request *request, magi_loadfiles *table); +void magi_loadfiles_set(struct magi_request *request, + struct magi_loadfiles *table); #endif diff --git a/include/magi/param.h b/include/magi/param.h index c0879a9..44ea384 100644 --- a/include/magi/param.h +++ b/include/magi/param.h @@ -4,32 +4,35 @@ */ -typedef struct magi_param { +struct magi_param { char *name; /* Cannot be null. */ char *data; /* Cannot be null. */ -} magi_param; +}; /* Parameters collection, implemented as a linked list. */ -typedef struct magi_params { +struct magi_params { struct magi_params *next; /* Pointer to next parameters. */ - magi_param item; /* Parameter on top. */ -} magi_params; + struct magi_param item; /* Parameter on top. */ +}; /* Free memory used by params. */ -void magi_params_free(magi_params *params); +void magi_params_free(struct magi_params *params); /* Add newitem onto top of params. */ -void magi_params_add(magi_params **params, magi_param *newitem); +void magi_params_add(struct magi_params **params, + struct magi_param *newitem); /* Set newitem in params. * If param with name of newitem is in params it will be replaced with newitem, * otherwise newitem will be added into the end of params. */ -void magi_params_set(magi_params **params, magi_param *newitem); +void magi_params_set(struct magi_params **params, + struct magi_param *newitem); /* Get data of the first from top of params parameter with name, * null if no such parameter. */ -char *magi_params_get(const magi_params *params, const char *name); +char *magi_params_get(const struct magi_params *params, + const char *name); #endif diff --git a/include/magi/parse.h b/include/magi/parse.h index a71018a..502af6e 100644 --- a/include/magi/parse.h +++ b/include/magi/parse.h @@ -5,14 +5,15 @@ #include "request.h" -/* Analyses non-post part of request from environment. True if ok. */ -int magi_parse_head(magi_request *request); +/* Analyses non-post part of request (everything except body and files) + * from environment. True if ok. */ +int magi_parse_head(struct magi_request *request); /* Complete request with post body from standard input. True if ok. */ -int magi_parse_body(magi_request *request); +int magi_parse_body(struct magi_request *request); /* Shortcut for analysing both head and body of request. True if ok. */ -int magi_parse(magi_request *request); +int magi_parse(struct magi_request *request); #endif diff --git a/include/magi/request.h b/include/magi/request.h index 0ec17e8..fe0f745 100644 --- a/include/magi/request.h +++ b/include/magi/request.h @@ -17,22 +17,22 @@ /* Limits on possibly enormous structures. Null means unlimited. */ -typedef struct magi_request_limits { +struct magi_request_limits { int cookies; int params_meta; int params_head; int params_body; int read_buffer; -} magi_request_limits; +}; -typedef struct magi_request { - magi_error error; +struct magi_request { + enum magi_error error; - magi_cookies *cookies; /* Passed HTTP cookies. */ - magi_params *meta; /* Request parameters. */ - magi_params *head; /* Form field values from URL. */ - magi_params *body; /* Form field values from body. */ - magi_files *files; /* Form field files metadatas. */ + struct magi_cookies *cookies; /* Passed HTTP cookies. */ + struct magi_params *meta; /* Request parameters. */ + struct magi_params *head; /* Form field values from URL. */ + struct magi_params *body; /* Form field values from body. */ + struct magi_files *files; /* Form field files metadatas. */ char *document_root; /* Server's document root (e.g. /var/www/htdocs). */ char *method; /* Request method (GET, HEAD, POST, etc...). */ @@ -43,33 +43,34 @@ typedef struct magi_request { char *path; /* Path requested for the script (e.g. /login). */ /* URL has form 'http[s]://{host}:{port}{script}{path}'. */ - magi_file_callback callback; /* Callback to actually load files. */ - magi_request_limits limits; -} magi_request; + struct magi_file_callback callback; /* Callback to load files. */ + struct magi_request_limits limits; +}; /* Request initialiser, setup defaults. */ -void magi_request_init(magi_request *r); +void magi_request_init(struct magi_request *r); /* Free memory used by request. */ -void magi_request_free(magi_request *r); +void magi_request_free(struct magi_request *r); /* Get value of meta-param with name. */ -char *magi_request_meta(const magi_request *r, const char *name); +char *magi_request_meta(const struct magi_request *r, const char *name); /* Get value of form field param (prioritising body) with name. */ -char *magi_request_param(const magi_request *r, const char *name); +char *magi_request_param(const struct magi_request *r, const char *name); /* Get value of form field param with name from url. */ -char *magi_request_urlparam(const magi_request *r, const char *name); +char *magi_request_urlparam(const struct magi_request *r, const char *name); /* Get metadata structure of file from file field with name. */ -const magi_file *magi_request_file(const magi_request *r, const char *name); +const struct magi_file *magi_request_file(const struct magi_request *r, + const char *name); /* Get value of cookie with name. */ -char *magi_request_cookie(const magi_request *r, const char *name); +char *magi_request_cookie(const struct magi_request *r, const char *name); /* Get cookie with name. */ -const magi_cookie *magi_request_cookie_complex(const magi_request *r, - const char *name); +const struct magi_cookie * +magi_request_cookie_complex(const struct magi_request *r, const char *name); #endif diff --git a/include/magi/response.h b/include/magi/response.h index c38e3f9..3ef053a 100644 --- a/include/magi/response.h +++ b/include/magi/response.h @@ -12,41 +12,49 @@ /* Response headers as three sequential groups. */ -typedef struct magi_response { - magi_params *head_response; - magi_params *head_general; - magi_params *head_entity; -} magi_response; +struct magi_response { + struct magi_params *head_response; + struct magi_params *head_general; + struct magi_params *head_entity; +}; /* Response initialiser, setup defaults. */ -void magi_response_init(magi_response *r); +void magi_response_init(struct magi_response *r); /* Send response headers. */ -void magi_response_send(magi_response *r); +void magi_response_send(struct magi_response *r); /* Free memory used by response headers. */ -void magi_response_free(magi_response *r); +void magi_response_free(struct magi_response *r); /* Just response defaults. (text/html, 200 Ok) */ void magi_response_default(); /* Change resposne status header. */ -void magi_response_status(magi_response *r, int code, const char *description); +void magi_response_status(struct magi_response *r, + int code, + const char *description); /* Add cookie to response. */ -void magi_response_cookie(magi_response *r, const char *n, const char *d); +void magi_response_cookie(struct magi_response *r, + const char *n, + const char *d); /* Add cookie with additional information to response. */ -void magi_response_cookie_complex(magi_response *r, magi_cookie *c); +void magi_response_cookie_complex(struct magi_response *r, + struct magi_cookie *c); /* Add request to discard cookie to response. */ -void magi_response_cookie_discard(magi_response *r, const char *name); +void magi_response_cookie_discard(struct magi_response *r, + const char *name); /* Just add some general custom header. */ -void magi_response_header(magi_response *r, const char *n, const char *d); +void magi_response_header(struct magi_response *r, + const char *n, + const char *d); /* Change Content-Length header. */ -void magi_response_content_length(magi_response *r, int length); +void magi_response_content_length(struct magi_response *r, int length); /* Change Content-Type header. */ -void magi_response_content_type(magi_response *r, const char *type); +void magi_response_content_type(struct magi_response *r, const char *type); #endif diff --git a/man/magi_param.3 b/man/magi_param.3 index e69de29..0044993 100644 --- a/man/magi_param.3 +++ b/man/magi_param.3 @@ -0,0 +1,8 @@ +.TH MAGI 3 2020-05-02 v0.0.1 "Library Manual" +.SH NAME +.B magi_param +\- parameter as key-value pair +.SH SYNOPSYS +.SH DESCRIPTION +.SH RETURN VALUES +.SH EXAMPLES diff --git a/src/cookie.c b/src/cookie.c index fc66428..ebca599 100644 --- a/src/cookie.c +++ b/src/cookie.c @@ -4,7 +4,7 @@ #include -void magi_cookies_free(magi_cookies *cookies) +void magi_cookies_free(struct magi_cookies *cookies) { if (cookies) { magi_cookies_free(cookies->next); @@ -17,9 +17,10 @@ void magi_cookies_free(magi_cookies *cookies) } } -void magi_cookies_add(magi_cookies **cookies, magi_cookie *newitem) +void magi_cookies_add(struct magi_cookies **cookies, + struct magi_cookie *newitem) { - magi_cookies *node = malloc(sizeof(*node)); + struct magi_cookies *node = malloc(sizeof(*node)); if (node) { node->next = *cookies; node->item = *newitem; @@ -27,8 +28,8 @@ void magi_cookies_add(magi_cookies **cookies, magi_cookie *newitem) } } -const magi_cookie *magi_cookies_get(const magi_cookies *cookies, - const char *name) +const struct magi_cookie *magi_cookies_get(const struct magi_cookies *cookies, + const char *name) { if (!name) { return 0; diff --git a/src/cookies.c b/src/cookies.c index d1aeb46..98c1794 100644 --- a/src/cookies.c +++ b/src/cookies.c @@ -5,23 +5,23 @@ #include -typedef enum dt { dt_plain = 0, dt_version, dt_path, dt_domain } dt; - -typedef struct automata { - magi_cookies **list; - magi_cookie cookie; - char *buf; - int buf_len; - int buf_size; - int is_first; - int is_advanced; - int is_quoted; - dt datatype; -} automata; -typedef void *(*state)(automata *a, char c); - - -static void nulify_cookie(automata *a) +enum dt { dt_plain = 0, dt_version, dt_path, dt_domain }; + +struct automata { + struct magi_cookies **list; + struct magi_cookie cookie; + char *buf; + int buf_len; + int buf_size; + int is_first; + int is_advanced; + int is_quoted; + enum dt datatype; +}; +typedef void *(*state)(struct automata *a, char c); + + +static void nulify_cookie(struct automata *a) { a->cookie.name = 0; a->cookie.data = 0; @@ -30,7 +30,7 @@ static void nulify_cookie(automata *a) a->cookie.max_age = 0; } -static void buf_new(automata *a) +static void buf_new(struct automata *a) { a->buf = 0; a->buf_len = 0; @@ -38,9 +38,9 @@ static void buf_new(automata *a) } -static dt what_is_name(const automata *a) +static enum dt what_is_name(const struct automata *a) { - dt datatype = dt_plain; + enum dt datatype = dt_plain; if (a->is_first && !strcmp(a->buf, "$Version")) { datatype = dt_version; } else if (a->is_advanced) { @@ -54,7 +54,7 @@ static dt what_is_name(const automata *a) } -static void end_name(automata *a) +static void end_name(struct automata *a) { a->datatype = what_is_name(a); if (a->datatype == dt_plain) { @@ -70,7 +70,7 @@ static void end_name(automata *a) buf_new(a); } -static int end_data(automata *a) +static int end_data(struct automata *a) { switch (a->datatype) { case dt_plain: a->cookie.data = a->buf; break; @@ -82,8 +82,8 @@ static int end_data(automata *a) return 1; } -static void *state_name(automata *a, char c); -static void *state_pre_name(automata *a, char c) +static void *state_name(struct automata *a, char c); +static void *state_pre_name(struct automata *a, char c) { if (c == ' ' || c == '\t'){ return state_pre_name; @@ -94,9 +94,9 @@ static void *state_pre_name(automata *a, char c) return 0; } -static void *state_pre_data(automata *a, char c); -static void *state_post_name(automata *a, char c); -static void *state_name(automata *a, char c) +static void *state_pre_data(struct automata *a, char c); +static void *state_post_name(struct automata *a, char c); +static void *state_name(struct automata *a, char c) { if (c == '=') { end_name(a); @@ -111,7 +111,7 @@ static void *state_name(automata *a, char c) return 0; } -static void *state_post_name(automata *a, char c) +static void *state_post_name(struct automata *a, char c) { if (c == '=') { return state_pre_data; @@ -121,9 +121,9 @@ static void *state_post_name(automata *a, char c) return 0; } -static void *state_data(automata *a, char c); -static void *state_data_quoted(automata *a, char c); -static void *state_pre_data(automata *a, char c) +static void *state_data(struct automata *a, char c); +static void *state_data_quoted(struct automata *a, char c); +static void *state_pre_data(struct automata *a, char c) { if (c == '"') { return state_data_quoted; @@ -136,8 +136,8 @@ static void *state_pre_data(automata *a, char c) return 0; } -static void *state_post_data(automata *a, char c); -static void *state_data(automata *a, char c) +static void *state_post_data(struct automata *a, char c); +static void *state_data(struct automata *a, char c) { if (c == ';' || (c == ',' && a->is_first)) { a->is_first = 0; @@ -151,7 +151,7 @@ static void *state_data(automata *a, char c) return 0; } -static void *state_data_quoted(automata *a, char c) +static void *state_data_quoted(struct automata *a, char c) { if (c == '"') { return end_data(a) ? state_post_data : 0; @@ -159,7 +159,7 @@ static void *state_data_quoted(automata *a, char c) return state_data_quoted; } -static void *state_post_data(automata *a, char c) +static void *state_post_data(struct automata *a, char c) { if (c == ';' || (c == ',' && a->is_first)) { a->is_first = 0; @@ -171,7 +171,7 @@ static void *state_post_data(automata *a, char c) } -static void parse_end(magi_error *e, automata *a, state s) +static void parse_end(enum magi_error *e, struct automata *a, state s) { if (s == state_data_quoted) { *e = magi_error_cookies; @@ -190,12 +190,12 @@ static void parse_end(magi_error *e, automata *a, state s) } -void magi_parse_cookies(magi_request *request, const char *data) +void magi_parse_cookies(struct magi_request *request, const char *data) { - state s; - automata a = { 0, { 0, 0, 0, 0, 0 }, 0, 0, 0, 1, 0, 0, 0 }; - a.list = &request->cookies; - request->cookies = 0; + state s; + struct automata a = { 0, { 0, 0, 0, 0, 0 }, 0, 0, 0, 1, 0, 0, 0 }; + a.list = &request->cookies; + request->cookies = 0; for (s = state_pre_name; s && *data; ++data) { s = s(&a, *data); } diff --git a/src/cookies.h b/src/cookies.h index 30b9746..62b7249 100644 --- a/src/cookies.h +++ b/src/cookies.h @@ -4,7 +4,7 @@ #include "request.h" -void magi_parse_cookies(magi_request *request, const char *data); +void magi_parse_cookies(struct magi_request *request, const char *data); #endif diff --git a/src/error.c b/src/error.c index 0bb553b..0ae2704 100644 --- a/src/error.c +++ b/src/error.c @@ -3,7 +3,7 @@ #include -const char *magi_error_message(magi_error error) +const char *magi_error_message(enum magi_error error) { static const char *const messages[] = { 0, /* magi_error_none */ @@ -19,7 +19,7 @@ const char *magi_error_message(magi_error error) return messages[error]; } -void magi_error_response(magi_error error) +void magi_error_response(enum magi_error error) { fputs("Status: 400 Bad Request\r\n" "Content-Type: text/html\r\n\r\n" diff --git a/src/file.c b/src/file.c index 7e3d2f8..30b085c 100644 --- a/src/file.c +++ b/src/file.c @@ -4,9 +4,9 @@ #include -void magi_files_add(magi_files **files, magi_file *newitem) +void magi_files_add(struct magi_files **files, struct magi_file *newitem) { - magi_files *node = malloc(sizeof(*node)); + struct magi_files *node = malloc(sizeof(*node)); if (node) { node->next = *files; node->item = *newitem; @@ -14,7 +14,8 @@ void magi_files_add(magi_files **files, magi_file *newitem) } } -const magi_file *magi_files_get(const magi_files *files, const char *name) +const struct magi_file *magi_files_get(const struct magi_files *files, + const char *name) { if (!files || !name) { return 0; @@ -25,7 +26,7 @@ const magi_file *magi_files_get(const magi_files *files, const char *name) } } -void magi_files_free(magi_files *files) +void magi_files_free(struct magi_files *files) { if (files) { magi_files_free(files->next); diff --git a/src/loadfiles.c b/src/loadfiles.c index 6d68895..963e351 100644 --- a/src/loadfiles.c +++ b/src/loadfiles.c @@ -5,10 +5,10 @@ #include -void magi_loadfiles_add(magi_loadfiles *table, - const char *name, - const char *path, - int max) +void magi_loadfiles_add(struct magi_loadfiles *table, + const char *name, + const char *path, + int max) { static const int size = sizeof(*table->files); if (!table) { @@ -25,7 +25,7 @@ void magi_loadfiles_add(magi_loadfiles *table, table->count++; } -void magi_loadfiles_free(magi_loadfiles *table) +void magi_loadfiles_free(struct magi_loadfiles *table) { if (!table) { return; @@ -34,14 +34,14 @@ void magi_loadfiles_free(magi_loadfiles *table) table->count = 0; } -static void loadfiles_callback(void *userdata, - int newfile, - magi_file *file, - char *addon, - int addon_len) +static void loadfiles_callback(void *userdata, + int newfile, + struct magi_file *file, + char *addon, + int addon_len) { - magi_loadfiles *table = userdata; - int pos; + int pos; + struct magi_loadfiles *table = userdata; if (!file->filename || !*file->filename) { return; } @@ -71,7 +71,8 @@ static void loadfiles_callback(void *userdata, } } -void magi_loadfiles_set(magi_request *request, magi_loadfiles *table) +void magi_loadfiles_set(struct magi_request *request, + struct magi_loadfiles *table) { request->callback.act = loadfiles_callback; request->callback.userdata = table; diff --git a/src/multipart.c b/src/multipart.c index 8164bfd..a412603 100644 --- a/src/multipart.c +++ b/src/multipart.c @@ -32,25 +32,25 @@ static int is_str_token(char *str) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Automata for multipart/form-data */ -typedef struct automata { - magi_request *request; - magi_file file; - magi_param param; - magi_param subparam; - char *buf; - int buf_size; - int size; - int len; - char *boundary; - int boundary_pos; - int boundary_len; - int is_end_suspected; - int is_CR_readed; - int is_quoted; - int readed; - int newfile; -} automata; -typedef void *(*state)(automata *a, char c); +struct automata { + struct magi_request *request; + struct magi_file file; + struct magi_param param; + struct magi_param subparam; + char *buf; + int buf_size; + int size; + int len; + char *boundary; + int boundary_pos; + int boundary_len; + int is_end_suspected; + int is_CR_readed; + int is_quoted; + int readed; + int newfile; +}; +typedef void *(*state)(struct automata *a, char c); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -71,7 +71,7 @@ static char *extract_filename(char *n) } } -static int content_disposition(automata *a) +static int content_disposition(struct automata *a) { char *n = strchr(a->subparam.data, '='); if (!n) { @@ -103,7 +103,7 @@ static int content_disposition(automata *a) return 1; } -static int subparam_end(automata *a) +static int subparam_end(struct automata *a) { a->size = 1; a->len = 0; @@ -117,7 +117,7 @@ static int subparam_end(automata *a) return 1; } -static int param_end(automata *a) +static int param_end(struct automata *a) { if (a->file.filename) { a->request->callback.act(a->request->callback.userdata, @@ -151,7 +151,7 @@ static int param_end(automata *a) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Boundary Interfaces */ -static char sepget(const automata *a) +static char sepget(const struct automata *a) { char c; const int pos_after = a->boundary_pos - 4 - a->boundary_len; @@ -173,9 +173,7 @@ static char sepget(const automata *a) return c; } -static int seplen(const automata *a) { return a->boundary_len + 6; } - -static char endget(const automata *a) +static char endget(const struct automata *a) { char c; const int pos_after = a->boundary_pos - 4 - a->boundary_len; @@ -199,12 +197,10 @@ static char endget(const automata *a) return c; } -static int endlen(const automata *a) -{ - return a->boundary_len + 8; -} +static int seplen(const struct automata *a) { return a->boundary_len + 6; } +static int endlen(const struct automata *a) { return a->boundary_len + 8; } -static int is_semiend(const automata *a) +static int is_semiend(const struct automata *a) { /* Is end readed, expect last two chars, which are CR LF? */ return a->is_end_suspected && (a->boundary_pos == endlen(a) - 2); } @@ -213,7 +209,7 @@ static int is_semiend(const automata *a) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Data Reading */ -static void apply_callback(automata *a) +static void apply_callback(struct automata *a) { int full = a->buf_size == a->request->callback.addon_max; if (a->file.filename && full) { @@ -225,9 +221,13 @@ static void apply_callback(automata *a) } } -static void *state_pname_pre(automata *a, char c); -static void *state_data(automata *a, char c); -static void *data_add_act(automata *a, char c, char **dest, int *l, int *s) +static void *state_pname_pre(struct automata *a, char c); +static void *state_data(struct automata *a, char c); +static void *data_add_act(struct automata *a, + char c, + char **dest, + int *l, + int *s) { int pos = a->boundary_pos; for (a->boundary_pos = 0; a->boundary_pos < pos; ++a->boundary_pos) { @@ -254,7 +254,7 @@ static void *data_add_act(automata *a, char c, char **dest, int *l, int *s) return state_data; } -static void *data_add(automata *a, char c) +static void *data_add(struct automata *a, char c) { if (a->file.filename) { int max = a->request->callback.addon_max + 1; @@ -268,7 +268,7 @@ static void *data_add(automata *a, char c) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * State analysers */ -static void *state_begin(automata *a, char c) +static void *state_begin(struct automata *a, char c) { if (sepget(a) != c) { /* 'c' is not wanted character from separator; */ a->boundary_pos = 0; /* so nullify progress in reading separator. */ @@ -281,8 +281,8 @@ static void *state_begin(automata *a, char c) return state_begin; } -static void *state_pname(automata *a, char c); -static void *state_pname_pre(automata *a, char c) +static void *state_pname(struct automata *a, char c); +static void *state_pname_pre(struct automata *a, char c) { if (a->is_CR_readed) { if (c != '\n') { @@ -301,9 +301,9 @@ static void *state_pname_pre(automata *a, char c) return 0; } -static void *state_pdata(automata *a, char c); -static void *state_pname_end(automata *a, char c); -static void *state_pname(automata *a, char c) +static void *state_pdata(struct automata *a, char c); +static void *state_pname_end(struct automata *a, char c); +static void *state_pname(struct automata *a, char c) { if (c == ':') { a->len = 0; @@ -318,7 +318,7 @@ static void *state_pname(automata *a, char c) return 0; } -static void *state_pname_end(automata *a, char c) +static void *state_pname_end(struct automata *a, char c) { if (c == ':') { a->len = 0; @@ -330,7 +330,7 @@ static void *state_pname_end(automata *a, char c) return 0; } -static void *state_pdata(automata *a, char c) +static void *state_pdata(struct automata *a, char c) { if (a->is_CR_readed) { a->is_CR_readed = 0; @@ -347,8 +347,8 @@ static void *state_pdata(automata *a, char c) return state_pdata; } -static void *state_end(automata *a, char c); -static void *state_data(automata *a, char c) +static void *state_end(struct automata *a, char c); +static void *state_data(struct automata *a, char c) { if (a->is_end_suspected) { if (endget(a) != c) { @@ -375,15 +375,15 @@ static void *state_data(automata *a, char c) return state_data; } -static void *state_end(automata *a, char c) { return state_end; } +static void *state_end(struct automata *a, char c) { return state_end; } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Automata Runner */ -static void run_automata(automata *a, - int (*next)(void *next_userdata), - void *next_userdata) +static void run_automata(struct automata *a, + int (*next)(void *next_userdata), + void *next_userdata) { state s; int c = next(next_userdata); @@ -409,12 +409,12 @@ static void run_automata(automata *a, /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Automata Interfaces */ -void magi_parse_multipart(magi_request *request, - char *boundary, - int (*next)(void *userdata), - void *next_userdata) +void magi_parse_multipart(struct magi_request *request, + char *boundary, + int (*next)(void *userdata), + void *next_userdata) { - automata a = { + struct automata a = { 0, { 0, 0, 0 }, { 0, 0 }, { 0, 0 }, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 1 }; a.request = request; diff --git a/src/multipart.h b/src/multipart.h index 617c3f8..0b55e8c 100644 --- a/src/multipart.h +++ b/src/multipart.h @@ -4,10 +4,10 @@ #include "request.h" -void magi_parse_multipart(magi_request *request, - char *boundary, - int (*next)(void *userdata), - void *next_userdata); +void magi_parse_multipart(struct magi_request *request, + char *boundary, + int (*next)(void *userdata), + void *next_userdata); #endif diff --git a/src/param.c b/src/param.c index dcf33d4..5854231 100644 --- a/src/param.c +++ b/src/param.c @@ -4,9 +4,9 @@ #include -void magi_params_add(magi_params **params, magi_param *newitem) +void magi_params_add(struct magi_params **params, struct magi_param *newitem) { - magi_params *node = malloc(sizeof(*node)); + struct magi_params *node = malloc(sizeof(*node)); if (node) { node->next = *params; node->item = *newitem; @@ -14,7 +14,7 @@ void magi_params_add(magi_params **params, magi_param *newitem) } } -void magi_params_set(magi_params **params, magi_param *newitem) +void magi_params_set(struct magi_params **params, struct magi_param *newitem) { if (!*params) { magi_params_add(params, newitem); @@ -28,7 +28,7 @@ void magi_params_set(magi_params **params, magi_param *newitem) } } -char *magi_params_get(const magi_params *params, const char *name) +char *magi_params_get(const struct magi_params *params, const char *name) { if (!params || !name) { return 0; @@ -39,7 +39,7 @@ char *magi_params_get(const magi_params *params, const char *name) } } -void magi_params_free(magi_params *params) +void magi_params_free(struct magi_params *params) { if (params) { magi_params_free(params->next); diff --git a/src/parse.c b/src/parse.c index 83884f0..fa8b54a 100644 --- a/src/parse.c +++ b/src/parse.c @@ -37,13 +37,13 @@ static char *lower_env(char *env_name) return env; } -static void cgi_http_env(magi_request *r) +static void cgi_http_env(struct magi_request *r) { char **env; int len = 0; r->meta = 0; for (env = environ; *env; ++env) { - magi_param meta; + struct magi_param meta; char *name_end = strchr(*env, '='); int nlen = name_end - *env; int dlen = strlen(name_end + 1); @@ -58,7 +58,7 @@ static void cgi_http_env(magi_request *r) } } -static void cgi_env(magi_request *r) +static void cgi_env(struct magi_request *r) { cgi_http_env(r); r->method = plain_env("REQUEST_METHOD"); @@ -78,7 +78,7 @@ static void cgi_env(magi_request *r) r->path = plain_env("PATH_INFO"); } -static void cgi_cookies(magi_request *r) +static void cgi_cookies(struct magi_request *r) { const char *env = getenv("HTTP_COOKIE"); if (!env || !*env) { @@ -92,7 +92,7 @@ static void cgi_cookies(magi_request *r) magi_parse_cookies(r, env); } -static void cgi_input_get(magi_error *e, char **input, int max) +static void cgi_input_get(enum magi_error *e, char **input, int max) { const char *env_input = getenv("QUERY_STRING"); if (env_input) { @@ -105,7 +105,7 @@ static void cgi_input_get(magi_error *e, char **input, int max) } } -static void cgi_url(magi_request *request) +static void cgi_url(struct magi_request *request) { char *in = 0; cgi_input_get(&request->error, &in, request->limits.params_head); @@ -115,7 +115,7 @@ static void cgi_url(magi_request *request) } } -static void cgi_input_post(magi_error *e, char **input, int max) +static void cgi_input_post(enum magi_error *e, char **input, int max) { int input_len = strtoul(getenv("CONTENT_LENGTH"), 0, 10); if (!input_len) { @@ -175,7 +175,7 @@ static int next(void *userdata) } /* Interfacial CGI Request Handling */ -int magi_parse_head(magi_request *request) +int magi_parse_head(struct magi_request *request) { request->cookies = 0; request->files = 0; @@ -189,10 +189,10 @@ int magi_parse_head(magi_request *request) return !request->error; } -int magi_parse_body(magi_request *request) +int magi_parse_body(struct magi_request *request) { - magi_error *e = &request->error; - request->error = magi_error_none; + enum magi_error *e = &request->error; + request->error = magi_error_none; if (request->method && !strcmp(request->method, "POST")) { const char *t = getenv("CONTENT_TYPE"); if (!t) { @@ -223,7 +223,7 @@ int magi_parse_body(magi_request *request) return !request->error; } -int magi_parse(magi_request *request) +int magi_parse(struct magi_request *request) { return magi_parse_head(request) && magi_parse_body(request); } diff --git a/src/request.c b/src/request.c index 22b3f30..e957889 100644 --- a/src/request.c +++ b/src/request.c @@ -3,7 +3,7 @@ #include -void magi_request_init(magi_request *request) +void magi_request_init(struct magi_request *request) { if (request) { request->callback.act = 0; @@ -18,7 +18,7 @@ void magi_request_init(magi_request *request) } -static void request_free(magi_request *request) +static void request_free(struct magi_request *request) { free(request->cookies); free(request->meta); @@ -32,7 +32,7 @@ static void request_free(magi_request *request) free(request->path); } -static void request_annul(magi_request *request) +static void request_annul(struct magi_request *request) { request->cookies = 0; request->meta = 0; @@ -45,7 +45,7 @@ static void request_annul(magi_request *request) request->path = 0; } -void magi_request_free(magi_request *request) +void magi_request_free(struct magi_request *request) { if (request) { magi_cookies_free(request->cookies); @@ -59,12 +59,12 @@ void magi_request_free(magi_request *request) } -char *magi_request_meta(const magi_request *r, const char *name) +char *magi_request_meta(const struct magi_request *r, const char *name) { return magi_params_get(r->meta, name); } -char *magi_request_param(const magi_request *r, const char *name) +char *magi_request_param(const struct magi_request *r, const char *name) { char *res = magi_params_get(r->body, name); if (!res) { @@ -73,27 +73,29 @@ char *magi_request_param(const magi_request *r, const char *name) return res; } -char *magi_request_urlparam(const magi_request *r, const char *name) +char *magi_request_urlparam(const struct magi_request *r, const char *name) { return magi_params_get(r->head, name); } -const magi_file *magi_request_file(const magi_request *r, const char *name) +const struct magi_file *magi_request_file(const struct magi_request *r, + const char *name) { return magi_files_get(r->files, name); } -char *magi_request_cookie(const magi_request *r, const char *name) +char *magi_request_cookie(const struct magi_request *r, const char *name) { - const magi_cookie *res = magi_cookies_get(r->cookies, name); + const struct magi_cookie *res = magi_cookies_get(r->cookies, name); if (!res) { return 0; } return res->data; } -const magi_cookie *magi_request_cookie_complex(const magi_request *r, - const char *name) +const struct magi_cookie * +magi_request_cookie_complex(const struct magi_request *r, + const char *name) { return magi_cookies_get(r->cookies, name); } diff --git a/src/response.c b/src/response.c index 7723e26..43da57e 100644 --- a/src/response.c +++ b/src/response.c @@ -6,7 +6,7 @@ #include -void magi_response_init(magi_response *r) +void magi_response_init(struct magi_response *r) { r->head_response = 0; r->head_general = 0; @@ -15,7 +15,7 @@ void magi_response_init(magi_response *r) magi_response_content_type(r, "text/html"); } -static void response_headers(magi_params *p) +static void response_headers(struct magi_params *p) { for (; p; p = p->next) { fputs(p->item.name, stdout); @@ -25,7 +25,7 @@ static void response_headers(magi_params *p) } } -void magi_response_send(magi_response *r) +void magi_response_send(struct magi_response *r) { response_headers(r->head_response); response_headers(r->head_general); @@ -33,7 +33,7 @@ void magi_response_send(magi_response *r) fputs("\r\n", stdout); } -void magi_response_free(magi_response *r) +void magi_response_free(struct magi_response *r) { magi_params_free(r->head_response); magi_params_free(r->head_general); @@ -54,10 +54,12 @@ void magi_response_default() } -void magi_response_status(magi_response *r, int code, const char *description) +void magi_response_status(struct magi_response *r, + int code, + const char *description) { - int dlen; - magi_param addon; + int dlen; + struct magi_param addon; if (code <= 99 || 600 <= code) { return; } @@ -72,11 +74,13 @@ void magi_response_status(magi_response *r, int code, const char *description) magi_params_set(&r->head_response, &addon); } -void magi_response_cookie(magi_response *r, const char *name, const char *data) +void magi_response_cookie(struct magi_response *r, + const char *name, + const char *data) { - magi_param addon; - int nlen; - int dlen; + int nlen; + int dlen; + struct magi_param addon; if (!name || !data) { return; } @@ -90,10 +94,11 @@ void magi_response_cookie(magi_response *r, const char *name, const char *data) magi_params_add(&r->head_general, &addon); } -void magi_response_cookie_complex(magi_response *r, magi_cookie *c) +void magi_response_cookie_complex(struct magi_response *r, + struct magi_cookie *c) { - magi_param addon; - char *pointer; + char *pointer; + struct magi_param addon; int nlen, dlen, dsize, psize, msize; const int cdsize = 9; const int cpsize = 7; @@ -134,10 +139,10 @@ void magi_response_cookie_complex(magi_response *r, magi_cookie *c) magi_params_add(&r->head_general, &addon); } -void magi_response_cookie_discard(magi_response *r, const char *name) +void magi_response_cookie_discard(struct magi_response *r, const char *name) { - magi_param addon; int len; + struct magi_param addon; if (!name) { return; } @@ -149,17 +154,19 @@ void magi_response_cookie_discard(magi_response *r, const char *name) magi_params_add(&r->head_general, &addon); } -void magi_response_header(magi_response *r, const char *name, const char *data) +void magi_response_header(struct magi_response *r, + const char *name, + const char *data) { - magi_param addon; + struct magi_param addon; addon.name = magi_str_create_copy(name, strlen(name)); addon.data = magi_str_create_copy(data, strlen(data)); magi_params_add(&r->head_general, &addon); } -void magi_response_content_length(magi_response *r, int length) +void magi_response_content_length(struct magi_response *r, int length) { - magi_param addon; + struct magi_param addon; int len = 1; addon.name = magi_str_create_copy("Content-Length", 14); addon.data = malloc(len + 1); @@ -173,9 +180,9 @@ void magi_response_content_length(magi_response *r, int length) magi_params_set(&r->head_entity, &addon); } -void magi_response_content_type(magi_response *r, const char *type) +void magi_response_content_type(struct magi_response *r, const char *type) { - magi_param addon; + struct magi_param addon; addon.name = magi_str_create_copy("Content-Type", 12); addon.data = magi_str_create_copy(type, strlen(type)); magi_params_set(&r->head_entity, &addon); diff --git a/src/urlencoded.c b/src/urlencoded.c index 27c9c21..f00492b 100644 --- a/src/urlencoded.c +++ b/src/urlencoded.c @@ -61,19 +61,19 @@ static int deurl(char **data) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Urlencoded Automata */ -typedef struct automata { - magi_params **list; - char *name; - int nlen; - int nsize; - char *data; - int dlen; - int dsize; -} automata; -typedef void *(*state)(automata *a, char c); +struct automata { + struct magi_params **list; + char *name; + int nlen; + int nsize; + char *data; + int dlen; + int dsize; +}; +typedef void *(*state)(struct automata *a, char c); -static void *state_parse_data(automata *a, char c); -static void *state_parse_name(automata *a, char c) +static void *state_parse_data(struct automata *a, char c); +static void *state_parse_name(struct automata *a, char c) { if (c == '&' || c == ';') { return 0; @@ -84,9 +84,9 @@ static void *state_parse_name(automata *a, char c) return state_parse_name; } -static void add_to_list(automata *a) +static void add_to_list(struct automata *a) { - magi_param param; + struct magi_param param; param.name = a->name; param.data = a->data; magi_params_add(a->list, ¶m); @@ -94,7 +94,7 @@ static void add_to_list(automata *a) a->data = 0; } -static void *state_parse_data(automata *a, char c) +static void *state_parse_data(struct automata *a, char c) { if (c == '=') { return 0; @@ -109,12 +109,13 @@ static void *state_parse_data(automata *a, char c) return state_parse_data; } -magi_error magi_parse_urlencoded(magi_params **list, const char *encoded) +enum magi_error magi_parse_urlencoded(struct magi_params **list, + const char *encoded) { - state s; - automata a = { 0, 0, 0, 0, 0, 0, 0 }; - a.list = list; - *list = 0; + state s; + struct automata a = { 0, 0, 0, 0, 0, 0, 0 }; + a.list = list; + *list = 0; if (!encoded || !*encoded) { return 0; } diff --git a/src/urlencoded.h b/src/urlencoded.h index df33fe4..5933669 100644 --- a/src/urlencoded.h +++ b/src/urlencoded.h @@ -6,7 +6,8 @@ /* Add decoded params from 'encoded' to 'list'. */ -magi_error magi_parse_urlencoded(magi_params **list, const char *encoded); +enum magi_error magi_parse_urlencoded(struct magi_params **list, + const char *encoded); #endif -- cgit v1.2.3