Add headers example

This commit is contained in:
Joris Vink 2014-08-04 20:06:59 +02:00
parent 4010bdd58d
commit 9dbcf5399f
4 changed files with 59 additions and 0 deletions

5
examples/headers/.gitignore vendored Executable file
View File

@ -0,0 +1,5 @@
*.o
.objs
headers.so
assets.h
cert

View File

@ -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.

View File

@ -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
}

29
examples/headers/src/headers.c Executable file
View File

@ -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);
}