Browse Source

Merge pull request 'static files: allow files in a subdirectory' (#541) from lbr/snac2:master into master

Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/541
grunfink 3 months ago
parent
commit
cf8f02765a
2 changed files with 10 additions and 7 deletions
  1. 9 6
      data.c
  2. 1 1
      html.c

+ 9 - 6
data.c

@@ -2673,14 +2673,17 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
             }
             else {
                 /* newer or never downloaded; read the full file */
+                struct stat sb;
                 FILE *f;
 
-                if ((f = fopen(fn, "rb")) != NULL) {
-                    *size = XS_ALL;
-                    *data = xs_read(f, size);
-                    fclose(f);
+                if (lstat(fn, &sb) == 0 && (sb.st_mode&S_IFMT) == S_IFREG) {
+                    if ((f = fopen(fn, "rb")) != NULL) {
+                        *size = XS_ALL;
+                        *data = xs_read(f, size);
+                        fclose(f);
 
-                    status = HTTP_STATUS_OK;
+                        status = HTTP_STATUS_OK;
+                    }
                 }
             }
 
@@ -2699,7 +2702,7 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
 xs_str *_static_fn(snac *snac, const char *id)
 /* gets the filename for a static file */
 {
-    if (strchr(id, '/'))
+    if (strstr(id, ".."))
         return NULL;
     else
         return xs_fmt("%s/static/%s", snac->basedir, id);

+ 1 - 1
html.c

@@ -5073,7 +5073,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
     }
     else
     if (xs_startswith(p_path, "s/")) { /** a static file **/
-        xs *l    = xs_split(p_path, "/");
+        xs *l    = xs_split_n(p_path, "/", 1);
         const char *id = xs_list_get(l, 1);
         int sz;