kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit dc55a48d875fe93fe710942b3dc04ed04720327f
parent e94cc2f3a861b46595d9b06c4e23eefdde2d37e8
Author: Joris Vink <joris@coders.se>
Date:   Sun, 20 Oct 2019 23:40:08 +0200

Add native JSON parser example

Diffstat:
examples/json/.gitignore | 5+++++
examples/json/README.md | 13+++++++++++++
examples/json/conf/build.conf | 18++++++++++++++++++
examples/json/conf/json.conf | 19+++++++++++++++++++
examples/json/src/json.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 105 insertions(+), 0 deletions(-)

diff --git a/examples/json/.gitignore b/examples/json/.gitignore @@ -0,0 +1,5 @@ +*.o +.objs +json.so +assets.h +cert diff --git a/examples/json/README.md b/examples/json/README.md @@ -0,0 +1,13 @@ +Native Kore JSON parser example. + +Run: +``` + $ kodev run +``` + +Test: +``` + $ curl -i -k -d '{"foo":{"bar": "Hello world"}}' https://127.0.0.1:8888 +``` + +The result should echo back the foo.bar JSON path value if it is a JSON string. diff --git a/examples/json/conf/build.conf b/examples/json/conf/build.conf @@ -0,0 +1,18 @@ +# json build config +# You can switch flavors using: kodev flavor [newflavor] + +# The cflags below are shared between flavors +cflags=-Wall -Wmissing-declarations -Wshadow +cflags=-Wstrict-prototypes -Wmissing-prototypes +cflags=-Wpointer-arith -Wcast-qual -Wsign-compare + +dev { + # These cflags are added to the shared ones when + # you build the "dev" flavor. + cflags=-g +} + +#prod { +# You can specify additional CFLAGS here which are only +# included if you build with the "prod" flavor. +#} diff --git a/examples/json/conf/json.conf b/examples/json/conf/json.conf @@ -0,0 +1,19 @@ +# Placeholder configuration + +server tls { + bind 127.0.0.1 8888 +} + +load ./json.so + +tls_dhparam dh2048.pem + +domain 127.0.0.1 { + attach tls + + certfile cert/server.pem + certkey cert/key.pem + + static / page + restrict / post +} diff --git a/examples/json/src/json.c b/examples/json/src/json.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 Joris Vink <joris@coders.se> + * + * 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> + +int page(struct http_request *); + +int +page(struct http_request *req) +{ + struct kore_buf buf; + struct kore_json json; + struct kore_json_item *item; + + kore_buf_init(&buf, 128); + kore_json_init(&json, req->http_body->data, req->http_body->length); + + if (!kore_json_parse(&json)) { + kore_buf_appendf(&buf, "%s\n", kore_json_strerror(&json)); + } else { + if ((item = kore_json_string(&json, "foo/bar")) != NULL) { + kore_buf_appendf(&buf, + "foo.bar = '%s'\n", item->data.string); + } else { + kore_buf_appendf(&buf, "%s\n", + kore_json_strerror(&json)); + } + } + + http_response(req, 200, buf.data, buf.offset); + + kore_buf_cleanup(&buf); + kore_json_cleanup(&json); + + return (KORE_RESULT_OK); +}