From c65fdedc7bedfc20da73cdbfc34c22bb80139896 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 28 Feb 2020 20:16:57 +0300 Subject: [magi] --- examples/Makefile | 2 +- examples/append.c | 33 ++++++------- examples/cookie.c | 43 ++++++++--------- examples/echo.c | 135 ++++++++++++++++++++++++++--------------------------- examples/fastcgi.c | 34 ++++++++++++++ examples/fcgi.c | 46 ------------------ examples/upload.c | 51 ++++++++++---------- 7 files changed, 162 insertions(+), 182 deletions(-) create mode 100644 examples/fastcgi.c delete mode 100644 examples/fcgi.c (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile index 28e9bed..f2c366b 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,7 +3,7 @@ # Debug mode (allowing to debug the examples via gdb): # DEBUG = yes # Examples to build by default: -EXAMPLES = append cookie upload echo +EXAMPLES = append cookie echo upload fastcgi # Specify your favourite C compiler here (e.g. tcc): CC = gcc diff --git a/examples/append.c b/examples/append.c index 702f0fb..cafdf9c 100644 --- a/examples/append.c +++ b/examples/append.c @@ -1,23 +1,23 @@ #include #include -#include -void response_request(magi_request *req, magi_response *res) +void response(magi_request *r) { - char *data = magi_param_list_get(req->params, "addon"); + char *data = magi_request_param(r, "addon"); + magi_response_add(r, + "" + "" + "Append to File" + ""); if (data) { FILE *file = fopen("file_to_append", "a"); fputs(data, file); fclose(file); + magi_response_add(r, "

Appended!

"); } - - magi_response_add(res, - "" - "" - "Append to File" - "" + magi_response_add(r, "
" "" "" @@ -29,15 +29,12 @@ void response_request(magi_request *req, magi_response *res) int main(int argc, char const *argv[]) { magi_request request; - magi_request_setup(&request); - if (magi_request_full_cgi(&request)) { - magi_response response; - magi_response_setup(&response); - response_request(&request, &response); - magi_response_cgi_clear(&response); + magi_request_init(&request); + if (magi_cgi(&request)) { + response(&request); } else { - magi_error_cgi(request.error); + magi_response_error(&request); } - magi_request_destroy(&request); + magi_request_free(&request); return 0; } diff --git a/examples/cookie.c b/examples/cookie.c index 365f989..5bbade1 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -1,41 +1,38 @@ #include -#include -#include -void response_request(magi_request *req, magi_response *res) +void list_cookies(magi_request *r) { - magi_cookie_list *current; + magi_cookies *current; + magi_response_add(r, "Cookies:"); + for (current = r->cookies; current; current = current->next) { + magi_cookie *c = ¤t->item; + magi_response_format(r, "
[%s] = [%s]", c->name, c->data); + } +} - magi_response_add(res, +void response(magi_request *r) +{ + magi_response_cookie(r, "cookie", "monster"); + magi_response_add(r, "" "" "Cookie Listing and Setting" - ""); - - for (current = req->cookies; current; current = current->next) { - magi_response_add_format(res, - "[%s] = [%s]
", current->item.name, current->item.data); - } - - magi_response_add(res, ""); - - magi_response_cookie_easy(res, "cookie", "monster"); + "

"); + list_cookies(r); + magi_response_add(r, "

"); } int main(int argc, char const *argv[]) { magi_request request; - magi_request_setup(&request); - if (magi_request_full_cgi(&request)) { - magi_response response; - magi_response_setup(&response); - response_request(&request, &response); - magi_response_cgi_clear(&response); + magi_request_init(&request); + if (magi_cgi(&request)) { + response(&request); } else { - magi_error_cgi(request.error); + magi_response_error(&request); } - magi_request_destroy(&request); + magi_request_free(&request); return 0; } diff --git a/examples/echo.c b/examples/echo.c index f665cd0..89230de 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -1,118 +1,113 @@ #include -#include -#include -void proceed_cookies(magi_cookie_list *cookies, magi_response *response) +void list_cookies(magi_request *r) { - while (cookies) { - magi_response_add(response, "Cookie with name ["); - magi_response_add(response, cookies->item.name); - if (cookies->item.data) { - magi_response_add(response, "] is ["); - magi_response_add(response, cookies->item.data); + magi_cookies *current = r->cookies; + for (current = r->cookies; current; current = current->next) { + magi_cookie *c = ¤t->item; + magi_response_add(r, "Cookie with name ["); + magi_response_add(r, c->name); + if (c->data) { + magi_response_add(r, "] is ["); + magi_response_add(r, c->data); } - if (cookies->item.domain) { - magi_response_add(response, "] for domain ["); - magi_response_add(response, cookies->item.domain); + if (c->domain) { + magi_response_add(r, "] for domain ["); + magi_response_add(r, c->domain); } - if (cookies->item.path) { - magi_response_add(response, "] for path ["); - magi_response_add(response, cookies->item.path); + if (c->path) { + magi_response_add(r, "] for path ["); + magi_response_add(r, c->path); } - magi_response_add(response, "]
"); - cookies = cookies->next; + magi_response_add(r, "]
"); } } -void proceed_params(magi_param_list *params, magi_response *response) +void list_params(magi_request *r, magi_params *current) { - while (params) { - magi_response_add_format(response, - "[%s] is [%s]
", params->item.name, params->item.data); - params = params->next; + for (; current; current = current->next) { + magi_param *p = ¤t->item; + magi_response_format(r, "[%s] is [%s]
", p->name, p->data); } } -void proceed_files(magi_file_list *files, magi_response *response) +void list_files(magi_request *r) { - while (files) { - magi_file f = files->item; - magi_response_add_format(response, - "[%s] was [%s] on userside
", f.param_name, f.file_name); - files = files->next; + magi_files *current; + for (current = r->files; current; current = current->next) { + magi_file *f = ¤t->item; + magi_response_format(r, "[%s] was [%s] on clientside
", + f->param_name, f->file_name); } } -void process_meta(magi_request *req, magi_response *res) +void show_meta(magi_request *r) { - magi_response_add(res, "I was called with method ["); - magi_response_add(res, req->method); - if (req->uri) { - magi_response_add(res, "] with URL ["); - magi_response_add(res, req->uri); + magi_response_add(r, "I was called with method ["); + magi_response_add(r, r->method); + if (r->uri) { + magi_response_add(r, "] with URL ["); + magi_response_add(r, r->uri); } - if (req->server_name) { - magi_response_add(res, "] for server ["); - magi_response_add(res, req->server_name); + if (r->server_name) { + magi_response_add(r, "] for server ["); + magi_response_add(r, r->server_name); } - if (req->server_port) { - magi_response_add(res, "] on port ["); - magi_response_add(res, req->server_port); + if (r->server_port) { + magi_response_add(r, "] on port ["); + magi_response_add(r, r->server_port); } - if (req->server_protocol) { - magi_response_add(res, "] with protocol ["); - magi_response_add(res, req->server_protocol); + if (r->server_protocol) { + magi_response_add(r, "] with protocol ["); + magi_response_add(r, r->server_protocol); } - if (req->server_software) { - magi_response_add(res, "] and I am running on software ["); - magi_response_add(res, req->server_software); + if (r->server_software) { + magi_response_add(r, "] and I am running on software ["); + magi_response_add(r, r->server_software); } - magi_response_add(res, "]
"); + magi_response_add(r, "]
"); } -void response_request(magi_request *req, magi_response *res) +void response(magi_request *r) { - magi_response_add(res, + magi_response_add(r, "" "" "Echo" ""); - magi_response_add(res, "

Echo CGI Script

"); - process_meta(req, res); + magi_response_add(r, "

Echo CGI Script

"); + show_meta(r); - magi_response_add(res, "

Cookies:

"); - proceed_cookies(req->cookies, res); + magi_response_add(r, "

Cookies:

"); + list_cookies(r); - magi_response_add(res, "

Parameters:

"); - proceed_params(req->params, res); + magi_response_add(r, "

Parameters:

"); + list_params(r, r->params); - magi_response_add(res, "

URL Parameters:

"); - proceed_params(req->url_params, res); + magi_response_add(r, "

URL Parameters:

"); + list_params(r, r->url_params); - magi_response_add(res, "

HTTP Parameters:

"); - proceed_params(req->http_params, res); + magi_response_add(r, "

HTTP Parameters:

"); + list_params(r, r->http_params); - magi_response_add(res, "

Files:

"); - proceed_files(req->files, res); + magi_response_add(r, "

Files:

"); + list_files(r); - magi_response_add(res, ""); + magi_response_add(r, ""); } int main(int argc, char const *argv[]) { magi_request request; - magi_request_setup(&request); - if (magi_request_full_cgi(&request)) { - magi_response response; - magi_response_setup(&response); - response_request(&request, &response); - magi_response_cgi_clear(&response); + magi_request_init(&request); + if (magi_cgi(&request)) { + response(&request); } else { - magi_error_cgi(request.error); + magi_response_error(&request); } - magi_request_destroy(&request); + magi_request_free(&request); return 0; } diff --git a/examples/fastcgi.c b/examples/fastcgi.c new file mode 100644 index 0000000..2d68a3b --- /dev/null +++ b/examples/fastcgi.c @@ -0,0 +1,34 @@ +#include + + +void response(magi_request *r) +{ + magi_response_add(r, + "" + "" + "Fast CGI" + "Hi!" + ""); +} + +int main(int argc, char const *argv[]) +{ + magi_session session; + magi_request request; + magi_request_init(&request); + magi_session_init(&session); + magi_session_inet(&session, "localhost", "9973"); + 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/fcgi.c b/examples/fcgi.c deleted file mode 100644 index de2f525..0000000 --- a/examples/fcgi.c +++ /dev/null @@ -1,46 +0,0 @@ -/* * * TODO -- not valid yet * * */ -#include -#include -#include -#include - - -void response_request(struct magi_request * req, struct magi_resopnse * res) -{ - magi_response_content_type(res, magi_xhtml); - magi_response_add( - res, "" - "" - "Fast CGI" - "Hi!" - ""); -} - -int main(int argc, char const * argv[]) -{ - struct magi_session session; - int sock = magi_socket_inet("localhost", 9973); - /* E.g. also magi_socket_file("fcgi.sock") can be used. */ - if (magi_session_fcgi(&session, sock)) { - struct magi_request request; - magi_request_setup(&request); - while (magi_request_fcgi(&request, &session) && - magi_request_resume_fcgi(&request, &session)) { - if (!request.error) { - struct magi_response response; - magi_response_setup(&response); - response_request(&request, &response); - magi_response_fcgi(&response, &session); - magi_response_destroy(&response); - } else { - magi_error_fcgi(request.error, &session); - } - magi_request_destroy(&request); - } - } - fputs(magi_error_message(session.error), stderr); - magi_session_destroy(&session); - magi_socket_close(sock); - return 0; -} diff --git a/examples/upload.c b/examples/upload.c index 1877d39..158dcad 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -1,23 +1,22 @@ #include #include -#include -#include -void response_request(magi_request *req, magi_response *res) +void response(magi_request *r) { - char *name = magi_param_list_get(req->params, "name"); - magi_file *data = magi_file_list_get(req->files, "data"); - if (name && data) { - rename("data", name); - } - - magi_response_add(res, + char *name = magi_request_param(r, "name"); + magi_file *data = magi_request_file(r, "data"); + magi_response_add(r, "" "" "Upload File" - "" + ""); + if (name && data) { + rename("data", name); + magi_response_add(r, "

Uploaded!

"); + } + magi_response_add(r, "
" "" @@ -28,22 +27,26 @@ void response_request(magi_request *req, magi_response *res) ""); } +void get(magi_request *r) +{ + magi_loadfiles rules; + magi_loadfiles_init(&rules); + magi_loadfiles_add(&rules, "data", "data", 0); + magi_loadfiles_set(&r, &rules); + magi_cgi(&r); + magi_loadfiles_free(&rules); +} + int main(int argc, char const *argv[]) { - magi_request request; - magi_tempfiles tmps = { 0, 0 }; - magi_request_setup(&request); - magi_tempfiles_add(&tmps, "data", "data", 0); - magi_request_setup_tempfiles(&request, &tmps); - if (magi_request_full_cgi(&request)) { - magi_response response; - magi_response_setup(&response); - response_request(&request, &response); - magi_response_cgi(&response); + magi_request request; + magi_request_init(request); + get(&request); + if (request.error) { + magi_response_error(&request); } else { - magi_error_cgi(request.error); + response(&request); } - magi_tempfiles_destroy(&tmps); - magi_request_destroy(&request); + magi_request_free(&request); return 0; } -- cgit v1.2.3