-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandle_get_request.c
More file actions
292 lines (252 loc) · 11.2 KB
/
Copy pathhandle_get_request.c
File metadata and controls
292 lines (252 loc) · 11.2 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <common.h>
// in CRUD : GET => READ
// resources : DATA , STRUCT(URE)
cJSON* handle_get_request(const char *url) {
// 1. URL parsing w/ sscanf
char version[64] = {0};
char resource[64] = {0};
char schema[64] = {0};
char table[64] = {0};
char column[64] = {0};
char value[296] = {0};
char query[1024] = {0};
#if DEBUG == 1
struct timeval tv;
char timestamp[30];
char microtimestamp[40];
struct tm *local;
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "begin", microtimestamp);
#endif
// URL length verification before parsing
if (strlen(url) > 506) {
// BAD REQUEST
cJSON_AddStringToObject(json_response, "error", "URL too long");
cJSON_AddNumberToObject(json_response, "httpcode", HTTP_BAD_REQUEST);
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "end", microtimestamp);
#endif
return json_response;
}
cJSON *json_response = cJSON_CreateObject();
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "begin", microtimestamp);
#endif
int nb_tokens = sscanf(url, "/%63[^/]/%63[^/]/%63[^/]/%63[^/]/%63[^/]/%295s",
version, resource, schema, table, column, value);
cJSON_AddStringToObject(json_response, "apiversion", version);
cJSON_AddStringToObject(json_response, "url", url);
// removing eventual trailing / to value
size_t length = strlen(value);
while (length > 0 && value[length - 1] == '/') {
value[length - 1] = '\0';
length--;
}
// 2. analyzing tokens to match patterns and resources
// 3 possibilites :
// - resource in (ping, status) && 2 tokens
// - resource = status && 3 tokens
// - resource = structure && 4 tokens
// - resource = data && 6 tokens
// - bad request format
if (strcasecmp(resource, "ping") == 0 && nb_tokens == 2) {
// PING
cJSON_AddStringToObject(json_response, "ping", "pong");
snprintf(query, sizeof(query), "SELECT now()");
cJSON_AddStringToObject(json_response, "SQL", query);
} else if (strcasecmp(resource, "status") == 0 && nb_tokens == 2) {
// STATUS
snprintf(query, sizeof(query), "SHOW GLOBAL STATUS");
cJSON_AddStringToObject(json_response, "SQL", query);
} else if (strcasecmp(resource, "status") == 0 && nb_tokens == 3) {
// STATUS LIKE
snprintf(query, sizeof(query), "SHOW GLOBAL STATUS like '%%%s%%'", schema);
cJSON_AddStringToObject(json_response, "SQL", query);
} else if (strcasecmp(resource, "struct") == 0 && nb_tokens == 4) {
// STRUCTURE
snprintf(query, sizeof(query), "SHOW COLUMNS FROM %s.%s", schema, table);
cJSON_AddStringToObject(json_response, "SQL", query);
} else if (strcasecmp(resource, "data") == 0 && nb_tokens == 6) {
// DATA
snprintf(query, sizeof(query), "SELECT * FROM %s.%s WHERE %s='%s'", schema, table, column, value);
cJSON_AddStringToObject(json_response, "SQL", query);
} else {
// BAD REQUEST
cJSON_AddStringToObject(json_response, "error", "BAD REQUEST");
cJSON_AddNumberToObject(json_response, "httpcode", HTTP_BAD_REQUEST);
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "end", microtimestamp);
#endif
return json_response;
}
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "after resource check", microtimestamp);
#endif
//// 3. Local connexion to MariaDB
// connexion handle
MYSQL *conn = mysql_init(NULL);
if (conn == NULL) {
cJSON_AddStringToObject(json_response, "cnx handle", "KO");
cJSON_AddStringToObject(json_response, "errno", mysql_error(conn));
cJSON_AddNumberToObject(json_response, "httpcode", HTTP_INTERNAL_SERVER_ERROR);
return json_response;
}
cJSON_AddStringToObject(json_response, "cnx handle", "OK");
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "before cnx", microtimestamp);
#endif
// actual connexion
// through socket
if(mysql_real_connect(conn, "localhost", "api", "passw0rd",
NULL, 0, "/var/lib/mysql/mysql.sock", 0) == NULL ) {
// through network port
// if(mysql_real_connect(conn, "localhost", "api", "passw0rd",
// NULL, 3306, NULL, 0) == NULL ) {
cJSON_AddStringToObject(json_response, "cnx", "KO");
cJSON_AddStringToObject(json_response, "errno", mysql_error(conn));
cJSON_AddNumberToObject(json_response, "httpcode", HTTP_INTERNAL_SERVER_ERROR);
mysql_close(conn);
return json_response;
}
cJSON_AddStringToObject(json_response, "cnx", "OK");
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "after cnx handle", microtimestamp);
#endif
//mysql_options(conn, MYSQL_OPT_PROTOCOL, MYSQL_PROTOCOL_SOCKET); // Force socket
// 4. statement execution
// if(mysql_query(conn, query)) {
if (mysql_real_query(conn, STRING_WITH_STRLEN(query))) {
cJSON_AddStringToObject(json_response, "stmt exec", "KO");
cJSON_AddStringToObject(json_response, "errno", mysql_error(conn));
cJSON_AddNumberToObject(json_response, "httpcode", HTTP_INTERNAL_SERVER_ERROR);
mysql_close(conn);
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "after stmt exec KO", microtimestamp);
#endif
return json_response;
}
cJSON_AddStringToObject(json_response, "stmt exec", "OK");
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "after stmt exec", microtimestamp);
#endif
//// creating the result buffer
MYSQL_RES *result = mysql_store_result(conn);
if(result == NULL) {
cJSON_AddStringToObject(json_response, "result fetch", "KO");
cJSON_AddStringToObject(json_response, "errno", mysql_error(conn));
cJSON_AddNumberToObject(json_response, "httpcode", HTTP_INTERNAL_SERVER_ERROR);
mysql_close(conn);
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "after result buffer KO", microtimestamp);
#endif
return json_response;
}
cJSON_AddStringToObject(json_response, "result fetch", "OK");
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "after result buffer", microtimestamp);
#endif
// 5. initializing resulset
int num_rows = mysql_num_rows(result);
int num_fields = mysql_num_fields(result);
MYSQL_FIELD *fields = mysql_fetch_fields(result);
MYSQL_ROW row;
// 6. resultset => JSON
cJSON *data_array = cJSON_CreateArray();
cJSON *fields_obj = cJSON_CreateObject();
cJSON_AddNumberToObject(fields_obj, "num_fields", num_fields);
cJSON *fields_array = cJSON_CreateArray();
while ((row = mysql_fetch_row(result))) {
cJSON *row_object = cJSON_CreateObject();
for (int i = 0; i < num_fields; i++) {
const char *field_name = fields[i].name;
const char *field_value = row[i] ? row[i] : "NULL";
cJSON_AddStringToObject(row_object, field_name, field_value);
cJSON_AddItemToArray(fields_array, cJSON_CreateString(fields[i].name));
}
cJSON_AddItemToArray(data_array, row_object);
cJSON_AddItemToObject(fields_obj, "names", fields_array);
}
cJSON_AddItemToObject(json_response, "fields", fields_obj);
cJSON_AddNumberToObject(json_response, "rows", num_rows);
cJSON_AddItemToObject(json_response, "data", data_array);
#if DEBUG == 1
//get current time with microsecond precision
gettimeofday(&tv, NULL);
// Convert to local time and format as a string
local = localtime(&tv.tv_sec);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", local);
snprintf(microtimestamp, sizeof(microtimestamp), "%s.%06ld", timestamp, tv.tv_usec);
cJSON_AddStringToObject(json_response, "end", microtimestamp);
#endif
cJSON_AddNumberToObject(json_response, "httpcode", HTTP_OK);
// Nettoyage
mysql_free_result(result);
mysql_close(conn);
free(conn);
return json_response;
}