aboutsummaryrefslogtreecommitdiff
path: root/examples/cookie.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cookie.c')
-rw-r--r--examples/cookie.c40
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 = &current->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;
}