mirror of
https://github.com/jorisvink/kore
synced 2025-03-09 12:39:01 -04:00
Add headers example
This commit is contained in:
parent
4010bdd58d
commit
9dbcf5399f
5
examples/headers/.gitignore
vendored
Executable file
5
examples/headers/.gitignore
vendored
Executable file
@ -0,0 +1,5 @@
|
||||
*.o
|
||||
.objs
|
||||
headers.so
|
||||
assets.h
|
||||
cert
|
13
examples/headers/README.md
Normal file
13
examples/headers/README.md
Normal 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.
|
12
examples/headers/conf/headers.conf
Executable file
12
examples/headers/conf/headers.conf
Executable 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
29
examples/headers/src/headers.c
Executable 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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user