aboutsummaryrefslogtreecommitdiff
path: root/examples/cookie.c
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-09-13 18:50:34 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-09-13 18:50:34 +0300
commitad6188f911af896c9c77e9215bea3c5c2a4e6cc3 (patch)
tree158b4015ff302f72fe2bb8a0ee3d5441ffb66719 /examples/cookie.c
downloadmagi-ad6188f911af896c9c77e9215bea3c5c2a4e6cc3.tar
magi-ad6188f911af896c9c77e9215bea3c5c2a4e6cc3.tar.xz
magi-ad6188f911af896c9c77e9215bea3c5c2a4e6cc3.zip
Project name and license are added. Minor changes.
Diffstat (limited to 'examples/cookie.c')
-rw-r--r--examples/cookie.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/cookie.c b/examples/cookie.c
new file mode 100644
index 0000000..8f4018b
--- /dev/null
+++ b/examples/cookie.c
@@ -0,0 +1,58 @@
+#include <cookie.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <request.h>
+
+
+void print_preamble()
+{
+ puts(
+ "Set-Cookie:cookie=monstre\r\n" /* Important to set cookies before: */
+ "Content-Type: application/xhtml+xml\r\n\r\n"
+ );
+}
+
+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>"
+ );
+}
+
+void read_and_print_cookies()
+{
+ struct magi_request request;
+ if (magi_request_build_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);
+ }
+}
+
+void print_webpage_bottom()
+{
+ puts(
+ "</body>"
+ "</html>"
+ );
+}
+
+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();
+ return 0;
+}