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

json.c (1834B)



      1 /*
      2  * Copyright (c) 2019 Joris Vink <joris@coders.se>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include <kore/kore.h>
     18 #include <kore/http.h>
     19 
     20 #include <inttypes.h>
     21 
     22 int		page(struct http_request *);
     23 
     24 int
     25 page(struct http_request *req)
     26 {
     27 	struct kore_buf		buf;
     28 	struct kore_json	json;
     29 	struct kore_json_item	*item;
     30 
     31 	kore_buf_init(&buf, 128);
     32 	kore_json_init(&json, req->http_body->data, req->http_body->length);
     33 
     34 	if (!kore_json_parse(&json)) {
     35 		kore_buf_appendf(&buf, "%s\n", kore_json_strerror());
     36 	} else {
     37 		item = kore_json_find_string(json.root, "foo/bar");
     38 		if (item != NULL) {
     39 			kore_buf_appendf(&buf,
     40 			    "foo.bar = '%s'\n", item->data.string);
     41 		} else {
     42 			kore_buf_appendf(&buf, "foo.bar %s\n",
     43 			    kore_json_strerror());
     44 		}
     45 
     46 		item = kore_json_find_integer_u64(json.root, "foo/integer");
     47 		if (item != NULL) {
     48 			kore_buf_appendf(&buf,
     49 			    "foo.integer = '%" PRIu64 "'\n", item->data.u64);
     50 		} else {
     51 			kore_buf_appendf(&buf, "foo.integer %s\n",
     52 			    kore_json_strerror());
     53 		}
     54 	}
     55 
     56 	http_response(req, 200, buf.data, buf.offset);
     57 
     58 	kore_buf_cleanup(&buf);
     59 	kore_json_cleanup(&json);
     60 
     61 	return (KORE_RESULT_OK);
     62 }