summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2023-08-19 09:52:04 +0200
committerGravatar default2023-08-19 09:52:04 +0200
commit5c7b26bcdcac85433f45e93a934525ecf1e92414 (patch)
tree82d0000c4a17bc8f09e9ec725cd235f1012135f4
parentMerge branch 'master' of triptico.com:git/snac2 (diff)
downloadsnac2-5c7b26bcdcac85433f45e93a934525ecf1e92414.tar.gz
snac2-5c7b26bcdcac85433f45e93a934525ecf1e92414.tar.xz
snac2-5c7b26bcdcac85433f45e93a934525ecf1e92414.zip
static_get() and history_get() both share the same code.
-rw-r--r--data.c52
-rw-r--r--html.c9
2 files changed, 31 insertions, 30 deletions
diff --git a/data.c b/data.c
index ecc227e..9c335be 100644
--- a/data.c
+++ b/data.c
@@ -1538,21 +1538,10 @@ int limited(snac *user, const char *id, int cmd)
1538 1538
1539/** static data **/ 1539/** static data **/
1540 1540
1541xs_str *_static_fn(snac *snac, const char *id) 1541static int _load_raw_file(const char *fn, xs_val **data, int *size,
1542/* gets the filename for a static file */ 1542 const char *inm, xs_str **etag)
1543/* loads a cached file */
1543{ 1544{
1544 if (strchr(id, '/'))
1545 return NULL;
1546 else
1547 return xs_fmt("%s/static/%s", snac->basedir, id);
1548}
1549
1550
1551int static_get(snac *snac, const char *id, xs_val **data, int *size,
1552 const char *inm, xs_str **etag)
1553/* returns static content */
1554{
1555 xs *fn = _static_fn(snac, id);
1556 int status = 404; 1545 int status = 404;
1557 1546
1558 if (fn) { 1547 if (fn) {
@@ -1584,7 +1573,7 @@ int static_get(snac *snac, const char *id, xs_val **data, int *size,
1584 if (etag != NULL) 1573 if (etag != NULL)
1585 *etag = xs_dup(e); 1574 *etag = xs_dup(e);
1586 1575
1587 srv_debug(1, xs_fmt("static_get(): %s %d", id, status)); 1576 srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
1588 } 1577 }
1589 } 1578 }
1590 1579
@@ -1592,6 +1581,26 @@ int static_get(snac *snac, const char *id, xs_val **data, int *size,
1592} 1581}
1593 1582
1594 1583
1584xs_str *_static_fn(snac *snac, const char *id)
1585/* gets the filename for a static file */
1586{
1587 if (strchr(id, '/'))
1588 return NULL;
1589 else
1590 return xs_fmt("%s/static/%s", snac->basedir, id);
1591}
1592
1593
1594int static_get(snac *snac, const char *id, xs_val **data, int *size,
1595 const char *inm, xs_str **etag)
1596/* returns static content */
1597{
1598 xs *fn = _static_fn(snac, id);
1599
1600 return _load_raw_file(fn, data, size, inm, etag);
1601}
1602
1603
1595void static_put(snac *snac, const char *id, const char *data, int size) 1604void static_put(snac *snac, const char *id, const char *data, int size)
1596/* writes status content */ 1605/* writes status content */
1597{ 1606{
@@ -1685,19 +1694,8 @@ int history_get(snac *snac, const char *id, xs_str **content, int *size,
1685 const char *inm, xs_str **etag) 1694 const char *inm, xs_str **etag)
1686{ 1695{
1687 xs *fn = _history_fn(snac, id); 1696 xs *fn = _history_fn(snac, id);
1688 FILE *f;
1689 int status = 404;
1690
1691 if (fn && (f = fopen(fn, "r")) != NULL) {
1692 *content = xs_readall(f);
1693 fclose(f);
1694 1697
1695 *size = strlen(*content); 1698 return _load_raw_file(fn, content, size, inm, etag);
1696
1697 status = 200;
1698 }
1699
1700 return status;
1701} 1699}
1702 1700
1703 1701
diff --git a/html.c b/html.c
index 19ab6b7..11c1918 100644
--- a/html.c
+++ b/html.c
@@ -1875,7 +1875,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
1875 if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) { 1875 if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
1876 snac_debug(&snac, 1, xs_fmt("serving cached local timeline")); 1876 snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
1877 1877
1878 status = history_get(&snac, h, body, b_size, NULL, NULL); 1878 status = history_get(&snac, h, body, b_size,
1879 xs_dict_get(req, "if-none-match"), etag);
1879 } 1880 }
1880 else { 1881 else {
1881 xs *list = timeline_list(&snac, "public", skip, show); 1882 xs *list = timeline_list(&snac, "public", skip, show);
@@ -1903,7 +1904,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
1903 if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) { 1904 if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
1904 snac_debug(&snac, 1, xs_fmt("serving cached timeline")); 1905 snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
1905 1906
1906 status = history_get(&snac, "timeline.html_", body, b_size, NULL, NULL); 1907 status = history_get(&snac, "timeline.html_", body, b_size,
1908 xs_dict_get(req, "if-none-match"), etag);
1907 } 1909 }
1908 else { 1910 else {
1909 snac_debug(&snac, 1, xs_fmt("building timeline")); 1911 snac_debug(&snac, 1, xs_fmt("building timeline"));
@@ -1992,7 +1994,8 @@ int html_get_handler(const xs_dict *req, const char *q_path,
1992 status = 404; 1994 status = 404;
1993 } 1995 }
1994 else 1996 else
1995 status = history_get(&snac, id, body, b_size, NULL, NULL); 1997 status = history_get(&snac, id, body, b_size,
1998 xs_dict_get(req, "if-none-match"), etag);
1996 } 1999 }
1997 } 2000 }
1998 else 2001 else