HttpRouter: Decode path parts before matching. #19

Merged
jordan merged 2 commits from lda/Cytoplasm:encode-http into master 2023-12-02 15:25:29 +00:00

View file

@ -23,6 +23,7 @@
*/ */
#include <HttpRouter.h> #include <HttpRouter.h>
#include <Http.h>
#include <Memory.h> #include <Memory.h>
#include <HashMap.h> #include <HashMap.h>
#include <Str.h> #include <Str.h>
@ -228,12 +229,15 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
regmatch_t pmatch[REG_MAX_SUB]; regmatch_t pmatch[REG_MAX_SUB];
pathPart = HttpUrlDecode(pathPart);
i = 0; i = 0;
while (HashMapIterateReentrant(node->children, &key, (void **) &val, &i)) while (HashMapIterateReentrant(node->children, &key, (void **) &val, &i))
{ {
if (regexec(&val->regex, pathPart, REG_MAX_SUB, pmatch, 0) == 0) if (regexec(&val->regex, pathPart, REG_MAX_SUB, pmatch, 0) == 0)
{ {
Free(pathPart);
break; break;
} }
@ -243,6 +247,7 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
if (!val) if (!val)
{ {
exec = NULL; exec = NULL;
Free(pathPart);
break; break;
} }
@ -263,12 +268,14 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
substr = StrSubstr(pathPart, cpmatch.rm_so, cpmatch.rm_eo); substr = StrSubstr(pathPart, cpmatch.rm_so, cpmatch.rm_eo);
if (pmatch[i].rm_so == -1) if (pmatch[i].rm_so == -1)
{ {
lda marked this conversation as resolved Outdated

I think the decoding should happen before the matching. It would be confusing to match on an encoded path part but then have the decoded part be something totally different. So, we should decode the pathPart before feeding it into regexec(), match on that, and then take the appropriate substring from the decoded path part.

I think the decoding should happen *before* the matching. It would be confusing to match on an encoded path part but then have the decoded part be something totally different. So, we should decode the `pathPart` before feeding it into `regexec()`, match on that, and then take the appropriate substring from the decoded path part.
Outdated
Review

Fixed!

Fixed!
Free(pathPart);
break; break;
} }
ArrayAdd(matches, substr); ArrayAdd(matches, substr);
} }
} }
Free(pathPart);
} }
Free(path); Free(path);
} }