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);
96 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_blame(struct request *);
97 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_summary(struct request *);
98 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tags(struct request *);
99 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_branches(struct request *);
101 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
102 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
104 95a4a5a1 2022-08-30 op struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
109 a596b957 2022-07-14 tracey const struct got_error *error = NULL, *error2 = NULL;
110 298f95fb 2023-01-05 op struct got_blob_object *blob = NULL;
111 a596b957 2022-07-14 tracey struct server *srv = NULL;
112 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
113 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
114 587550a5 2023-01-10 op FILE *fp = NULL;
115 a596b957 2022-07-14 tracey uint8_t err[] = "gotwebd experienced an error: ";
116 298f95fb 2023-01-05 op int r, html = 0, fd = -1;
118 a596b957 2022-07-14 tracey /* init the transport */
119 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
120 a596b957 2022-07-14 tracey if (error) {
121 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
124 a596b957 2022-07-14 tracey /* don't process any further if client disconnected */
125 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
127 a596b957 2022-07-14 tracey /* get the gotwebd server */
128 95a4a5a1 2022-08-30 op srv = gotweb_get_server(c->server_name, c->http_host);
129 a596b957 2022-07-14 tracey if (srv == NULL) {
130 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
131 a596b957 2022-07-14 tracey goto err;
133 a596b957 2022-07-14 tracey c->srv = srv;
134 a596b957 2022-07-14 tracey /* parse our querystring */
135 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
136 a596b957 2022-07-14 tracey if (error) {
137 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
138 a596b957 2022-07-14 tracey goto err;
140 a596b957 2022-07-14 tracey c->t->qs = qs;
141 a596b957 2022-07-14 tracey error = gotweb_parse_querystring(&qs, c->querystring);
142 a596b957 2022-07-14 tracey if (error) {
143 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
144 a596b957 2022-07-14 tracey goto err;
148 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
149 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
150 a596b957 2022-07-14 tracey * querystring.
153 298f95fb 2023-01-05 op if (qs->action == BLAME || qs->action == BLOB ||
154 298f95fb 2023-01-05 op qs->action == BLOBRAW || qs->action == DIFF) {
155 298f95fb 2023-01-05 op if (qs->commit == NULL) {
156 298f95fb 2023-01-05 op error2 = got_error(GOT_ERR_QUERYSTRING);
161 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
162 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
163 a596b957 2022-07-14 tracey if (error)
164 a596b957 2022-07-14 tracey goto done;
165 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
166 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
167 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
168 a596b957 2022-07-14 tracey goto err;
171 298f95fb 2023-01-05 op if (qs->action == BLOBRAW) {
172 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
173 a596b957 2022-07-14 tracey if (error)
174 a596b957 2022-07-14 tracey goto done;
175 a596b957 2022-07-14 tracey error = got_output_file_blob(c);
177 1abb18e1 2022-12-20 op log_warnx("%s: %s", __func__, error->msg);
183 298f95fb 2023-01-05 op if (qs->action == BLOB) {
185 298f95fb 2023-01-05 op struct gotweb_url url = {
186 298f95fb 2023-01-05 op .index_page = -1,
188 298f95fb 2023-01-05 op .action = BLOBRAW,
189 298f95fb 2023-01-05 op .path = qs->path,
190 298f95fb 2023-01-05 op .commit = qs->commit,
191 298f95fb 2023-01-05 op .folder = qs->folder,
192 298f95fb 2023-01-05 op .file = qs->file,
195 298f95fb 2023-01-05 op error = got_get_repo_commits(c, 1);
199 298f95fb 2023-01-05 op error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
202 298f95fb 2023-01-05 op if (binary) {
203 298f95fb 2023-01-05 op fcgi_puts(c->tp, "Status: 302\r\n");
204 298f95fb 2023-01-05 op fcgi_puts(c->tp, "Location: ");
205 298f95fb 2023-01-05 op gotweb_render_url(c, &url);
206 298f95fb 2023-01-05 op fcgi_puts(c->tp, "\r\n\r\n");
211 1abb18e1 2022-12-20 op if (qs->action == RSS) {
212 92c8ec64 2023-01-03 op error = gotweb_render_content_type_file(c,
213 92c8ec64 2023-01-03 op "application/rss+xml;charset=utf-8",
214 92c8ec64 2023-01-03 op repo_dir->name, ".rss");
216 1abb18e1 2022-12-20 op log_warnx("%s: %s", __func__, error->msg);
220 1abb18e1 2022-12-20 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
221 a596b957 2022-07-14 tracey if (error) {
222 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
223 a596b957 2022-07-14 tracey goto err;
225 1abb18e1 2022-12-20 op if (gotweb_render_rss(c->tp) == -1)
227 a596b957 2022-07-14 tracey goto done;
231 6970304f 2022-12-04 op error = gotweb_render_content_type(c, "text/html");
233 6970304f 2022-12-04 op log_warnx("%s: %s", __func__, error->msg);
238 ed619ca0 2022-12-14 op if (gotweb_render_header(c->tp) == -1)
239 a596b957 2022-07-14 tracey goto err;
241 a596b957 2022-07-14 tracey if (error2) {
242 a596b957 2022-07-14 tracey error = error2;
243 a596b957 2022-07-14 tracey goto err;
246 a596b957 2022-07-14 tracey switch(qs->action) {
247 a596b957 2022-07-14 tracey case BLAME:
248 a596b957 2022-07-14 tracey error = gotweb_render_blame(c);
249 a596b957 2022-07-14 tracey if (error) {
250 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
251 a596b957 2022-07-14 tracey goto err;
255 298f95fb 2023-01-05 op if (gotweb_render_blob(c->tp, blob) == -1)
258 a596b957 2022-07-14 tracey case BRIEFS:
259 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
260 a596b957 2022-07-14 tracey goto err;
262 a596b957 2022-07-14 tracey case COMMITS:
263 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
264 a596b957 2022-07-14 tracey if (error) {
265 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
266 a596b957 2022-07-14 tracey goto err;
268 156a1144 2022-12-17 op if (gotweb_render_commits(c->tp) == -1)
271 a596b957 2022-07-14 tracey case DIFF:
272 587550a5 2023-01-10 op error = got_get_repo_commits(c, 1);
274 169b1631 2023-01-06 op log_warnx("%s: %s", __func__, error->msg);
277 587550a5 2023-01-10 op error = got_open_diff_for_output(&fp, &fd, c);
279 587550a5 2023-01-10 op log_warnx("%s: %s", __func__, error->msg);
282 587550a5 2023-01-10 op if (gotweb_render_diff(c->tp, fp) == -1)
285 a596b957 2022-07-14 tracey case INDEX:
286 a596b957 2022-07-14 tracey error = gotweb_render_index(c);
287 a596b957 2022-07-14 tracey if (error) {
288 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
289 a596b957 2022-07-14 tracey goto err;
292 a596b957 2022-07-14 tracey case SUMMARY:
293 a596b957 2022-07-14 tracey error = gotweb_render_summary(c);
294 a596b957 2022-07-14 tracey if (error) {
295 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
296 a596b957 2022-07-14 tracey goto err;
299 a596b957 2022-07-14 tracey case TAG:
300 dc07f76c 2023-01-09 op error = got_get_repo_tags(c, 1);
301 a596b957 2022-07-14 tracey if (error) {
302 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
305 dc07f76c 2023-01-09 op if (c->t->tag_count == 0) {
306 dc07f76c 2023-01-09 op error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
307 dc07f76c 2023-01-09 op "bad commit id");
308 a596b957 2022-07-14 tracey goto err;
310 dc07f76c 2023-01-09 op if (gotweb_render_tag(c->tp) == -1)
313 a596b957 2022-07-14 tracey case TAGS:
314 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
315 a596b957 2022-07-14 tracey if (error) {
316 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
317 a596b957 2022-07-14 tracey goto err;
320 a596b957 2022-07-14 tracey case TREE:
321 43d421de 2023-01-05 op error = got_get_repo_commits(c, 1);
322 a596b957 2022-07-14 tracey if (error) {
323 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
324 a596b957 2022-07-14 tracey goto err;
326 43d421de 2023-01-05 op if (gotweb_render_tree(c->tp) == -1)
329 a596b957 2022-07-14 tracey case ERR:
331 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
332 01498c42 2022-08-19 op "Erorr: Bad Querystring");
334 a596b957 2022-07-14 tracey goto err;
338 a596b957 2022-07-14 tracey goto done;
340 01498c42 2022-08-19 op if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
342 b2e7d31e 2022-10-31 landry if (fcgi_printf(c, "\n%s", err) == -1)
344 a596b957 2022-07-14 tracey if (error) {
345 01498c42 2022-08-19 op if (fcgi_printf(c, "%s", error->msg) == -1)
348 01498c42 2022-08-19 op if (fcgi_printf(c, "see daemon logs for details") == -1)
351 01498c42 2022-08-19 op if (html && fcgi_printf(c, "</div>\n") == -1)
355 298f95fb 2023-01-05 op got_object_blob_close(blob);
357 587550a5 2023-01-10 op error = got_gotweb_flushfile(fp, fd);
359 587550a5 2023-01-10 op log_warnx("%s: got_gotweb_flushfile failure: %s",
360 587550a5 2023-01-10 op __func__, error->msg);
363 298f95fb 2023-01-05 op if (fd != -1)
365 a596b957 2022-07-14 tracey if (html && srv != NULL)
366 ed619ca0 2022-12-14 op gotweb_render_footer(c->tp);
369 a596b957 2022-07-14 tracey struct server *
370 95a4a5a1 2022-08-30 op gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
372 a596b957 2022-07-14 tracey struct server *srv = NULL;
374 95a4a5a1 2022-08-30 op /* check against the server name first */
375 a596b957 2022-07-14 tracey if (strlen(server_name) > 0)
376 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
377 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
378 a596b957 2022-07-14 tracey goto done;
380 95a4a5a1 2022-08-30 op /* check against subdomain second */
381 a596b957 2022-07-14 tracey if (strlen(subdomain) > 0)
382 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
383 a596b957 2022-07-14 tracey if (strcmp(srv->name, subdomain) == 0)
384 a596b957 2022-07-14 tracey goto done;
386 a596b957 2022-07-14 tracey /* if those fail, send first server */
387 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
388 a596b957 2022-07-14 tracey if (srv != NULL)
391 a596b957 2022-07-14 tracey return srv;
394 a596b957 2022-07-14 tracey const struct got_error *
395 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
397 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
399 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
400 a596b957 2022-07-14 tracey if (*t == NULL)
401 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
403 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
404 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
406 a596b957 2022-07-14 tracey (*t)->repo = NULL;
407 a596b957 2022-07-14 tracey (*t)->repo_dir = NULL;
408 a596b957 2022-07-14 tracey (*t)->qs = NULL;
409 a596b957 2022-07-14 tracey (*t)->next_id = NULL;
410 a596b957 2022-07-14 tracey (*t)->prev_id = NULL;
411 a596b957 2022-07-14 tracey (*t)->next_disp = 0;
412 a596b957 2022-07-14 tracey (*t)->prev_disp = 0;
414 a596b957 2022-07-14 tracey return error;
417 a596b957 2022-07-14 tracey static const struct got_error *
418 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
420 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
422 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
423 a596b957 2022-07-14 tracey if (*qs == NULL)
424 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
426 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
427 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
430 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
433 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
434 6c37ad7b 2022-09-01 op (*qs)->commit = NULL;
435 6c37ad7b 2022-09-01 op (*qs)->file = NULL;
436 6c37ad7b 2022-09-01 op (*qs)->folder = NULL;
437 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
438 a596b957 2022-07-14 tracey (*qs)->path = NULL;
440 a596b957 2022-07-14 tracey return error;
443 a596b957 2022-07-14 tracey static const struct got_error *
444 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
446 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
447 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
448 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
450 a596b957 2022-07-14 tracey if (qst == NULL)
451 a596b957 2022-07-14 tracey return error;
453 a596b957 2022-07-14 tracey tok1 = strdup(qst);
454 a596b957 2022-07-14 tracey if (tok1 == NULL)
455 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
457 a596b957 2022-07-14 tracey tok1_pair = tok1;
458 a596b957 2022-07-14 tracey tok1_end = tok1;
460 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
461 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
463 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
464 a596b957 2022-07-14 tracey if (tok2 == NULL) {
465 a596b957 2022-07-14 tracey free(tok1);
466 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
469 a596b957 2022-07-14 tracey tok2_pair = tok2;
470 a596b957 2022-07-14 tracey tok2_end = tok2;
472 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
473 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
474 a596b957 2022-07-14 tracey if (tok2_end) {
475 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
476 a596b957 2022-07-14 tracey tok2_end);
477 a596b957 2022-07-14 tracey if (error)
478 a596b957 2022-07-14 tracey goto err;
480 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
482 a596b957 2022-07-14 tracey free(tok2);
483 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
485 a596b957 2022-07-14 tracey free(tok1);
486 a596b957 2022-07-14 tracey return error;
488 a596b957 2022-07-14 tracey free(tok2);
489 a596b957 2022-07-14 tracey free(tok1);
490 a596b957 2022-07-14 tracey return error;
494 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
496 a596b957 2022-07-14 tracey static const struct got_error *
497 58381f70 2022-09-03 op gotweb_urldecode(char *url)
501 58381f70 2022-09-03 op unsigned long x;
503 58381f70 2022-09-03 op hex[2] = '\0';
506 58381f70 2022-09-03 op while (*p != '\0') {
507 58381f70 2022-09-03 op switch (*p) {
509 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
510 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
511 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
512 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
513 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
515 58381f70 2022-09-03 op hex[0] = p[1];
516 58381f70 2022-09-03 op hex[1] = p[2];
519 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
520 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
522 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
523 58381f70 2022-09-03 op *q = (char)x;
538 58381f70 2022-09-03 op static const struct got_error *
539 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
541 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
542 a596b957 2022-07-14 tracey const char *errstr;
543 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
545 58381f70 2022-09-03 op error = gotweb_urldecode(value);
547 58381f70 2022-09-03 op return error;
549 a596b957 2022-07-14 tracey for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
550 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
551 a596b957 2022-07-14 tracey continue;
553 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
554 a596b957 2022-07-14 tracey case ACTION:
555 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
556 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
557 a596b957 2022-07-14 tracey continue;
558 a596b957 2022-07-14 tracey else if (strcmp(value,
559 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
560 a596b957 2022-07-14 tracey (*qs)->action =
561 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
562 a596b957 2022-07-14 tracey goto qa_found;
565 a596b957 2022-07-14 tracey (*qs)->action = ERR;
566 a596b957 2022-07-14 tracey qa_found:
568 a596b957 2022-07-14 tracey case COMMIT:
569 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
570 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
571 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
572 a596b957 2022-07-14 tracey __func__);
573 a596b957 2022-07-14 tracey goto done;
576 a596b957 2022-07-14 tracey case RFILE:
577 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
578 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
579 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
580 a596b957 2022-07-14 tracey __func__);
581 a596b957 2022-07-14 tracey goto done;
584 a596b957 2022-07-14 tracey case FOLDER:
585 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
586 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
587 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
588 a596b957 2022-07-14 tracey __func__);
589 a596b957 2022-07-14 tracey goto done;
592 a596b957 2022-07-14 tracey case HEADREF:
593 f8faf9f1 2022-09-01 op free((*qs)->headref);
594 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
595 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
596 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
597 a596b957 2022-07-14 tracey __func__);
598 a596b957 2022-07-14 tracey goto done;
601 a596b957 2022-07-14 tracey case INDEX_PAGE:
602 a596b957 2022-07-14 tracey if (strlen(value) == 0)
604 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
605 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
606 a596b957 2022-07-14 tracey if (errstr) {
607 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
608 a596b957 2022-07-14 tracey __func__, errstr);
609 a596b957 2022-07-14 tracey goto done;
611 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
612 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
614 a596b957 2022-07-14 tracey case PATH:
615 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
616 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
617 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
618 a596b957 2022-07-14 tracey __func__);
619 a596b957 2022-07-14 tracey goto done;
622 a596b957 2022-07-14 tracey case PAGE:
623 a596b957 2022-07-14 tracey if (strlen(value) == 0)
625 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
626 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
627 a596b957 2022-07-14 tracey if (errstr) {
628 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
629 a596b957 2022-07-14 tracey __func__, errstr);
630 a596b957 2022-07-14 tracey goto done;
632 03f6a843 2022-12-17 op if ((*qs)->page < 0)
633 a596b957 2022-07-14 tracey (*qs)->page = 0;
640 a596b957 2022-07-14 tracey return error;
644 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
646 a596b957 2022-07-14 tracey if (rt != NULL) {
647 a596b957 2022-07-14 tracey free(rt->commit_id);
648 625e5896 2022-09-01 op free(rt->tag_name);
649 625e5896 2022-09-01 op free(rt->tag_commit);
650 625e5896 2022-09-01 op free(rt->commit_msg);
651 a596b957 2022-07-14 tracey free(rt->tagger);
653 a596b957 2022-07-14 tracey free(rt);
657 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
659 a596b957 2022-07-14 tracey if (rc != NULL) {
660 a596b957 2022-07-14 tracey free(rc->path);
661 a596b957 2022-07-14 tracey free(rc->refs_str);
662 a596b957 2022-07-14 tracey free(rc->commit_id);
663 a596b957 2022-07-14 tracey free(rc->parent_id);
664 a596b957 2022-07-14 tracey free(rc->tree_id);
665 a596b957 2022-07-14 tracey free(rc->author);
666 a596b957 2022-07-14 tracey free(rc->committer);
667 a596b957 2022-07-14 tracey free(rc->commit_msg);
669 a596b957 2022-07-14 tracey free(rc);
672 a596b957 2022-07-14 tracey static void
673 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
675 a596b957 2022-07-14 tracey if (qs != NULL) {
676 a596b957 2022-07-14 tracey free(qs->commit);
677 a596b957 2022-07-14 tracey free(qs->file);
678 a596b957 2022-07-14 tracey free(qs->folder);
679 a596b957 2022-07-14 tracey free(qs->headref);
680 a596b957 2022-07-14 tracey free(qs->path);
682 a596b957 2022-07-14 tracey free(qs);
685 a596b957 2022-07-14 tracey static void
686 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
688 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
689 a596b957 2022-07-14 tracey free(repo_dir->name);
690 a596b957 2022-07-14 tracey free(repo_dir->owner);
691 a596b957 2022-07-14 tracey free(repo_dir->description);
692 a596b957 2022-07-14 tracey free(repo_dir->url);
693 a596b957 2022-07-14 tracey free(repo_dir->age);
694 a596b957 2022-07-14 tracey free(repo_dir->path);
696 a596b957 2022-07-14 tracey free(repo_dir);
700 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
702 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
703 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
705 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
706 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
707 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
709 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
710 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
711 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
713 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
714 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
715 341fa7ca 2022-09-01 op free(t->next_id);
716 341fa7ca 2022-09-01 op free(t->prev_id);
720 a596b957 2022-07-14 tracey const struct got_error *
721 345b67f2 2023-01-03 op gotweb_render_content_type(struct request *c, const char *type)
723 4d648b92 2022-08-20 op const char *csp = "default-src 'self'; script-src 'none'; "
724 4d648b92 2022-08-20 op "object-src 'none';";
726 4d648b92 2022-08-20 op fcgi_printf(c,
727 4d648b92 2022-08-20 op "Content-Security-Policy: %s\r\n"
728 4d648b92 2022-08-20 op "Content-Type: %s\r\n\r\n",
733 a596b957 2022-07-14 tracey const struct got_error *
734 1b18f4cd 2023-01-03 op gotweb_render_content_type_file(struct request *c, const char *type,
735 92c8ec64 2023-01-03 op const char *file, const char *suffix)
737 01498c42 2022-08-19 op fcgi_printf(c, "Content-type: %s\r\n"
738 92c8ec64 2023-01-03 op "Content-disposition: attachment; filename=%s%s\r\n\r\n",
739 92c8ec64 2023-01-03 op type, file, suffix ? suffix : "");
744 b4c0bd72 2022-12-17 op gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
745 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
747 a596b957 2022-07-14 tracey struct transport *t = c->t;
748 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
749 a596b957 2022-07-14 tracey struct server *srv = c->srv;
751 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
753 a596b957 2022-07-14 tracey switch(qs->action) {
754 a596b957 2022-07-14 tracey case INDEX:
755 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
756 b4c0bd72 2022-12-17 op *have_prev = 1;
757 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
758 8d02314f 2022-09-07 op .action = -1,
759 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
763 b4c0bd72 2022-12-17 op if (t->next_disp == srv->max_repos_display &&
764 b4c0bd72 2022-12-17 op t->repos_total != (qs->index_page + 1) *
765 b4c0bd72 2022-12-17 op srv->max_repos_display) {
766 b4c0bd72 2022-12-17 op *have_next = 1;
767 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
768 b4c0bd72 2022-12-17 op .action = -1,
769 b4c0bd72 2022-12-17 op .index_page = qs->index_page + 1,
774 a596b957 2022-07-14 tracey case BRIEFS:
775 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
776 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
777 b4c0bd72 2022-12-17 op *have_prev = 1;
778 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
779 8d02314f 2022-09-07 op .action = BRIEFS,
780 8d02314f 2022-09-07 op .index_page = -1,
781 8d02314f 2022-09-07 op .page = qs->page - 1,
782 8d02314f 2022-09-07 op .path = qs->path,
783 8d02314f 2022-09-07 op .commit = t->prev_id,
784 8d02314f 2022-09-07 op .headref = qs->headref,
787 b4c0bd72 2022-12-17 op if (t->next_id) {
788 b4c0bd72 2022-12-17 op *have_next = 1;
789 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
790 b4c0bd72 2022-12-17 op .action = BRIEFS,
791 b4c0bd72 2022-12-17 op .index_page = -1,
792 b4c0bd72 2022-12-17 op .page = qs->page + 1,
793 b4c0bd72 2022-12-17 op .path = qs->path,
794 b4c0bd72 2022-12-17 op .commit = t->next_id,
795 b4c0bd72 2022-12-17 op .headref = qs->headref,
799 a596b957 2022-07-14 tracey case COMMITS:
800 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
801 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
802 b4c0bd72 2022-12-17 op *have_prev = 1;
803 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
804 6169d054 2022-12-17 op .action = COMMITS,
805 8d02314f 2022-09-07 op .index_page = -1,
806 8d02314f 2022-09-07 op .page = qs->page - 1,
807 8d02314f 2022-09-07 op .path = qs->path,
808 8d02314f 2022-09-07 op .commit = t->prev_id,
809 8d02314f 2022-09-07 op .headref = qs->headref,
810 8d02314f 2022-09-07 op .folder = qs->folder,
811 8d02314f 2022-09-07 op .file = qs->file,
814 b4c0bd72 2022-12-17 op if (t->next_id) {
815 b4c0bd72 2022-12-17 op *have_next = 1;
816 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
817 6169d054 2022-12-17 op .action = COMMITS,
818 8d02314f 2022-09-07 op .index_page = -1,
819 8d02314f 2022-09-07 op .page = qs->page + 1,
820 8d02314f 2022-09-07 op .path = qs->path,
821 8d02314f 2022-09-07 op .commit = t->next_id,
822 8d02314f 2022-09-07 op .headref = qs->headref,
823 8d02314f 2022-09-07 op .folder = qs->folder,
824 8d02314f 2022-09-07 op .file = qs->file,
828 a596b957 2022-07-14 tracey case TAGS:
829 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
830 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
831 b4c0bd72 2022-12-17 op *have_prev = 1;
832 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
833 b4c0bd72 2022-12-17 op .action = TAGS,
834 b4c0bd72 2022-12-17 op .index_page = -1,
835 b4c0bd72 2022-12-17 op .page = qs->page - 1,
836 b4c0bd72 2022-12-17 op .path = qs->path,
837 b4c0bd72 2022-12-17 op .commit = t->prev_id,
838 b4c0bd72 2022-12-17 op .headref = qs->headref,
841 a596b957 2022-07-14 tracey if (t->next_id) {
842 b4c0bd72 2022-12-17 op *have_next = 1;
843 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
844 8d02314f 2022-09-07 op .action = TAGS,
845 8d02314f 2022-09-07 op .index_page = -1,
846 8d02314f 2022-09-07 op .page = qs->page + 1,
847 8d02314f 2022-09-07 op .path = qs->path,
848 8d02314f 2022-09-07 op .commit = t->next_id,
849 8d02314f 2022-09-07 op .headref = qs->headref,
856 a596b957 2022-07-14 tracey static const struct got_error *
857 a596b957 2022-07-14 tracey gotweb_render_index(struct request *c)
859 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
860 a596b957 2022-07-14 tracey struct server *srv = c->srv;
861 a596b957 2022-07-14 tracey struct transport *t = c->t;
862 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
863 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
865 2db401bd 2022-09-01 op struct dirent **sd_dent = NULL;
866 a596b957 2022-07-14 tracey unsigned int d_cnt, d_i, d_disp = 0;
867 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
870 a596b957 2022-07-14 tracey d = opendir(srv->repos_path);
871 a596b957 2022-07-14 tracey if (d == NULL) {
872 a596b957 2022-07-14 tracey error = got_error_from_errno2("opendir", srv->repos_path);
873 a596b957 2022-07-14 tracey return error;
876 a596b957 2022-07-14 tracey d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
877 a596b957 2022-07-14 tracey if (d_cnt == -1) {
878 2db401bd 2022-09-01 op sd_dent = NULL;
879 a596b957 2022-07-14 tracey error = got_error_from_errno2("scandir", srv->repos_path);
880 a596b957 2022-07-14 tracey goto done;
883 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
884 a596b957 2022-07-14 tracey goto done;
886 a596b957 2022-07-14 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
887 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
890 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
891 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
896 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
897 525dfdf4 2022-11-22 op sd_dent[d_i]);
900 525dfdf4 2022-11-22 op if (type != DT_DIR) {
902 a596b957 2022-07-14 tracey continue;
905 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
906 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
907 a596b957 2022-07-14 tracey t->prev_disp++;
908 a596b957 2022-07-14 tracey continue;
911 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
912 a596b957 2022-07-14 tracey if (error)
913 a596b957 2022-07-14 tracey goto done;
915 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
916 a596b957 2022-07-14 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
917 a596b957 2022-07-14 tracey error = NULL;
918 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
919 a596b957 2022-07-14 tracey repo_dir = NULL;
921 a596b957 2022-07-14 tracey continue;
923 525dfdf4 2022-11-22 op if (error && error->code != GOT_ERR_LONELY_PACKIDX)
926 a596b957 2022-07-14 tracey d_disp++;
927 a596b957 2022-07-14 tracey t->prev_disp++;
929 ed619ca0 2022-12-14 op if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
932 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
933 a596b957 2022-07-14 tracey repo_dir = NULL;
934 a596b957 2022-07-14 tracey t->next_disp++;
935 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
938 525dfdf4 2022-11-22 op t->repos_total = d_cnt - d_skipped;
940 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
942 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
944 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
945 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
948 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
949 a596b957 2022-07-14 tracey goto done;
951 2db401bd 2022-09-01 op if (sd_dent) {
952 2db401bd 2022-09-01 op for (d_i = 0; d_i < d_cnt; d_i++)
953 2db401bd 2022-09-01 op free(sd_dent[d_i]);
954 2db401bd 2022-09-01 op free(sd_dent);
956 a596b957 2022-07-14 tracey if (d != NULL && closedir(d) == EOF && error == NULL)
957 a596b957 2022-07-14 tracey error = got_error_from_errno("closedir");
958 a596b957 2022-07-14 tracey return error;
961 a596b957 2022-07-14 tracey static const struct got_error *
962 a596b957 2022-07-14 tracey gotweb_render_blame(struct request *c)
964 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
965 a596b957 2022-07-14 tracey struct transport *t = c->t;
966 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
967 d927f8c8 2022-08-20 op char *age = NULL, *msg = NULL;
970 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
971 a596b957 2022-07-14 tracey if (error)
972 a596b957 2022-07-14 tracey return error;
974 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
976 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
979 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
980 a596b957 2022-07-14 tracey if (error)
981 a596b957 2022-07-14 tracey goto done;
983 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
984 01498c42 2022-08-19 op "<div id='blame_title'>Blame</div>\n"
985 01498c42 2022-08-19 op "</div>\n" /* #blame_title_wrapper */
986 01498c42 2022-08-19 op "<div id='blame_content'>\n"
987 01498c42 2022-08-19 op "<div id='blame_header_wrapper'>\n"
988 01498c42 2022-08-19 op "<div id='blame_header'>\n"
989 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
990 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
991 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
992 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
993 01498c42 2022-08-19 op "</div>\n" /* #blame_header */
994 01498c42 2022-08-19 op "</div>\n" /* #blame_header_wrapper */
995 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
996 01498c42 2022-08-19 op "<div id='blame'>\n",
1000 a596b957 2022-07-14 tracey goto done;
1002 a596b957 2022-07-14 tracey error = got_output_file_blame(c);
1003 a596b957 2022-07-14 tracey if (error)
1004 a596b957 2022-07-14 tracey goto done;
1006 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n" /* #blame */
1007 01498c42 2022-08-19 op "</div>\n"); /* #blame_content */
1011 a596b957 2022-07-14 tracey return error;
1014 a596b957 2022-07-14 tracey static const struct got_error *
1015 a596b957 2022-07-14 tracey gotweb_render_branches(struct request *c)
1017 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1018 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1019 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
1020 a596b957 2022-07-14 tracey struct transport *t = c->t;
1021 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1022 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1023 8d02314f 2022-09-07 op char *escaped_refname = NULL;
1024 a596b957 2022-07-14 tracey char *age = NULL;
1027 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1029 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
1030 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
1031 a596b957 2022-07-14 tracey if (error)
1032 a596b957 2022-07-14 tracey goto done;
1034 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1035 01498c42 2022-08-19 op "<div id='branches_title'>Branches</div>\n"
1036 01498c42 2022-08-19 op "</div>\n" /* #branches_title_wrapper */
1037 01498c42 2022-08-19 op "<div id='branches_content'>\n");
1038 01498c42 2022-08-19 op if (r == -1)
1039 a596b957 2022-07-14 tracey goto done;
1041 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
1042 d927f8c8 2022-08-20 op const char *refname = NULL;
1044 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
1045 a596b957 2022-07-14 tracey continue;
1047 d927f8c8 2022-08-20 op refname = got_ref_get_name(re->ref);
1048 a596b957 2022-07-14 tracey if (refname == NULL) {
1049 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1050 a596b957 2022-07-14 tracey goto done;
1052 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) != 0)
1053 a596b957 2022-07-14 tracey continue;
1055 c127fc49 2022-11-22 op error = got_get_repo_age(&age, c, refname, TM_DIFF);
1056 a596b957 2022-07-14 tracey if (error)
1057 a596b957 2022-07-14 tracey goto done;
1059 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
1060 a596b957 2022-07-14 tracey refname += 11;
1061 d927f8c8 2022-08-20 op error = gotweb_escape_html(&escaped_refname, refname);
1065 01498c42 2022-08-19 op r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1066 01498c42 2022-08-19 op "<div class='branches_age'>%s</div>\n"
1067 01498c42 2022-08-19 op "<div class='branches_space'> </div>\n"
1068 8d02314f 2022-09-07 op "<div class='branch'>", age);
1069 8d02314f 2022-09-07 op if (r == -1)
1072 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1073 8d02314f 2022-09-07 op .action = SUMMARY,
1074 8d02314f 2022-09-07 op .index_page = -1,
1076 8d02314f 2022-09-07 op .path = qs->path,
1077 8d02314f 2022-09-07 op .headref = refname,
1078 8d02314f 2022-09-07 op }, "%s", escaped_refname);
1079 8d02314f 2022-09-07 op if (r == -1)
1082 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .branch */
1083 01498c42 2022-08-19 op "<div class='navs_wrapper'>\n"
1084 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1087 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1088 8d02314f 2022-09-07 op .action = SUMMARY,
1089 8d02314f 2022-09-07 op .index_page = -1,
1091 8d02314f 2022-09-07 op .path = qs->path,
1092 8d02314f 2022-09-07 op .headref = refname,
1093 8d02314f 2022-09-07 op }, "summary");
1094 8d02314f 2022-09-07 op if (r == -1)
1097 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1100 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1101 8d02314f 2022-09-07 op .action = BRIEFS,
1102 8d02314f 2022-09-07 op .index_page = -1,
1104 8d02314f 2022-09-07 op .path = qs->path,
1105 8d02314f 2022-09-07 op .headref = refname,
1106 8d02314f 2022-09-07 op }, "commit briefs");
1107 8d02314f 2022-09-07 op if (r == -1)
1110 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1113 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1114 8d02314f 2022-09-07 op .action = COMMITS,
1115 8d02314f 2022-09-07 op .index_page = -1,
1117 8d02314f 2022-09-07 op .path = qs->path,
1118 8d02314f 2022-09-07 op .headref = refname,
1119 8d02314f 2022-09-07 op }, "commits");
1120 8d02314f 2022-09-07 op if (r == -1)
1123 8d02314f 2022-09-07 op r = fcgi_printf(c, "</div>\n" /* .navs */
1124 8d02314f 2022-09-07 op "</div>\n" /* .navs_wrapper */
1125 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1126 8d02314f 2022-09-07 op "</div>\n"); /* .branches_wrapper */
1127 01498c42 2022-08-19 op if (r == -1)
1128 a596b957 2022-07-14 tracey goto done;
1130 a596b957 2022-07-14 tracey free(age);
1131 a596b957 2022-07-14 tracey age = NULL;
1132 8d02314f 2022-09-07 op free(escaped_refname);
1133 8d02314f 2022-09-07 op escaped_refname = NULL;
1135 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #branches_content */
1138 8d02314f 2022-09-07 op free(escaped_refname);
1139 f49cdcf5 2022-09-02 op got_ref_list_free(&refs);
1140 a596b957 2022-07-14 tracey return error;
1143 a596b957 2022-07-14 tracey static const struct got_error *
1144 a596b957 2022-07-14 tracey gotweb_render_summary(struct request *c)
1146 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1147 a596b957 2022-07-14 tracey struct transport *t = c->t;
1148 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1151 01498c42 2022-08-19 op if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1152 a596b957 2022-07-14 tracey goto done;
1154 01498c42 2022-08-19 op if (srv->show_repo_description) {
1155 01498c42 2022-08-19 op r = fcgi_printf(c,
1156 01498c42 2022-08-19 op "<div id='description_title'>Description:</div>\n"
1157 01498c42 2022-08-19 op "<div id='description'>%s</div>\n",
1158 f897bb24 2022-08-20 op t->repo_dir->description ? t->repo_dir->description : "");
1159 01498c42 2022-08-19 op if (r == -1)
1163 01498c42 2022-08-19 op if (srv->show_repo_owner) {
1164 01498c42 2022-08-19 op r = fcgi_printf(c,
1165 01498c42 2022-08-19 op "<div id='repo_owner_title'>Owner:</div>\n"
1166 01498c42 2022-08-19 op "<div id='repo_owner'>%s</div>\n",
1167 f897bb24 2022-08-20 op t->repo_dir->owner ? t->repo_dir->owner : "");
1168 01498c42 2022-08-19 op if (r == -1)
1172 01498c42 2022-08-19 op if (srv->show_repo_age) {
1173 01498c42 2022-08-19 op r = fcgi_printf(c,
1174 01498c42 2022-08-19 op "<div id='last_change_title'>Last Change:</div>\n"
1175 01498c42 2022-08-19 op "<div id='last_change'>%s</div>\n",
1176 01498c42 2022-08-19 op t->repo_dir->age);
1177 01498c42 2022-08-19 op if (r == -1)
1181 01498c42 2022-08-19 op if (srv->show_repo_cloneurl) {
1182 01498c42 2022-08-19 op r = fcgi_printf(c,
1183 01498c42 2022-08-19 op "<div id='cloneurl_title'>Clone URL:</div>\n"
1184 01498c42 2022-08-19 op "<div id='cloneurl'>%s</div>\n",
1185 01498c42 2022-08-19 op t->repo_dir->url ? t->repo_dir->url : "");
1186 01498c42 2022-08-19 op if (r == -1)
1190 01498c42 2022-08-19 op r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1191 01498c42 2022-08-19 op if (r == -1)
1192 a596b957 2022-07-14 tracey goto done;
1194 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
1195 a596b957 2022-07-14 tracey goto done;
1197 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
1198 a596b957 2022-07-14 tracey if (error) {
1199 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1200 a596b957 2022-07-14 tracey goto done;
1203 a596b957 2022-07-14 tracey error = gotweb_render_branches(c);
1204 a596b957 2022-07-14 tracey if (error)
1205 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1207 a596b957 2022-07-14 tracey return error;
1210 a596b957 2022-07-14 tracey static const struct got_error *
1211 a596b957 2022-07-14 tracey gotweb_render_tags(struct request *c)
1213 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1214 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1215 a596b957 2022-07-14 tracey struct transport *t = c->t;
1216 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1218 a596b957 2022-07-14 tracey if (qs->action == BRIEFS) {
1219 a596b957 2022-07-14 tracey qs->action = TAGS;
1220 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1222 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, srv->max_commits_display);
1223 a596b957 2022-07-14 tracey if (error)
1224 a596b957 2022-07-14 tracey goto done;
1226 067396e6 2023-01-09 op if (gotweb_render_tags_tmpl(c->tp) == -1)
1227 a596b957 2022-07-14 tracey goto done;
1230 a596b957 2022-07-14 tracey return error;
1233 a596b957 2022-07-14 tracey const struct got_error *
1234 a596b957 2022-07-14 tracey gotweb_escape_html(char **escaped_html, const char *orig_html)
1236 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1237 a596b957 2022-07-14 tracey struct escape_pair {
1239 a596b957 2022-07-14 tracey const char *s;
1240 a596b957 2022-07-14 tracey } esc[] = {
1241 a596b957 2022-07-14 tracey { '>', ">" },
1242 a596b957 2022-07-14 tracey { '<', "<" },
1243 a596b957 2022-07-14 tracey { '&', "&" },
1244 a596b957 2022-07-14 tracey { '"', """ },
1245 a596b957 2022-07-14 tracey { '\'', "'" },
1246 a596b957 2022-07-14 tracey { '\n', "<br />" },
1248 a596b957 2022-07-14 tracey size_t orig_len, len;
1249 a596b957 2022-07-14 tracey int i, j, x;
1251 a596b957 2022-07-14 tracey orig_len = strlen(orig_html);
1252 a596b957 2022-07-14 tracey len = orig_len;
1253 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1254 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1255 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1256 a596b957 2022-07-14 tracey continue;
1257 a596b957 2022-07-14 tracey len += strlen(esc[j].s) - 1 /* escaped char */;
1261 a596b957 2022-07-14 tracey *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1262 a596b957 2022-07-14 tracey if (*escaped_html == NULL)
1263 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1266 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1267 a596b957 2022-07-14 tracey int escaped = 0;
1268 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1269 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1270 a596b957 2022-07-14 tracey continue;
1272 a596b957 2022-07-14 tracey if (strlcat(*escaped_html, esc[j].s, len + 1)
1273 a596b957 2022-07-14 tracey >= len + 1) {
1274 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_SPACE);
1275 a596b957 2022-07-14 tracey goto done;
1277 a596b957 2022-07-14 tracey x += strlen(esc[j].s);
1278 a596b957 2022-07-14 tracey escaped = 1;
1281 a596b957 2022-07-14 tracey if (!escaped) {
1282 a596b957 2022-07-14 tracey (*escaped_html)[x] = orig_html[i];
1287 a596b957 2022-07-14 tracey if (error) {
1288 a596b957 2022-07-14 tracey free(*escaped_html);
1289 a596b957 2022-07-14 tracey *escaped_html = NULL;
1290 a596b957 2022-07-14 tracey } else {
1291 a596b957 2022-07-14 tracey (*escaped_html)[x] = '\0';
1294 a596b957 2022-07-14 tracey return error;
1297 8d02314f 2022-09-07 op static inline int
1298 8d02314f 2022-09-07 op should_urlencode(int c)
1300 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
1303 8d02314f 2022-09-07 op switch (c) {
1304 8d02314f 2022-09-07 op /* gen-delim */
1312 8d02314f 2022-09-07 op /* sub-delims */
1324 4a7f5bae 2023-01-05 op /* needed because the URLs are embedded into the HTML */
1332 8d02314f 2022-09-07 op static char *
1333 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
1335 8d02314f 2022-09-07 op const char *s;
1336 8d02314f 2022-09-07 op char *escaped;
1337 8d02314f 2022-09-07 op size_t i, len;
1341 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1343 8d02314f 2022-09-07 op if (should_urlencode(*s))
1347 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
1348 8d02314f 2022-09-07 op if (escaped == NULL)
1349 8d02314f 2022-09-07 op return NULL;
1352 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1353 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
1354 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
1355 8d02314f 2022-09-07 op b = (*s & 0x0F);
1357 8d02314f 2022-09-07 op escaped[i++] = '%';
1358 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1359 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1361 8d02314f 2022-09-07 op escaped[i++] = *s;
1364 8d02314f 2022-09-07 op return escaped;
1367 ed619ca0 2022-12-14 op const char *
1368 ed619ca0 2022-12-14 op gotweb_action_name(int action)
1370 8d02314f 2022-09-07 op switch (action) {
1372 8d02314f 2022-09-07 op return "blame";
1374 8d02314f 2022-09-07 op return "blob";
1375 298f95fb 2023-01-05 op case BLOBRAW:
1376 298f95fb 2023-01-05 op return "blobraw";
1377 8d02314f 2022-09-07 op case BRIEFS:
1378 8d02314f 2022-09-07 op return "briefs";
1379 8d02314f 2022-09-07 op case COMMITS:
1380 8d02314f 2022-09-07 op return "commits";
1382 8d02314f 2022-09-07 op return "diff";
1384 8d02314f 2022-09-07 op return "err";
1386 8d02314f 2022-09-07 op return "index";
1387 8d02314f 2022-09-07 op case SUMMARY:
1388 8d02314f 2022-09-07 op return "summary";
1390 8d02314f 2022-09-07 op return "tag";
1392 8d02314f 2022-09-07 op return "tags";
1394 8d02314f 2022-09-07 op return "tree";
1396 1abb18e1 2022-12-20 op return "rss";
1398 8d02314f 2022-09-07 op return NULL;
1403 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1405 8d02314f 2022-09-07 op const char *sep = "?", *action;
1409 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1410 8d02314f 2022-09-07 op if (action != NULL) {
1411 8d02314f 2022-09-07 op if (fcgi_printf(c, "?action=%s", action) == -1)
1416 8d02314f 2022-09-07 op if (url->commit) {
1417 8d02314f 2022-09-07 op if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1422 8d02314f 2022-09-07 op if (url->previd) {
1423 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1428 8d02314f 2022-09-07 op if (url->prevset) {
1429 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1434 8d02314f 2022-09-07 op if (url->file) {
1435 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1436 8d02314f 2022-09-07 op if (tmp == NULL)
1438 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1440 8d02314f 2022-09-07 op if (r == -1)
1445 8d02314f 2022-09-07 op if (url->folder) {
1446 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1447 8d02314f 2022-09-07 op if (tmp == NULL)
1449 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1451 8d02314f 2022-09-07 op if (r == -1)
1456 8d02314f 2022-09-07 op if (url->headref) {
1457 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1458 8d02314f 2022-09-07 op if (tmp == NULL)
1460 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1462 8d02314f 2022-09-07 op if (r == -1)
1467 8d02314f 2022-09-07 op if (url->index_page != -1) {
1468 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sindex_page=%d", sep,
1469 8d02314f 2022-09-07 op url->index_page) == -1)
1474 8d02314f 2022-09-07 op if (url->path) {
1475 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1476 8d02314f 2022-09-07 op if (tmp == NULL)
1478 8d02314f 2022-09-07 op r = fcgi_printf(c, "%spath=%s", sep, tmp);
1480 8d02314f 2022-09-07 op if (r == -1)
1485 8d02314f 2022-09-07 op if (url->page != -1) {
1486 8d02314f 2022-09-07 op if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1495 1abb18e1 2022-12-20 op gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1497 1abb18e1 2022-12-20 op struct template *tp = c->tp;
1498 1abb18e1 2022-12-20 op const char *proto = c->https ? "https" : "http";
1500 1abb18e1 2022-12-20 op if (fcgi_puts(tp, proto) == -1 ||
1501 1abb18e1 2022-12-20 op fcgi_puts(tp, "://") == -1 ||
1502 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->server_name) == -1 ||
1503 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->document_uri) == -1)
1506 1abb18e1 2022-12-20 op return gotweb_render_url(c, url);
1510 8d02314f 2022-09-07 op gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1515 8d02314f 2022-09-07 op if (fcgi_printf(c, "<a href='") == -1)
1518 ed619ca0 2022-12-14 op if (gotweb_render_url(c, url) == -1)
1521 8d02314f 2022-09-07 op if (fcgi_printf(c, "'>") == -1)
1524 8d02314f 2022-09-07 op va_start(ap, fmt);
1525 8d02314f 2022-09-07 op r = fcgi_vprintf(c, fmt, ap);
1527 8d02314f 2022-09-07 op if (r == -1)
1530 8d02314f 2022-09-07 op if (fcgi_printf(c, "</a>"))
1535 b5c757f5 2022-09-01 stsp static struct got_repository *
1536 b5c757f5 2022-09-01 stsp find_cached_repo(struct server *srv, const char *path)
1540 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++) {
1541 b5c757f5 2022-09-01 stsp if (strcmp(srv->cached_repos[i].path, path) == 0)
1542 b5c757f5 2022-09-01 stsp return srv->cached_repos[i].repo;
1545 b5c757f5 2022-09-01 stsp return NULL;
1548 a596b957 2022-07-14 tracey static const struct got_error *
1549 b5c757f5 2022-09-01 stsp cache_repo(struct got_repository **new, struct server *srv,
1550 b5c757f5 2022-09-01 stsp struct repo_dir *repo_dir, struct socket *sock)
1552 b5c757f5 2022-09-01 stsp const struct got_error *error = NULL;
1553 b5c757f5 2022-09-01 stsp struct got_repository *repo;
1554 b5c757f5 2022-09-01 stsp struct cached_repo *cr;
1555 b5c757f5 2022-09-01 stsp int evicted = 0;
1557 7e0ec052 2022-09-06 op if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1558 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos - 1];
1559 b5c757f5 2022-09-01 stsp error = got_repo_close(cr->repo);
1560 b5c757f5 2022-09-01 stsp memset(cr, 0, sizeof(*cr));
1561 b5c757f5 2022-09-01 stsp srv->ncached_repos--;
1562 b5c757f5 2022-09-01 stsp if (error)
1563 b5c757f5 2022-09-01 stsp return error;
1564 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1565 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1566 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[0];
1567 b5c757f5 2022-09-01 stsp evicted = 1;
1569 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos];
1572 b5c757f5 2022-09-01 stsp error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1573 b5c757f5 2022-09-01 stsp if (error) {
1574 b5c757f5 2022-09-01 stsp if (evicted) {
1575 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1576 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1578 b5c757f5 2022-09-01 stsp return error;
1581 b5c757f5 2022-09-01 stsp if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1582 b5c757f5 2022-09-01 stsp >= sizeof(cr->path)) {
1583 b5c757f5 2022-09-01 stsp if (evicted) {
1584 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1585 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1587 b5c757f5 2022-09-01 stsp return got_error(GOT_ERR_NO_SPACE);
1590 b5c757f5 2022-09-01 stsp cr->repo = repo;
1591 b5c757f5 2022-09-01 stsp srv->ncached_repos++;
1592 b5c757f5 2022-09-01 stsp *new = repo;
1593 b5c757f5 2022-09-01 stsp return NULL;
1596 b5c757f5 2022-09-01 stsp static const struct got_error *
1597 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1599 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1600 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1601 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1602 a596b957 2022-07-14 tracey struct transport *t = c->t;
1603 b5c757f5 2022-09-01 stsp struct got_repository *repo = NULL;
1604 a596b957 2022-07-14 tracey DIR *dt;
1605 a596b957 2022-07-14 tracey char *dir_test;
1607 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1608 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1609 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1611 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1612 a596b957 2022-07-14 tracey if (dt == NULL) {
1613 a596b957 2022-07-14 tracey free(dir_test);
1614 a596b957 2022-07-14 tracey } else {
1615 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1616 a596b957 2022-07-14 tracey dir_test = NULL;
1620 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1621 0fad85dd 2022-09-01 op repo_dir->name) == -1)
1622 0fad85dd 2022-09-01 op return got_error_from_errno("asprintf");
1624 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1625 a596b957 2022-07-14 tracey if (dt == NULL) {
1626 a596b957 2022-07-14 tracey error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1627 a596b957 2022-07-14 tracey goto err;
1629 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1630 0fad85dd 2022-09-01 op dir_test = NULL;
1634 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1635 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1636 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1637 d5996b9e 2022-10-31 landry goto err;
1640 b5c757f5 2022-09-01 stsp repo = find_cached_repo(srv, repo_dir->path);
1641 b5c757f5 2022-09-01 stsp if (repo == NULL) {
1642 b5c757f5 2022-09-01 stsp error = cache_repo(&repo, srv, repo_dir, sock);
1643 b5c757f5 2022-09-01 stsp if (error)
1646 b5c757f5 2022-09-01 stsp t->repo = repo;
1647 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1648 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1649 a596b957 2022-07-14 tracey if (error)
1650 a596b957 2022-07-14 tracey goto err;
1651 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1652 a596b957 2022-07-14 tracey if (error)
1653 a596b957 2022-07-14 tracey goto err;
1654 c127fc49 2022-11-22 op error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1655 a596b957 2022-07-14 tracey if (error)
1656 a596b957 2022-07-14 tracey goto err;
1657 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1660 a596b957 2022-07-14 tracey free(dir_test);
1661 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1662 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1663 a596b957 2022-07-14 tracey return error;
1666 a596b957 2022-07-14 tracey static const struct got_error *
1667 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1669 a596b957 2022-07-14 tracey const struct got_error *error;
1671 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
1672 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
1673 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1675 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1676 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1677 a596b957 2022-07-14 tracey free(*repo_dir);
1678 a596b957 2022-07-14 tracey *repo_dir = NULL;
1679 a596b957 2022-07-14 tracey return error;
1681 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
1682 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
1683 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
1684 a596b957 2022-07-14 tracey (*repo_dir)->age = NULL;
1685 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
1687 a596b957 2022-07-14 tracey return NULL;
1690 a596b957 2022-07-14 tracey static const struct got_error *
1691 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1692 3b81530f 2022-11-22 op const char *dirpath, int dir)
1694 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1695 3b81530f 2022-11-22 op struct stat sb;
1696 3b81530f 2022-11-22 op int fd = -1;
1699 a596b957 2022-07-14 tracey *description = NULL;
1700 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1701 a596b957 2022-07-14 tracey return NULL;
1703 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1704 3b81530f 2022-11-22 op if (fd == -1) {
1705 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1706 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1707 3b81530f 2022-11-22 op dirpath, "description");
1709 a596b957 2022-07-14 tracey goto done;
1712 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1713 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1714 3b81530f 2022-11-22 op dirpath, "description");
1715 a596b957 2022-07-14 tracey goto done;
1718 3b81530f 2022-11-22 op len = sb.st_size;
1719 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1720 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1722 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1723 a596b957 2022-07-14 tracey if (*description == NULL) {
1724 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1725 a596b957 2022-07-14 tracey goto done;
1728 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1729 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1731 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1732 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1733 a596b957 2022-07-14 tracey return error;
1736 a596b957 2022-07-14 tracey static const struct got_error *
1737 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1740 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1741 3b81530f 2022-11-22 op struct stat sb;
1742 3b81530f 2022-11-22 op int fd = -1;
1745 a596b957 2022-07-14 tracey *url = NULL;
1746 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1747 a596b957 2022-07-14 tracey return NULL;
1749 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1750 3b81530f 2022-11-22 op if (fd == -1) {
1751 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1752 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1753 3b81530f 2022-11-22 op dirpath, "cloneurl");
1755 a596b957 2022-07-14 tracey goto done;
1758 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1759 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1760 3b81530f 2022-11-22 op dirpath, "cloneurl");
1761 a596b957 2022-07-14 tracey goto done;
1764 3b81530f 2022-11-22 op len = sb.st_size;
1765 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1766 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1768 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1769 a596b957 2022-07-14 tracey if (*url == NULL) {
1770 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1771 a596b957 2022-07-14 tracey goto done;
1774 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1775 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1777 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1778 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1779 a596b957 2022-07-14 tracey return error;
1782 a596b957 2022-07-14 tracey const struct got_error *
1783 a596b957 2022-07-14 tracey gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1785 a596b957 2022-07-14 tracey struct tm tm;
1786 fced5a66 2022-07-20 naddy long long diff_time;
1787 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
1788 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
1789 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
1790 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
1791 a596b957 2022-07-14 tracey char *s;
1792 1abb18e1 2022-12-20 op char datebuf[64];
1795 a596b957 2022-07-14 tracey *repo_age = NULL;
1797 a596b957 2022-07-14 tracey switch (ref_tm) {
1798 a596b957 2022-07-14 tracey case TM_DIFF:
1799 a596b957 2022-07-14 tracey diff_time = time(NULL) - committer_time;
1800 a596b957 2022-07-14 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1801 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1802 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 365), years) == -1)
1803 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1804 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1805 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1806 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1807 a596b957 2022-07-14 tracey months) == -1)
1808 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1809 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1810 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1811 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1812 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1813 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1814 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1815 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24), days) == -1)
1816 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1817 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 2) {
1818 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1819 a596b957 2022-07-14 tracey (diff_time / 60 / 60), hours) == -1)
1820 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1821 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 2) {
1822 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1823 a596b957 2022-07-14 tracey minutes) == -1)
1824 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1825 a596b957 2022-07-14 tracey } else if (diff_time > 2) {
1826 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", diff_time,
1827 a596b957 2022-07-14 tracey seconds) == -1)
1828 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1829 a596b957 2022-07-14 tracey } else {
1830 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s", now) == -1)
1831 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1834 a596b957 2022-07-14 tracey case TM_LONG:
1835 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1836 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
1838 a596b957 2022-07-14 tracey s = asctime_r(&tm, datebuf);
1839 a596b957 2022-07-14 tracey if (s == NULL)
1840 a596b957 2022-07-14 tracey return got_error_from_errno("asctime_r");
1842 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1843 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1845 1abb18e1 2022-12-20 op case TM_RFC822:
1846 1abb18e1 2022-12-20 op if (gmtime_r(&committer_time, &tm) == NULL)
1847 1abb18e1 2022-12-20 op return got_error_from_errno("gmtime_r");
1849 1abb18e1 2022-12-20 op r = strftime(datebuf, sizeof(datebuf),
1850 1abb18e1 2022-12-20 op "%a, %d %b %Y %H:%M:%S GMT", &tm);
1852 1abb18e1 2022-12-20 op return got_error(GOT_ERR_NO_SPACE);
1854 1abb18e1 2022-12-20 op *repo_age = strdup(datebuf);
1855 1abb18e1 2022-12-20 op if (*repo_age == NULL)
1856 1abb18e1 2022-12-20 op return got_error_from_errno("asprintf");
1859 a596b957 2022-07-14 tracey return NULL;