From b08db17162fddda97e6ee1ac625eae1430d22b64 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 29 Nov 2019 18:50:04 +0300 Subject: [magi] Almost done! --- examples/append.c | 17 +++++++++-------- examples/cookie.c | 4 ++-- examples/echo.c | 19 +++++++++++++++++-- examples/fcgi.c | 1 + examples/upload.c | 43 ++++++++++++------------------------------- 5 files changed, 41 insertions(+), 43 deletions(-) (limited to 'examples') diff --git a/examples/append.c b/examples/append.c index 3320fe0..e6e4a8a 100644 --- a/examples/append.c +++ b/examples/append.c @@ -7,6 +7,13 @@ void response_request(struct magi_request * req, struct magi_response * res) { + char * data = magi_param_list_get(req->params, "addon"); + if (data) { + FILE * file = fopen("file_to_append", "a"); + fputs(data, file); + fclose(file); + } + magi_response_content_type(res, magi_xhtml); magi_response_add( res, @@ -15,19 +22,12 @@ void response_request(struct magi_request * req, struct magi_response * res) "" "Append to File" "" - "
" + "
" "" "" "
" "" ""); - - struct magi_param * addon = magi_param_list_get(req->url_params, "addon"); - if (addon && addon->data) { - FILE * file = fopen("file_to_append", "a"); - fputs(addon->data, file); - fclose(file); - } } int main(int argc, char const * argv[]) @@ -36,6 +36,7 @@ int main(int argc, char const * argv[]) magi_request_setup(&request); if (magi_request_cgi(&request)) { struct magi_response response; + magi_response_setup(&response); response_request(&request, &response); magi_response_cgi(&response); magi_response_destroy(&response); diff --git a/examples/cookie.c b/examples/cookie.c index 01d6dea..f21cb4a 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -26,7 +26,6 @@ void response_request(struct magi_request * req, struct magi_response * res) magi_response_add(res, ""); magi_response_cookie_easy(res, "cookie", "monster"); - magi_response_cookie_delete(res, "monster"); } int main(int argc, char const * argv[]) @@ -35,9 +34,10 @@ int main(int argc, char const * argv[]) magi_request_setup(&request); if (magi_request_cgi(&request)) { struct magi_response response; + magi_response_setup(&response); response_request(&request, &response); magi_response_cgi(&response); - magi_response_destroy(); + magi_response_destroy(&response); } else { magi_error_cgi(request.error); } diff --git a/examples/echo.c b/examples/echo.c index 95219fc..3f72f8a 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -47,11 +47,23 @@ void proceed_params(struct magi_param_list * params, magi_response_add(response, "
"); } +void proceed_files(struct magi_file_list * files, + struct magi_response * response) +{ + while (files) { + magi_response_add_format(response, "[%s] was [%s] on userside
", + files->item.param_name, + files->item.file_name); + files = files->next; + } + magi_response_add(response, "
"); +} + void process_meta(struct magi_request * req, struct magi_response * res) { magi_response_add(res, - "

Echo CGI Script

I was called with method [%s", - req->method); + "

Echo CGI Script

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); @@ -92,6 +104,8 @@ void response_request(struct magi_request * req, struct magi_response * res) proceed_params(req->url_params, res); magi_response_add(res, "

HTTP Parameters:

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

Files:

"); + proceed_files(req->files, res); magi_response_add(res, ""); } @@ -101,6 +115,7 @@ int main(int argc, char const * argv[]) magi_request_setup(&request); if (magi_request_cgi(&request)) { struct magi_response response; + magi_response_setup(&response); response_request(&request, &response); magi_response_cgi(&response); magi_response_destroy(&response); diff --git a/examples/fcgi.c b/examples/fcgi.c index 7d93614..06384c4 100644 --- a/examples/fcgi.c +++ b/examples/fcgi.c @@ -30,6 +30,7 @@ int main(int argc, char const * argv[]) while (magi_request_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); diff --git a/examples/upload.c b/examples/upload.c index 25fefaf..f4350ee 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -7,30 +7,14 @@ #include -void tempfile_callback(struct magi_file * file, - char * addon, - int addon_len, - int is_addon_last, - void * unused) +void response_request(struct magi_request * req, struct magi_response * res) { - if (!strcmp(file->param_name, "data")) { - static FILE * f = 0; - if (!f) { - remove(file->param_name); - f = fopen(file->param_name, "wb"); - } - if (addon_len) { - fwrite(addon, 1, addon_len, f); - } - if (is_addon_last) { - fclose(f); - f = 0; - } + char * name = magi_param_list_get(req->params, "name"); + struct magi_file * data = magi_file_list_get(req->files, "data"); + if (name && data) { + rename("data", name); } -} -void response_request(struct magi_request * req, struct magi_response * res) -{ magi_response_content_type(res, magi_xhtml); magi_response_add( res, "" "Upload File" "" - "
" "" "" @@ -46,24 +30,21 @@ void response_request(struct magi_request * req, struct magi_response * res) "
" "" ""); - - 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); - } } int main(int argc, char const * argv[]) { - struct magi_request request; + struct magi_request request; + struct magi_tempfiles tmps = { 0, 0, 0, 0 }; magi_request_setup(&request); - request.file_callback = tempfile_callback; + magi_tempfiles_add(&tmps, "data", "data", 0); + magi_request_setup_tempfiles(&request, &tmps); if (magi_request_cgi(&request)) { struct magi_response response; + magi_response_setup(&response); response_request(&request, &response); magi_response_cgi(&response); - magi_response_destroy(); + magi_response_destroy(&response); } else { magi_error_cgi(request.error); } -- cgit v1.2.3