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

This commit is contained in:
default 2023-08-19 09:52:04 +02:00
parent 3bdc484206
commit 5c7b26bcdc
2 changed files with 31 additions and 30 deletions

52
data.c
View file

@ -1538,21 +1538,10 @@ int limited(snac *user, const char *id, int cmd)
/** static data **/ /** static data **/
xs_str *_static_fn(snac *snac, const char *id) static int _load_raw_file(const char *fn, xs_val **data, int *size,
/* gets the filename for a static file */ 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; int status = 404;
if (fn) { if (fn) {
@ -1584,7 +1573,7 @@ int static_get(snac *snac, const char *id, xs_val **data, int *size,
if (etag != NULL) if (etag != NULL)
*etag = xs_dup(e); *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) void static_put(snac *snac, const char *id, const char *data, int size)
/* writes status content */ /* 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) const char *inm, xs_str **etag)
{ {
xs *fn = _history_fn(snac, id); xs *fn = _history_fn(snac, id);
FILE *f;
int status = 404;
if (fn && (f = fopen(fn, "r")) != NULL) { return _load_raw_file(fn, content, size, inm, etag);
*content = xs_readall(f);
fclose(f);
*size = strlen(*content);
status = 200;
}
return status;
} }

9
html.c
View file

@ -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)) { if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
snac_debug(&snac, 1, xs_fmt("serving cached local timeline")); 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 { else {
xs *list = timeline_list(&snac, "public", skip, show); 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)) { if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
snac_debug(&snac, 1, xs_fmt("serving cached timeline")); 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 { else {
snac_debug(&snac, 1, xs_fmt("building timeline")); 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; status = 404;
} }
else 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 else