mirror of
https://github.com/owntone/owntone-server.git
synced 2025-11-22 10:37:44 -05:00
[httpd/jsonapi] Add cache control headers to some json api endpoints
Adds utility functions to httpd.c for checking the request headers for either an "If-None-Match" or an "If-Not-Modified-Since" headers. If the header value is found and it matches the current value for the requested resource, we return early with a http response code 403 (Not Modified). If the request header value is not present or does not match we add the current ETag/Last-Modified values to the response headers and process the request normally.
This commit is contained in:
@@ -1706,6 +1706,7 @@ jsonapi_reply_queue(struct httpd_request *hreq)
|
||||
int start_pos, end_pos;
|
||||
int version;
|
||||
int count;
|
||||
char etag[21];
|
||||
struct player_status status;
|
||||
struct db_queue_item queue_item;
|
||||
json_object *reply;
|
||||
@@ -1713,12 +1714,16 @@ jsonapi_reply_queue(struct httpd_request *hreq)
|
||||
json_object *item;
|
||||
int ret = 0;
|
||||
|
||||
memset(&query_params, 0, sizeof(struct query_params));
|
||||
reply = json_object_new_object();
|
||||
|
||||
version = db_admin_getint(DB_ADMIN_QUEUE_VERSION);
|
||||
count = db_queue_get_count();
|
||||
|
||||
snprintf(etag, sizeof(etag), "%d", version);
|
||||
if (httpd_request_etag_matches(hreq->req, etag))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
memset(&query_params, 0, sizeof(struct query_params));
|
||||
reply = json_object_new_object();
|
||||
|
||||
json_object_object_add(reply, "version", json_object_new_int(version));
|
||||
json_object_object_add(reply, "count", json_object_new_int(count));
|
||||
|
||||
@@ -1880,6 +1885,7 @@ jsonapi_reply_player_volume(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_artists(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
struct query_params query_params;
|
||||
const char *param;
|
||||
enum media_kind media_kind;
|
||||
@@ -1888,6 +1894,11 @@ jsonapi_reply_library_artists(struct httpd_request *hreq)
|
||||
int total;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
media_kind = 0;
|
||||
param = evhttp_find_header(hreq->query, "media_kind");
|
||||
if (param)
|
||||
@@ -1940,10 +1951,16 @@ jsonapi_reply_library_artists(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_artist(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
const char *artist_id;
|
||||
json_object *reply;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
artist_id = hreq->uri_parsed->path_parts[3];
|
||||
|
||||
reply = fetch_artist(artist_id);
|
||||
@@ -1969,6 +1986,7 @@ jsonapi_reply_library_artist(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_artist_albums(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
struct query_params query_params;
|
||||
const char *artist_id;
|
||||
json_object *reply;
|
||||
@@ -1976,6 +1994,11 @@ jsonapi_reply_library_artist_albums(struct httpd_request *hreq)
|
||||
int total;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
artist_id = hreq->uri_parsed->path_parts[3];
|
||||
|
||||
reply = json_object_new_object();
|
||||
@@ -2018,6 +2041,7 @@ jsonapi_reply_library_artist_albums(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_albums(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
struct query_params query_params;
|
||||
const char *param;
|
||||
enum media_kind media_kind;
|
||||
@@ -2026,6 +2050,11 @@ jsonapi_reply_library_albums(struct httpd_request *hreq)
|
||||
int total;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
media_kind = 0;
|
||||
param = evhttp_find_header(hreq->query, "media_kind");
|
||||
if (param)
|
||||
@@ -2078,10 +2107,16 @@ jsonapi_reply_library_albums(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_album(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
const char *album_id;
|
||||
json_object *reply;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
album_id = hreq->uri_parsed->path_parts[3];
|
||||
|
||||
reply = fetch_album(album_id);
|
||||
@@ -2107,6 +2142,7 @@ jsonapi_reply_library_album(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_album_tracks(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
struct query_params query_params;
|
||||
const char *album_id;
|
||||
json_object *reply;
|
||||
@@ -2114,6 +2150,11 @@ jsonapi_reply_library_album_tracks(struct httpd_request *hreq)
|
||||
int total;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
album_id = hreq->uri_parsed->path_parts[3];
|
||||
|
||||
reply = json_object_new_object();
|
||||
@@ -2156,12 +2197,18 @@ jsonapi_reply_library_album_tracks(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_playlists(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
struct query_params query_params;
|
||||
json_object *reply;
|
||||
json_object *items;
|
||||
int total;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
reply = json_object_new_object();
|
||||
items = json_object_new_array();
|
||||
json_object_object_add(reply, "items", items);
|
||||
@@ -2202,10 +2249,16 @@ jsonapi_reply_library_playlists(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_playlist(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
const char *playlist_id;
|
||||
json_object *reply;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
playlist_id = hreq->uri_parsed->path_parts[3];
|
||||
|
||||
reply = fetch_playlist(playlist_id);
|
||||
@@ -2231,6 +2284,7 @@ jsonapi_reply_library_playlist(struct httpd_request *hreq)
|
||||
static int
|
||||
jsonapi_reply_library_playlist_tracks(struct httpd_request *hreq)
|
||||
{
|
||||
time_t db_update;
|
||||
struct query_params query_params;
|
||||
json_object *reply;
|
||||
json_object *items;
|
||||
@@ -2238,6 +2292,11 @@ jsonapi_reply_library_playlist_tracks(struct httpd_request *hreq)
|
||||
int total;
|
||||
int ret = 0;
|
||||
|
||||
db_update = (time_t) db_admin_getint64(DB_ADMIN_START_TIME);
|
||||
if (db_update && httpd_request_not_modified_since(hreq->req, &db_update))
|
||||
return HTTP_NOTMODIFIED;
|
||||
|
||||
|
||||
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &playlist_id);
|
||||
if (ret < 0)
|
||||
{
|
||||
@@ -2666,6 +2725,9 @@ jsonapi_request(struct evhttp_request *req, struct httpd_uri_parsed *uri_parsed)
|
||||
case HTTP_NOCONTENT: /* 204 No Content */
|
||||
httpd_send_reply(req, status_code, "No Content", hreq->reply, HTTPD_SEND_NO_GZIP);
|
||||
break;
|
||||
case HTTP_NOTMODIFIED: /* 304 Not Modified */
|
||||
httpd_send_reply(req, HTTP_NOTMODIFIED, NULL, NULL, HTTPD_SEND_NO_GZIP);
|
||||
break;
|
||||
|
||||
case HTTP_BADREQUEST: /* 400 Bad Request */
|
||||
httpd_send_error(req, status_code, "Bad Request");
|
||||
|
||||
Reference in New Issue
Block a user