diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2020-04-04 19:39:38 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2020-04-04 19:39:38 +0300 |
commit | 07657b8a9f5c2fd9047594ec8604b9c439a999e4 (patch) | |
tree | efc15cde781a74f5782c780422dde6ec0dfb1af4 /examples/cookie.c | |
parent | cb6a86b8db5a26b92073c4fc5ceffffa8cd842c2 (diff) | |
download | magi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.tar magi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.tar.xz magi-07657b8a9f5c2fd9047594ec8604b9c439a999e4.zip |
[magi] Finalization.
Diffstat (limited to 'examples/cookie.c')
-rw-r--r-- | examples/cookie.c | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/examples/cookie.c b/examples/cookie.c index e18c100..c64ca0c 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -1,38 +1,46 @@ +/* Basic example of cookie handling through response headers. + * It lists all recieved cookies and set one via magi_response, + * showing work with headers in magi. + */ #include <magi.h> void list_cookies(magi_request *r) { magi_cookies *current; - magi_response(r, "Cookies:"); + printf("Cookies:"); + /* 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_format(r, "<br />[%s]=[%s]", c->name, c->data); + printf("<br />[%s]=[%s]", c->name, c->data); } } void response(magi_request *r) { - magi_response_cookie(r, "cookie", "monster"); - magi_response(r, - "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' " - "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>" - "<html xmlns='http://www.w3.org/1999/xhtml'>" - "<head><title>Cookie Listing and Setting</title></head>" - "<body><p>"); + magi_response head; + magi_response_init(&head); /* Setting defaults. */ + /* Set cookie "cookie" with value "monster" on clientside: */ + magi_response_cookie(&head, "cookie", "monster"); + magi_response_free(&head); /* Send headers and start sending body: */ + printf("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' " + "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>" + "<html xmlns='http://www.w3.org/1999/xhtml'>" + "<head><title>Cookie Listing and Setting</title></head>" + "<body><p>"); list_cookies(r); - magi_response(r, "</p></body></html>"); + printf("</p></body></html>"); } 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; } |