Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: http_server: fix URL matching with '?' character in resource #84226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions subsys/net/lib/http/http_server_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,16 +700,19 @@ static int http_server_run(struct http_server_ctx *ctx)
return ret;
}

/* Compare two strings where the terminator is either "\0" or "?" */
static int compare_strings(const char *s1, const char *s2)
/* Compare a path and a resource string. The path string comes from the HTTP request and may be
* terminated by either '?' or '\0'. The resource string is registered along with the resource and
* may only be terminated by `\0`.
*/
static int compare_strings(const char *path, const char *resource)
{
while ((*s1 && *s2) && (*s1 == *s2) && (*s1 != '?')) {
s1++;
s2++;
while ((*path && *resource) && (*path == *resource) && (*path != '?')) {
path++;
resource++;
}

/* Check if both strings have reached their terminators or '?' */
if ((*s1 == '\0' || *s1 == '?') && (*s2 == '\0' || *s2 == '?')) {
/* Check if both strings have reached their terminators */
if ((*path == '\0' || *path == '?') && (*resource == '\0')) {
return 0; /* Strings are equal */
}

Expand Down
4 changes: 4 additions & 0 deletions tests/net/lib/http_server/common/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ ZTEST(http_service, test_HTTP_RESOURCE_WILDCARD)
zassert_true(len > 0, "Length not set");
zassert_equal(res, RES(3), "Resource mismatch");

res = CHECK_PATH(service_D, "/fb", &len);
zassert_is_null(res, "Resource found");
zassert_equal(len, 0, "Length set");

res = CHECK_PATH(service_A, "/fs/index.html", &len);
zassert_not_null(res, "Cannot find resource");
zassert_true(len > 0, "Length not set");
Expand Down
Loading