commit 5c7b26b

default  ·  2023-08-19 07:52:04 +0000 UTC
parent 3bdc484
static_get() and history_get() both share the same code.
2 files changed,  +31, -30
M data.c
M html.c
M data.c
+25, -27
 1@@ -1538,21 +1538,10 @@ int limited(snac *user, const char *id, int cmd)
 2 
 3 /** static data **/
 4 
 5-xs_str *_static_fn(snac *snac, const char *id)
 6-/* gets the filename for a static file */
 7+static int _load_raw_file(const char *fn, xs_val **data, int *size,
 8+                        const char *inm, xs_str **etag)
 9+/* loads a cached file */
10 {
11-    if (strchr(id, '/'))
12-        return NULL;
13-    else
14-        return xs_fmt("%s/static/%s", snac->basedir, id);
15-}
16-
17-
18-int static_get(snac *snac, const char *id, xs_val **data, int *size,
19-                const char *inm, xs_str **etag)
20-/* returns static content */
21-{
22-    xs *fn = _static_fn(snac, id);
23     int status = 404;
24 
25     if (fn) {
26@@ -1584,7 +1573,7 @@ int static_get(snac *snac, const char *id, xs_val **data, int *size,
27             if (etag != NULL)
28                 *etag = xs_dup(e);
29 
30-            srv_debug(1, xs_fmt("static_get(): %s %d", id, status));
31+            srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
32         }
33     }
34 
35@@ -1592,6 +1581,26 @@ int static_get(snac *snac, const char *id, xs_val **data, int *size,
36 }
37 
38 
39+xs_str *_static_fn(snac *snac, const char *id)
40+/* gets the filename for a static file */
41+{
42+    if (strchr(id, '/'))
43+        return NULL;
44+    else
45+        return xs_fmt("%s/static/%s", snac->basedir, id);
46+}
47+
48+
49+int static_get(snac *snac, const char *id, xs_val **data, int *size,
50+                const char *inm, xs_str **etag)
51+/* returns static content */
52+{
53+    xs *fn = _static_fn(snac, id);
54+
55+    return _load_raw_file(fn, data, size, inm, etag);
56+}
57+
58+
59 void static_put(snac *snac, const char *id, const char *data, int size)
60 /* writes status content */
61 {
62@@ -1685,19 +1694,8 @@ int history_get(snac *snac, const char *id, xs_str **content, int *size,
63                 const char *inm, xs_str **etag)
64 {
65     xs *fn = _history_fn(snac, id);
66-    FILE *f;
67-    int status = 404;
68-
69-    if (fn && (f = fopen(fn, "r")) != NULL) {
70-        *content = xs_readall(f);
71-        fclose(f);
72 
73-        *size = strlen(*content);
74-
75-        status = 200;
76-    }
77-
78-    return status;
79+    return _load_raw_file(fn, content, size, inm, etag);
80 }
81 
82 
M html.c
+6, -3
 1@@ -1875,7 +1875,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
 2         if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
 3             snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
 4 
 5-            status = history_get(&snac, h, body, b_size, NULL, NULL);
 6+            status = history_get(&snac, h, body, b_size,
 7+                        xs_dict_get(req, "if-none-match"), etag);
 8         }
 9         else {
10             xs *list = timeline_list(&snac, "public", skip, show);
11@@ -1903,7 +1904,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
12             if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
13                 snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
14 
15-                status = history_get(&snac, "timeline.html_", body, b_size, NULL, NULL);
16+                status = history_get(&snac, "timeline.html_", body, b_size,
17+                            xs_dict_get(req, "if-none-match"), etag);
18             }
19             else {
20                 snac_debug(&snac, 1, xs_fmt("building timeline"));
21@@ -1992,7 +1994,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
22                 status = 404;
23             }
24             else
25-                status = history_get(&snac, id, body, b_size, NULL, NULL);
26+                status = history_get(&snac, id, body, b_size,
27+                            xs_dict_get(req, "if-none-match"), etag);
28         }
29     }
30     else