cookies.c (2047B)
1 /*
2 * Copyright (c) 2017 Stanislav Yudin <stan@endlessinsomnia.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <kore/kore.h>
18 #include <kore/http.h>
19
20 static char *html = "<html><body><h1>Reload this page</h1></body></html>";
21
22 int serve_cookies(struct http_request *);
23
24 int
25 serve_cookies(struct http_request *req)
26 {
27 char *value;
28 struct http_cookie *cookie;
29
30 http_populate_cookies(req);
31
32 if (http_request_cookie(req, "Simple", &value))
33 kore_log(LOG_DEBUG, "Got simple: %s", value);
34 if (http_request_cookie(req, "Complex", &value))
35 kore_log(LOG_DEBUG, "Got complex: %s", value);
36 if (http_request_cookie(req, "Formatted", &value))
37 kore_log(LOG_DEBUG, "Got formatted: %s", value);
38
39 /* no expire, no maxage for current path. */
40 http_response_cookie(req, "Simple", "Hello World!",
41 req->path, 0, 0, NULL);
42
43 /* expire, no maxage, for /secure. */
44 http_response_cookie(req, "Complex", "Secure Value!", "/secure",
45 time(NULL) + (1 * 60 * 60), 0, NULL);
46
47 /* maxage, no httponly, for current path. */
48 http_response_cookie(req, "key", "value", req->path, 0, 60, &cookie);
49 cookie->flags &= ~HTTP_COOKIE_HTTPONLY;
50
51 /* set formatted cookie via header directly. */
52 http_response_header(req, "set-cookie",
53 "Formatted=TheValue; Path=/vault; HttpOnly");
54
55 http_response(req, 200, html, strlen(html));
56
57 return (KORE_RESULT_OK);
58 }