summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--httpd.c5
-rw-r--r--mastoapi.c31
-rw-r--r--snac.h2
3 files changed, 35 insertions, 3 deletions
diff --git a/httpd.c b/httpd.c
index 81d2f9e..0eff657 100644
--- a/httpd.c
+++ b/httpd.c
@@ -279,6 +279,7 @@ void httpd_connection(FILE *f)
279 xs *payload = NULL; 279 xs *payload = NULL;
280 xs *etag = NULL; 280 xs *etag = NULL;
281 xs *last_modified = NULL; 281 xs *last_modified = NULL;
282 xs *link = NULL;
282 int p_size = 0; 283 int p_size = 0;
283 const char *p; 284 const char *p;
284 int fcgi_id; 285 int fcgi_id;
@@ -326,7 +327,7 @@ void httpd_connection(FILE *f)
326 status = oauth_get_handler(req, q_path, &body, &b_size, &ctype); 327 status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
327 328
328 if (status == 0) 329 if (status == 0)
329 status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype); 330 status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype, &link);
330#endif /* NO_MASTODON_API */ 331#endif /* NO_MASTODON_API */
331 332
332 if (status == 0) 333 if (status == 0)
@@ -426,6 +427,8 @@ void httpd_connection(FILE *f)
426 headers = xs_dict_append(headers, "etag", etag); 427 headers = xs_dict_append(headers, "etag", etag);
427 if (!xs_is_null(last_modified)) 428 if (!xs_is_null(last_modified))
428 headers = xs_dict_append(headers, "last-modified", last_modified); 429 headers = xs_dict_append(headers, "last-modified", last_modified);
430 if (!xs_is_null(link))
431 headers = xs_dict_append(headers, "Link", link);
429 432
430 /* if there are any additional headers, add them */ 433 /* if there are any additional headers, add them */
431 const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers"); 434 const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers");
diff --git a/mastoapi.c b/mastoapi.c
index be8be80..6b816f4 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -1434,8 +1434,35 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn
1434} 1434}
1435 1435
1436 1436
1437xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
1438/* returns a Link header with paging information */
1439{
1440 xs_str *s = NULL;
1441
1442 if (xs_list_len(timeline) == 0)
1443 return NULL;
1444
1445 const xs_dict *first_st = xs_list_get(timeline, 0);
1446 const xs_dict *last_st = xs_list_get(timeline, -1);
1447 const char *first_id = xs_dict_get(first_st, "id");
1448 const char *last_id = xs_dict_get(last_st, "id");
1449 const char *host = xs_dict_get(srv_config, "host");
1450 const char *protocol = xs_dict_get_def(srv_config, "protocol", "https");
1451
1452 s = xs_fmt(
1453 "<%s:/" "/%s%s?max_id=%s>; rel=\"next\", "
1454 "<%s:/" "/%s%s?since_id=%s>; rel=\"prev\"",
1455 protocol, host, endpoint, last_id,
1456 protocol, host, endpoint, first_id);
1457
1458 srv_debug(1, xs_fmt("timeline_link_header %s", s));
1459
1460 return s;
1461}
1462
1463
1437int mastoapi_get_handler(const xs_dict *req, const char *q_path, 1464int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1438 char **body, int *b_size, char **ctype) 1465 char **body, int *b_size, char **ctype, xs_str **link)
1439{ 1466{
1440 (void)b_size; 1467 (void)b_size;
1441 1468
@@ -1695,6 +1722,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1695 xs *ifn = user_index_fn(&snac1, "private"); 1722 xs *ifn = user_index_fn(&snac1, "private");
1696 xs *out = mastoapi_timeline(&snac1, args, ifn); 1723 xs *out = mastoapi_timeline(&snac1, args, ifn);
1697 1724
1725 *link = timeline_link_header(cmd, out);
1726
1698 *body = xs_json_dumps(out, 4); 1727 *body = xs_json_dumps(out, 4);
1699 *ctype = "application/json"; 1728 *ctype = "application/json";
1700 status = HTTP_STATUS_OK; 1729 status = HTTP_STATUS_OK;
diff --git a/snac.h b/snac.h
index acc175f..5b8b2f8 100644
--- a/snac.h
+++ b/snac.h
@@ -384,7 +384,7 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
384 const char *payload, int p_size, 384 const char *payload, int p_size,
385 char **body, int *b_size, char **ctype); 385 char **body, int *b_size, char **ctype);
386int mastoapi_get_handler(const xs_dict *req, const char *q_path, 386int mastoapi_get_handler(const xs_dict *req, const char *q_path,
387 char **body, int *b_size, char **ctype); 387 char **body, int *b_size, char **ctype, xs_str **link);
388int mastoapi_post_handler(const xs_dict *req, const char *q_path, 388int mastoapi_post_handler(const xs_dict *req, const char *q_path,
389 const char *payload, int p_size, 389 const char *payload, int p_size,
390 char **body, int *b_size, char **ctype); 390 char **body, int *b_size, char **ctype);