Repository navigation
Expand file tree
/
Copy pathhttp.c
More file actions
255 lines (219 loc) · 7.96 KB
/
Copy pathhttp.c
File metadata and controls
255 lines (219 loc) · 7.96 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <microhttpd.h>
#include <json-glib/json-glib.h>
#include <string.h>
#include "http.h"
#include "network.h"
#include "utils.h"
#include "apps.h"
#include "jsonbuilderutils.h"
struct postconninfo {
GByteArray* payload;
};
#define PORT 1338
#define ENDPOINT_SCAN "/scan"
#define ENDPOINT_CONFIG "/config"
#define ENDPOINT_STATUS "/status"
#define ENDPOINT_DEBUG "/debug"
static struct MHD_Daemon* mhd = NULL;
static int http_handleconnection_debug(struct MHD_Connection* connection) {
JsonBuilder* jsonbuilder = json_builder_new();
json_builder_begin_object(jsonbuilder);
json_builder_end_object(jsonbuilder);
gsize contentln;
char* content = jsonbuilder_freetostring(jsonbuilder, &contentln, FALSE);
int ret = 0;
struct MHD_Response* response = MHD_create_response_from_buffer(contentln,
(void*) content, MHD_RESPMEM_MUST_COPY);
if (response) {
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
} else
g_message("failed to create response");
g_free(content);
return ret;
}
static int http_handleconnection_status(struct MHD_Connection* connection) {
JsonBuilder* jsonbuilder = json_builder_new();
json_builder_begin_object(jsonbuilder);
network_dumpstatus(jsonbuilder);
apps_dumpstatus(jsonbuilder);
json_builder_end_object(jsonbuilder);
gsize contentln;
char* content = jsonbuilder_freetostring(jsonbuilder, &contentln, FALSE);
int ret = 0;
struct MHD_Response* response = MHD_create_response_from_buffer(contentln,
(void*) content, MHD_RESPMEM_MUST_COPY);
if (response) {
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
} else
g_message("failed to create response");
g_free(content);
return ret;
}
static void http_handleconnection_scan_addscanresult(gpointer data,
gpointer userdata) {
struct network_scanresult* scanresult = (struct network_scanresult*) data;
JsonBuilder* jsonbuilder = (JsonBuilder*) userdata;
json_builder_begin_object(jsonbuilder);
json_builder_set_member_name(jsonbuilder, "bssid");
json_builder_add_string_value(jsonbuilder, scanresult->bssid);
json_builder_set_member_name(jsonbuilder, "frequency");
json_builder_add_int_value(jsonbuilder, scanresult->frequency);
json_builder_set_member_name(jsonbuilder, "rssi");
json_builder_add_int_value(jsonbuilder, scanresult->rssi);
json_builder_set_member_name(jsonbuilder, "ssid");
json_builder_add_string_value(jsonbuilder, scanresult->ssid);
json_builder_end_object(jsonbuilder);
}
static int http_handleconnection_scan(struct MHD_Connection* connection) {
int ret = MHD_NO;
GPtrArray* scanresults = network_scan();
JsonBuilder* jsonbuilder = json_builder_new();
json_builder_begin_object(jsonbuilder);
json_builder_set_member_name(jsonbuilder, "scanresults");
json_builder_begin_array(jsonbuilder);
g_ptr_array_foreach(scanresults, http_handleconnection_scan_addscanresult,
jsonbuilder);
json_builder_end_array(jsonbuilder);
json_builder_end_object(jsonbuilder);
gsize jsonlen;
char* content = jsonbuilder_freetostring(jsonbuilder, &jsonlen, FALSE);
struct MHD_Response* response = MHD_create_response_from_buffer(jsonlen,
(void*) content, MHD_RESPMEM_MUST_COPY);
if (response) {
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
} else
g_message("failed to create response");
g_free(content);
return ret;
}
static int http_handleconnection_invalid(struct MHD_Connection* connection) {
int ret = MHD_NO;
static const char* content = "";
struct MHD_Response* response = MHD_create_response_from_buffer(
strlen(content), (void*) content, MHD_RESPMEM_PERSISTENT);
if (response) {
ret = MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, response);
MHD_destroy_response(response);
} else
g_message("failed to create response");
return ret;
}
static int http_handleconnection_configure(struct MHD_Connection* connection,
void** con_cls) {
struct postconninfo* con_info = *con_cls;
int ret = MHD_NO;
GBytes* bytes = g_byte_array_free_to_bytes(con_info->payload);
con_info->payload = NULL;
JsonParser* jsonparser = json_parser_new();
gsize sz;
gconstpointer data = g_bytes_get_data(bytes, &sz);
if (sz == 0) {
g_message("0 byte payload");
goto invalidrequest;
}
if (!json_parser_load_from_data(jsonparser, data, sz, NULL)) {
g_message("failed to parse json");
goto invalidrequest;
}
JsonNode* root = json_parser_get_root(jsonparser);
struct network_config* ntwkcfg = network_model_config_deserialise(root);
if (ntwkcfg == NULL) {
goto invalidrequest;
}
gboolean configuring = network_configure(ntwkcfg);
JsonBuilder* jsonbuilder = json_builder_new();
json_builder_begin_object(jsonbuilder);
json_builder_set_member_name(jsonbuilder, "configuring");
json_builder_add_boolean_value(jsonbuilder, configuring);
json_builder_end_object(jsonbuilder);
gsize contentln;
char* content = jsonbuilder_freetostring(jsonbuilder, &contentln, FALSE);
struct MHD_Response* response = MHD_create_response_from_buffer(contentln,
(void*) content, MHD_RESPMEM_MUST_COPY);
if (response) {
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
} else
g_message("failed to create response");
g_free(content);
goto out;
invalidrequest: ret = http_handleconnection_invalid(connection);
out: g_object_unref(jsonparser);
g_bytes_unref(bytes);
return ret;
}
static int http_handleconnection_continuemunchingpost(const char* upload_data,
size_t* upload_data_size, void** con_cls) {
struct postconninfo* con_info = *con_cls;
g_message("continuing to eat post, %zu", *upload_data_size);
g_byte_array_append(con_info->payload, (const guint8*) upload_data,
*upload_data_size);
*upload_data_size = 0;
return MHD_YES;
}
static int http_handleconnection_startmunchingpost(
struct MHD_Connection* connection, void** con_cls) {
g_message("starting to eat post");
struct postconninfo* con_info = g_malloc0(sizeof(struct postconninfo));
con_info->payload = g_byte_array_new();
*con_cls = con_info;
return MHD_YES;
}
static int http_handleconnection(void* cls, struct MHD_Connection* connection,
const char* url, const char* method, const char* version,
const char* upload_data, size_t* upload_data_size, void** con_cls) {
g_message("handling request %s for %s", method, url);
int ret = MHD_NO;
gboolean isget = (strcmp(method, MHD_HTTP_METHOD_GET) == 0);
gboolean ispost = (strcmp(method, MHD_HTTP_METHOD_POST) == 0);
if (isget && (strcmp(url, ENDPOINT_STATUS) == 0))
ret = http_handleconnection_status(connection);
else if (isget && (strcmp(url, ENDPOINT_SCAN) == 0))
ret = http_handleconnection_scan(connection);
else if (isget && (strcmp(url, ENDPOINT_DEBUG) == 0)) {
ret = http_handleconnection_debug(connection);
} else if (ispost && (strcmp(url, ENDPOINT_CONFIG) == 0)) {
if (*con_cls == NULL
&& strcmp(
MHD_lookup_connection_value(connection, MHD_HEADER_KIND,
MHD_HTTP_HEADER_CONTENT_TYPE), "application/json")
== 0) {
ret = http_handleconnection_startmunchingpost(connection, con_cls);
} else if (*upload_data_size != 0)
ret = http_handleconnection_continuemunchingpost(upload_data,
upload_data_size, con_cls);
else
ret = http_handleconnection_configure(connection, con_cls);
} else {
g_message("invalid request");
ret = http_handleconnection_invalid(connection);
}
return ret;
}
static void http_requestcompleted(void *cls, struct MHD_Connection *connection,
void **con_cls, enum MHD_RequestTerminationCode toe) {
struct postconninfo* con_info = *con_cls;
if (NULL == con_info)
return;
if (con_info->payload)
g_byte_array_free(con_info->payload, TRUE);
g_free(con_info);
*con_cls = NULL;
}
int http_start() {
mhd = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
http_handleconnection, NULL,
// options
MHD_OPTION_NOTIFY_COMPLETED, http_requestcompleted, NULL,
MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 2, MHD_OPTION_END);
if (mhd == NULL)
return 1;
return 0;
}
void http_stop() {
MHD_stop_daemon(mhd);
mhd = NULL;
}