From ca08ed93e67a99868f01d21f0d2e34d6a6757c75 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Wed, 13 Nov 2019 20:55:11 +0300 Subject: [magi] --- examples/append.c | 4 +- examples/cookie.c | 55 +++++++++---------- examples/echo.c | 157 ++++++++++++++++++++++++++++-------------------------- examples/fcgi.c | 40 ++++++++++++++ examples/upload.c | 5 +- 5 files changed, 152 insertions(+), 109 deletions(-) create mode 100644 examples/fcgi.c (limited to 'examples') diff --git a/examples/append.c b/examples/append.c index b6ee217..0c7df7e 100644 --- a/examples/append.c +++ b/examples/append.c @@ -14,8 +14,10 @@ void handle_request() fputs(a->data, file); fclose(file); } - magi_request_destroy(&request); + } else { + magi_cgi_error(request.error); } + magi_request_destroy(&request); } void print_preamble() diff --git a/examples/cookie.c b/examples/cookie.c index 5ef56ac..6ba5bd0 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -5,45 +5,38 @@ #include -void print_preamble() +void response_request(struct magi_request * req, struct magi_response * res) { - puts("Set-Cookie:cookie=monstre\r\n" /* Important to set cookies before: */ - "Content-Type: application/xhtml+xml\r\n\r\n"); -} + struct magi_cookie_list * cookie; -void print_webpage_top() -{ - puts("" - "" - "Cookie Listing and Setting" - ""); -} + magi_response_content_type(res, magi_xhtml); + magi_response_content(res, + "" + "" + "Cookie Listing and Setting" + ""); -void read_and_print_cookies() -{ - struct magi_request request; - if (magi_cgi(&request, 0, 0)) { - struct magi_cookie_list * cookie; - for (cookie = request.cookies; cookie; cookie = cookie->next) { - printf("[%s] = [%s]
", cookie->item.name, cookie->item.data); - } - magi_request_destroy(&request); + for (cookie = req->cookies; cookie; cookie = cookie->next) { + printf("[%s] = [%s]
", cookie->item.name, cookie->item.data); } -} -void print_webpage_bottom() -{ - puts("" - ""); + magi_response_content(res, ""); + + magi_response_cookie_build(res, "cookie", "monstre", 0, 0, 0); } int main(int argc, char const * argv[]) { - print_preamble(); - /* Following probably will be much more pleasant with use of templates. */ - print_webpage_top(); - read_and_print_cookies(); - print_webpage_bottom(); + struct magi_request request; + if (magi_cgi(&request, 0, 0)) { + 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); return 0; } diff --git a/examples/echo.c b/examples/echo.c index 0345b8c..853e6ce 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -6,116 +6,121 @@ #include -void print_preamble() +void proceed_cookies( + struct magi_cookie_list * cookies, struct magi_response * response) { - puts("Content-type: application/xhtml+xml\r\n\r\n" - "" - "" - "Echo" - ""); -} - -void proceed_cookies(struct magi_cookie_list * cookies) -{ - puts("

Cookies:

"); + magi_response_content(response, "

Cookies:

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

Feilds:

"); + magi_response_content(response, "

Feilds:

"); while (fields) { - puts("Feild ["); - puts(fields->item.name); - puts("] = ["); - puts(fields->item.data); - puts("]
"); + 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; } - puts("
"); + magi_response_content(response, "
"); } -void proceed_params(struct magi_param_list * params) +void proceed_params( + struct magi_param_list * params, struct magi_response * response) { - puts("

HTTP Parameters:

"); + magi_response_content(response, "

HTTP Parameters:

"); while (params) { - puts("Param ["); - puts(params->item.name); - puts("] is ["); - puts(params->item.data); - puts("]
"); + 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, "]
"); params = params->next; } - puts("
"); + magi_response_content(response, "
"); } -void handle_request() +void process_meta(struct magi_request * req, struct magi_response * res) { - struct magi_request request; - if (magi_cgi(&request, 0, 0)) { - puts("

Echo CGI Script

"); - puts("I was called with method ["); - puts(request.method); - if (request.uri) { - puts("] with URL ["); - puts(request.uri); - } - if (request.server_name) { - puts("] for server ["); - puts(request.server_name); - } - if (request.server_port) { - puts("] on port ["); - puts(request.server_port); - } - if (request.server_protocol) { - puts("] with protocol ["); - puts(request.server_protocol); - } - if (request.server_software) { - puts("] and I am running on software ["); - puts(request.server_software); - } - puts("]
"); - proceed_cookies(request.cookies); - proceed_fields(request.fields); - proceed_params(request.http_params); - magi_request_destroy(&request); + 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 ["); + magi_response_content(res, req->uri); + } + if (req->server_name) { + magi_response_content(res, "] for server ["); + magi_response_content(res, req->server_name); + } + if (req->server_port) { + magi_response_content(res, "] on port ["); + magi_response_content(res, req->server_port); + } + if (req->server_protocol) { + magi_response_content(res, "] with protocol ["); + magi_response_content(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_content(res, "]
"); } -void print_footer() +void response_request(struct magi_request * req, struct magi_response * res) { - puts(""); + magi_response_content_type(res, magi_xhtml); + magi_response_content(res, + "" + "" + "Echo" + ""); + process_meta(req, res); + proceed_cookies(req->cookies, res); + proceed_fields(req->fields, res); + proceed_params(req->http_params, res); + magi_response_content(res, ""); } int main(int argc, char const * argv[]) { - print_preamble(); - handle_request(); - print_footer(); + struct magi_request request; + if (magi_cgi(&request, 0, 0)) { + 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); return 0; } diff --git a/examples/fcgi.c b/examples/fcgi.c new file mode 100644 index 0000000..5972cdd --- /dev/null +++ b/examples/fcgi.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + + +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!" + ""); +} + +int main(int argc, char const * argv[]) +{ + struct magi_session session; + if (magi_fcgi(&session)) { + struct magi_request request; + while (magi_accept(&request, &session)) { + if (!request.error) { + struct magi_response response; + response_request(&request, &response); + magi_fcgi_response(response); + magi_reponse_destroy(response); + } else { + magi_fcgi_error(request.error, &session); + } + magi_request_destroy(&request); + } + /* Fast CGI session error */ + } + /* Fast CGI session error */ + return 0; +} diff --git a/examples/upload.c b/examples/upload.c index 1cf6f5f..06a3cf9 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -5,6 +5,7 @@ #include #include + void tempfile_callback(struct magi_field * field, char * buffer, int len) { static FILE * file = 0; @@ -37,8 +38,10 @@ void handle_request() if (name && name->data && data) { rename("data", name->data); } - magi_request_destroy(&request); + } else { + magi_cgi_error(request.error); } + magi_request_destroy(&request); } void print_preamble() -- cgit v1.2.3