Skip to content

Commit 8c9961b

Browse files
committed
Improve json parsing
1 parent 9121ede commit 8c9961b

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

single-HTTP/main.c

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -323,44 +323,47 @@ static char decode_http_post(HashTable *ht, String data) {
323323
return '\0';
324324
}
325325

326-
static char decode_json_post(HashTable *ht, char *data) {
326+
static char decode_json_post(HashTable *ht, String data) {
327+
unsigned short buf_count = 0;
327328
char buffer[STR_MAX];
328-
char *traveler = buffer, key[1024], value[1024];
329-
unsigned short cpy_index = 0;
329+
String traveler = buffer;
330330

331331
strncpy(buffer, data, STR_MAX);
332332

333-
while (*traveler != '\0') {
334-
for (unsigned int i = 0; i < 1024; i++) {
335-
key[i] = '\0';
336-
value[i] = '\0';
337-
}
333+
while ((*traveler != '\0') && (buf_count < STR_MAX)) {
334+
unsigned short cpy_idx = 0;
335+
char key[STR_MAX] = "", value[STR_MAX] = "";
336+
338337
// Key
339-
while ((*traveler != '\"') && (*traveler != '\0')) {
338+
while ((*traveler != '\"') && (*traveler != '\0') && (buf_count < STR_MAX))
340339
traveler++;
341-
}
342-
if (*traveler == '\0')
340+
341+
if ((*traveler == '\0') || (buf_count > STR_MAX))
343342
break;
344343
traveler++;
345344

346-
while (*traveler != '\"') {
347-
key[cpy_index] = *traveler;
345+
while ((*traveler != '\"') && (buf_count < STR_MAX)) {
346+
key[cpy_idx] = *traveler;
348347
traveler++;
349-
cpy_index++;
348+
cpy_idx++;
350349
}
351350
traveler++;
352-
cpy_index = 0;
351+
cpy_idx = 0;
352+
353353
// Value
354-
while (*traveler != '\"')
354+
while ((*traveler != '\"') && (buf_count < STR_MAX))
355355
traveler++;
356+
357+
if ((*traveler == '\0') || (buf_count > STR_MAX))
358+
break;
356359
traveler++;
357-
while (*traveler != '\"') {
358-
value[cpy_index] = *traveler;
360+
361+
while ((*traveler != '\"') && (buf_count < STR_MAX)) {
362+
value[cpy_idx] = *traveler;
359363
traveler++;
360-
cpy_index++;
364+
cpy_idx++;
361365
}
362366
traveler++;
363-
cpy_index = 0;
364367
ht_insert(ht, key, value);
365368
}
366369
return '\0';
@@ -437,7 +440,6 @@ static HashTable parse_post_request(const String msg) {
437440
end++;
438441
start = end;
439442
}
440-
ht_print(data);
441443
return data;
442444
}
443445

@@ -755,23 +757,23 @@ int main(const int argc, String *const argv) {
755757

756758
compute_flags(argc, argv, &_verbose_flag);
757759

758-
parse_post_request(
759-
"Host: 192.168.1.77:8888\n"
760-
"Connection: keep-alive\n"
761-
"Content-Length: 41\n"
762-
"Cache-Control: max-age=0\n"
763-
"Origin: http://192.168.1.77:8888\n"
764-
"Upgrade-Insecure-Requests: 1\n"
765-
"DNT: 1\n"
766-
"Content-Type: application/x-www-form-urlencoded\n"
767-
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36\n"
768-
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\n"
769-
"Referer: http://192.168.1.77:8888/\n"
770-
"Accept-Encoding: gzip, deflate\n"
771-
"Accept-Language: en-US,en;q=0.9\n\n"
772-
"name=a%24aa&email=qqq%40ddd.com&pas%21sword=zzz\n"
773-
);
774-
return 0;
760+
// parse_post_request(
761+
// "Host: 192.168.1.77:8888\n"
762+
// "Connection: keep-alive\n"
763+
// "Content-Length: 41\n"
764+
// "Cache-Control: max-age=0\n"
765+
// "Origin: http://192.168.1.77:8888\n"
766+
// "Upgrade-Insecure-Requests: 1\n"
767+
// "DNT: 1\n"
768+
// "Content-Type: application/x-www-form-urlencoded\n"
769+
// "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36\n"
770+
// "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\n"
771+
// "Referer: http://192.168.1.77:8888/\n"
772+
// "Accept-Encoding: gzip, deflate\n"
773+
// "Accept-Language: en-US,en;q=0.9\n\n"
774+
// "name=a%24aa&email=qqq%40ddd.com&pas%21sword=zzz\n"
775+
// );
776+
// return 0;
775777
if (!is_valid_port()) {
776778
fprintf(stderr, RED "Port Error: Invalid port %s\n" RESET, _port);
777779
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)