Skip to content

Commit 63d789a

Browse files
jukkardkalowsk
authored andcommitted
net: http: server: The detail length of wildcard detail was wrong
The path length of the detail resource was not set properly. Signed-off-by: Jukka Rissanen <[email protected]> (cherry picked from commit 33cf7dc)
1 parent c08390d commit 63d789a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

subsys/net/lib/http/http_server_core.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,17 @@ static int compare_strings(const char *s1, const char *s2)
678678
return 1; /* Strings are not equal */
679679
}
680680

681+
static int path_len_without_query(const char *path)
682+
{
683+
int len = 0;
684+
685+
while ((path[len] != '\0') && (path[len] != '?')) {
686+
len++;
687+
}
688+
689+
return len;
690+
}
691+
681692
static bool skip_this(struct http_resource_desc *resource, bool is_websocket)
682693
{
683694
struct http_resource_detail *detail;
@@ -713,7 +724,7 @@ struct http_resource_detail *get_resource_detail(const char *path,
713724
ret = fnmatch(resource->resource, path,
714725
(FNM_PATHNAME | FNM_LEADING_DIR));
715726
if (ret == 0) {
716-
*path_len = strlen(resource->resource);
727+
*path_len = path_len_without_query(path);
717728
return resource->detail;
718729
}
719730
}

tests/net/lib/http_server/common/src/main.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ HTTP_RESOURCE_DEFINE(resource_6, service_D, "/fo*", RES(1));
7777
HTTP_RESOURCE_DEFINE(resource_7, service_D, "/f[ob]o3.html", RES(1));
7878
HTTP_RESOURCE_DEFINE(resource_8, service_D, "/fb?3.htm", RES(0));
7979
HTTP_RESOURCE_DEFINE(resource_9, service_D, "/f*4.html", RES(3));
80+
HTTP_RESOURCE_DEFINE(resource_11, service_D, "/foo/*", RES(3));
81+
HTTP_RESOURCE_DEFINE(resource_12, service_D, "/foo/b?r", RES(3));
8082

8183
ZTEST(http_service, test_HTTP_SERVICE_DEFINE)
8284
{
@@ -345,6 +347,25 @@ ZTEST(http_service, test_HTTP_RESOURCE_WILDCARD)
345347
zassert_not_null(res, "Cannot find resource");
346348
zassert_true(len > 0, "Length not set");
347349
zassert_equal(res, RES(5), "Resource mismatch");
350+
351+
res = CHECK_PATH("/foo/bar", &len);
352+
zassert_not_null(res, "Resource not found");
353+
zassert_true(len == (sizeof("/foo/bar") - 1), "Length not set correctly");
354+
zassert_equal(res, RES(3), "Resource mismatch");
355+
356+
res = CHECK_PATH("/foo/bar?param=value", &len);
357+
zassert_not_null(res, "Resource not found");
358+
zassert_true(len == (sizeof("/foo/bar") - 1), "Length not set correctly");
359+
zassert_equal(res, RES(3), "Resource mismatch");
360+
361+
res = CHECK_PATH("/bar?foo=value", &len);
362+
zassert_is_null(res, "Resource found");
363+
zassert_equal(len, 0, "Length set");
364+
365+
res = CHECK_PATH("/foo/bar?param=value", &len);
366+
zassert_not_null(res, "Resource not found");
367+
zassert_true(len == (sizeof("/foo/bar") - 1), "Length not set correctly");
368+
zassert_equal(res, RES(3), "Resource mismatch");
348369
}
349370

350371
extern void http_server_get_content_type_from_extension(char *url, char *content_type,

0 commit comments

Comments
 (0)