diff options
author | Joursoir <chat@joursoir.net> | 2021-05-22 21:04:54 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-05-22 21:04:54 +0000 |
commit | 2d2d545966d5b5058a6a2dba434cc86679e06765 (patch) | |
tree | ef6efbf29d5b44192e2b6a48034d167c581ffd9f | |
parent | 30e291682b3d6e10430b683c062cb2b9491b8025 (diff) | |
download | ctimeline-2d2d545966d5b5058a6a2dba434cc86679e06765.tar.gz ctimeline-2d2d545966d5b5058a6a2dba434cc86679e06765.tar.bz2 ctimeline-2d2d545966d5b5058a6a2dba434cc86679e06765.zip |
-rw-r--r-- | url.c | 62 | ||||
-rw-r--r-- | url.h | 7 |
2 files changed, 69 insertions, 0 deletions
@@ -0,0 +1,62 @@ +#include <stddef.h> +#include <ctype.h> + +#include "xstring.h" +#include "url.h" + +static int url_decode_hex_char(const char *query) +{ + int out = 0, ch, i; + for(i = 0; i < 2; i++) { + ch = *query; + out <<= 4; + if(isdigit(ch)) + out += ch - '0'; + else if(isalpha(ch)) + out += tolower(ch) - 'a' + 10; + else + return -1; + query++; + } + return out; +} + +static string *url_decode(const char **query, char delimiter) +{ + int ch; + const char *str = *query; + string *dest = string_alloc(NULL, NULL); + + while((ch = *str)) { + str++; + + if(ch == delimiter) + break; + + if(ch == '%') { + ch = url_decode_hex_char(str); + if(ch) { + string_addch(dest, ch); + str += 2; + } + continue; + } + + if(ch == '+') + string_addch(dest, ' '); + else + string_addch(dest, ch); + } + *query = str; + return dest; +} + +string *url_get_param_name(const char **query) +{ + return url_decode(query, '='); +} + +string *url_get_param_value(const char **query) +{ + return url_decode(query, '&'); +}
\ No newline at end of file @@ -0,0 +1,7 @@ +#ifndef CTIMELINE_URL_H +#define CTIMELINE_URL_H + +string *url_get_param_name(const char **query); +string *url_get_param_value(const char **query); + +#endif /* CTIMELINE_UI_COMMON_H */ |