diff --git a/examples/json/.gitignore b/examples/json/.gitignore new file mode 100755 index 0000000..034aa90 --- /dev/null +++ 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 new file mode 100644 index 0000000..b634bca --- /dev/null +++ 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 new file mode 100644 index 0000000..473b697 --- /dev/null +++ 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 new file mode 100755 index 0000000..c7ee1b6 --- /dev/null +++ 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 new file mode 100755 index 0000000..ae997b6 --- /dev/null +++ b/examples/json/src/json.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 Joris Vink + * + * 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 +#include + +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); +}