diff options
| author | ame <[email protected]> | 2025-08-12 21:54:57 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2025-08-12 21:54:57 -0500 |
| commit | 432f7792d12dadc3adb605c018176bbc7359b503 (patch) | |
| tree | fac1e35fd470e1c6b3a3bdeb99daf1098b43b64e /src | |
| parent | 5666cbf8830c8b26337eb92f4da434d0d2242dc2 (diff) | |
support percent encoding
Diffstat (limited to 'src')
| -rw-r--r-- | src/net.c | 23 | ||||
| -rw-r--r-- | src/net/util.c | 26 | ||||
| -rw-r--r-- | src/net/util.h | 3 | ||||
| -rw-r--r-- | src/types/str.c | 13 | ||||
| -rw-r--r-- | src/types/str.h | 5 |
5 files changed, 62 insertions, 8 deletions
@@ -678,7 +678,7 @@ void* handle_client(void *_arg){ if(val == -2) net_error(client_fd, 414);
if(val >= 0){
- str* sk = (str*)parray_get(table, "Path");
+ str* path = (str*)parray_get(table, "Path");
str* sR = (str*)parray_get(table, "Request");
str* sT = (str*)parray_get(table, "Content-Type");
str* sC = (str*)parray_get(table, "Cookie");
@@ -693,13 +693,22 @@ void* handle_client(void *_arg){ sprintf(portc, "%i", args->port);
str* aa = str_init(portc);
- str_push(aa, sk->c);
+ str* decoded_path;
+ int decoded_err = percent_decode(path, &decoded_path);
+ larray_t* params = NULL;
+ parray_t* v = NULL;
- larray_t* params = larray_init();
- parray_t* v = route_match(paths, aa->c, ¶ms);
-
- if(sT != NULL)
- rolling_file_parse(L, &files_idx, &body_idx, header + 4, sT, bite - header_eof - 4, file_cont);
+ if(decoded_err == 1){
+ net_error(client_fd, 400);
+ } else {
+ str_push(aa, decoded_path->c);
+
+ params = larray_init();
+ v = route_match(paths, aa->c, ¶ms);
+
+ if(sT != NULL)
+ rolling_file_parse(L, &files_idx, &body_idx, header + 4, sT, bite - header_eof - 4, file_cont);
+ }
str_free(aa);
if(v != NULL){
diff --git a/src/net/util.c b/src/net/util.c index b41f0ed..92e538b 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -522,3 +522,29 @@ int net_error(int fd, int code){ send(fd, out, strlen(out), MSG_NOSIGNAL); return 0; } + +int percent_decode(str* input, str** _output){ + str* output = str_init(""); + + //could maybe make this better by using memmem to find occurrences of % + for(int i = 0; i <= input->len; i++){ + if(input->c[i] == '%' && input->len - i >= 3){ + str* hex = str_initfl(input->c + i + 1, 2); + + //casting a long to a char pointer is a horrible idea + long c = strtol(hex->c, NULL, 16); + if(c == 0){ + *_output = output; + return 1; + } + + str_pushl(output, ((char*)&c), 1); + str_free(hex); + i += 2; + } else { + str_pushl(output, input->c + i, 1); + } + } + *_output = output; + return 0; +} diff --git a/src/net/util.h b/src/net/util.h index cfac065..b8bc824 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -56,3 +56,6 @@ int match_param(char* path, char* match, parray_t* arr); void parse_mimetypes(); int net_error(int fd, int code); + +int percent_decode(str* input, str** _output); + diff --git a/src/types/str.c b/src/types/str.c index 0c8d63a..e1818a2 100644 --- a/src/types/str.c +++ b/src/types/str.c @@ -16,6 +16,19 @@ str* str_initl(const char* init, size_t len){ return s;
}
+str* str_initfl(const char* init, size_t len){
+
+ str* s = malloc(sizeof * s);
+ s->_bytes = len + 1 + alloc_buffer;
+ s->c = malloc(s->_bytes);
+ if(s->c == NULL) p_fatal("failed to allocate string\n");
+ s->len = len ;
+
+ memcpy(s->c, init, (len) * sizeof * init);
+ s->c[len] = '\0';
+ return s;
+}
+
str* str_init(const char* init){
return str_initl(init, strlen(init));
}
diff --git a/src/types/str.h b/src/types/str.h index 86490cc..e650542 100644 --- a/src/types/str.h +++ b/src/types/str.h @@ -12,6 +12,9 @@ typedef struct { } str; str* str_initl(const char*, size_t len); +//str_initfl has the 'correct' behaviour where it forces the len and doesnt read extra bytes +//plan to switch everything to str_initfl, when everything will work with it +str* str_initfl(const char*, size_t len); str* str_init(const char*); void str_free(str*); void str_push(str*, const char*); @@ -19,4 +22,4 @@ void str_pushl(str*, const char*, size_t); void str_clear(str*); void str_popf(str*, int); void str_popb(str*, int); -#endif //__STR_H
\ No newline at end of file +#endif //__STR_H |
