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 9dbcf5399f2d1f17d66194c5bc43e5d6350fc4df
parent 4010bdd58dcb23b01a7835f4ce68265aa1c5997d
Author: Joris Vink <joris@coders.se>
Date:   Mon,  4 Aug 2014 20:06:59 +0200

Add headers example

Diffstat:
examples/headers/.gitignore | 5+++++
examples/headers/README.md | 13+++++++++++++
examples/headers/conf/headers.conf | 12++++++++++++
examples/headers/src/headers.c | 29+++++++++++++++++++++++++++++
4 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/examples/headers/.gitignore b/examples/headers/.gitignore @@ -0,0 +1,5 @@ +*.o +.objs +headers.so +assets.h +cert diff --git a/examples/headers/README.md b/examples/headers/README.md @@ -0,0 +1,13 @@ +Example on how to read HTTP request headers and set your own custom ones. + +Run: +``` + # kore run +``` + +Test: +``` + # curl -H "X-Custom-Header: testing" -i -k https://127.0.0.1:8888 +``` + +If X-Custom-Header is given, it will be mirrored in the response. diff --git a/examples/headers/conf/headers.conf b/examples/headers/conf/headers.conf @@ -0,0 +1,12 @@ +# Placeholder configuration + +bind 127.0.0.1 8888 +pidfile kore.pid +ssl_no_compression +load ./headers.so + +domain 127.0.0.1 { + certfile cert/server.crt + certkey cert/server.key + static / page +} diff --git a/examples/headers/src/headers.c b/examples/headers/src/headers.c @@ -0,0 +1,29 @@ +#include <kore/kore.h> +#include <kore/http.h> + +int page(struct http_request *); + +int +page(struct http_request *req) +{ + char *custom; + + /* + * We'll lookup if the X-Custom-Header is given in the request. + * If it is we'll set it as a response header as well. + * + * The value returned by http_request_header_get() must be freed. + * + * NOTE: All custom headers you set must be in lower case due to + * the SPDYv3 specification requiring this. + */ + if (http_request_header_get(req, "x-custom-header", &custom)) { + http_response_header_add(req, "x-custom-header", custom); + kore_mem_free(custom); + } + + /* Return 200 with "ok\n" to the client. */ + http_response(req, 200, "ok\n", 3); + + return (KORE_RESULT_OK); +}