From 07657b8a9f5c2fd9047594ec8604b9c439a999e4 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Sat, 4 Apr 2020 19:39:38 +0300 Subject: [magi] Finalization. --- examples/append.c | 52 ++++++++++++++++------------- examples/cookie.c | 40 ++++++++++++++--------- examples/echo.c | 96 +++++++++++++++++++++++++++--------------------------- examples/fastcgi.c | 33 ------------------- examples/upload.c | 62 ++++++++++++++++++++--------------- 5 files changed, 136 insertions(+), 147 deletions(-) delete mode 100644 examples/fastcgi.c (limited to 'examples') diff --git a/examples/append.c b/examples/append.c index 8417796..fe85492 100644 --- a/examples/append.c +++ b/examples/append.c @@ -1,40 +1,46 @@ +/* This is the very basic example, showing work with form fields to append + * specified by user in form addon into some file (i.e. "file_to_append"). + */ #include #include -void response(magi_request *r) +void append(magi_request *r) { char *data = magi_request_param(r, "addon"); - magi_response(r, - "" - "" - "Append to File" - ""); - if (data) { + if (data) { /* If field "addon" was in request: */ FILE *file = fopen("file_to_append", "a"); - fputs(data, file); + fputs(data, file); /* Append addon into the file. */ fclose(file); - magi_response(r, "

Appended!

"); + printf("

Appended!

"); /* And display success message. */ } - magi_response(r, - "
" - "" - "" - "
" - "" - ""); +} + +void response(magi_request *r) +{ + magi_response_default(); /* Pass default headers and send body: */ + printf("" + "" + "Append to File" + ""); + append(r); + printf("
" + "" + "" + "
" + ""); } int main() { magi_request request; - magi_request_init(&request); - if (magi_cgi(&request)) { - response(&request); - } else { - magi_response_error(&request); + magi_request_init(&request); /* Setting defaults. */ + if (magi_parse(&request)) { /* If parsing was done successful */ + response(&request); /* we need to response the request. */ + } else { /* And display error overwise: */ + magi_error_response(request.error); } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } diff --git a/examples/cookie.c b/examples/cookie.c index e18c100..c64ca0c 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -1,38 +1,46 @@ +/* Basic example of cookie handling through response headers. + * It lists all recieved cookies and set one via magi_response, + * showing work with headers in magi. + */ #include void list_cookies(magi_request *r) { magi_cookies *current; - magi_response(r, "Cookies:"); + 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; - magi_response_format(r, "
[%s]=[%s]", c->name, c->data); + printf("
[%s]=[%s]", c->name, c->data); } } void response(magi_request *r) { - magi_response_cookie(r, "cookie", "monster"); - magi_response(r, - "" - "" - "Cookie Listing and Setting" - "

"); + magi_response head; + magi_response_init(&head); /* Setting defaults. */ + /* Set cookie "cookie" with value "monster" on clientside: */ + magi_response_cookie(&head, "cookie", "monster"); + magi_response_free(&head); /* Send headers and start sending body: */ + printf("" + "" + "Cookie Listing and Setting" + "

"); list_cookies(r); - magi_response(r, "

"); + printf("

"); } int main() { magi_request request; - magi_request_init(&request); - if (magi_cgi(&request)) { - response(&request); - } else { - magi_response_error(&request); + magi_request_init(&request); /* Setting defaults. */ + if (magi_parse(&request)) { /* If parsing was done successful */ + response(&request); /* we need to response the request. */ + } else { /* And display error overwise: */ + magi_error_response(request.error); } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } diff --git a/examples/echo.c b/examples/echo.c index 7cb4a3b..4f6975a 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -1,111 +1,111 @@ +/* This is useful example echoing request data in response. + */ #include void list_cookies(magi_request *r) { magi_cookies *current = r->cookies; - magi_response(r, "

"); + 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; - magi_response(r, "Cookie with name ["); - magi_response(r, c->name); + printf("Cookie with name [%s", c->name); if (c->data) { - magi_response(r, "] is ["); - magi_response(r, c->data); + printf("] is [%s", c->data); } if (c->domain) { - magi_response(r, "] for domain ["); - magi_response(r, c->domain); + printf("] for domain [%s", c->domain); } if (c->path) { - magi_response(r, "] for path ["); - magi_response(r, c->path); + printf("] for path [%s", c->path); } - magi_response(r, "]
"); + printf("]
"); } - magi_response(r, "

"); + printf("

"); } -void list_params(magi_request *r, magi_params *current) +void list_params(magi_params *current) { - magi_response(r, "

"); + printf("

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

