Skip to content

Commit d1d85fa

Browse files
mrodgers-witekiokartben
authored andcommitted
net: http_server: fix URL matching with '?' character in resource
Fixes zephyrproject-rtos#84198. If a '?' character is used as part of a wildcard resource, do not treat this as the end of the string when comparing with a path from the HTTP request. Only the path from the HTTP request may be terminated by '?' (in the case of a request with query parameters). Signed-off-by: Matt Rodgers <[email protected]>
1 parent 8ef1b34 commit d1d85fa

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

subsys/net/lib/http/http_server_core.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -700,16 +700,19 @@ static int http_server_run(struct http_server_ctx *ctx)
700700
return ret;
701701
}
702702

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

711-
/* Check if both strings have reached their terminators or '?' */
712-
if ((*s1 == '\0' || *s1 == '?') && (*s2 == '\0' || *s2 == '?')) {
714+
/* Check if both strings have reached their terminators */
715+
if ((*path == '\0' || *path == '?') && (*resource == '\0')) {
713716
return 0; /* Strings are equal */
714717
}
715718

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ ZTEST(http_service, test_HTTP_RESOURCE_WILDCARD)
342342
zassert_true(len > 0, "Length not set");
343343
zassert_equal(res, RES(3), "Resource mismatch");
344344

345+
res = CHECK_PATH(service_D, "/fb", &len);
346+
zassert_is_null(res, "Resource found");
347+
zassert_equal(len, 0, "Length set");
348+
345349
res = CHECK_PATH(service_A, "/fs/index.html", &len);
346350
zassert_not_null(res, "Cannot find resource");
347351
zassert_true(len > 0, "Length not set");

0 commit comments

Comments
 (0)