2 a596b957 2022-07-14 tracey * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 58381f70 2022-09-03 op * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 a596b957 2022-07-14 tracey * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
9 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
10 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
12 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 a596b957 2022-07-14 tracey #include <net/if.h>
22 a596b957 2022-07-14 tracey #include <netinet/in.h>
23 b4c20a19 2022-07-15 naddy #include <sys/queue.h>
24 a596b957 2022-07-14 tracey #include <sys/stat.h>
25 a596b957 2022-07-14 tracey #include <sys/types.h>
27 58381f70 2022-09-03 op #include <ctype.h>
28 a596b957 2022-07-14 tracey #include <dirent.h>
29 a596b957 2022-07-14 tracey #include <errno.h>
30 a596b957 2022-07-14 tracey #include <event.h>
31 3b81530f 2022-11-22 op #include <fcntl.h>
32 a596b957 2022-07-14 tracey #include <imsg.h>
33 a596b957 2022-07-14 tracey #include <sha1.h>
34 a596b957 2022-07-14 tracey #include <stdio.h>
35 a596b957 2022-07-14 tracey #include <stdlib.h>
36 a596b957 2022-07-14 tracey #include <string.h>
37 a596b957 2022-07-14 tracey #include <unistd.h>
39 a596b957 2022-07-14 tracey #include "got_error.h"
40 a596b957 2022-07-14 tracey #include "got_object.h"
41 a596b957 2022-07-14 tracey #include "got_reference.h"
42 a596b957 2022-07-14 tracey #include "got_repository.h"
43 a596b957 2022-07-14 tracey #include "got_path.h"
44 a596b957 2022-07-14 tracey #include "got_cancel.h"
45 a596b957 2022-07-14 tracey #include "got_worktree.h"
46 a596b957 2022-07-14 tracey #include "got_diff.h"
47 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
48 a596b957 2022-07-14 tracey #include "got_blame.h"
49 a596b957 2022-07-14 tracey #include "got_privsep.h"
51 a596b957 2022-07-14 tracey #include "proc.h"
52 a596b957 2022-07-14 tracey #include "gotwebd.h"
53 1abb18e1 2022-12-20 op #include "tmpl.h"
55 a596b957 2022-07-14 tracey static const struct querystring_keys querystring_keys[] = {
56 a596b957 2022-07-14 tracey { "action", ACTION },
57 a596b957 2022-07-14 tracey { "commit", COMMIT },
58 a596b957 2022-07-14 tracey { "file", RFILE },
59 a596b957 2022-07-14 tracey { "folder", FOLDER },
60 a596b957 2022-07-14 tracey { "headref", HEADREF },
61 a596b957 2022-07-14 tracey { "index_page", INDEX_PAGE },
62 a596b957 2022-07-14 tracey { "path", PATH },
63 a596b957 2022-07-14 tracey { "page", PAGE },
66 a596b957 2022-07-14 tracey static const struct action_keys action_keys[] = {
67 a596b957 2022-07-14 tracey { "blame", BLAME },
68 a596b957 2022-07-14 tracey { "blob", BLOB },
69 298f95fb 2023-01-05 op { "blobraw", BLOBRAW },
70 a596b957 2022-07-14 tracey { "briefs", BRIEFS },
71 a596b957 2022-07-14 tracey { "commits", COMMITS },
72 a596b957 2022-07-14 tracey { "diff", DIFF },
73 a596b957 2022-07-14 tracey { "error", ERR },
74 a596b957 2022-07-14 tracey { "index", INDEX },
75 a596b957 2022-07-14 tracey { "summary", SUMMARY },
76 a596b957 2022-07-14 tracey { "tag", TAG },
77 a596b957 2022-07-14 tracey { "tags", TAGS },
78 a596b957 2022-07-14 tracey { "tree", TREE },
79 1abb18e1 2022-12-20 op { "rss", RSS },
82 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_querystring(struct querystring **);
83 a596b957 2022-07-14 tracey static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 a596b957 2022-07-14 tracey static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 a596b957 2022-07-14 tracey char *, char *);
87 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_index(struct request *);
88 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 a596b957 2022-07-14 tracey const char *);
90 a596b957 2022-07-14 tracey static const struct got_error *gotweb_load_got_path(struct request *c,
91 a596b957 2022-07-14 tracey struct repo_dir *);
92 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_repo_description(char **,
93 3b81530f 2022-11-22 op struct server *, const char *, int);
94 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 3b81530f 2022-11-22 op const char *, int);
97 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
98 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
100 95a4a5a1 2022-08-30 op struct server *gotweb_get_server(uint8_t *, uint8_t *);
103 6cdf29f9 2023-01-21 op gotweb_reply(struct request *c, int status, const char *ctype,
104 6cdf29f9 2023-01-21 op struct gotweb_url *location)
106 6cdf29f9 2023-01-21 op const char *csp;
108 6cdf29f9 2023-01-21 op if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
111 6cdf29f9 2023-01-21 op if (location) {
112 6cdf29f9 2023-01-21 op if (fcgi_puts(c->tp, "Location: ") == -1 ||
113 6cdf29f9 2023-01-21 op gotweb_render_url(c, location) == -1 ||
114 6cdf29f9 2023-01-21 op fcgi_puts(c->tp, "\r\n") == -1)
118 6cdf29f9 2023-01-21 op csp = "Content-Security-Policy: default-src 'self'; "
119 6cdf29f9 2023-01-21 op "script-src 'none'; object-src 'none';\r\n";
120 6cdf29f9 2023-01-21 op if (fcgi_puts(c->tp, csp) == -1)
123 6cdf29f9 2023-01-21 op if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
126 6cdf29f9 2023-01-21 op return fcgi_puts(c->tp, "\r\n");
130 6cdf29f9 2023-01-21 op gotweb_reply_file(struct request *c, const char *ctype, const char *file,
131 6cdf29f9 2023-01-21 op const char *suffix)
135 6cdf29f9 2023-01-21 op r = fcgi_printf(c, "Content-Disposition: attachment; "
136 6cdf29f9 2023-01-21 op "filename=%s%s\r\n", file, suffix ? suffix : "");
139 6cdf29f9 2023-01-21 op return gotweb_reply(c, 200, ctype, NULL);
143 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
145 a596b957 2022-07-14 tracey const struct got_error *error = NULL, *error2 = NULL;
146 298f95fb 2023-01-05 op struct got_blob_object *blob = NULL;
147 a596b957 2022-07-14 tracey struct server *srv = NULL;
148 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
149 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
150 69525b4e 2023-01-13 op struct got_reflist_head refs;
151 587550a5 2023-01-10 op FILE *fp = NULL;
152 a596b957 2022-07-14 tracey uint8_t err[] = "gotwebd experienced an error: ";
153 298f95fb 2023-01-05 op int r, html = 0, fd = -1;
155 69525b4e 2023-01-13 op TAILQ_INIT(&refs);
157 a596b957 2022-07-14 tracey /* init the transport */
158 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
159 a596b957 2022-07-14 tracey if (error) {
160 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
163 a596b957 2022-07-14 tracey /* don't process any further if client disconnected */
164 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
166 a596b957 2022-07-14 tracey /* get the gotwebd server */
167 95a4a5a1 2022-08-30 op srv = gotweb_get_server(c->server_name, c->http_host);
168 a596b957 2022-07-14 tracey if (srv == NULL) {
169 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
170 a596b957 2022-07-14 tracey goto err;
172 a596b957 2022-07-14 tracey c->srv = srv;
173 a596b957 2022-07-14 tracey /* parse our querystring */
174 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
175 a596b957 2022-07-14 tracey if (error) {
176 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
177 a596b957 2022-07-14 tracey goto err;
179 a596b957 2022-07-14 tracey c->t->qs = qs;
180 a596b957 2022-07-14 tracey error = gotweb_parse_querystring(&qs, c->querystring);
181 a596b957 2022-07-14 tracey if (error) {
182 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
183 a596b957 2022-07-14 tracey goto err;
187 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
188 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
189 a596b957 2022-07-14 tracey * querystring.
192 298f95fb 2023-01-05 op if (qs->action == BLAME || qs->action == BLOB ||
193 298f95fb 2023-01-05 op qs->action == BLOBRAW || qs->action == DIFF) {
194 298f95fb 2023-01-05 op if (qs->commit == NULL) {
195 298f95fb 2023-01-05 op error2 = got_error(GOT_ERR_QUERYSTRING);
200 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
201 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
202 a596b957 2022-07-14 tracey if (error)
203 a596b957 2022-07-14 tracey goto done;
204 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
205 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
206 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
207 a596b957 2022-07-14 tracey goto err;
210 298f95fb 2023-01-05 op if (qs->action == BLOBRAW) {
211 76007998 2023-01-15 op const uint8_t *buf;
213 6cdf29f9 2023-01-21 op int binary, r;
215 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
216 a596b957 2022-07-14 tracey if (error)
217 a596b957 2022-07-14 tracey goto done;
219 76007998 2023-01-15 op error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
224 6cdf29f9 2023-01-21 op r = gotweb_reply_file(c, "application/octet-stream",
225 6cdf29f9 2023-01-21 op qs->file, NULL);
227 6cdf29f9 2023-01-21 op r = gotweb_reply(c, 200, "text/plain", NULL);
232 76007998 2023-01-15 op error = got_object_blob_read_block(&len, blob);
235 76007998 2023-01-15 op if (len == 0)
237 76007998 2023-01-15 op buf = got_object_blob_get_read_buf(blob);
238 76007998 2023-01-15 op if (fcgi_gen_binary_response(c, buf, len) == -1)
245 298f95fb 2023-01-05 op if (qs->action == BLOB) {
247 298f95fb 2023-01-05 op struct gotweb_url url = {
248 298f95fb 2023-01-05 op .index_page = -1,
250 298f95fb 2023-01-05 op .action = BLOBRAW,
251 298f95fb 2023-01-05 op .path = qs->path,
252 298f95fb 2023-01-05 op .commit = qs->commit,
253 298f95fb 2023-01-05 op .folder = qs->folder,
254 298f95fb 2023-01-05 op .file = qs->file,
257 298f95fb 2023-01-05 op error = got_get_repo_commits(c, 1);
261 298f95fb 2023-01-05 op error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
264 298f95fb 2023-01-05 op if (binary) {
265 6cdf29f9 2023-01-21 op gotweb_reply(c, 302, NULL, &url);
270 1abb18e1 2022-12-20 op if (qs->action == RSS) {
271 6cdf29f9 2023-01-21 op const char *ctype = "application/rss+xml;charset=utf-8";
273 6cdf29f9 2023-01-21 op if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
276 1abb18e1 2022-12-20 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
277 a596b957 2022-07-14 tracey if (error) {
278 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
279 a596b957 2022-07-14 tracey goto err;
281 1abb18e1 2022-12-20 op if (gotweb_render_rss(c->tp) == -1)
283 a596b957 2022-07-14 tracey goto done;
287 6cdf29f9 2023-01-21 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
291 ed619ca0 2022-12-14 op if (gotweb_render_header(c->tp) == -1)
292 a596b957 2022-07-14 tracey goto err;
294 a596b957 2022-07-14 tracey if (error2) {
295 a596b957 2022-07-14 tracey error = error2;
296 a596b957 2022-07-14 tracey goto err;
299 a596b957 2022-07-14 tracey switch(qs->action) {
300 a596b957 2022-07-14 tracey case BLAME:
301 8319855f 2023-01-15 op error = got_get_repo_commits(c, 1);
302 a596b957 2022-07-14 tracey if (error) {
303 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
304 a596b957 2022-07-14 tracey goto err;
306 8319855f 2023-01-15 op if (gotweb_render_blame(c->tp) == -1)
310 298f95fb 2023-01-05 op if (gotweb_render_blob(c->tp, blob) == -1)
313 a596b957 2022-07-14 tracey case BRIEFS:
314 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
315 a596b957 2022-07-14 tracey goto err;
317 a596b957 2022-07-14 tracey case COMMITS:
318 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
319 a596b957 2022-07-14 tracey if (error) {
320 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
321 a596b957 2022-07-14 tracey goto err;
323 156a1144 2022-12-17 op if (gotweb_render_commits(c->tp) == -1)
326 a596b957 2022-07-14 tracey case DIFF:
327 587550a5 2023-01-10 op error = got_get_repo_commits(c, 1);
329 169b1631 2023-01-06 op log_warnx("%s: %s", __func__, error->msg);
332 587550a5 2023-01-10 op error = got_open_diff_for_output(&fp, &fd, c);
334 587550a5 2023-01-10 op log_warnx("%s: %s", __func__, error->msg);
337 587550a5 2023-01-10 op if (gotweb_render_diff(c->tp, fp) == -1)
340 a596b957 2022-07-14 tracey case INDEX:
341 a596b957 2022-07-14 tracey error = gotweb_render_index(c);
342 a596b957 2022-07-14 tracey if (error) {
343 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
344 a596b957 2022-07-14 tracey goto err;
347 a596b957 2022-07-14 tracey case SUMMARY:
348 69525b4e 2023-01-13 op error = got_ref_list(&refs, c->t->repo, "refs/heads",
349 69525b4e 2023-01-13 op got_ref_cmp_by_name, NULL);
350 a596b957 2022-07-14 tracey if (error) {
351 69525b4e 2023-01-13 op log_warnx("%s: got_ref_list: %s", __func__,
353 a596b957 2022-07-14 tracey goto err;
355 69525b4e 2023-01-13 op qs->action = TAGS;
356 69525b4e 2023-01-13 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
358 69525b4e 2023-01-13 op log_warnx("%s: got_get_repo_tags: %s", __func__,
362 69525b4e 2023-01-13 op qs->action = SUMMARY;
363 69525b4e 2023-01-13 op if (gotweb_render_summary(c->tp, &refs) == -1)
366 a596b957 2022-07-14 tracey case TAG:
367 dc07f76c 2023-01-09 op error = got_get_repo_tags(c, 1);
368 a596b957 2022-07-14 tracey if (error) {
369 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
372 dc07f76c 2023-01-09 op if (c->t->tag_count == 0) {
373 dc07f76c 2023-01-09 op error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
374 dc07f76c 2023-01-09 op "bad commit id");
375 a596b957 2022-07-14 tracey goto err;
377 dc07f76c 2023-01-09 op if (gotweb_render_tag(c->tp) == -1)
380 a596b957 2022-07-14 tracey case TAGS:
381 d60961d2 2023-01-13 op error = got_get_repo_tags(c, srv->max_commits_display);
382 a596b957 2022-07-14 tracey if (error) {
383 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
384 a596b957 2022-07-14 tracey goto err;
386 d60961d2 2023-01-13 op if (gotweb_render_tags(c->tp) == -1)
389 a596b957 2022-07-14 tracey case TREE:
390 43d421de 2023-01-05 op error = got_get_repo_commits(c, 1);
391 a596b957 2022-07-14 tracey if (error) {
392 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
393 a596b957 2022-07-14 tracey goto err;
395 43d421de 2023-01-05 op if (gotweb_render_tree(c->tp) == -1)
398 a596b957 2022-07-14 tracey case ERR:
400 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
401 01498c42 2022-08-19 op "Erorr: Bad Querystring");
403 a596b957 2022-07-14 tracey goto err;
407 a596b957 2022-07-14 tracey goto done;
409 01498c42 2022-08-19 op if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
411 b2e7d31e 2022-10-31 landry if (fcgi_printf(c, "\n%s", err) == -1)
413 a596b957 2022-07-14 tracey if (error) {
414 01498c42 2022-08-19 op if (fcgi_printf(c, "%s", error->msg) == -1)
417 01498c42 2022-08-19 op if (fcgi_printf(c, "see daemon logs for details") == -1)
420 01498c42 2022-08-19 op if (html && fcgi_printf(c, "</div>\n") == -1)
424 298f95fb 2023-01-05 op got_object_blob_close(blob);
426 587550a5 2023-01-10 op error = got_gotweb_flushfile(fp, fd);
428 587550a5 2023-01-10 op log_warnx("%s: got_gotweb_flushfile failure: %s",
429 587550a5 2023-01-10 op __func__, error->msg);
432 298f95fb 2023-01-05 op if (fd != -1)
434 a596b957 2022-07-14 tracey if (html && srv != NULL)
435 ed619ca0 2022-12-14 op gotweb_render_footer(c->tp);
437 69525b4e 2023-01-13 op got_ref_list_free(&refs);
440 a596b957 2022-07-14 tracey struct server *
441 95a4a5a1 2022-08-30 op gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
443 a596b957 2022-07-14 tracey struct server *srv = NULL;
445 95a4a5a1 2022-08-30 op /* check against the server name first */
446 a596b957 2022-07-14 tracey if (strlen(server_name) > 0)
447 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
448 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
449 a596b957 2022-07-14 tracey goto done;
451 95a4a5a1 2022-08-30 op /* check against subdomain second */
452 a596b957 2022-07-14 tracey if (strlen(subdomain) > 0)
453 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
454 a596b957 2022-07-14 tracey if (strcmp(srv->name, subdomain) == 0)
455 a596b957 2022-07-14 tracey goto done;
457 a596b957 2022-07-14 tracey /* if those fail, send first server */
458 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
459 a596b957 2022-07-14 tracey if (srv != NULL)
462 a596b957 2022-07-14 tracey return srv;
465 a596b957 2022-07-14 tracey const struct got_error *
466 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
468 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
470 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
471 a596b957 2022-07-14 tracey if (*t == NULL)
472 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
474 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
475 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
477 a596b957 2022-07-14 tracey (*t)->repo = NULL;
478 a596b957 2022-07-14 tracey (*t)->repo_dir = NULL;
479 a596b957 2022-07-14 tracey (*t)->qs = NULL;
480 a596b957 2022-07-14 tracey (*t)->next_id = NULL;
481 a596b957 2022-07-14 tracey (*t)->prev_id = NULL;
482 a596b957 2022-07-14 tracey (*t)->next_disp = 0;
483 a596b957 2022-07-14 tracey (*t)->prev_disp = 0;
485 a596b957 2022-07-14 tracey return error;
488 a596b957 2022-07-14 tracey static const struct got_error *
489 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
491 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
493 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
494 a596b957 2022-07-14 tracey if (*qs == NULL)
495 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
497 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
498 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
501 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
504 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
505 6c37ad7b 2022-09-01 op (*qs)->commit = NULL;
506 6c37ad7b 2022-09-01 op (*qs)->file = NULL;
507 6c37ad7b 2022-09-01 op (*qs)->folder = NULL;
508 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
509 a596b957 2022-07-14 tracey (*qs)->path = NULL;
511 a596b957 2022-07-14 tracey return error;
514 a596b957 2022-07-14 tracey static const struct got_error *
515 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
517 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
518 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
519 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
521 a596b957 2022-07-14 tracey if (qst == NULL)
522 a596b957 2022-07-14 tracey return error;
524 a596b957 2022-07-14 tracey tok1 = strdup(qst);
525 a596b957 2022-07-14 tracey if (tok1 == NULL)
526 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
528 a596b957 2022-07-14 tracey tok1_pair = tok1;
529 a596b957 2022-07-14 tracey tok1_end = tok1;
531 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
532 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
534 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
535 a596b957 2022-07-14 tracey if (tok2 == NULL) {
536 a596b957 2022-07-14 tracey free(tok1);
537 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
540 a596b957 2022-07-14 tracey tok2_pair = tok2;
541 a596b957 2022-07-14 tracey tok2_end = tok2;
543 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
544 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
545 a596b957 2022-07-14 tracey if (tok2_end) {
546 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
547 a596b957 2022-07-14 tracey tok2_end);
548 a596b957 2022-07-14 tracey if (error)
549 a596b957 2022-07-14 tracey goto err;
551 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
553 a596b957 2022-07-14 tracey free(tok2);
554 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
556 a596b957 2022-07-14 tracey free(tok1);
557 a596b957 2022-07-14 tracey return error;
559 a596b957 2022-07-14 tracey free(tok2);
560 a596b957 2022-07-14 tracey free(tok1);
561 a596b957 2022-07-14 tracey return error;
565 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
567 a596b957 2022-07-14 tracey static const struct got_error *
568 58381f70 2022-09-03 op gotweb_urldecode(char *url)
572 58381f70 2022-09-03 op unsigned long x;
574 58381f70 2022-09-03 op hex[2] = '\0';
577 58381f70 2022-09-03 op while (*p != '\0') {
578 58381f70 2022-09-03 op switch (*p) {
580 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
581 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
582 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
583 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
584 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
586 58381f70 2022-09-03 op hex[0] = p[1];
587 58381f70 2022-09-03 op hex[1] = p[2];
590 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
591 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
593 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
594 58381f70 2022-09-03 op *q = (char)x;
609 58381f70 2022-09-03 op static const struct got_error *
610 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
612 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
613 a596b957 2022-07-14 tracey const char *errstr;
614 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
616 58381f70 2022-09-03 op error = gotweb_urldecode(value);
618 58381f70 2022-09-03 op return error;
620 a596b957 2022-07-14 tracey for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
621 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
622 a596b957 2022-07-14 tracey continue;
624 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
625 a596b957 2022-07-14 tracey case ACTION:
626 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
627 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
628 a596b957 2022-07-14 tracey continue;
629 a596b957 2022-07-14 tracey else if (strcmp(value,
630 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
631 a596b957 2022-07-14 tracey (*qs)->action =
632 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
633 a596b957 2022-07-14 tracey goto qa_found;
636 a596b957 2022-07-14 tracey (*qs)->action = ERR;
637 a596b957 2022-07-14 tracey qa_found:
639 a596b957 2022-07-14 tracey case COMMIT:
640 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
641 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
642 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
643 a596b957 2022-07-14 tracey __func__);
644 a596b957 2022-07-14 tracey goto done;
647 a596b957 2022-07-14 tracey case RFILE:
648 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
649 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
650 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
651 a596b957 2022-07-14 tracey __func__);
652 a596b957 2022-07-14 tracey goto done;
655 a596b957 2022-07-14 tracey case FOLDER:
656 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
657 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
658 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
659 a596b957 2022-07-14 tracey __func__);
660 a596b957 2022-07-14 tracey goto done;
663 a596b957 2022-07-14 tracey case HEADREF:
664 f8faf9f1 2022-09-01 op free((*qs)->headref);
665 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
666 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
667 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
668 a596b957 2022-07-14 tracey __func__);
669 a596b957 2022-07-14 tracey goto done;
672 a596b957 2022-07-14 tracey case INDEX_PAGE:
673 a596b957 2022-07-14 tracey if (strlen(value) == 0)
675 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
676 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
677 a596b957 2022-07-14 tracey if (errstr) {
678 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
679 a596b957 2022-07-14 tracey __func__, errstr);
680 a596b957 2022-07-14 tracey goto done;
682 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
683 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
685 a596b957 2022-07-14 tracey case PATH:
686 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
687 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
688 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
689 a596b957 2022-07-14 tracey __func__);
690 a596b957 2022-07-14 tracey goto done;
693 a596b957 2022-07-14 tracey case PAGE:
694 a596b957 2022-07-14 tracey if (strlen(value) == 0)
696 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
697 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
698 a596b957 2022-07-14 tracey if (errstr) {
699 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
700 a596b957 2022-07-14 tracey __func__, errstr);
701 a596b957 2022-07-14 tracey goto done;
703 03f6a843 2022-12-17 op if ((*qs)->page < 0)
704 a596b957 2022-07-14 tracey (*qs)->page = 0;
711 a596b957 2022-07-14 tracey return error;
715 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
717 a596b957 2022-07-14 tracey if (rt != NULL) {
718 a596b957 2022-07-14 tracey free(rt->commit_id);
719 625e5896 2022-09-01 op free(rt->tag_name);
720 625e5896 2022-09-01 op free(rt->tag_commit);
721 625e5896 2022-09-01 op free(rt->commit_msg);
722 a596b957 2022-07-14 tracey free(rt->tagger);
724 a596b957 2022-07-14 tracey free(rt);
728 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
730 a596b957 2022-07-14 tracey if (rc != NULL) {
731 a596b957 2022-07-14 tracey free(rc->path);
732 a596b957 2022-07-14 tracey free(rc->refs_str);
733 a596b957 2022-07-14 tracey free(rc->commit_id);
734 a596b957 2022-07-14 tracey free(rc->parent_id);
735 a596b957 2022-07-14 tracey free(rc->tree_id);
736 a596b957 2022-07-14 tracey free(rc->author);
737 a596b957 2022-07-14 tracey free(rc->committer);
738 a596b957 2022-07-14 tracey free(rc->commit_msg);
740 a596b957 2022-07-14 tracey free(rc);
743 a596b957 2022-07-14 tracey static void
744 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
746 a596b957 2022-07-14 tracey if (qs != NULL) {
747 a596b957 2022-07-14 tracey free(qs->commit);
748 a596b957 2022-07-14 tracey free(qs->file);
749 a596b957 2022-07-14 tracey free(qs->folder);
750 a596b957 2022-07-14 tracey free(qs->headref);
751 a596b957 2022-07-14 tracey free(qs->path);
753 a596b957 2022-07-14 tracey free(qs);
756 a596b957 2022-07-14 tracey static void
757 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
759 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
760 a596b957 2022-07-14 tracey free(repo_dir->name);
761 a596b957 2022-07-14 tracey free(repo_dir->owner);
762 a596b957 2022-07-14 tracey free(repo_dir->description);
763 a596b957 2022-07-14 tracey free(repo_dir->url);
764 a596b957 2022-07-14 tracey free(repo_dir->path);
766 a596b957 2022-07-14 tracey free(repo_dir);
770 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
772 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
773 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
775 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
776 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
777 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
779 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
780 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
781 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
783 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
784 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
785 341fa7ca 2022-09-01 op free(t->next_id);
786 341fa7ca 2022-09-01 op free(t->prev_id);
791 b4c0bd72 2022-12-17 op gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
792 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
794 a596b957 2022-07-14 tracey struct transport *t = c->t;
795 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
796 a596b957 2022-07-14 tracey struct server *srv = c->srv;
798 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
800 a596b957 2022-07-14 tracey switch(qs->action) {
801 a596b957 2022-07-14 tracey case INDEX:
802 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
803 b4c0bd72 2022-12-17 op *have_prev = 1;
804 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
805 8d02314f 2022-09-07 op .action = -1,
806 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
810 b4c0bd72 2022-12-17 op if (t->next_disp == srv->max_repos_display &&
811 b4c0bd72 2022-12-17 op t->repos_total != (qs->index_page + 1) *
812 b4c0bd72 2022-12-17 op srv->max_repos_display) {
813 b4c0bd72 2022-12-17 op *have_next = 1;
814 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
815 b4c0bd72 2022-12-17 op .action = -1,
816 b4c0bd72 2022-12-17 op .index_page = qs->index_page + 1,
821 a596b957 2022-07-14 tracey case BRIEFS:
822 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
823 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
824 b4c0bd72 2022-12-17 op *have_prev = 1;
825 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
826 8d02314f 2022-09-07 op .action = BRIEFS,
827 8d02314f 2022-09-07 op .index_page = -1,
828 8d02314f 2022-09-07 op .page = qs->page - 1,
829 8d02314f 2022-09-07 op .path = qs->path,
830 8d02314f 2022-09-07 op .commit = t->prev_id,
831 8d02314f 2022-09-07 op .headref = qs->headref,
834 b4c0bd72 2022-12-17 op if (t->next_id) {
835 b4c0bd72 2022-12-17 op *have_next = 1;
836 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
837 b4c0bd72 2022-12-17 op .action = BRIEFS,
838 b4c0bd72 2022-12-17 op .index_page = -1,
839 b4c0bd72 2022-12-17 op .page = qs->page + 1,
840 b4c0bd72 2022-12-17 op .path = qs->path,
841 b4c0bd72 2022-12-17 op .commit = t->next_id,
842 b4c0bd72 2022-12-17 op .headref = qs->headref,
846 a596b957 2022-07-14 tracey case COMMITS:
847 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
848 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
849 b4c0bd72 2022-12-17 op *have_prev = 1;
850 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
851 6169d054 2022-12-17 op .action = COMMITS,
852 8d02314f 2022-09-07 op .index_page = -1,
853 8d02314f 2022-09-07 op .page = qs->page - 1,
854 8d02314f 2022-09-07 op .path = qs->path,
855 8d02314f 2022-09-07 op .commit = t->prev_id,
856 8d02314f 2022-09-07 op .headref = qs->headref,
857 8d02314f 2022-09-07 op .folder = qs->folder,
858 8d02314f 2022-09-07 op .file = qs->file,
861 b4c0bd72 2022-12-17 op if (t->next_id) {
862 b4c0bd72 2022-12-17 op *have_next = 1;
863 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
864 6169d054 2022-12-17 op .action = COMMITS,
865 8d02314f 2022-09-07 op .index_page = -1,
866 8d02314f 2022-09-07 op .page = qs->page + 1,
867 8d02314f 2022-09-07 op .path = qs->path,
868 8d02314f 2022-09-07 op .commit = t->next_id,
869 8d02314f 2022-09-07 op .headref = qs->headref,
870 8d02314f 2022-09-07 op .folder = qs->folder,
871 8d02314f 2022-09-07 op .file = qs->file,
875 a596b957 2022-07-14 tracey case TAGS:
876 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
877 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
878 b4c0bd72 2022-12-17 op *have_prev = 1;
879 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
880 b4c0bd72 2022-12-17 op .action = TAGS,
881 b4c0bd72 2022-12-17 op .index_page = -1,
882 b4c0bd72 2022-12-17 op .page = qs->page - 1,
883 b4c0bd72 2022-12-17 op .path = qs->path,
884 b4c0bd72 2022-12-17 op .commit = t->prev_id,
885 b4c0bd72 2022-12-17 op .headref = qs->headref,
888 a596b957 2022-07-14 tracey if (t->next_id) {
889 b4c0bd72 2022-12-17 op *have_next = 1;
890 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
891 8d02314f 2022-09-07 op .action = TAGS,
892 8d02314f 2022-09-07 op .index_page = -1,
893 8d02314f 2022-09-07 op .page = qs->page + 1,
894 8d02314f 2022-09-07 op .path = qs->path,
895 8d02314f 2022-09-07 op .commit = t->next_id,
896 8d02314f 2022-09-07 op .headref = qs->headref,
903 a596b957 2022-07-14 tracey static const struct got_error *
904 a596b957 2022-07-14 tracey gotweb_render_index(struct request *c)
906 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
907 a596b957 2022-07-14 tracey struct server *srv = c->srv;
908 a596b957 2022-07-14 tracey struct transport *t = c->t;
909 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
910 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
912 2db401bd 2022-09-01 op struct dirent **sd_dent = NULL;
913 a596b957 2022-07-14 tracey unsigned int d_cnt, d_i, d_disp = 0;
914 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
917 a596b957 2022-07-14 tracey d = opendir(srv->repos_path);
918 a596b957 2022-07-14 tracey if (d == NULL) {
919 a596b957 2022-07-14 tracey error = got_error_from_errno2("opendir", srv->repos_path);
920 a596b957 2022-07-14 tracey return error;
923 a596b957 2022-07-14 tracey d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
924 a596b957 2022-07-14 tracey if (d_cnt == -1) {
925 2db401bd 2022-09-01 op sd_dent = NULL;
926 a596b957 2022-07-14 tracey error = got_error_from_errno2("scandir", srv->repos_path);
927 a596b957 2022-07-14 tracey goto done;
930 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
931 a596b957 2022-07-14 tracey goto done;
933 a596b957 2022-07-14 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
934 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
937 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
938 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
943 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
944 525dfdf4 2022-11-22 op sd_dent[d_i]);
947 525dfdf4 2022-11-22 op if (type != DT_DIR) {
949 a596b957 2022-07-14 tracey continue;
952 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
953 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
954 a596b957 2022-07-14 tracey t->prev_disp++;
955 a596b957 2022-07-14 tracey continue;
958 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
959 a596b957 2022-07-14 tracey if (error)
960 a596b957 2022-07-14 tracey goto done;
962 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
963 a596b957 2022-07-14 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
964 a596b957 2022-07-14 tracey error = NULL;
965 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
966 a596b957 2022-07-14 tracey repo_dir = NULL;
968 a596b957 2022-07-14 tracey continue;
970 525dfdf4 2022-11-22 op if (error && error->code != GOT_ERR_LONELY_PACKIDX)
973 a596b957 2022-07-14 tracey d_disp++;
974 a596b957 2022-07-14 tracey t->prev_disp++;
976 ed619ca0 2022-12-14 op if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
979 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
980 a596b957 2022-07-14 tracey repo_dir = NULL;
981 a596b957 2022-07-14 tracey t->next_disp++;
982 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
985 525dfdf4 2022-11-22 op t->repos_total = d_cnt - d_skipped;
987 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
989 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
991 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
992 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
995 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
996 a596b957 2022-07-14 tracey goto done;
998 2db401bd 2022-09-01 op if (sd_dent) {
999 2db401bd 2022-09-01 op for (d_i = 0; d_i < d_cnt; d_i++)
1000 2db401bd 2022-09-01 op free(sd_dent[d_i]);
1001 2db401bd 2022-09-01 op free(sd_dent);
1003 a596b957 2022-07-14 tracey if (d != NULL && closedir(d) == EOF && error == NULL)
1004 a596b957 2022-07-14 tracey error = got_error_from_errno("closedir");
1005 a596b957 2022-07-14 tracey return error;
1008 8d02314f 2022-09-07 op static inline int
1009 8d02314f 2022-09-07 op should_urlencode(int c)
1011 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
1014 8d02314f 2022-09-07 op switch (c) {
1015 8d02314f 2022-09-07 op /* gen-delim */
1023 8d02314f 2022-09-07 op /* sub-delims */
1035 4a7f5bae 2023-01-05 op /* needed because the URLs are embedded into the HTML */
1043 8d02314f 2022-09-07 op static char *
1044 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
1046 8d02314f 2022-09-07 op const char *s;
1047 8d02314f 2022-09-07 op char *escaped;
1048 8d02314f 2022-09-07 op size_t i, len;
1052 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1054 8d02314f 2022-09-07 op if (should_urlencode(*s))
1058 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
1059 8d02314f 2022-09-07 op if (escaped == NULL)
1060 8d02314f 2022-09-07 op return NULL;
1063 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1064 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
1065 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
1066 8d02314f 2022-09-07 op b = (*s & 0x0F);
1068 8d02314f 2022-09-07 op escaped[i++] = '%';
1069 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1070 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1072 8d02314f 2022-09-07 op escaped[i++] = *s;
1075 8d02314f 2022-09-07 op return escaped;
1078 ed619ca0 2022-12-14 op const char *
1079 ed619ca0 2022-12-14 op gotweb_action_name(int action)
1081 8d02314f 2022-09-07 op switch (action) {
1083 8d02314f 2022-09-07 op return "blame";
1085 8d02314f 2022-09-07 op return "blob";
1086 298f95fb 2023-01-05 op case BLOBRAW:
1087 298f95fb 2023-01-05 op return "blobraw";
1088 8d02314f 2022-09-07 op case BRIEFS:
1089 8d02314f 2022-09-07 op return "briefs";
1090 8d02314f 2022-09-07 op case COMMITS:
1091 8d02314f 2022-09-07 op return "commits";
1093 8d02314f 2022-09-07 op return "diff";
1095 8d02314f 2022-09-07 op return "err";
1097 8d02314f 2022-09-07 op return "index";
1098 8d02314f 2022-09-07 op case SUMMARY:
1099 8d02314f 2022-09-07 op return "summary";
1101 8d02314f 2022-09-07 op return "tag";
1103 8d02314f 2022-09-07 op return "tags";
1105 8d02314f 2022-09-07 op return "tree";
1107 1abb18e1 2022-12-20 op return "rss";
1109 8d02314f 2022-09-07 op return NULL;
1114 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1116 8d02314f 2022-09-07 op const char *sep = "?", *action;
1120 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1121 8d02314f 2022-09-07 op if (action != NULL) {
1122 8d02314f 2022-09-07 op if (fcgi_printf(c, "?action=%s", action) == -1)
1127 8d02314f 2022-09-07 op if (url->commit) {
1128 8d02314f 2022-09-07 op if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1133 8d02314f 2022-09-07 op if (url->previd) {
1134 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1139 8d02314f 2022-09-07 op if (url->prevset) {
1140 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1145 8d02314f 2022-09-07 op if (url->file) {
1146 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1147 8d02314f 2022-09-07 op if (tmp == NULL)
1149 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1151 8d02314f 2022-09-07 op if (r == -1)
1156 8d02314f 2022-09-07 op if (url->folder) {
1157 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1158 8d02314f 2022-09-07 op if (tmp == NULL)
1160 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1162 8d02314f 2022-09-07 op if (r == -1)
1167 8d02314f 2022-09-07 op if (url->headref) {
1168 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1169 8d02314f 2022-09-07 op if (tmp == NULL)
1171 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1173 8d02314f 2022-09-07 op if (r == -1)
1178 8d02314f 2022-09-07 op if (url->index_page != -1) {
1179 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sindex_page=%d", sep,
1180 8d02314f 2022-09-07 op url->index_page) == -1)
1185 8d02314f 2022-09-07 op if (url->path) {
1186 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1187 8d02314f 2022-09-07 op if (tmp == NULL)
1189 8d02314f 2022-09-07 op r = fcgi_printf(c, "%spath=%s", sep, tmp);
1191 8d02314f 2022-09-07 op if (r == -1)
1196 8d02314f 2022-09-07 op if (url->page != -1) {
1197 8d02314f 2022-09-07 op if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1206 1abb18e1 2022-12-20 op gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1208 1abb18e1 2022-12-20 op struct template *tp = c->tp;
1209 1abb18e1 2022-12-20 op const char *proto = c->https ? "https" : "http";
1211 1abb18e1 2022-12-20 op if (fcgi_puts(tp, proto) == -1 ||
1212 1abb18e1 2022-12-20 op fcgi_puts(tp, "://") == -1 ||
1213 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->server_name) == -1 ||
1214 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->document_uri) == -1)
1217 1abb18e1 2022-12-20 op return gotweb_render_url(c, url);
1220 b5c757f5 2022-09-01 stsp static struct got_repository *
1221 b5c757f5 2022-09-01 stsp find_cached_repo(struct server *srv, const char *path)
1225 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++) {
1226 b5c757f5 2022-09-01 stsp if (strcmp(srv->cached_repos[i].path, path) == 0)
1227 b5c757f5 2022-09-01 stsp return srv->cached_repos[i].repo;
1230 b5c757f5 2022-09-01 stsp return NULL;
1233 a596b957 2022-07-14 tracey static const struct got_error *
1234 b5c757f5 2022-09-01 stsp cache_repo(struct got_repository **new, struct server *srv,
1235 b5c757f5 2022-09-01 stsp struct repo_dir *repo_dir, struct socket *sock)
1237 b5c757f5 2022-09-01 stsp const struct got_error *error = NULL;
1238 b5c757f5 2022-09-01 stsp struct got_repository *repo;
1239 b5c757f5 2022-09-01 stsp struct cached_repo *cr;
1240 b5c757f5 2022-09-01 stsp int evicted = 0;
1242 7e0ec052 2022-09-06 op if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1243 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos - 1];
1244 b5c757f5 2022-09-01 stsp error = got_repo_close(cr->repo);
1245 b5c757f5 2022-09-01 stsp memset(cr, 0, sizeof(*cr));
1246 b5c757f5 2022-09-01 stsp srv->ncached_repos--;
1247 b5c757f5 2022-09-01 stsp if (error)
1248 b5c757f5 2022-09-01 stsp return error;
1249 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1250 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1251 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[0];
1252 b5c757f5 2022-09-01 stsp evicted = 1;
1254 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos];
1257 b5c757f5 2022-09-01 stsp error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1258 b5c757f5 2022-09-01 stsp if (error) {
1259 b5c757f5 2022-09-01 stsp if (evicted) {
1260 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1261 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1263 b5c757f5 2022-09-01 stsp return error;
1266 b5c757f5 2022-09-01 stsp if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1267 b5c757f5 2022-09-01 stsp >= sizeof(cr->path)) {
1268 b5c757f5 2022-09-01 stsp if (evicted) {
1269 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1270 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1272 b5c757f5 2022-09-01 stsp return got_error(GOT_ERR_NO_SPACE);
1275 b5c757f5 2022-09-01 stsp cr->repo = repo;
1276 b5c757f5 2022-09-01 stsp srv->ncached_repos++;
1277 b5c757f5 2022-09-01 stsp *new = repo;
1278 b5c757f5 2022-09-01 stsp return NULL;
1281 b5c757f5 2022-09-01 stsp static const struct got_error *
1282 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1284 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1285 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1286 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1287 a596b957 2022-07-14 tracey struct transport *t = c->t;
1288 b5c757f5 2022-09-01 stsp struct got_repository *repo = NULL;
1289 a596b957 2022-07-14 tracey DIR *dt;
1290 a596b957 2022-07-14 tracey char *dir_test;
1292 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1293 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1294 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1296 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1297 a596b957 2022-07-14 tracey if (dt == NULL) {
1298 a596b957 2022-07-14 tracey free(dir_test);
1299 a596b957 2022-07-14 tracey } else {
1300 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1301 a596b957 2022-07-14 tracey dir_test = NULL;
1305 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1306 0fad85dd 2022-09-01 op repo_dir->name) == -1)
1307 0fad85dd 2022-09-01 op return got_error_from_errno("asprintf");
1309 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1310 a596b957 2022-07-14 tracey if (dt == NULL) {
1311 a596b957 2022-07-14 tracey error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1312 a596b957 2022-07-14 tracey goto err;
1314 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1315 0fad85dd 2022-09-01 op dir_test = NULL;
1319 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1320 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1321 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1322 d5996b9e 2022-10-31 landry goto err;
1325 b5c757f5 2022-09-01 stsp repo = find_cached_repo(srv, repo_dir->path);
1326 b5c757f5 2022-09-01 stsp if (repo == NULL) {
1327 b5c757f5 2022-09-01 stsp error = cache_repo(&repo, srv, repo_dir, sock);
1328 b5c757f5 2022-09-01 stsp if (error)
1331 b5c757f5 2022-09-01 stsp t->repo = repo;
1332 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1333 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1334 a596b957 2022-07-14 tracey if (error)
1335 a596b957 2022-07-14 tracey goto err;
1336 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1337 a596b957 2022-07-14 tracey if (error)
1338 a596b957 2022-07-14 tracey goto err;
1339 cb93ab40 2023-01-22 op error = got_get_repo_age(&repo_dir->age, c, NULL);
1340 a596b957 2022-07-14 tracey if (error)
1341 a596b957 2022-07-14 tracey goto err;
1342 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1345 a596b957 2022-07-14 tracey free(dir_test);
1346 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1347 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1348 a596b957 2022-07-14 tracey return error;
1351 a596b957 2022-07-14 tracey static const struct got_error *
1352 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1354 a596b957 2022-07-14 tracey const struct got_error *error;
1356 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
1357 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
1358 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1360 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1361 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1362 a596b957 2022-07-14 tracey free(*repo_dir);
1363 a596b957 2022-07-14 tracey *repo_dir = NULL;
1364 a596b957 2022-07-14 tracey return error;
1366 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
1367 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
1368 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
1369 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
1371 a596b957 2022-07-14 tracey return NULL;
1374 a596b957 2022-07-14 tracey static const struct got_error *
1375 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1376 3b81530f 2022-11-22 op const char *dirpath, int dir)
1378 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1379 3b81530f 2022-11-22 op struct stat sb;
1380 3b81530f 2022-11-22 op int fd = -1;
1383 a596b957 2022-07-14 tracey *description = NULL;
1384 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1385 a596b957 2022-07-14 tracey return NULL;
1387 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1388 3b81530f 2022-11-22 op if (fd == -1) {
1389 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1390 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1391 3b81530f 2022-11-22 op dirpath, "description");
1393 a596b957 2022-07-14 tracey goto done;
1396 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1397 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1398 3b81530f 2022-11-22 op dirpath, "description");
1399 a596b957 2022-07-14 tracey goto done;
1402 3b81530f 2022-11-22 op len = sb.st_size;
1403 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1404 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1406 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1407 a596b957 2022-07-14 tracey if (*description == NULL) {
1408 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1409 a596b957 2022-07-14 tracey goto done;
1412 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1413 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1415 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1416 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1417 a596b957 2022-07-14 tracey return error;
1420 a596b957 2022-07-14 tracey static const struct got_error *
1421 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1424 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1425 3b81530f 2022-11-22 op struct stat sb;
1426 3b81530f 2022-11-22 op int fd = -1;
1429 a596b957 2022-07-14 tracey *url = NULL;
1430 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1431 a596b957 2022-07-14 tracey return NULL;
1433 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1434 3b81530f 2022-11-22 op if (fd == -1) {
1435 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1436 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1437 3b81530f 2022-11-22 op dirpath, "cloneurl");
1439 a596b957 2022-07-14 tracey goto done;
1442 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1443 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1444 3b81530f 2022-11-22 op dirpath, "cloneurl");
1445 a596b957 2022-07-14 tracey goto done;
1448 3b81530f 2022-11-22 op len = sb.st_size;
1449 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1450 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1452 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1453 a596b957 2022-07-14 tracey if (*url == NULL) {
1454 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1455 a596b957 2022-07-14 tracey goto done;
1458 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1459 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1461 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1462 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1463 a596b957 2022-07-14 tracey return error;
1467 cb93ab40 2023-01-22 op gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1469 cb93ab40 2023-01-22 op struct request *c = tp->tp_arg;
1470 a596b957 2022-07-14 tracey struct tm tm;
1471 fced5a66 2022-07-20 naddy long long diff_time;
1472 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
1473 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
1474 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
1475 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
1476 a596b957 2022-07-14 tracey char *s;
1477 1abb18e1 2022-12-20 op char datebuf[64];
1480 a596b957 2022-07-14 tracey switch (ref_tm) {
1481 a596b957 2022-07-14 tracey case TM_DIFF:
1482 a596b957 2022-07-14 tracey diff_time = time(NULL) - committer_time;
1483 a596b957 2022-07-14 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1484 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1485 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 365), years) == -1)
1487 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1488 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1489 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1490 a596b957 2022-07-14 tracey months) == -1)
1492 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1493 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1494 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1496 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1497 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1498 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24), days) == -1)
1500 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 2) {
1501 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1502 a596b957 2022-07-14 tracey (diff_time / 60 / 60), hours) == -1)
1504 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 2) {
1505 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1506 a596b957 2022-07-14 tracey minutes) == -1)
1508 cb93ab40 2023-01-22 op } else if (diff_time > 2) {
1509 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s", diff_time,
1510 a596b957 2022-07-14 tracey seconds) == -1)
1512 a596b957 2022-07-14 tracey } else {
1513 cb93ab40 2023-01-22 op if (fcgi_puts(tp, now) == -1)
1517 a596b957 2022-07-14 tracey case TM_LONG:
1518 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1521 a596b957 2022-07-14 tracey s = asctime_r(&tm, datebuf);
1522 a596b957 2022-07-14 tracey if (s == NULL)
1525 cb93ab40 2023-01-22 op if (fcgi_puts(tp, datebuf) == -1 ||
1526 cb93ab40 2023-01-22 op fcgi_puts(tp, " UTC") == -1)
1529 1abb18e1 2022-12-20 op case TM_RFC822:
1530 1abb18e1 2022-12-20 op if (gmtime_r(&committer_time, &tm) == NULL)
1533 1abb18e1 2022-12-20 op r = strftime(datebuf, sizeof(datebuf),
1534 1abb18e1 2022-12-20 op "%a, %d %b %Y %H:%M:%S GMT", &tm);
1538 cb93ab40 2023-01-22 op if (fcgi_puts(tp, datebuf) == -1)