summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authorGravatar Louis Brauer2024-05-26 21:45:41 +0200
committerGravatar Louis Brauer2024-05-26 21:45:41 +0200
commit0e21d35e802bf859aa14bce688cd9544458e9e9c (patch)
tree55861603d4f702d56edafe24aa1019484b714fbc /data.c
parentEnable deletion of avatar and header image in user settings (diff)
downloadpenes-snac2-0e21d35e802bf859aa14bce688cd9544458e9e9c.tar.gz
penes-snac2-0e21d35e802bf859aa14bce688cd9544458e9e9c.tar.xz
penes-snac2-0e21d35e802bf859aa14bce688cd9544458e9e9c.zip
Use enum instead of numeric status codes for HTTP statuses
Diffstat (limited to 'data.c')
-rw-r--r--data.c68
1 files changed, 34 insertions, 34 deletions
diff --git a/data.c b/data.c
index edbc64f..8fdd292 100644
--- a/data.c
+++ b/data.c
@@ -355,12 +355,12 @@ int is_md5_hex(const char *md5)
355int index_add_md5(const char *fn, const char *md5) 355int index_add_md5(const char *fn, const char *md5)
356/* adds an md5 to an index */ 356/* adds an md5 to an index */
357{ 357{
358 int status = 201; /* Created */ 358 int status = HTTP_STATUS_CREATED;
359 FILE *f; 359 FILE *f;
360 360
361 if (!is_md5_hex(md5)) { 361 if (!is_md5_hex(md5)) {
362 srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5)); 362 srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5));
363 return 400; 363 return HTTP_STATUS_BAD_REQUEST;
364 } 364 }
365 365
366 pthread_mutex_lock(&data_mutex); 366 pthread_mutex_lock(&data_mutex);
@@ -375,7 +375,7 @@ int index_add_md5(const char *fn, const char *md5)
375 fclose(f); 375 fclose(f);
376 } 376 }
377 else 377 else
378 status = 500; 378 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
379 379
380 pthread_mutex_unlock(&data_mutex); 380 pthread_mutex_unlock(&data_mutex);
381 381
@@ -394,7 +394,7 @@ int index_add(const char *fn, const char *id)
394int index_del_md5(const char *fn, const char *md5) 394int index_del_md5(const char *fn, const char *md5)
395/* deletes an md5 from an index */ 395/* deletes an md5 from an index */
396{ 396{
397 int status = 404; 397 int status = HTTP_STATUS_NOT_FOUND;
398 FILE *f; 398 FILE *f;
399 399
400 pthread_mutex_lock(&data_mutex); 400 pthread_mutex_lock(&data_mutex);
@@ -411,7 +411,7 @@ int index_del_md5(const char *fn, const char *md5)
411 [yes: this breaks index_len()] */ 411 [yes: this breaks index_len()] */
412 fseek(f, -33, SEEK_CUR); 412 fseek(f, -33, SEEK_CUR);
413 fwrite("-", 1, 1, f); 413 fwrite("-", 1, 1, f);
414 status = 200; 414 status = HTTP_STATUS_OK;
415 415
416 break; 416 break;
417 } 417 }
@@ -420,7 +420,7 @@ int index_del_md5(const char *fn, const char *md5)
420 fclose(f); 420 fclose(f);
421 } 421 }
422 else 422 else
423 status = 410; 423 status = HTTP_STATUS_GONE;
424 424
425 pthread_mutex_unlock(&data_mutex); 425 pthread_mutex_unlock(&data_mutex);
426 426
@@ -660,7 +660,7 @@ int object_here(const char *id)
660int object_get_by_md5(const char *md5, xs_dict **obj) 660int object_get_by_md5(const char *md5, xs_dict **obj)
661/* returns a stored object, optionally of the requested type */ 661/* returns a stored object, optionally of the requested type */
662{ 662{
663 int status = 404; 663 int status = HTTP_STATUS_NOT_FOUND;
664 xs *fn = _object_fn_by_md5(md5, "object_get_by_md5"); 664 xs *fn = _object_fn_by_md5(md5, "object_get_by_md5");
665 FILE *f; 665 FILE *f;
666 666
@@ -669,7 +669,7 @@ int object_get_by_md5(const char *md5, xs_dict **obj)
669 fclose(f); 669 fclose(f);
670 670
671 if (*obj) 671 if (*obj)
672 status = 200; 672 status = HTTP_STATUS_OK;
673 } 673 }
674 else 674 else
675 *obj = NULL; 675 *obj = NULL;
@@ -689,7 +689,7 @@ int object_get(const char *id, xs_dict **obj)
689int _object_add(const char *id, const xs_dict *obj, int ow) 689int _object_add(const char *id, const xs_dict *obj, int ow)
690/* stores an object */ 690/* stores an object */
691{ 691{
692 int status = 201; /* Created */ 692 int status = HTTP_STATUS_CREATED; /* Created */
693 xs *fn = _object_fn(id); 693 xs *fn = _object_fn(id);
694 FILE *f; 694 FILE *f;
695 695
@@ -697,10 +697,10 @@ int _object_add(const char *id, const xs_dict *obj, int ow)
697 if (!ow) { 697 if (!ow) {
698 /* object already here */ 698 /* object already here */
699 srv_debug(1, xs_fmt("object_add object already here %s", id)); 699 srv_debug(1, xs_fmt("object_add object already here %s", id));
700 return 204; /* No content */ 700 return HTTP_STATUS_NO_CONTENT;
701 } 701 }
702 else 702 else
703 status = 200; 703 status = HTTP_STATUS_OK;
704 } 704 }
705 705
706 if ((f = fopen(fn, "w")) != NULL) { 706 if ((f = fopen(fn, "w")) != NULL) {
@@ -736,7 +736,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow)
736 } 736 }
737 else { 737 else {
738 srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno)); 738 srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
739 status = 500; 739 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
740 } 740 }
741 741
742 srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status)); 742 srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
@@ -762,11 +762,11 @@ int object_add_ow(const char *id, const xs_dict *obj)
762int object_del_by_md5(const char *md5) 762int object_del_by_md5(const char *md5)
763/* deletes an object by its md5 */ 763/* deletes an object by its md5 */
764{ 764{
765 int status = 404; 765 int status = HTTP_STATUS_NOT_FOUND;
766 xs *fn = _object_fn_by_md5(md5, "object_del_by_md5"); 766 xs *fn = _object_fn_by_md5(md5, "object_del_by_md5");
767 767
768 if (unlink(fn) != -1) { 768 if (unlink(fn) != -1) {
769 status = 200; 769 status = HTTP_STATUS_OK;
770 770
771 /* also delete associated indexes */ 771 /* also delete associated indexes */
772 xs *spec = xs_dup(fn); 772 xs *spec = xs_dup(fn);
@@ -907,7 +907,7 @@ int object_parent(const char *md5, char *buf, int size)
907int object_admire(const char *id, const char *actor, int like) 907int object_admire(const char *id, const char *actor, int like)
908/* actor likes or announces this object */ 908/* actor likes or announces this object */
909{ 909{
910 int status = 200; 910 int status = HTTP_STATUS_OK;
911 xs *fn = _object_fn(id); 911 xs *fn = _object_fn(id);
912 912
913 fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); 913 fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
@@ -1007,7 +1007,7 @@ int follower_add(snac *snac, const char *actor)
1007 1007
1008 snac_debug(snac, 2, xs_fmt("follower_add %s", actor)); 1008 snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
1009 1009
1010 return ret == -1 ? 500 : 200; 1010 return ret == -1 ? HTTP_STATUS_INTERNAL_SERVER_ERROR : HTTP_STATUS_OK;
1011} 1011}
1012 1012
1013 1013
@@ -1018,7 +1018,7 @@ int follower_del(snac *snac, const char *actor)
1018 1018
1019 snac_debug(snac, 2, xs_fmt("follower_del %s", actor)); 1019 snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
1020 1020
1021 return ret == -1 ? 404 : 200; 1021 return ret == -1 ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK;
1022} 1022}
1023 1023
1024 1024
@@ -1109,7 +1109,7 @@ int timeline_here(snac *snac, const char *md5)
1109int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg) 1109int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
1110/* gets a message from the timeline */ 1110/* gets a message from the timeline */
1111{ 1111{
1112 int status = 404; 1112 int status = HTTP_STATUS_NOT_FOUND;
1113 FILE *f = NULL; 1113 FILE *f = NULL;
1114 1114
1115 xs *fn = timeline_fn_by_md5(snac, md5); 1115 xs *fn = timeline_fn_by_md5(snac, md5);
@@ -1119,7 +1119,7 @@ int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
1119 fclose(f); 1119 fclose(f);
1120 1120
1121 if (*msg != NULL) 1121 if (*msg != NULL)
1122 status = 200; 1122 status = HTTP_STATUS_OK;
1123 } 1123 }
1124 1124
1125 return status; 1125 return status;
@@ -1282,7 +1282,7 @@ xs_str *_following_fn(snac *snac, const char *actor)
1282int following_add(snac *snac, const char *actor, const xs_dict *msg) 1282int following_add(snac *snac, const char *actor, const xs_dict *msg)
1283/* adds to the following list */ 1283/* adds to the following list */
1284{ 1284{
1285 int ret = 201; /* created */ 1285 int ret = HTTP_STATUS_CREATED;
1286 xs *fn = _following_fn(snac, actor); 1286 xs *fn = _following_fn(snac, actor);
1287 FILE *f; 1287 FILE *f;
1288 xs *p_object = NULL; 1288 xs *p_object = NULL;
@@ -1295,7 +1295,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg)
1295 1295
1296 if (!xs_is_null(type) && strcmp(type, "Accept") == 0) { 1296 if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1297 snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor)); 1297 snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
1298 return 200; 1298 return HTTP_STATUS_OK;
1299 } 1299 }
1300 } 1300 }
1301 1301
@@ -1311,7 +1311,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg)
1311 link(actor_fn, fn); 1311 link(actor_fn, fn);
1312 } 1312 }
1313 else 1313 else
1314 ret = 500; 1314 ret = HTTP_STATUS_INTERNAL_SERVER_ERROR;
1315 1315
1316 snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn)); 1316 snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
1317 1317
@@ -1332,7 +1332,7 @@ int following_del(snac *snac, const char *actor)
1332 fn = xs_replace_i(fn, ".json", "_a.json"); 1332 fn = xs_replace_i(fn, ".json", "_a.json");
1333 unlink(fn); 1333 unlink(fn);
1334 1334
1335 return 200; 1335 return HTTP_STATUS_OK;
1336} 1336}
1337 1337
1338 1338
@@ -1350,14 +1350,14 @@ int following_get(snac *snac, const char *actor, xs_dict **data)
1350{ 1350{
1351 xs *fn = _following_fn(snac, actor); 1351 xs *fn = _following_fn(snac, actor);
1352 FILE *f; 1352 FILE *f;
1353 int status = 200; 1353 int status = HTTP_STATUS_OK;
1354 1354
1355 if ((f = fopen(fn, "r")) != NULL) { 1355 if ((f = fopen(fn, "r")) != NULL) {
1356 *data = xs_json_load(f); 1356 *data = xs_json_load(f);
1357 fclose(f); 1357 fclose(f);
1358 } 1358 }
1359 else 1359 else
1360 status = 404; 1360 status = HTTP_STATUS_NOT_FOUND;
1361 1361
1362 return status; 1362 return status;
1363} 1363}
@@ -1576,7 +1576,7 @@ int actor_add(const char *actor, const xs_dict *msg)
1576int actor_get(const char *actor, xs_dict **data) 1576int actor_get(const char *actor, xs_dict **data)
1577/* returns an already downloaded actor */ 1577/* returns an already downloaded actor */
1578{ 1578{
1579 int status = 200; 1579 int status = HTTP_STATUS_OK;
1580 xs_dict *d = NULL; 1580 xs_dict *d = NULL;
1581 1581
1582 if (xs_startswith(actor, srv_baseurl)) { 1582 if (xs_startswith(actor, srv_baseurl)) {
@@ -1590,10 +1590,10 @@ int actor_get(const char *actor, xs_dict **data)
1590 *data = msg_actor(&user); 1590 *data = msg_actor(&user);
1591 1591
1592 user_free(&user); 1592 user_free(&user);
1593 return 200; 1593 return HTTP_STATUS_OK;
1594 } 1594 }
1595 else 1595 else
1596 return 404; 1596 return HTTP_STATUS_NOT_FOUND;
1597 } 1597 }
1598 1598
1599 /* read the object */ 1599 /* read the object */
@@ -1606,7 +1606,7 @@ int actor_get(const char *actor, xs_dict **data)
1606 if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) { 1606 if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
1607 srv_debug(1, xs_fmt("corrupted actor object %s", actor)); 1607 srv_debug(1, xs_fmt("corrupted actor object %s", actor));
1608 d = xs_free(d); 1608 d = xs_free(d);
1609 return 404; 1609 return HTTP_STATUS_NOT_FOUND;
1610 } 1610 }
1611 1611
1612 if (data) 1612 if (data)
@@ -1622,7 +1622,7 @@ int actor_get(const char *actor, xs_dict **data)
1622 1622
1623 if (mtime(fn) + max_time < (double) time(NULL)) { 1623 if (mtime(fn) + max_time < (double) time(NULL)) {
1624 /* actor data exists but also stinks */ 1624 /* actor data exists but also stinks */
1625 status = 205; /* "205: Reset Content" "110: Response Is Stale" */ 1625 status = HTTP_STATUS_RESET_CONTENT; /* "110: Response Is Stale" */
1626 } 1626 }
1627 1627
1628 return status; 1628 return status;
@@ -1634,7 +1634,7 @@ int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
1634{ 1634{
1635 int status = actor_get(actor, data); 1635 int status = actor_get(actor, data);
1636 1636
1637 if (status == 205 && user && !xs_startswith(actor, srv_baseurl)) 1637 if (status == HTTP_STATUS_RESET_CONTENT && user && !xs_startswith(actor, srv_baseurl))
1638 enqueue_actor_refresh(user, actor, 0); 1638 enqueue_actor_refresh(user, actor, 0);
1639 1639
1640 return status; 1640 return status;
@@ -1953,7 +1953,7 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
1953 const char *inm, xs_str **etag) 1953 const char *inm, xs_str **etag)
1954/* loads a cached file */ 1954/* loads a cached file */
1955{ 1955{
1956 int status = 404; 1956 int status = HTTP_STATUS_NOT_FOUND;
1957 1957
1958 if (fn) { 1958 if (fn) {
1959 double tm = mtime(fn); 1959 double tm = mtime(fn);
@@ -1965,7 +1965,7 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
1965 /* if if-none-match is set, check if it's the same */ 1965 /* if if-none-match is set, check if it's the same */
1966 if (!xs_is_null(inm) && strcmp(e, inm) == 0) { 1966 if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
1967 /* client has the newest version */ 1967 /* client has the newest version */
1968 status = 304; 1968 status = HTTP_STATUS_NOT_MODIFIED;
1969 } 1969 }
1970 else { 1970 else {
1971 /* newer or never downloaded; read the full file */ 1971 /* newer or never downloaded; read the full file */
@@ -1976,7 +1976,7 @@ static int _load_raw_file(const char *fn, xs_val **data, int *size,
1976 *data = xs_read(f, size); 1976 *data = xs_read(f, size);
1977 fclose(f); 1977 fclose(f);
1978 1978
1979 status = 200; 1979 status = HTTP_STATUS_OK;
1980 } 1980 }
1981 } 1981 }
1982 1982