commit a5daea3
uint
·
2026-10-26 17:45:40 +0000 UTC
parent 1e6b0af
use booleans where appropriate
3 files changed,
+89,
-88
+71,
-71
1@@ -28,13 +28,13 @@ struct response {
2 const void* body;
3
4 size_t len;
5- int send_body;
6- int preflight;
7+ bool send_body;
8+ bool preflight;
9 };
10
11 static void auth_delay_sleep(void);
12-static int cors_build(char* out, size_t outsz, const char* hdr, int preflight);
13-static int cors_origin_allowed(const char* origin);
14+static bool cors_build(char* out, size_t outsz, const char* hdr, bool preflight);
15+static bool cors_origin_allowed(const char* origin);
16 static const struct item* find_item(uint64_t id);
17 static int item_path_for_id(char out[4096], uint64_t id, const struct user* u);
18 static const char* mime_from_path(const char* path);
19@@ -42,13 +42,13 @@ static int parse_hex64(const char* s, uint64_t* out);
20 static int parse_range(const char* hdr, size_t total, size_t* start, size_t* end);
21 static int read_request(int c, char* method, size_t msz, char* path, size_t psz, char* hdr, size_t hsz);
22 static void reply(int c, const char* hdr, const struct response* res);
23-static int route_dispatch(int c, const char* hdr, const char* path, const struct user* u, int head_only);
24+static int route_dispatch(int c, const char* hdr, const char* path, const struct user* u, bool head_only);
25 static int route_item_path(struct item* it, const char* path, const char* prefix, const struct user* u, int c, const char* hdr);
26-static int route_library(int c, const char* hdr, const struct user* u, int head_only);
27-static int route_method(int c, const char* hdr, const char* method, int* head_only);
28+static int route_library(int c, const char* hdr, const struct user* u, bool head_only);
29+static bool route_method(int c, const char* hdr, const char* method, bool* head_only);
30 static int route_rescan(int c, const char* hdr, const struct user* u);
31 static int stat_item(const struct item* it, struct stat* st);
32-static int stream_file(int c, const struct item* it, const char* hdr, int head_only);
33+static int stream_file(int c, const struct item* it, const char* hdr, bool head_only);
34
35 extern struct library lib;
36 extern pthread_mutex_t lib_lock;
37@@ -75,14 +75,14 @@ static void auth_delay_sleep(void)
38 }
39
40 /* Check whether origin is allowed by CORS config
41- Returns 1=Allowed, 0=Not allowed */
42-static int cors_origin_allowed(const char* origin)
43+ Returns true=Allowed, false=Not allowed */
44+static bool cors_origin_allowed(const char* origin)
45 {
46 if (cors_origin[0] == '\0')
47- return 0;
48+ return false;
49
50 if (strcmp(cors_origin, "*") == 0)
51- return 1;
52+ return true;
53
54 /* comma/space separated allowlist */
55 const char* p = cors_origin;
56@@ -100,34 +100,34 @@ static int cors_origin_allowed(const char* origin)
57
58 size_t n = (size_t)(e - s);
59 if (n > 0 && strlen(origin) == n && strncmp(origin, s, n) == 0)
60- return 1;
61+ return true;
62
63 if (*p == ',')
64 p++;
65 }
66
67- return 0;
68+ return false;
69 }
70
71 /* Build CORS response headers
72- Returns 1=Built, 0=Not emitted */
73-static int cors_build(char* out, size_t outsz, const char* hdr, int preflight)
74+ Returns true=Built, false=Not emitted */
75+static bool cors_build(char* out, size_t outsz, const char* hdr, bool preflight)
76 {
77 char origin[512];
78
79 if (!out || outsz == 0)
80- return 0;
81+ return false;
82
83 out[0] = '\0';
84
85 if (cors_origin[0] == '\0')
86- return 0;
87+ return false;
88
89 if (hdr_get_value(origin, hdr, "origin") < 0)
90- return 0;
91+ return false;
92
93 if (!cors_origin_allowed(origin))
94- return 0;
95+ return false;
96
97 int n;
98 const char* ao = (strcmp(cors_origin, "*") == 0) ? "*" : origin;
99@@ -155,12 +155,12 @@ static int cors_build(char* out, size_t outsz, const char* hdr, int preflight)
100 }
101
102 if (n < 0)
103- return 0;
104+ return false;
105 if ((size_t)n >= outsz) {
106 out[0] = '\0';
107- return 0;
108+ return false;
109 }
110- return 1;
111+ return true;
112 }
113
114 /* Find library item by id
115@@ -444,8 +444,8 @@ static void reply(int c, const char* hdr, const struct response* res)
116 .extra = NULL,
117 .body = "Server Error\n",
118 .len = sizeof("Server Error\n") - 1,
119- .send_body = 1,
120- .preflight = 0,
121+ .send_body = true,
122+ .preflight = false,
123 };
124 reply(c, hdr, &err);
125 return;
126@@ -459,11 +459,11 @@ static void reply(int c, const char* hdr, const struct response* res)
127
128 /* Dispatch request path to route handler
129 Returns 0=Handled, -1=Failure */
130-static int route_dispatch(int c, const char* hdr, const char* path, const struct user* u, int head_only)
131+static int route_dispatch(int c, const char* hdr, const char* path, const struct user* u, bool head_only)
132 {
133 if (strcmp(path, "/ping") == 0) {
134 LOG_FIELD(verbose_log, "HTTP", "Route", "/ping");
135- reply(c, hdr, &(struct response){ HTTP_200, HTTP_TEXT, NULL, "OK\n", sizeof("OK\n") - 1, 1, 0 });
136+ reply(c, hdr, &(struct response){ HTTP_200, HTTP_TEXT, NULL, "OK\n", sizeof("OK\n") - 1, true, false });
137 return 0;
138 }
139
140@@ -502,13 +502,13 @@ static int route_dispatch(int c, const char* hdr, const char* path, const struct
141 return rc < 0 ? -1 : 0;
142
143 if (stat_item(&tmp, &st) < 0) {
144- reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, 1, 0 });
145+ reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, true, false });
146 return 0;
147 }
148
149 type = mime_from_path(tmp.path);
150 if (json_meta(&j, &tmp, (size_t)st.st_size, (long)st.st_mtime, type) < 0) {
151- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "JSON Failed\n", sizeof("JSON Failed\n") - 1, 1, 0 });
152+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "JSON Failed\n", sizeof("JSON Failed\n") - 1, true, false });
153 return -1;
154 }
155
156@@ -518,7 +518,7 @@ static int route_dispatch(int c, const char* hdr, const char* path, const struct
157 }
158
159 LOG_FIELD(verbose_log, "HTTP", "Route not found", "%s", path);
160- reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, 1, 0 });
161+ reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, true, false });
162 return 0;
163 }
164
165@@ -536,8 +536,8 @@ static int route_item_path(struct item* it, const char* path, const char* prefix
166 .extra = NULL,
167 .body = "Bad Request\n",
168 .len = sizeof("Bad Request\n") - 1,
169- .send_body = 1,
170- .preflight = 0,
171+ .send_body = true,
172+ .preflight = false,
173 });
174 return 1;
175 }
176@@ -550,8 +550,8 @@ static int route_item_path(struct item* it, const char* path, const char* prefix
177 .extra = NULL,
178 .body = "Not Found\n",
179 .len = sizeof("Not Found\n") - 1,
180- .send_body = 1,
181- .preflight = 0,
182+ .send_body = true,
183+ .preflight = false,
184 });
185 return 1;
186 }
187@@ -562,8 +562,8 @@ static int route_item_path(struct item* it, const char* path, const char* prefix
188 .extra = NULL,
189 .body = "Server Error\n",
190 .len = sizeof("Server Error\n") - 1,
191- .send_body = 1,
192- .preflight = 0,
193+ .send_body = true,
194+ .preflight = false,
195 });
196 return -1;
197 }
198@@ -574,7 +574,7 @@ static int route_item_path(struct item* it, const char* path, const char* prefix
199
200 /* Handle /library route
201 Returns 0=Handled, -1=Failure */
202-static int route_library(int c, const char* hdr, const struct user* u, int head_only)
203+static int route_library(int c, const char* hdr, const struct user* u, bool head_only)
204 {
205 struct json j;
206 struct library view;
207@@ -593,7 +593,7 @@ static int route_library(int c, const char* hdr, const struct user* u, int head_
208 view.items = calloc(lib.len, sizeof(*view.items));
209 if (!view.items) {
210 (void)pthread_mutex_unlock(&lib_lock);
211- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, 1, 0 });
212+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, true, false });
213 return -1;
214 }
215 }
216@@ -612,7 +612,7 @@ static int route_library(int c, const char* hdr, const struct user* u, int head_
217
218 (void)pthread_mutex_unlock(&lib_lock);
219 LOG_FIELD(verbose_log, "JSON", "Encode", "FAILED");
220- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "JSON Encode Failed\n", sizeof("JSON Encode Failed\n") - 1, 1, 0 });
221+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "JSON Encode Failed\n", sizeof("JSON Encode Failed\n") - 1, true, false });
222 return -1;
223 }
224
225@@ -627,25 +627,25 @@ static int route_library(int c, const char* hdr, const struct user* u, int head_
226 }
227
228 /* Handle request method and OPTIONS
229- Returns 0=Continue, 1=Response sent */
230-static int route_method(int c, const char* hdr, const char* method, int* head_only)
231+ Returns false=Continue, true=Response sent */
232+static bool route_method(int c, const char* hdr, const char* method, bool* head_only)
233 {
234 if (strcmp(method, "GET") == 0) {
235- *head_only = 0;
236- return 0;
237+ *head_only = false;
238+ return false;
239 }
240 if (strcmp(method, "HEAD") == 0) {
241- *head_only = 1;
242- return 0;
243+ *head_only = true;
244+ return false;
245 }
246 if (strcmp(method, "OPTIONS") == 0) {
247- reply(c, hdr, &(struct response){ HTTP_204, NULL, NULL, NULL, 0, 0, 1 });
248- return 1;
249+ reply(c, hdr, &(struct response){ HTTP_204, NULL, NULL, NULL, 0, false, true });
250+ return true;
251 }
252
253 LOG_FIELD(verbose_log, "HTTP", "Method forbidden", "%s", method);
254- reply(c, hdr, &(struct response){ HTTP_405, HTTP_TEXT, NULL, "Method Not Allowed\n", sizeof("Method Not Allowed\n") - 1, 1, 0 });
255- return 1;
256+ reply(c, hdr, &(struct response){ HTTP_405, HTTP_TEXT, NULL, "Method Not Allowed\n", sizeof("Method Not Allowed\n") - 1, true, false });
257+ return true;
258 }
259
260 /* Handle /rescan route
261@@ -660,7 +660,7 @@ static int route_rescan(int c, const char* hdr, const struct user* u)
262 LOG_FIELD(verbose_log, "HTTP", "Route", "/rescan");
263 if (users.len == 0) {
264 LOG_FIELD(true, "HTTP", "Rescan forbidden", "Auth disabled");
265- reply(c, hdr, &(struct response){ HTTP_403, HTTP_TEXT, NULL, "Forbidden\n", sizeof("Forbidden\n") - 1, 1, 0 });
266+ reply(c, hdr, &(struct response){ HTTP_403, HTTP_TEXT, NULL, "Forbidden\n", sizeof("Forbidden\n") - 1, true, false });
267 return 0;
268 }
269
270@@ -679,12 +679,12 @@ static int route_rescan(int c, const char* hdr, const struct user* u)
271
272 if (ok < 0) {
273 LOG_FIELD(true, "SCAN", "Rescan FAILED", "%s (%ld ms)", media_dir, ms);
274- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Rescan Failed\n", sizeof("Rescan Failed\n") - 1, 1, 0 });
275+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Rescan Failed\n", sizeof("Rescan Failed\n") - 1, true, false });
276 return -1;
277 }
278
279 LOG_FIELD(true, "SCAN", "Rescan OK", "%zu -> %zu (%ld ms)", before, after, ms);
280- reply(c, hdr, &(struct response){ HTTP_200, HTTP_TEXT, NULL, "OK\n", sizeof("OK\n") - 1, 1, 0 });
281+ reply(c, hdr, &(struct response){ HTTP_200, HTTP_TEXT, NULL, "OK\n", sizeof("OK\n") - 1, true, false });
282 return 0;
283 }
284
285@@ -708,14 +708,14 @@ static int stat_item(const struct item* it, struct stat* st)
286
287 /* Stream item file to HTTP client
288 Returns 0=Handled, -1=Failure */
289-static int stream_file(int c, const struct item* it, const char* hdr, int head_only)
290+static int stream_file(int c, const struct item* it, const char* hdr, bool head_only)
291 {
292 char full[4096];
293
294 /* build absolute path */
295 if (join_path(full, sizeof(full), media_dir, it->path) < 0) {
296 LOG_FIELD(verbose_log, "HTTP", "Stream FAILED", "Path too long");
297- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, 1, 0 });
298+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, true, false });
299 return -1;
300 }
301
302@@ -723,7 +723,7 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
303 int fd = open(full, O_RDONLY);
304 if (fd < 0) {
305 LOG_FIELD(verbose_log, "HTTP", "Open FAILED", "%s", it->path);
306- reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, 1, 0 });
307+ reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, true, false });
308 return 0;
309 }
310
311@@ -731,14 +731,14 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
312 struct stat st;
313 if (fstat(fd, &st) < 0) {
314 close(fd);
315- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, 1, 0 });
316+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, true, false });
317 return -1;
318 }
319
320 /* reject non-regular files */
321 if (!S_ISREG(st.st_mode)) {
322 close(fd);
323- reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, 1, 0 });
324+ reply(c, hdr, &(struct response){ HTTP_404, HTTP_TEXT, NULL, "Not Found\n", sizeof("Not Found\n") - 1, true, false });
325 return 0;
326 }
327
328@@ -750,20 +750,20 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
329 int partial = parse_range(hdr, total, &start, &end);
330 if (partial == RANGE_BAD) {
331 close(fd);
332- reply(c, hdr, &(struct response){ HTTP_400, HTTP_TEXT, NULL, "Bad Range\n", sizeof("Bad Range\n") - 1, 1, 0 });
333+ reply(c, hdr, &(struct response){ HTTP_400, HTTP_TEXT, NULL, "Bad Range\n", sizeof("Bad Range\n") - 1, true, false });
334 return 0;
335 }
336
337 if (partial == RANGE_UNSAT) {
338 close(fd);
339- reply(c, hdr, &(struct response){ HTTP_416, HTTP_TEXT, NULL, "Range Not Satisfiable\n", sizeof("Range Not Satisfiable\n") - 1, 1, 0 });
340+ reply(c, hdr, &(struct response){ HTTP_416, HTTP_TEXT, NULL, "Range Not Satisfiable\n", sizeof("Range Not Satisfiable\n") - 1, true, false });
341 return 0;
342 }
343
344 if (partial == RANGE_OK) {
345 if (lseek(fd, (off_t)start, SEEK_SET) < 0) {
346 close(fd);
347- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, 1, 0 });
348+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, true, false });
349 return -1;
350 }
351 }
352@@ -776,7 +776,7 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
353
354 if (snprintf(ctype, sizeof(ctype), HTTP_CTYPE, type) >= (int)sizeof(ctype)) {
355 close(fd);
356- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, 1, 0 });
357+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, true, false });
358 return -1;
359 }
360
361@@ -789,7 +789,7 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
362 start, end, total
363 ) >= (int)sizeof(extra)) {
364 close(fd);
365- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, 1, 0 });
366+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, true, false });
367 return -1;
368 }
369
370@@ -799,15 +799,15 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
371 .extra = extra,
372 .body = NULL,
373 .len = len,
374- .send_body = 0,
375- .preflight = 0,
376+ .send_body = false,
377+ .preflight = false,
378 });
379 }
380 else {
381 /* non-partial */
382 if (snprintf(extra, sizeof(extra), HTTP_RANGE) >= (int)sizeof(extra)) {
383 close(fd);
384- reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, 1, 0 });
385+ reply(c, hdr, &(struct response){ HTTP_500, HTTP_TEXT, NULL, "Server Error\n", sizeof("Server Error\n") - 1, true, false });
386 return -1;
387 }
388
389@@ -817,8 +817,8 @@ static int stream_file(int c, const struct item* it, const char* hdr, int head_o
390 .extra = extra,
391 .body = NULL,
392 .len = len,
393- .send_body = 0,
394- .preflight = 0,
395+ .send_body = false,
396+ .preflight = false,
397 });
398 }
399
400@@ -880,13 +880,13 @@ int http_handle(int c)
401 hdr[0] = '\0';
402
403 if (read_request(c, method, sizeof(method), path, sizeof(path), hdr, sizeof(hdr)) < 0) {
404- reply(c, hdr, &(struct response){ HTTP_400, HTTP_TEXT, NULL, "Bad Request\n", sizeof("Bad Request\n") - 1, 1, 0 });
405+ reply(c, hdr, &(struct response){ HTTP_400, HTTP_TEXT, NULL, "Bad Request\n", sizeof("Bad Request\n") - 1, true, false });
406 return -1;
407 }
408 LOG_FIELD(verbose_log, "HTTP", "Request", "%s %s", method, path);
409
410- int head_only = 0;
411- if (route_method(c, hdr, method, &head_only) != 0)
412+ bool head_only = false;
413+ if (route_method(c, hdr, method, &head_only))
414 return 0;
415
416 if (users.len > 0) {
417@@ -900,7 +900,7 @@ int http_handle(int c)
418 .body = "unauthorized\n",
419 .len = sizeof("unauthorized\n") - 1,
420 .send_body = !head_only,
421- .preflight = 0,
422+ .preflight = false,
423 });
424 return 0;
425 }
+12,
-12
1@@ -12,7 +12,7 @@ struct users users;
2 static int allow_push(struct user* u, const char* s);
3 static int b64_decode(unsigned char* out, size_t outsz, const char* in);
4 static int b64_val(int c);
5-static int ct_equal(const char* a, const char* b);
6+static bool ct_equal(const char* a, const char* b);
7 static const struct user* find_user(const char* name);
8
9 /* Append allow prefix to user entry
10@@ -92,8 +92,8 @@ static int b64_val(int c)
11 return -1;
12 }
13 /* Compare two strings in constant time
14- Returns 1=Equal, 0=Not equal */
15-static int ct_equal(const char* a, const char* b)
16+ Returns true=Equal, false=Not equal */
17+static bool ct_equal(const char* a, const char* b)
18 {
19 size_t la = strlen(a);
20 size_t lb = strlen(b);
21@@ -122,28 +122,28 @@ static const struct user* find_user(const char* name)
22 }
23
24 /* Check whether path matches allow prefix
25- Returns 1=Match, 0=No match */
26-static int prefix_ok(const char* path, const char* pre)
27+ Returns true=Match, false=No match */
28+static bool prefix_ok(const char* path, const char* pre)
29 {
30 size_t n = strlen(pre);
31 if (n == 0)
32- return 0;
33+ return false;
34
35 if (strcmp(pre, "*") == 0)
36- return 1;
37+ return true;
38
39 if (strncmp(path, pre, n) != 0)
40- return 0;
41+ return false;
42
43 /* allow match or dir boundary */
44 if (path[n] == '\0')
45- return 1;
46+ return true;
47 if (pre[n - 1] == '/')
48- return 1;
49+ return true;
50 if (path[n] == '/')
51- return 1;
52+ return true;
53
54- return 0;
55+ return false;
56 }
57
58 bool user_allows_path(const struct user* u, const char* relpath)
+6,
-5
1@@ -1,24 +1,25 @@
2 #include <ctype.h>
3 #include <errno.h>
4+#include <stdbool.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #include "utils.h"
10
11-static int ci_key_match(const char* a, const char* b, size_t n);
12+static bool ci_key_match(const char* a, const char* b, size_t n);
13
14 /* Compare two header keys case-insensitively
15- Returns 1=Match, 0=No match */
16-static int ci_key_match(const char* a, const char* b, size_t n)
17+ Returns true=Match, false=No match */
18+static bool ci_key_match(const char* a, const char* b, size_t n)
19 {
20 for (size_t i = 0; i < n; i++) {
21 unsigned char ca = (unsigned char)a[i];
22 unsigned char cb = (unsigned char)b[i];
23 if (tolower(ca) != tolower(cb))
24- return 0;
25+ return false;
26 }
27- return 1;
28+ return true;
29 }
30
31 const char* cistrstr(const char* hay, const char* nee)