-
Notifications
You must be signed in to change notification settings - Fork 8
/
json.c
96 lines (82 loc) · 2.87 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @file json.c
* @author Joe Bayer (joexbayer)
* @brief Example program that uses jansson to serialize a list into JSON
* @usage: curl http://localhost:8080/list
* @usage: curl -X POST http://localhost:8080/json/add -d "item=Test Item"
* @version 0.1
* @date 2024-11-17
*
* @copyright Copyright (c) 2024
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cweb.h>
#define MAX_ITEMS 10
static const char* list[MAX_ITEMS];
static int list_count = 0;
/* Helper: Serialize the list into JSON */
static void serialize_list(char *buffer, size_t buffer_size) {
json_t *json_arr = json_array();
for (int i = 0; i < list_count; i++) {
json_array_append_new(json_arr, json_string(list[i]));
}
char *json_string_data = json_dumps(json_arr, JSON_COMPACT);
snprintf(buffer, buffer_size, "%s", json_string_data);
free(json_string_data);
json_decref(json_arr);
}
/* Route: /list - Method GET */
int get_list_route(struct http_request *req, struct http_response *res) {
char json_response[1024];
serialize_list(json_response, sizeof(json_response));
/* HTTP response */
snprintf(res->body, HTTP_RESPONSE_SIZE, "%s", json_response);
map_insert(res->headers, "Content-Type", "application/json");
res->status = HTTP_200_OK;
return 0;
}
/* Route: /json/add - Method POST */
int add_item_route(struct http_request *req, struct http_response *res) {
if (list_count >= MAX_ITEMS) {
json_t *error = json_pack("{s:s}", "error", "List is full");
char *error_json = json_dumps(error, JSON_COMPACT);
snprintf(res->body, HTTP_RESPONSE_SIZE, "%s", error_json);
map_insert(res->headers, "Content-Type", "application/json");
free(error_json);
json_decref(error);
res->status = HTTP_400_BAD_REQUEST;
return 0;
}
const char *new_item = map_get(req->data, "item");
if (new_item && strlen(new_item) < 256) {
list[list_count++] = strdup(new_item); /* uses malloc */
}
json_t *message = json_pack("{s:s}", "message", "Item added");
char *message_json = json_dumps(message, JSON_COMPACT);
snprintf(res->body, HTTP_RESPONSE_SIZE, "%s", message_json);
map_insert(res->headers, "Content-Type", "application/json");
free(message_json);
json_decref(message);
res->status = HTTP_200_OK;
return 0;
}
void unload() {
printf("Unloading json_example_jansson %d\n", list_count);
for (int i = 0; i < list_count; i++) {
free((void*)list[i]);
}
}
/* Export module */
export module_t config = {
.name = "json_example_jansson",
.author = "cweb",
.size = 2,
.routes = {
{"/list", "GET", get_list_route, NONE},
{"/json/add", "POST", add_item_route, NONE},
},
.unload = unload,
};