--- a/src/server/util/util.c Tue Aug 13 22:14:32 2019 +0200 +++ b/src/server/util/util.c Sat Sep 24 16:26:10 2022 +0200 @@ -55,6 +55,7 @@ #include "../public/nsapi.h" #include <ucx/string.h> #include <ucx/mempool.h> +#include <ucx/utils.h> #include "pblock.h" #include "util.h" @@ -288,6 +289,24 @@ return vsnprintf(s, n, fmt, args); } +NSAPI_PUBLIC int util_vasprintf(pool_handle_t *pool, char **s, const char *fmt, + va_list args) +{ + UcxAllocator a = util_pool_allocator(pool); + va_list ap; + va_copy(ap, args); + sstr_t str = ucx_vasprintf(&a, fmt, ap); + *s = str.ptr; + return str.length; +} + +NSAPI_PUBLIC int util_asprintf(pool_handle_t *pool, char **s, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + return util_vasprintf(pool, s, fmt, args); +} + NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args) { return vsprintf(s, fmt, args); @@ -394,7 +413,7 @@ return def; } -int util_getboolean_s(sstr_t s, int def) { +int util_getboolean_s(scstr_t s, int def) { if(s.length == 0) { return def; } @@ -407,7 +426,7 @@ return def; } -NSAPI_PUBLIC int util_strtoint(char *str, int64_t *value) { +NSAPI_PUBLIC int util_strtoint(const char *str, int64_t *value) { char *end; errno = 0; int64_t val = strtoll(str, &end, 0); @@ -419,6 +438,41 @@ } } +NSAPI_PUBLIC const char* util_resource_name(const char *url) { + scstr_t urlstr = scstr(url); + if(urlstr.ptr[urlstr.length-1] == '/') { + urlstr.length--; + } + scstr_t resname = scstrrchr(urlstr, '/'); + if(resname.length > 1) { + return resname.ptr+1; + } else { + return url; + } +} + +NSAPI_PUBLIC char* util_parent_path(const char *path) { + char *name = (char*)util_resource_name((char*)path); + size_t namelen = strlen(name); + size_t pathlen = strlen(path); + size_t parentlen = pathlen - namelen; + char *parent = MALLOC(parentlen + 1); + memcpy(parent, path, parentlen); + parent[parentlen] = '\0'; + return parent; +} + +NSAPI_PUBLIC char* util_parent_path_pool(pool_handle_t *pool, const char *path) { + // maybe we can unify this function with util_parent_path + char *name = (char*)util_resource_name((char*)path); + size_t namelen = strlen(name); + size_t pathlen = strlen(path); + size_t parentlen = pathlen - namelen; + char *parent = pool_malloc(pool, parentlen + 1); + memcpy(parent, path, parentlen); + parent[parentlen] = '\0'; + return parent; +} /* ------------------------------ util_itoa ------------------------------- */ /* @@ -1033,3 +1087,97 @@ return pos; } + + +/* ------------------------- util_html_escape -------------------------- */ + +NSAPI_PUBLIC char *util_html_escape(const char *s) +{ + const char *in; + + int len = 0; + for (in = s; *in; in++) { + switch (*in) { + case '<': + len += 4; // < + break; + case '>': + len += 4; // > + break; + case '&': + len += 5; // & + break; + case '"': + len += 6; // " + break; + case '\'': + len += 6; // ' + break; + case '+': + len += 5; // + + break; + default: + len++; + break; + } + } + + char *ns = (char *) MALLOC(len + 1); + if (!ns) + return ns; + + char *out = ns; + for (in = s; *in; in++) { + switch (*in) { + case '<': + *out++ = '&'; + *out++ = 'l'; + *out++ = 't'; + *out++ = ';'; + break; + case '>': + *out++ = '&'; + *out++ = 'g'; + *out++ = 't'; + *out++ = ';'; + break; + case '&': + *out++ = '&'; + *out++ = 'a'; + *out++ = 'm'; + *out++ = 'p'; + *out++ = ';'; + break; + case '"': + *out++ = '&'; + *out++ = 'q'; + *out++ = 'u'; + *out++ = 'o'; + *out++ = 't'; + *out++ = ';'; + break; + case '\'': + *out++ = '&'; + *out++ = 'a'; + *out++ = 'p'; + *out++ = 'o'; + *out++ = 's'; + *out++ = ';'; + break; + case '+': + *out++ = '&'; + *out++ = '#'; + *out++ = '4'; + *out++ = '3'; + *out++ = ';'; + break; + default: + *out++ = *in; + break; + } + } + *out = '\0'; + + return ns; +} +