commit 8ca733d22bc164f60481bf18807d554f91d0691d
parent ae17b4d7c573b4bde4bcfe5fab8ef27c0a5ed17f
Author: Anton Kling <anton@kling.gg>
Date: Fri, 4 Mar 2022 21:41:37 +0100
BUG FIX: smol_http fails to parse certain file extension should their be two.
For example:
main.c.html
would have the extension:
c.html
when it really should be:
html
This was fixed by scanning for the file extension dot backwards instead
of forwards.
Diffstat:
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/smol_http.c b/smol_http.c
@@ -105,12 +105,21 @@ static const struct {
};
const char *const get_mime(const char *file) {
- for (; *file && *file++ != '.';)
+ const char *ext = file;
+ for (; *ext++;) // Move ext to end of string
;
+ for (; ext != file && *(ext - 1) != '.';
+ ext--) // Move ext back until we find a dot
+ ;
+ if (file == ext)
+ goto ret_default; // If there is no dot then there is no file
+ // extension.
+
for (size_t i = 0; i < sizeof(mimes) / sizeof(mimes[0]) - 1; i++)
- if (0 == strcmp(mimes[i].ext, file))
+ if (0 == strcmp(mimes[i].ext, ext))
return mimes[i].type;
+ret_default:
return "application/octet-stream";
}