"); + printf("

"); } void list_files(magi_request *r) { magi_files *current; - magi_response(r, "

"); + 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; - magi_response_format(r, "[%s] was [%s] on clientside
", - f->field, f->filename); + printf("[%s] was [%s] on clientside
", f->field, f->filename); } - magi_response(r, "

"); + printf("

"); } void show_meta(magi_request *r) { - magi_response(r, "

I was called "); + printf("

I was called "); if (r->is_secure) { - magi_response(r, "securely "); + printf("securely "); } - magi_response_format(r, "with method [%s", r->method); + printf("with method [%s", r->method); if (r->host) { - magi_response_format(r, "] on server [%s", r->host); + printf("] on server [%s", r->host); } if (r->script) { - magi_response_format(r, "] being script on [%s", r->script); + printf("] being script on [%s", r->script); } if (r->path) { - magi_response_format(r, "] with requested path [%s", r->path); + printf("] with requested path [%s", r->path); } - magi_response(r, "]

"); + printf("]

"); } void response(magi_request *r) { - magi_response(r, - "" - "" - "Echo" - ""); + magi_response_default(); /* Pass default headers and send body: */ + printf("" + "" + "Echo" + ""); - magi_response(r, "

Echo CGI Script

"); + printf("

Echo CGI Script

"); show_meta(r); - magi_response(r, "

Cookies:

"); + printf("

Cookies:

"); list_cookies(r); - magi_response(r, "

Parameters:

"); - list_params(r, r->meta); + printf("

Parameters:

"); + list_params(r->meta); - magi_response(r, "

URL Parameters:

"); - list_params(r, r->head); + printf("

URL Parameters:

"); + list_params(r->head); - magi_response(r, "

Body Parameters:

"); - list_params(r, r->body); + printf("

Body Parameters:

"); + list_params(r->body); - magi_response(r, "

Files:

"); + printf("

Files:

"); list_files(r); - magi_response(r, ""); + printf(""); } int main() { magi_request request; - magi_request_init(&request); - if (magi_cgi(&request)) { - response(&request); - } else { - magi_response_error(&request); + magi_request_init(&request); /* Setting defaults. */ + if (magi_parse(&request)) { /* If parsing was done successful */ + response(&request); /* we need to response the request. */ + } else { /* And display error overwise: */ + magi_error_response(request.error); } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } diff --git a/examples/fastcgi.c b/examples/fastcgi.c deleted file mode 100644 index 16668f3..0000000 --- a/examples/fastcgi.c +++ /dev/null @@ -1,33 +0,0 @@ -#include - - -void response(magi_request *r) -{ - magi_response(r, - "" - "" - "Fast CGI" - "Hi!" - ""); -} - -int main() -{ - magi_session session; - magi_request request; - magi_request_init(&request); - magi_session_init(&session); - magi_session_unix(&session, "unix.sock"); - while (magi_fastcgi(&session, &request)) { - if (request.error) { - magi_response_error(&request); - } else { - response(&request); - } - magi_request_free(&request); - } - magi_request_free(&request); - magi_session_free(&session); - return 0; -} diff --git a/examples/upload.c b/examples/upload.c index bf8eed1..d07db16 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -1,51 +1,59 @@ +/* This is basic example of handling file uploading. + * It is using magi_loadfiles callback to load file temporarily and change its + * name into the one specified by user. + */ #include #include -void response(magi_request *r) +void upload(magi_request *r) { char *name = magi_request_param(r, "name"); const magi_file *data = magi_request_file(r, "data"); - magi_response(r, - "" - "" - "Upload File" - ""); - if (name && data) { - rename("data", name); - magi_response(r, "

Uploaded!

"); + 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. */ } - magi_response(r, - "
" - "" - "" - "" - "
" - "" - ""); +} + +void response(magi_request *r) +{ + magi_response_default(); /* Pass default headers and send body: */ + printf("" + "" + "Upload File" + ""); + upload(r); + printf("
" + "" + "" + "" + "
" + ""); } void get(magi_request *r) { 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); - magi_cgi(r); - magi_loadfiles_free(&rules); + magi_loadfiles_set(r, &rules); /* Setup request to use the callback. */ + magi_parse(r); + magi_loadfiles_free(&rules); /* Free data of callback. */ } int main() { magi_request request; magi_request_init(&request); - get(&request); - if (request.error) { - magi_response_error(&request); + get(&request); /* Parse request. */ + if (request.error) { /* If error occurred display error message: */ + magi_error_response(request.error); } else { - response(&request); + response(&request); /* Overwise response request. */ } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } -- cgit v1.2.3