Sun, 22 Feb 2026 09:24:41 +0100
add util_uri_escape_s function
--- a/src/server/test/main.c Sat Feb 21 21:52:08 2026 +0100 +++ b/src/server/test/main.c Sun Feb 22 09:24:41 2026 +0100 @@ -87,6 +87,8 @@ cx_test_register(suite, test_util_uri_escape_kanji); cx_test_register(suite, test_util_parse_uri); cx_test_register(suite, test_util_parse_uri_error); + cx_test_register(suite, test_util_uri_escape_s); + cx_test_register(suite, test_util_uri_escape_s_error); cx_test_register(suite, test_pblock_iterator); // httpparser tests @@ -201,7 +203,7 @@ cx_test_register(suite, test_webdav_put); // saf tests - http_reverse_proxy_add_tests(suite); + // http tests http_client_add_tests(suite);
--- a/src/server/test/uri.c Sat Feb 21 21:52:08 2026 +0100 +++ b/src/server/test/uri.c Sun Feb 22 09:24:41 2026 +0100 @@ -190,3 +190,43 @@ CX_TEST_ASSERT(!util_parse_uri("http://localhost:9999999", &uri)); } } + +CX_TEST(test_util_uri_escape_s) { + CX_TEST_DO { + char out[64]; + memset(out, 'x', 64); + CX_TEST_ASSERT(util_uri_escape_s(out, 64, "") == 0); + CX_TEST_ASSERT(out[0] == 0); + + CX_TEST_ASSERT(util_uri_escape_s(out, 64, "/") == 1); + CX_TEST_ASSERT(out[0] == '/' && out[1] == 0); + + CX_TEST_ASSERT(util_uri_escape_s(out, 64, "/copy/path") == 10); + CX_TEST_ASSERT(out[10] == 0); + CX_TEST_ASSERT(!strcmp(out, "/copy/path")); + + CX_TEST_ASSERT(util_uri_escape_s(out, 64, "/wspace /") == 11); + CX_TEST_ASSERT(out[11] == 0); + CX_TEST_ASSERT(!strcmp(out, "/wspace%20/")); + + CX_TEST_ASSERT(util_uri_escape_s(out, 64, "<>%&*") == 15); + CX_TEST_ASSERT(out[15] == 0); + CX_TEST_ASSERT(!strcasecmp(out, "%3c%3e%25%26%2a")); + } +} + +CX_TEST(test_util_uri_escape_s_error) { + CX_TEST_DO { + char out[64]; + memset(out, 'x', 64); + CX_TEST_ASSERT(util_uri_escape_s(out, 0, "X") == 0); + CX_TEST_ASSERT(util_uri_escape_s(out, 1, "X") == 0); + CX_TEST_ASSERT(util_uri_escape_s(out, 2, "/*") == 0); + CX_TEST_ASSERT(util_uri_escape_s(out, 3, "/*") == 0); + CX_TEST_ASSERT(util_uri_escape_s(out, 4, "/*") == 0); + CX_TEST_ASSERT(util_uri_escape_s(out, 12, "<>%&*") == 0); + CX_TEST_ASSERT(util_uri_escape_s(out, 13, "<>%&*") == 0); + CX_TEST_ASSERT(util_uri_escape_s(out, 14, "<>%&*") == 0); + + } +}
--- a/src/server/test/uri.h Sat Feb 21 21:52:08 2026 +0100 +++ b/src/server/test/uri.h Sun Feb 22 09:24:41 2026 +0100 @@ -42,9 +42,10 @@ CX_TEST(test_util_uri_escape_space); CX_TEST(test_util_uri_escape_latin); CX_TEST(test_util_uri_escape_kanji); - CX_TEST(test_util_parse_uri); CX_TEST(test_util_parse_uri_error); +CX_TEST(test_util_uri_escape_s); +CX_TEST(test_util_uri_escape_s_error); #ifdef __cplusplus }
--- a/src/server/util/uri.c Sat Feb 21 21:52:08 2026 +0100 +++ b/src/server/util/uri.c Sun Feb 22 09:24:41 2026 +0100 @@ -267,6 +267,37 @@ return od; } +NSAPI_PUBLIC size_t util_uri_escape_s(char *buf, size_t len, const char *s) { + char *d = buf; + ssize_t dlen = len; + if(dlen <= 0) { + return 0; + } + + while (*s) { + if (strchr("% ?#:+&*\"'<>\r\n", *s)) { + snprintf(d, dlen, "%%%02x", (unsigned char)*s); + ++s; + d += 3; + dlen -= 3; + } else if (0x80 & *s) { + snprintf(d, dlen, "%%%02x", (unsigned char)*s); + ++s; + d += 3; + dlen -= 3; + } else { + *d++ = *s++; + dlen--; + } + + if(dlen <= 0) { + return 0; + } + } + *d = '\0'; + return d - buf; +} + /* --------------------------- util_url_escape ---------------------------- */
--- a/src/server/util/util.h Sat Feb 21 21:52:08 2026 +0100 +++ b/src/server/util/util.h Sun Feb 22 09:24:41 2026 +0100 @@ -166,6 +166,9 @@ NSAPI_PUBLIC char *INTutil_uri_escape(char *d, const char *s); +// new +NSAPI_PUBLIC size_t util_uri_escape_s(char *buf, size_t len, const char *s); + NSAPI_PUBLIC char *INTutil_uri_strip_params(char *uri); NSAPI_PUBLIC char* util_canonicalize_uri(pool_handle_t *pool, const char *uri, int len, int *pcanonlen);