From 32c1fbd2cf779a65e807d0d82412ffffa2401962 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 15 Nov 2019 18:01:45 +0300 Subject: [magi] --- examples/append.c | 64 +++++++++++++++++++++++++----------------------------- examples/cookie.c | 12 +++++----- examples/echo.c | 29 ++++++++++++------------- examples/fcgi.c | 21 +++++++++--------- examples/upload.c | 65 +++++++++++++++++++++++++------------------------------ 5 files changed, 91 insertions(+), 100 deletions(-) (limited to 'examples') diff --git a/examples/append.c b/examples/append.c index 0c7df7e..e51745a 100644 --- a/examples/append.c +++ b/examples/append.c @@ -4,46 +4,42 @@ #include -void handle_request() +void response_request(struct magi_request * req, struct magi_response * res) +{ + magi_response_content_type(res, magi_xhtml); + magi_response_content( + res, + "" + "" + "Append to File" + "" + "
" + "" + "" + "
" + "" + ""); + + struct magi_field * a = magi_field_list_get(req->fields, "addon"); + if (a && a->data) { + FILE * file = fopen("file_to_append", "a"); + fputs(a->data, file); + fclose(file); + } +} + +int main(int argc, char const * argv[]) { struct magi_request request; if (magi_cgi(&request, 0, 0)) { - struct magi_field * a = magi_field_list_get(request.fields, "addon"); - if (a && a->data) { - FILE * file = fopen("file_to_append", "a"); - fputs(a->data, file); - fclose(file); - } + struct magi_response response; + response_request(&request, &response); + magi_cgi_response(&response); + magi_response_destroy(); } else { magi_cgi_error(request.error); } magi_request_destroy(&request); -} - -void print_preamble() -{ - puts("Content-type: application/xhtml+xml\r\n\r\n"); -} - -void print_webpage() -{ - puts("" - "" - "Append to File" - "" - "
" - "" - "" - "
" - "" - ""); -} - -int main(int argc, char const * argv[]) -{ - handle_request(); - print_preamble(); - print_webpage(); return 0; } diff --git a/examples/cookie.c b/examples/cookie.c index a2b9425..47929cb 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -11,12 +11,12 @@ void response_request(struct magi_request * req, struct magi_response * res) struct magi_cookie_list * cookie; magi_response_content_type(res, magi_xhtml); - magi_response_content(res, - "" - "" - "Cookie Listing and Setting" - ""); + magi_response_content( + res, "" + "" + "Cookie Listing and Setting" + ""); for (cookie = req->cookies; cookie; cookie = cookie->next) { printf("[%s] = [%s]
", cookie->item.name, cookie->item.data); diff --git a/examples/echo.c b/examples/echo.c index 853e6ce..6901b71 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -6,8 +6,8 @@ #include -void proceed_cookies( - struct magi_cookie_list * cookies, struct magi_response * response) +void proceed_cookies(struct magi_cookie_list * cookies, + struct magi_response * response) { magi_response_content(response, "

Cookies:

"); while (cookies) { @@ -35,8 +35,8 @@ void proceed_cookies( magi_response_content(response, "
"); } -void proceed_fields( - struct magi_field_list * fields, struct magi_response * response) +void proceed_fields(struct magi_field_list * fields, + struct magi_response * response) { magi_response_content(response, "

Feilds:

"); while (fields) { @@ -50,8 +50,8 @@ void proceed_fields( magi_response_content(response, "
"); } -void proceed_params( - struct magi_param_list * params, struct magi_response * response) +void proceed_params(struct magi_param_list * params, + struct magi_response * response) { magi_response_content(response, "

HTTP Parameters:

"); while (params) { @@ -67,9 +67,8 @@ void proceed_params( 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, "

Echo CGI Script

" + "I was called with method ["); magi_response_content(res, req->method); if (req->uri) { magi_response_content(res, "] with URL ["); @@ -97,12 +96,12 @@ void process_meta(struct magi_request * req, struct magi_response * res) void response_request(struct magi_request * req, struct magi_response * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content(res, - "" - "" - "Echo" - ""); + magi_response_content( + res, "" + "" + "Echo" + ""); process_meta(req, res); proceed_cookies(req->cookies, res); proceed_fields(req->fields, res); diff --git a/examples/fcgi.c b/examples/fcgi.c index 5972cdd..9cdbe90 100644 --- a/examples/fcgi.c +++ b/examples/fcgi.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -8,13 +9,13 @@ void response_request(struct magi_request * req, struct magi_resopnse * res) { magi_response_content_type(res, magi_xhtml); - magi_response_content(res, - "" - "" - "Fast CGI" - "Hi!" - ""); + magi_response_content( + res, "" + "" + "Fast CGI" + "Hi!" + ""); } int main(int argc, char const * argv[]) @@ -22,7 +23,7 @@ int main(int argc, char const * argv[]) struct magi_session session; if (magi_fcgi(&session)) { struct magi_request request; - while (magi_accept(&request, &session)) { + while (magi_fcgi_accept(&request, &session)) { if (!request.error) { struct magi_response response; response_request(&request, &response); @@ -33,8 +34,8 @@ int main(int argc, char const * argv[]) } magi_request_destroy(&request); } - /* Fast CGI session error */ } - /* Fast CGI session error */ + puts(session.error->message); + magi_session_destroy(&session); return 0; } diff --git a/examples/upload.c b/examples/upload.c index 06a3cf9..5fe3338 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -29,47 +29,42 @@ void tempfile_callback(struct magi_field * field, char * buffer, int len) } } -void handle_request() +void response_request(struct magi_request * req, struct magi_response * res) +{ + magi_response_content_type(res, magi_xhtml); + magi_response_content( + res, "" + "" + "Upload File" + "" + "
" + "" + "" + "" + "
" + "" + ""); + + struct magi_field * name = magi_field_list_get(req->fields, "name"); + struct magi_field * data = magi_field_list_get(req->fields, "data"); + if (name && name->data && data) { + rename("data", name->data); + } +} + +int main(int argc, char const * argv[]) { struct magi_request request; if (magi_cgi(&request, tempfile_callback, 0)) { - struct magi_field * name = magi_field_list_get(request.fields, "name"); - struct magi_field * data = magi_field_list_get(request.fields, "data"); - if (name && name->data && data) { - rename("data", name->data); - } + struct magi_response response; + response_request(&request, &response); + magi_cgi_response(&response); + magi_response_destroy(); } else { magi_cgi_error(request.error); } magi_request_destroy(&request); -} - -void print_preamble() -{ - puts("Content-type: application/xhtml+xml\r\n\r\n"); -} - -void print_webpage() -{ - puts("" - "" - "Upload File" - "" - "
" - "" - "" - "" - "
" - "" - ""); -} - -int main(int argc, char const * argv[]) -{ - handle_request(); - print_preamble(); - print_webpage(); return 0; } -- cgit v1.2.3