From 07657b8a9f5c2fd9047594ec8604b9c439a999e4 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Sat, 4 Apr 2020 19:39:38 +0300 Subject: [magi] Finalization. --- examples/echo.c | 96 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) (limited to 'examples/echo.c') diff --git a/examples/echo.c b/examples/echo.c index 7cb4a3b..4f6975a 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -1,111 +1,111 @@ +/* This is useful example echoing request data in response. + */ #include void list_cookies(magi_request *r) { magi_cookies *current = r->cookies; - magi_response(r, "

"); + printf("

"); + /* Iterate through all cookies in request to show them in body: */ for (current = r->cookies; current; current = current->next) { magi_cookie *c = ¤t->item; - magi_response(r, "Cookie with name ["); - magi_response(r, c->name); + printf("Cookie with name [%s", c->name); if (c->data) { - magi_response(r, "] is ["); - magi_response(r, c->data); + printf("] is [%s", c->data); } if (c->domain) { - magi_response(r, "] for domain ["); - magi_response(r, c->domain); + printf("] for domain [%s", c->domain); } if (c->path) { - magi_response(r, "] for path ["); - magi_response(r, c->path); + printf("] for path [%s", c->path); } - magi_response(r, "]
"); + printf("]
"); } - magi_response(r, "

"); + printf("

"); } -void list_params(magi_request *r, magi_params *current) +void list_params(magi_params *current) { - magi_response(r, "

"); + printf("

"); + /* Iterate through specified params to show them in body: */ for (; current; current = current->next) { magi_param *p = ¤t->item; - magi_response_format(r, "[%s] is [%s]
", p->name, p->data); + printf("[%s] is [%s]
", p->name, p->data); } - magi_response(r, "

"); + printf("

"); } void list_files(magi_request *r) { magi_files *current; - magi_response(r, "

"); + printf("

"); + /* Iterate through all field files in request to show them in body: */ for (current = r->files; current; current = current->next) { magi_file *f = ¤t->item; - magi_response_format(r, "[%s] was [%s] on clientside
", - f->field, f->filename); + printf("[%s] was [%s] on clientside
", f->field, f->filename); } - magi_response(r, "

"); + printf("

"); } void show_meta(magi_request *r) { - magi_response(r, "

I was called "); + printf("

I was called "); if (r->is_secure) { - magi_response(r, "securely "); + printf("securely "); } - magi_response_format(r, "with method [%s", r->method); + printf("with method [%s", r->method); if (r->host) { - magi_response_format(r, "] on server [%s", r->host); + printf("] on server [%s", r->host); } if (r->script) { - magi_response_format(r, "] being script on [%s", r->script); + printf("] being script on [%s", r->script); } if (r->path) { - magi_response_format(r, "] with requested path [%s", r->path); + printf("] with requested path [%s", r->path); } - magi_response(r, "]

"); + printf("]

"); } void response(magi_request *r) { - magi_response(r, - "" - "" - "Echo" - ""); + magi_response_default(); /* Pass default headers and send body: */ + printf("" + "" + "Echo" + ""); - magi_response(r, "

Echo CGI Script

"); + printf("

Echo CGI Script

"); show_meta(r); - magi_response(r, "

Cookies:

"); + printf("

Cookies:

"); list_cookies(r); - magi_response(r, "

Parameters:

"); - list_params(r, r->meta); + printf("

Parameters:

"); + list_params(r->meta); - magi_response(r, "

URL Parameters:

"); - list_params(r, r->head); + printf("

URL Parameters:

"); + list_params(r->head); - magi_response(r, "

Body Parameters:

"); - list_params(r, r->body); + printf("

Body Parameters:

"); + list_params(r->body); - magi_response(r, "

Files:

"); + printf("

Files:

"); list_files(r); - magi_response(r, ""); + printf(""); } int main() { magi_request request; - magi_request_init(&request); - if (magi_cgi(&request)) { - response(&request); - } else { - magi_response_error(&request); + magi_request_init(&request); /* Setting defaults. */ + if (magi_parse(&request)) { /* If parsing was done successful */ + response(&request); /* we need to response the request. */ + } else { /* And display error overwise: */ + magi_error_response(request.error); } - magi_request_free(&request); + magi_request_free(&request); /* Don't forget to free everything after. */ return 0; } -- cgit v1.2.3