summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authorGravatar default2023-07-02 11:11:01 +0200
committerGravatar default2023-07-02 11:11:01 +0200
commitd343b40ee553de8d98c18d3547e6c9b12ab96b48 (patch)
tree2f2d9ab495d2ad4e8187f31530d8019968b905bf /data.c
parentFixed footer link. (diff)
downloadsnac2-d343b40ee553de8d98c18d3547e6c9b12ab96b48.tar.gz
snac2-d343b40ee553de8d98c18d3547e6c9b12ab96b48.tar.xz
snac2-d343b40ee553de8d98c18d3547e6c9b12ab96b48.zip
Added HTTP caching to static data.
Diffstat (limited to 'data.c')
-rw-r--r--data.c39
1 files changed, 32 insertions, 7 deletions
diff --git a/data.c b/data.c
index 117e16f..87cfc98 100644
--- a/data.c
+++ b/data.c
@@ -1533,19 +1533,44 @@ xs_str *_static_fn(snac *snac, const char *id)
1533} 1533}
1534 1534
1535 1535
1536int static_get(snac *snac, const char *id, xs_val **data, int *size) 1536int static_get(snac *snac, const char *id, xs_val **data, int *size,
1537 const char *inm, xs_str **etag)
1537/* returns static content */ 1538/* returns static content */
1538{ 1539{
1539 xs *fn = _static_fn(snac, id); 1540 xs *fn = _static_fn(snac, id);
1540 FILE *f;
1541 int status = 404; 1541 int status = 404;
1542 1542
1543 if (fn && (f = fopen(fn, "rb")) != NULL) { 1543 if (fn) {
1544 *size = XS_ALL; 1544 double tm = mtime(fn);
1545 *data = xs_read(f, size);
1546 fclose(f);
1547 1545
1548 status = 200; 1546 if (tm > 0.0) {
1547 /* file exists; build the etag */
1548 xs *e = xs_fmt("W/\"snac-%.0lf\"", tm);
1549
1550 /* if if-none-match is set, check if it's the same */
1551 if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
1552 /* client has the newest version */
1553 status = 304;
1554 }
1555 else {
1556 /* newer or never downloaded; read the full file */
1557 FILE *f;
1558
1559 if ((f = fopen(fn, "rb")) != NULL) {
1560 *size = XS_ALL;
1561 *data = xs_read(f, size);
1562 fclose(f);
1563
1564 status = 200;
1565 }
1566 }
1567
1568 /* if caller wants the etag, return it */
1569 if (etag != NULL)
1570 *etag = xs_dup(e);
1571
1572 srv_debug(1, xs_fmt("static_get(): %d %s %s", status, id, e));
1573 }
1549 } 1574 }
1550 1575
1551 return status; 1576 return status;