aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorame <[email protected]>2025-08-12 21:54:57 -0500
committerame <[email protected]>2025-08-12 21:54:57 -0500
commit432f7792d12dadc3adb605c018176bbc7359b503 (patch)
treefac1e35fd470e1c6b3a3bdeb99daf1098b43b64e /src/net
parent5666cbf8830c8b26337eb92f4da434d0d2242dc2 (diff)
support percent encoding
Diffstat (limited to 'src/net')
-rw-r--r--src/net/util.c26
-rw-r--r--src/net/util.h3
2 files changed, 29 insertions, 0 deletions
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);
+