From 88081d9a4376b1ee4feed3bc843c46096e087b1e Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 22 Nov 2019 18:55:54 +0300 Subject: . --- examples/append.c | 18 +++++----- examples/cookie.c | 21 +++++++----- examples/echo.c | 99 ++++++++++++++++++++++++------------------------------- examples/fcgi.c | 12 ++++--- examples/upload.c | 28 +++++++++------- 5 files changed, 88 insertions(+), 90 deletions(-) (limited to 'examples') diff --git a/examples/append.c b/examples/append.c index 30060fa..3320fe0 100644 --- a/examples/append.c +++ b/examples/append.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -7,7 +8,7 @@ void response_request(struct magi_request * req, struct magi_response * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content( + magi_response_add( res, "" @@ -21,10 +22,10 @@ void response_request(struct magi_request * req, struct magi_response * res) "" ""); - struct magi_field * a = magi_field_list_get(req->fields, "addon"); - if (a && a->data) { + struct magi_param * addon = magi_param_list_get(req->url_params, "addon"); + if (addon && addon->data) { FILE * file = fopen("file_to_append", "a"); - fputs(a->data, file); + fputs(addon->data, file); fclose(file); } } @@ -32,13 +33,14 @@ void response_request(struct magi_request * req, struct magi_response * res) int main(int argc, char const * argv[]) { struct magi_request request; - if (magi_cgi(&request, 0, 0, 0)) { + magi_request_setup(&request); + if (magi_request_cgi(&request)) { struct magi_response response; response_request(&request, &response); - magi_cgi_response(&response); - magi_response_destroy(); + magi_response_cgi(&response); + magi_response_destroy(&response); } else { - magi_cgi_error(request.error); + magi_error_cgi(request.error); } magi_request_destroy(&request); return 0; diff --git a/examples/cookie.c b/examples/cookie.c index f99f07f..01d6dea 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -8,35 +8,38 @@ void response_request(struct magi_request * req, struct magi_response * res) { - struct magi_cookie_list * cookie; + struct magi_cookie_list * current; magi_response_content_type(res, magi_xhtml); - magi_response_content( + magi_response_add( res, "" "" "Cookie Listing and Setting" ""); - for (cookie = req->cookies; cookie; cookie = cookie->next) { - printf("[%s] = [%s]
", cookie->item.name, cookie->item.data); + for (current = req->cookies; current; current = current->next) { + magi_response_add_format(res, "[%s] = [%s]
", current->item.name, + current->item.data); } - magi_response_content(res, ""); + magi_response_add(res, ""); - magi_response_cookie_build(res, "cookie", "monstre", 0, 0, 0); + magi_response_cookie_easy(res, "cookie", "monster"); + magi_response_cookie_delete(res, "monster"); } int main(int argc, char const * argv[]) { struct magi_request request; - if (magi_cgi(&request, 0, 0, 0)) { + magi_request_setup(&request); + if (magi_request_cgi(&request)) { struct magi_response response; response_request(&request, &response); - magi_cgi_response(&response); + magi_response_cgi(&response); magi_response_destroy(); } else { - magi_cgi_error(request.error); + magi_error_cgi(request.error); } magi_request_destroy(&request); return 0; diff --git a/examples/echo.c b/examples/echo.c index 16e9d56..95219fc 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -9,94 +10,75 @@ void proceed_cookies(struct magi_cookie_list * cookies, struct magi_response * response) { - magi_response_content(response, "

Cookies:

"); + magi_response_add(response, "

Cookies:

"); while (cookies) { - magi_response_content(response, "Cookie with name ["); - magi_response_content(response, cookies->item.name); + magi_response_add(response, "Cookie with name ["); + magi_response_add(response, cookies->item.name); if (cookies->item.data) { - magi_response_content(response, "] is ["); - magi_response_content(response, cookies->item.data); + magi_response_add(response, "] is ["); + magi_response_add(response, cookies->item.data); } if (cookies->item.domain) { - magi_response_content(response, "] for domain ["); - magi_response_content(response, cookies->item.domain); + magi_response_add(response, "] for domain ["); + magi_response_add(response, cookies->item.domain); } if (cookies->item.port) { - magi_response_content(response, "] for port ["); - magi_response_content(response, cookies->item.port); + magi_response_add(response, "] for port ["); + magi_response_add(response, cookies->item.port); } if (cookies->item.path) { - magi_response_content(response, "] for path ["); - magi_response_content(response, cookies->item.path); + magi_response_add(response, "] for path ["); + magi_response_add(response, cookies->item.path); } - magi_response_content(response, "]
"); + magi_response_add(response, "]
"); cookies = cookies->next; } - magi_response_content(response, "
"); -} - -void proceed_fields(struct magi_field_list * fields, - struct magi_response * response) -{ - magi_response_content(response, "

Feilds:

"); - while (fields) { - magi_response_content(response, "Feild ["); - magi_response_content(response, fields->item.name); - magi_response_content(response, "] = ["); - magi_response_content(response, fields->item.data); - magi_response_content(response, "]
"); - fields = fields->next; - } - magi_response_content(response, "
"); + magi_response_add(response, "
"); } void proceed_params(struct magi_param_list * params, struct magi_response * response) { - magi_response_content(response, "

HTTP Parameters:

"); while (params) { - magi_response_content(response, "Param ["); - magi_response_content(response, params->item.name); - magi_response_content(response, "] is ["); - magi_response_content(response, params->item.data); - magi_response_content(response, "]
"); + magi_response_add_format(response, "[%s] is [%s]
", + params->item.name, params->item.data); params = params->next; } - magi_response_content(response, "
"); + magi_response_add(response, "
"); } void process_meta(struct magi_request * req, struct magi_response * res) { - magi_response_content(res, "

Echo CGI Script

" - "I was called with method ["); - magi_response_content(res, req->method); + magi_response_add(res, + "

Echo CGI Script

I was called with method [%s", + req->method); if (req->uri) { - magi_response_content(res, "] with URL ["); - magi_response_content(res, req->uri); + magi_response_add(res, "] with URL ["); + magi_response_add(res, req->uri); } if (req->server_name) { - magi_response_content(res, "] for server ["); - magi_response_content(res, req->server_name); + magi_response_add(res, "] for server ["); + magi_response_add(res, req->server_name); } if (req->server_port) { - magi_response_content(res, "] on port ["); - magi_response_content(res, req->server_port); + magi_response_add(res, "] on port ["); + magi_response_add(res, req->server_port); } if (req->server_protocol) { - magi_response_content(res, "] with protocol ["); - magi_response_content(res, req->server_protocol); + magi_response_add(res, "] with protocol ["); + magi_response_add(res, req->server_protocol); } if (req->server_software) { - magi_response_content(res, "] and I am running on software ["); - magi_response_content(res, req->server_software); + magi_response_add(res, "] and I am running on software ["); + magi_response_add(res, req->server_software); } - magi_response_content(res, "]
"); + magi_response_add(res, "]
"); } void response_request(struct magi_request * req, struct magi_response * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content( + magi_response_add( res, "" "" @@ -104,21 +86,26 @@ void response_request(struct magi_request * req, struct magi_response * res) ""); process_meta(req, res); proceed_cookies(req->cookies, res); - proceed_fields(req->fields, res); + magi_response_add(res, "

Parameters:

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

URL Parameters:

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

HTTP Parameters:

"); proceed_params(req->http_params, res); - magi_response_content(res, ""); + magi_response_add(res, ""); } int main(int argc, char const * argv[]) { struct magi_request request; - if (magi_cgi(&request, 0, 0, 0)) { + magi_request_setup(&request); + if (magi_request_cgi(&request)) { struct magi_response response; response_request(&request, &response); - magi_cgi_response(&response); - magi_response_destroy(); + magi_response_cgi(&response); + magi_response_destroy(&response); } else { - magi_cgi_error(request.error); + magi_error_cgi(request.error); } magi_request_destroy(&request); return 0; diff --git a/examples/fcgi.c b/examples/fcgi.c index d6cc478..72fe83e 100644 --- a/examples/fcgi.c +++ b/examples/fcgi.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -9,7 +10,7 @@ void response_request(struct magi_request * req, struct magi_resopnse * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content( + magi_response_add( res, "" "" @@ -23,16 +24,17 @@ 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_fcgi(&session, sock)) { + if (magi_session_fcgi(&session, sock)) { struct magi_request request; - while (magi_fcgi_accept(&request, &session)) { + magi_request_setup(&request); + while (magi_request_fcgi(&request, &session)) { if (!request.error) { struct magi_response response; response_request(&request, &response); - magi_fcgi_response(&response, &session); + magi_response_fcgi(&response, &session); magi_response_destroy(&response); } else { - magi_fcgi_error(request.error, &session); + magi_error_fcgi(request.error, &session); } magi_request_destroy(&request); } diff --git a/examples/upload.c b/examples/upload.c index 2ef8bc5..25fefaf 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -1,15 +1,17 @@ #include #include #include +#include #include #include #include void tempfile_callback(struct magi_file * file, - char * buffer, - int len, - void * _) + char * addon, + int addon_len, + int is_addon_last, + void * unused) { if (!strcmp(file->param_name, "data")) { static FILE * f = 0; @@ -17,10 +19,10 @@ void tempfile_callback(struct magi_file * file, remove(file->param_name); f = fopen(file->param_name, "wb"); } - if (len) { - fwrite(buffer, 1, len, f); + if (addon_len) { + fwrite(addon, 1, addon_len, f); } - if (len < magi_file_callback_portion_max) { + if (is_addon_last) { fclose(f); f = 0; } @@ -30,7 +32,7 @@ void tempfile_callback(struct magi_file * file, void response_request(struct magi_request * req, struct magi_response * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content( + magi_response_add( res, "" "" @@ -45,8 +47,8 @@ void response_request(struct magi_request * req, struct magi_response * res) "" ""); - struct magi_field * name = magi_field_list_get(req->fields, "name"); - struct magi_field * data = magi_field_list_get(req->fields, "data"); + struct magi_param * name = magi_param_list_get(req->params, "name"); + struct magi_param * data = magi_param_list_get(req->params, "data"); if (name && name->data && data) { rename("data", name->data); } @@ -55,13 +57,15 @@ void response_request(struct magi_request * req, struct magi_response * res) int main(int argc, char const * argv[]) { struct magi_request request; - if (magi_cgi(&request, tempfile_callback, 0, 0)) { + magi_request_setup(&request); + request.file_callback = tempfile_callback; + if (magi_request_cgi(&request)) { struct magi_response response; response_request(&request, &response); - magi_cgi_response(&response); + magi_response_cgi(&response); magi_response_destroy(); } else { - magi_cgi_error(request.error); + magi_error_cgi(request.error); } magi_request_destroy(&request); return 0; -- cgit v1.2.3