diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2019-11-13 20:55:11 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2019-11-13 20:55:11 +0300 |
commit | ca08ed93e67a99868f01d21f0d2e34d6a6757c75 (patch) | |
tree | 0475bec225b561f018b3d7b3d2961266c9d5b191 /examples/cookie.c | |
parent | 23f3f8592c21ee58b0ac040736b5b766d52de193 (diff) | |
download | magi-ca08ed93e67a99868f01d21f0d2e34d6a6757c75.tar magi-ca08ed93e67a99868f01d21f0d2e34d6a6757c75.tar.xz magi-ca08ed93e67a99868f01d21f0d2e34d6a6757c75.zip |
[magi]
Diffstat (limited to 'examples/cookie.c')
-rw-r--r-- | examples/cookie.c | 55 |
1 files changed, 24 insertions, 31 deletions
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 <stdlib.h> -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("<!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>"); -} + magi_response_content_type(res, magi_xhtml); + magi_response_content(res, + "<!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>"); -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]<br/>", cookie->item.name, cookie->item.data); - } - magi_request_destroy(&request); + for (cookie = req->cookies; cookie; cookie = cookie->next) { + printf("[%s] = [%s]<br/>", cookie->item.name, cookie->item.data); } -} -void print_webpage_bottom() -{ - puts("</body>" - "</html>"); + magi_response_content(res, "</body></html>"); + + 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; } |