Просмотр исходного кода

static_get() and history_get() both share the same code.

default 3 лет назад
Родитель
Сommit
5c7b26bcdc
2 измененных файлов с 31 добавлено и 30 удалено
  1. 25 27
      data.c
  2. 6 3
      html.c

+ 25 - 27
data.c

@@ -1538,21 +1538,10 @@ int limited(snac *user, const char *id, int cmd)
 
 /** static data **/
 
-xs_str *_static_fn(snac *snac, const char *id)
-/* gets the filename for a static file */
+static int _load_raw_file(const char *fn, xs_val **data, int *size,
+                        const char *inm, xs_str **etag)
+/* loads a cached file */
 {
-    if (strchr(id, '/'))
-        return NULL;
-    else
-        return xs_fmt("%s/static/%s", snac->basedir, id);
-}
-
-
-int static_get(snac *snac, const char *id, xs_val **data, int *size,
-                const char *inm, xs_str **etag)
-/* returns static content */
-{
-    xs *fn = _static_fn(snac, id);
     int status = 404;
 
     if (fn) {
@@ -1584,7 +1573,7 @@ int static_get(snac *snac, const char *id, xs_val **data, int *size,
             if (etag != NULL)
                 *etag = xs_dup(e);
 
-            srv_debug(1, xs_fmt("static_get(): %s %d", id, status));
+            srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
         }
     }
 
@@ -1592,6 +1581,26 @@ int static_get(snac *snac, const char *id, xs_val **data, int *size,
 }
 
 
+xs_str *_static_fn(snac *snac, const char *id)
+/* gets the filename for a static file */
+{
+    if (strchr(id, '/'))
+        return NULL;
+    else
+        return xs_fmt("%s/static/%s", snac->basedir, id);
+}
+
+
+int static_get(snac *snac, const char *id, xs_val **data, int *size,
+                const char *inm, xs_str **etag)
+/* returns static content */
+{
+    xs *fn = _static_fn(snac, id);
+
+    return _load_raw_file(fn, data, size, inm, etag);
+}
+
+
 void static_put(snac *snac, const char *id, const char *data, int size)
 /* writes status content */
 {
@@ -1685,19 +1694,8 @@ int history_get(snac *snac, const char *id, xs_str **content, int *size,
                 const char *inm, xs_str **etag)
 {
     xs *fn = _history_fn(snac, id);
-    FILE *f;
-    int status = 404;
-
-    if (fn && (f = fopen(fn, "r")) != NULL) {
-        *content = xs_readall(f);
-        fclose(f);
 
-        *size = strlen(*content);
-
-        status = 200;
-    }
-
-    return status;
+    return _load_raw_file(fn, content, size, inm, etag);
 }
 
 

+ 6 - 3
html.c

@@ -1875,7 +1875,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
         if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
             snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
 
-            status = history_get(&snac, h, body, b_size, NULL, NULL);
+            status = history_get(&snac, h, body, b_size,
+                        xs_dict_get(req, "if-none-match"), etag);
         }
         else {
             xs *list = timeline_list(&snac, "public", skip, show);
@@ -1903,7 +1904,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
             if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
                 snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
 
-                status = history_get(&snac, "timeline.html_", body, b_size, NULL, NULL);
+                status = history_get(&snac, "timeline.html_", body, b_size,
+                            xs_dict_get(req, "if-none-match"), etag);
             }
             else {
                 snac_debug(&snac, 1, xs_fmt("building timeline"));
@@ -1992,7 +1994,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
                 status = 404;
             }
             else
-                status = history_get(&snac, id, body, b_size, NULL, NULL);
+                status = history_get(&snac, id, body, b_size,
+                            xs_dict_get(req, "if-none-match"), etag);
         }
     }
     else