Skip to content

Commit 5b82a68

Browse files
jukkarnashif
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]>
1 parent cab2f9d commit 5b82a68

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
@@ -651,6 +651,17 @@ static int compare_strings(const char *s1, const char *s2)
651651
return 1; /* Strings are not equal */
652652
}
653653

654+
static int path_len_without_query(const char *path)
655+
{
656+
int len = 0;
657+
658+
while ((path[len] != '\0') && (path[len] != '?')) {
659+
len++;
660+
}
661+
662+
return len;
663+
}
664+
654665
static bool skip_this(struct http_resource_desc *resource, bool is_websocket)
655666
{
656667
struct http_resource_detail *detail;
@@ -685,7 +696,7 @@ struct http_resource_detail *get_resource_detail(const char *path,
685696

686697
ret = fnmatch(resource->resource, path, FNM_PATHNAME);
687698
if (ret == 0) {
688-
*path_len = strlen(resource->resource);
699+
*path_len = path_len_without_query(path);
689700
return resource->detail;
690701
}
691702
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ HTTP_RESOURCE_DEFINE(resource_5, service_D, "/fo*", RES(1));
7272
HTTP_RESOURCE_DEFINE(resource_6, service_D, "/f[ob]o3.html", RES(1));
7373
HTTP_RESOURCE_DEFINE(resource_7, service_D, "/fb?3.htm", RES(0));
7474
HTTP_RESOURCE_DEFINE(resource_8, service_D, "/f*4.html", RES(3));
75+
HTTP_RESOURCE_DEFINE(resource_11, service_D, "/foo/*", RES(3));
76+
HTTP_RESOURCE_DEFINE(resource_12, service_D, "/foo/b?r", RES(3));
7577

7678

7779
ZTEST(http_service, test_HTTP_SERVICE_DEFINE)
@@ -327,6 +329,25 @@ ZTEST(http_service, test_HTTP_RESOURCE_WILDCARD)
327329
zassert_not_null(res, "Cannot find resource");
328330
zassert_true(len > 0, "Length not set");
329331
zassert_equal(res, RES(3), "Resource mismatch");
332+
333+
res = CHECK_PATH("/foo/bar", &len);
334+
zassert_not_null(res, "Resource not found");
335+
zassert_true(len == (sizeof("/foo/bar") - 1), "Length not set correctly");
336+
zassert_equal(res, RES(3), "Resource mismatch");
337+
338+
res = CHECK_PATH("/foo/bar?param=value", &len);
339+
zassert_not_null(res, "Resource not found");
340+
zassert_true(len == (sizeof("/foo/bar") - 1), "Length not set correctly");
341+
zassert_equal(res, RES(3), "Resource mismatch");
342+
343+
res = CHECK_PATH("/bar?foo=value", &len);
344+
zassert_is_null(res, "Resource found");
345+
zassert_equal(len, 0, "Length set");
346+
347+
res = CHECK_PATH("/foo/bar?param=value", &len);
348+
zassert_not_null(res, "Resource not found");
349+
zassert_true(len == (sizeof("/foo/bar") - 1), "Length not set correctly");
350+
zassert_equal(res, RES(3), "Resource mismatch");
330351
}
331352

332353
ZTEST_SUITE(http_service, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)