kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 4e556dbd8f17d24a94792a8b809949115b0182ab
parent 1296802e06d15a9b78b35c9f1133b1f4fe366c3d
Author: Joris Vink <joris@coders.se>
Date:   Tue,  7 Feb 2017 22:06:31 +0100

rename example cookies source file.

Diffstat:
examples/cookies/src/cookies.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
examples/cookies/src/example.c | 62--------------------------------------------------------------
2 files changed, 62 insertions(+), 62 deletions(-)

diff --git a/examples/cookies/src/cookies.c b/examples/cookies/src/cookies.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2017 Stanislav Yudin <stan@endlessinsomnia.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <kore/kore.h> +#include <kore/http.h> + +#include <openssl/sha.h> + +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + +static char *html = "<html><body><h1>Reload this page</h1></body></html>"; + +int serve_cookies(struct http_request *); + +int +serve_cookies(struct http_request *req) +{ + char *read; + struct http_cookie *cookie; + + http_populate_cookies(req); + + if (http_request_cookie(req, "Simple", &read)) + kore_log(LOG_DEBUG, "Got simple: %s", read); + if (http_request_cookie(req, "Complex", &read)) + kore_log(LOG_DEBUG, "Got complex: %s", read); + if (http_request_cookie(req, "Formatted", &read)) + kore_log(LOG_DEBUG, "Got formatted: %s", read); + + /* set simple cookie */ + http_response_cookie(req, "Simple", "Hello World!", 0); + + /* set complex cookie */ + cookie = http_response_cookie(req, "Complex", "Secure Value!", + HTTP_COOKIE_HTTPONLY | HTTP_COOKIE_SECURE); + cookie ->path = kore_strdup("/secure"); + cookie->expires = time(NULL) + 1 * 60 * 60; + cookie->domain = kore_strdup(req->host); + + /* set formatted cookie */ + http_response_header(req, "set-cookie", + "Formatted=TheValue; Path=/vault; HttpOnly"); + + http_response(req, 200, html, strlen(html)); + + return (KORE_RESULT_OK); +} diff --git a/examples/cookies/src/example.c b/examples/cookies/src/example.c @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2017 Stanislav Yudin <stan@endlessinsomnia.com> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <kore/kore.h> -#include <kore/http.h> - -#include <openssl/sha.h> - -#include <fcntl.h> -#include <stdlib.h> -#include <unistd.h> - -static char *html = "<html><body><h1>Reload this page</h1></body></html>"; - -int serve_cookies(struct http_request *); - -int -serve_cookies(struct http_request *req) -{ - char *read; - struct http_cookie *cookie; - - http_populate_cookies(req); - - if (http_request_cookie(req, "Simple", &read)) - kore_log(LOG_DEBUG, "Got simple: %s", read); - if (http_request_cookie(req, "Complex", &read)) - kore_log(LOG_DEBUG, "Got complex: %s", read); - if (http_request_cookie(req, "Formatted", &read)) - kore_log(LOG_DEBUG, "Got formatted: %s", read); - - /* set simple cookie */ - http_response_cookie(req, "Simple", "Hello World!", 0); - - /* set complex cookie */ - cookie = http_response_cookie(req, "Complex", "Secure Value!", - HTTP_COOKIE_HTTPONLY | HTTP_COOKIE_SECURE); - cookie ->path = kore_strdup("/secure"); - cookie->expires = time(NULL) + 1 * 60 * 60; - cookie->domain = kore_strdup(req->host); - - /* set formatted cookie */ - http_response_header(req, "set-cookie", - "Formatted=TheValue; Path=/vault; HttpOnly"); - - http_response(req, 200, html, strlen(html)); - - return (KORE_RESULT_OK); -}