Skip to content

Commit 5e68944

Browse files
authored
http: decode POST parameters before applying options. (#174)
Code greatly reduced by @ayufan. Fixes #115
1 parent f7b673c commit 5e68944

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

util/http/http.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,31 @@ static void *http_get_param_fn(struct http_worker_s *worker, FILE *stream, const
103103
return NULL;
104104
}
105105

106+
// Helper function to decode percent-encoded strings
107+
static void http_url_decode(const char *in, char *out)
108+
{
109+
while (*in) {
110+
if (*in == '%' && isxdigit(in[1]) && isxdigit(in[2])) {
111+
char hex[3] = {in[1], in[2], '\0'};
112+
*out++ = (char)strtoul(hex, NULL, 16);
113+
in += 3;
114+
} else if (*in == '+') {
115+
*out++ = ' ';
116+
in++;
117+
} else {
118+
*out++ = *in++;
119+
}
120+
}
121+
*out = '\0';
122+
}
123+
106124
char *http_get_param(http_worker_t *worker, const char *key)
107125
{
108-
return http_enum_params(worker, NULL, http_get_param_fn, (void*)key);
126+
char *param = http_enum_params(worker, NULL, http_get_param_fn, (void*)key);
127+
if (param) {
128+
http_url_decode(param, param);
129+
}
130+
return param;
109131
}
110132

111133
static void http_process(http_worker_t *worker, FILE *stream)

util/http/http.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <string.h>
7+
#include <ctype.h>
78
#include <pthread.h>
89
#include <netinet/ip.h>
910

0 commit comments

Comments
 (0)