commit 3093334
uint
·
2026-04-28 20:29:46 +0000 UTC
parent ab03866
refactor item route lookup (add parse_item) dedupe /stream, /meta item resolution
1 files changed,
+48,
-39
+48,
-39
1@@ -27,6 +27,7 @@ static const struct item* find_item(uint64_t id);
2 static int item_path_for_id(char out[4096], uint64_t id, const struct user* u);
3 static const char* mime_from_path(const char* path);
4 static int parse_hex64(const char* s, uint64_t* out);
5+static int path_item(struct item* it, const char* path, const char* prefix, const struct user* u, int c, const char* hdr);
6 static int parse_range(const char* hdr, size_t total, size_t* start, size_t* end);
7 static int read_request(int c, char* method, size_t msz, char* path, size_t psz, char* hdr, size_t hsz);
8 static void reply(int c, const char* hdr, const char* status, const char* ctype, const char* extra, const void* body, size_t len, int send_body, int preflight);
9@@ -317,6 +318,42 @@ static int parse_hex64(const char* s, uint64_t* out)
10 return 0;
11 }
12
13+/**
14+ * @brief Parse route id and resolve to an item
15+ *
16+ * @param it Output item
17+ * @param path Request path
18+ * @param prefix Route prefix
19+ * @param u Authenticated user filter
20+ * @param c Connected client socket file descriptor
21+ * @param hdr Request header block
22+ *
23+ * @return 0=Success, 1=Response sent, -1=Failure
24+ */
25+static int path_item(struct item* it, const char* path, const char* prefix, const struct user* u, int c, const char* hdr)
26+{
27+ uint64_t id;
28+ int fr;
29+
30+ if (parse_hex64(path + strlen(prefix), &id) < 0) {
31+ reply_text(c, hdr, HTTP_400, "Bad Request\n");
32+ return 1;
33+ }
34+
35+ fr = item_path_for_id(it->path, id, u);
36+ if (fr == 1) {
37+ reply_text(c, hdr, HTTP_404, "Not Found\n");
38+ return 1;
39+ }
40+ if (fr < 0) {
41+ reply_text(c, hdr, HTTP_500, "Server Error\n");
42+ return -1;
43+ }
44+
45+ it->id = id;
46+ return 0;
47+}
48+
49 /**
50 * @brief Parse HTTP Range header
51 *
52@@ -914,62 +951,35 @@ int http_handle(int c)
53 if (strncmp(path, "/stream/", 8) == 0) {
54 LOG(verbose_log, "HTTP", "Route /stream");
55
56- uint64_t id;
57- if (parse_hex64(path + 8, &id) < 0) {
58- reply_text(c, hdr, HTTP_400, "Bad Request\n");
59- return 0;
60- }
61-
62- char rel[4096];
63- int fr = item_path_for_id(rel, id, u);
64- if (fr == 1) {
65- reply_text(c, hdr, HTTP_404, "Not Found\n");
66- return 0;
67- }
68- if (fr < 0) {
69- reply_text(c, hdr, HTTP_500, "Server Error\n");
70- return -1;
71- }
72-
73 struct item tmp;
74- tmp.id = id;
75+ char rel[4096];
76 tmp.path = rel;
77
78+ int rc = path_item(&tmp, path, "/stream/", u, c, hdr);
79+ if (rc != 0)
80+ return rc < 0 ? -1 : 0;
81+
82 return stream_file(c, &tmp, hdr, head_only);
83 }
84
85 if (strncmp(path, "/meta/", 6) == 0) {
86 LOG(verbose_log, "HTTP", "Route /meta");
87
88- uint64_t id;
89- if (parse_hex64(path + 6, &id) < 0) {
90- reply_text(c, hdr, HTTP_400, "Bad Request\n");
91- return 0;
92- }
93-
94- char rel[4096];
95- int fr = item_path_for_id(rel, id, u);
96- if (fr == 1) {
97- reply_text(c, hdr, HTTP_404, "Not Found\n");
98- return 0;
99- }
100-
101- if (fr < 0) {
102- reply_text(c, hdr, HTTP_500, "Server Error\n");
103- return -1;
104- }
105-
106 struct item tmp;
107- tmp.id = id;
108+ char rel[4096];
109 tmp.path = rel;
110
111+ int rc = path_item(&tmp, path, "/meta/", u, c, hdr);
112+ if (rc != 0)
113+ return rc < 0 ? -1 : 0;
114+
115 struct stat st;
116 if (stat_item(&tmp, &st) < 0) {
117 reply_text(c, hdr, HTTP_404, "Not Found\n");
118 return 0;
119 }
120
121- const char* type = mime_from_path(rel);
122+ const char* type = mime_from_path(tmp.path);
123
124 struct json j;
125 if (json_meta(&j, &tmp, (size_t)st.st_size, (long)st.st_mtime, type) < 0) {
126@@ -988,4 +998,3 @@ int http_handle(int c)
127
128 return 0;
129 }
130-