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 5822e79e 2023-02-23 op #include <sha2.h>
35 a596b957 2022-07-14 tracey #include <stdio.h>
36 a596b957 2022-07-14 tracey #include <stdlib.h>
37 a596b957 2022-07-14 tracey #include <string.h>
38 a596b957 2022-07-14 tracey #include <unistd.h>
40 a596b957 2022-07-14 tracey #include "got_error.h"
41 a596b957 2022-07-14 tracey #include "got_object.h"
42 a596b957 2022-07-14 tracey #include "got_reference.h"
43 a596b957 2022-07-14 tracey #include "got_repository.h"
44 a596b957 2022-07-14 tracey #include "got_path.h"
45 a596b957 2022-07-14 tracey #include "got_cancel.h"
46 a596b957 2022-07-14 tracey #include "got_worktree.h"
47 a596b957 2022-07-14 tracey #include "got_diff.h"
48 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
49 a596b957 2022-07-14 tracey #include "got_blame.h"
50 a596b957 2022-07-14 tracey #include "got_privsep.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 df2d3cd2 2023-03-11 op static int gotweb_render_index(struct template *);
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 c8af7691 2023-06-22 op struct server *gotweb_get_server(const char *);
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 62eab86e 2023-09-13 op if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
111 6cdf29f9 2023-01-21 op if (location) {
112 62eab86e 2023-09-13 op if (tp_writes(c->tp, "Location: ") == -1 ||
113 6cdf29f9 2023-01-21 op gotweb_render_url(c, location) == -1 ||
114 62eab86e 2023-09-13 op tp_writes(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 62eab86e 2023-09-13 op if (tp_writes(c->tp, csp) == -1)
123 62eab86e 2023-09-13 op if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
126 62eab86e 2023-09-13 op return tp_writes(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 62eab86e 2023-09-13 op r = tp_writef(c->tp, "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 8f37175d 2023-03-11 op const struct got_error *error = NULL;
146 a596b957 2022-07-14 tracey struct server *srv = NULL;
147 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
148 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
149 f9b5f5fb 2023-04-01 op const char *rss_ctype = "application/rss+xml;charset=utf-8";
150 f9b5f5fb 2023-04-01 op const uint8_t *buf;
152 f9b5f5fb 2023-04-01 op int r, binary = 0;
154 a596b957 2022-07-14 tracey /* init the transport */
155 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
156 a596b957 2022-07-14 tracey if (error) {
157 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
160 a596b957 2022-07-14 tracey /* don't process any further if client disconnected */
161 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
163 a596b957 2022-07-14 tracey /* get the gotwebd server */
164 c8af7691 2023-06-22 op srv = gotweb_get_server(c->server_name);
165 a596b957 2022-07-14 tracey if (srv == NULL) {
166 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
167 a596b957 2022-07-14 tracey goto err;
169 a596b957 2022-07-14 tracey c->srv = srv;
170 a596b957 2022-07-14 tracey /* parse our querystring */
171 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
172 a596b957 2022-07-14 tracey if (error) {
173 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
174 a596b957 2022-07-14 tracey goto err;
176 a596b957 2022-07-14 tracey c->t->qs = qs;
177 a596b957 2022-07-14 tracey error = gotweb_parse_querystring(&qs, c->querystring);
178 a596b957 2022-07-14 tracey if (error) {
179 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
180 a596b957 2022-07-14 tracey goto err;
184 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
185 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
186 a596b957 2022-07-14 tracey * querystring.
189 298f95fb 2023-01-05 op if (qs->action == BLAME || qs->action == BLOB ||
190 298f95fb 2023-01-05 op qs->action == BLOBRAW || qs->action == DIFF) {
191 298f95fb 2023-01-05 op if (qs->commit == NULL) {
192 de2b82f3 2023-06-18 op error = got_error(GOT_ERR_BAD_QUERYSTRING);
197 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
198 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
199 a596b957 2022-07-14 tracey if (error)
201 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
202 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
203 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
204 a596b957 2022-07-14 tracey goto err;
207 f9b5f5fb 2023-04-01 op if (qs->action == BLOBRAW || qs->action == BLOB) {
208 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
209 a596b957 2022-07-14 tracey if (error)
212 8f37175d 2023-03-11 op error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
218 7607b8e0 2023-11-17 stsp switch (qs->action) {
220 f9b5f5fb 2023-04-01 op error = got_get_repo_commits(c, 1);
222 f9b5f5fb 2023-04-01 op log_warnx("%s: %s", __func__, error->msg);
225 f9b5f5fb 2023-04-01 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
227 f9b5f5fb 2023-04-01 op gotweb_render_page(c->tp, gotweb_render_blame);
230 f9b5f5fb 2023-04-01 op if (binary) {
231 f9b5f5fb 2023-04-01 op struct gotweb_url url = {
232 f9b5f5fb 2023-04-01 op .index_page = -1,
234 f9b5f5fb 2023-04-01 op .action = BLOBRAW,
235 f9b5f5fb 2023-04-01 op .path = qs->path,
236 f9b5f5fb 2023-04-01 op .commit = qs->commit,
237 f9b5f5fb 2023-04-01 op .folder = qs->folder,
238 f9b5f5fb 2023-04-01 op .file = qs->file,
241 f9b5f5fb 2023-04-01 op gotweb_reply(c, 302, NULL, &url);
245 f9b5f5fb 2023-04-01 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
247 f9b5f5fb 2023-04-01 op gotweb_render_page(c->tp, gotweb_render_blob);
249 f9b5f5fb 2023-04-01 op case BLOBRAW:
251 6cdf29f9 2023-01-21 op r = gotweb_reply_file(c, "application/octet-stream",
252 6cdf29f9 2023-01-21 op qs->file, NULL);
254 6cdf29f9 2023-01-21 op r = gotweb_reply(c, 200, "text/plain", NULL);
257 62eab86e 2023-09-13 op if (template_flush(c->tp) == -1)
261 df2d3cd2 2023-03-11 op error = got_object_blob_read_block(&len, c->t->blob);
264 76007998 2023-01-15 op if (len == 0)
266 df2d3cd2 2023-03-11 op buf = got_object_blob_get_read_buf(c->t->blob);
267 62eab86e 2023-09-13 op if (fcgi_write(c, buf, len) == -1)
271 a596b957 2022-07-14 tracey case BRIEFS:
272 13d9dc7e 2023-06-26 op error = got_get_repo_commits(c, srv->max_commits_display);
275 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
277 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_briefs);
279 a596b957 2022-07-14 tracey case COMMITS:
280 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
281 a596b957 2022-07-14 tracey if (error) {
282 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
283 a596b957 2022-07-14 tracey goto err;
285 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
287 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_commits);
289 a596b957 2022-07-14 tracey case DIFF:
290 587550a5 2023-01-10 op error = got_get_repo_commits(c, 1);
292 169b1631 2023-01-06 op log_warnx("%s: %s", __func__, error->msg);
295 18069c98 2023-05-16 op error = got_open_diff_for_output(&c->t->fp, c);
297 587550a5 2023-01-10 op log_warnx("%s: %s", __func__, error->msg);
300 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
302 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_diff);
304 a596b957 2022-07-14 tracey case INDEX:
305 df2d3cd2 2023-03-11 op c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
307 df2d3cd2 2023-03-11 op if (c->t->nrepos == -1) {
308 df2d3cd2 2023-03-11 op c->t->repos = NULL;
309 df2d3cd2 2023-03-11 op error = got_error_from_errno2("scandir",
310 df2d3cd2 2023-03-11 op srv->repos_path);
311 a596b957 2022-07-14 tracey goto err;
313 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
315 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_index);
318 f9b5f5fb 2023-04-01 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
321 f9b5f5fb 2023-04-01 op if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
324 f9b5f5fb 2023-04-01 op gotweb_render_rss(c->tp);
326 a596b957 2022-07-14 tracey case SUMMARY:
327 df2d3cd2 2023-03-11 op error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
328 69525b4e 2023-01-13 op got_ref_cmp_by_name, NULL);
329 a596b957 2022-07-14 tracey if (error) {
330 69525b4e 2023-01-13 op log_warnx("%s: got_ref_list: %s", __func__,
332 a596b957 2022-07-14 tracey goto err;
334 13d9dc7e 2023-06-26 op error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
337 69525b4e 2023-01-13 op qs->action = TAGS;
338 69525b4e 2023-01-13 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
340 69525b4e 2023-01-13 op log_warnx("%s: got_get_repo_tags: %s", __func__,
344 69525b4e 2023-01-13 op qs->action = SUMMARY;
345 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
347 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_summary);
349 a596b957 2022-07-14 tracey case TAG:
350 dc07f76c 2023-01-09 op error = got_get_repo_tags(c, 1);
351 a596b957 2022-07-14 tracey if (error) {
352 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
355 dc07f76c 2023-01-09 op if (c->t->tag_count == 0) {
356 dc07f76c 2023-01-09 op error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
357 dc07f76c 2023-01-09 op "bad commit id");
358 a596b957 2022-07-14 tracey goto err;
360 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
362 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tag);
364 a596b957 2022-07-14 tracey case TAGS:
365 d60961d2 2023-01-13 op error = got_get_repo_tags(c, srv->max_commits_display);
366 a596b957 2022-07-14 tracey if (error) {
367 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
368 a596b957 2022-07-14 tracey goto err;
370 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
372 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tags);
374 a596b957 2022-07-14 tracey case TREE:
375 43d421de 2023-01-05 op error = got_get_repo_commits(c, 1);
376 a596b957 2022-07-14 tracey if (error) {
377 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
378 a596b957 2022-07-14 tracey goto err;
380 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
382 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tree);
384 a596b957 2022-07-14 tracey case ERR:
386 8f37175d 2023-03-11 op error = got_error(GOT_ERR_BAD_QUERYSTRING);
390 8f37175d 2023-03-11 op c->t->error = error;
391 8f37175d 2023-03-11 op if (gotweb_reply(c, 400, "text/html", NULL) == -1)
393 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_error);
396 a596b957 2022-07-14 tracey struct server *
397 c8af7691 2023-06-22 op gotweb_get_server(const char *server_name)
399 c8af7691 2023-06-22 op struct server *srv;
401 95a4a5a1 2022-08-30 op /* check against the server name first */
402 4448825a 2023-06-16 op if (*server_name != '\0')
403 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
404 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
407 c8af7691 2023-06-22 op /* otherwise, use the first server */
408 c8af7691 2023-06-22 op return TAILQ_FIRST(&gotwebd_env->servers);
411 a596b957 2022-07-14 tracey const struct got_error *
412 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
414 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
416 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
417 a596b957 2022-07-14 tracey if (*t == NULL)
418 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "calloc");
420 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
421 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
422 df2d3cd2 2023-03-11 op TAILQ_INIT(&(*t)->refs);
424 df2d3cd2 2023-03-11 op (*t)->fd = -1;
426 a596b957 2022-07-14 tracey return error;
429 a596b957 2022-07-14 tracey static const struct got_error *
430 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
432 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
434 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
435 a596b957 2022-07-14 tracey if (*qs == NULL)
436 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "calloc");
438 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
439 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
442 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "strdup");
445 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
446 6c37ad7b 2022-09-01 op (*qs)->commit = NULL;
447 6c37ad7b 2022-09-01 op (*qs)->file = NULL;
448 6c37ad7b 2022-09-01 op (*qs)->folder = NULL;
449 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
450 a596b957 2022-07-14 tracey (*qs)->path = NULL;
452 a596b957 2022-07-14 tracey return error;
455 a596b957 2022-07-14 tracey static const struct got_error *
456 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
458 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
459 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
460 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
462 a596b957 2022-07-14 tracey if (qst == NULL)
463 a596b957 2022-07-14 tracey return error;
465 a596b957 2022-07-14 tracey tok1 = strdup(qst);
466 a596b957 2022-07-14 tracey if (tok1 == NULL)
467 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "strdup");
469 a596b957 2022-07-14 tracey tok1_pair = tok1;
470 a596b957 2022-07-14 tracey tok1_end = tok1;
472 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
473 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
475 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
476 a596b957 2022-07-14 tracey if (tok2 == NULL) {
477 a596b957 2022-07-14 tracey free(tok1);
478 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "strdup");
481 a596b957 2022-07-14 tracey tok2_pair = tok2;
482 a596b957 2022-07-14 tracey tok2_end = tok2;
484 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
485 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
486 a596b957 2022-07-14 tracey if (tok2_end) {
487 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
488 a596b957 2022-07-14 tracey tok2_end);
489 a596b957 2022-07-14 tracey if (error)
490 a596b957 2022-07-14 tracey goto err;
492 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
494 a596b957 2022-07-14 tracey free(tok2);
495 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
497 a596b957 2022-07-14 tracey free(tok1);
498 a596b957 2022-07-14 tracey return error;
500 a596b957 2022-07-14 tracey free(tok2);
501 a596b957 2022-07-14 tracey free(tok1);
502 a596b957 2022-07-14 tracey return error;
506 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
508 a596b957 2022-07-14 tracey static const struct got_error *
509 58381f70 2022-09-03 op gotweb_urldecode(char *url)
513 58381f70 2022-09-03 op unsigned long x;
515 58381f70 2022-09-03 op hex[2] = '\0';
518 58381f70 2022-09-03 op while (*p != '\0') {
519 58381f70 2022-09-03 op switch (*p) {
521 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
522 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
523 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
524 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
525 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
527 58381f70 2022-09-03 op hex[0] = p[1];
528 58381f70 2022-09-03 op hex[1] = p[2];
531 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
532 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
534 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
535 58381f70 2022-09-03 op *q = (char)x;
550 58381f70 2022-09-03 op static const struct got_error *
551 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
553 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
554 a596b957 2022-07-14 tracey const char *errstr;
555 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
557 58381f70 2022-09-03 op error = gotweb_urldecode(value);
559 58381f70 2022-09-03 op return error;
561 03e70dd4 2023-11-16 op for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
562 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
563 a596b957 2022-07-14 tracey continue;
565 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
566 a596b957 2022-07-14 tracey case ACTION:
567 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
568 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
569 a596b957 2022-07-14 tracey continue;
570 a596b957 2022-07-14 tracey else if (strcmp(value,
571 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
572 a596b957 2022-07-14 tracey (*qs)->action =
573 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
574 a596b957 2022-07-14 tracey goto qa_found;
577 a596b957 2022-07-14 tracey (*qs)->action = ERR;
578 a596b957 2022-07-14 tracey qa_found:
580 a596b957 2022-07-14 tracey case COMMIT:
581 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
582 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
583 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
585 a596b957 2022-07-14 tracey goto done;
588 a596b957 2022-07-14 tracey case RFILE:
589 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
590 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
591 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
593 a596b957 2022-07-14 tracey goto done;
596 a596b957 2022-07-14 tracey case FOLDER:
597 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
598 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
599 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
601 a596b957 2022-07-14 tracey goto done;
604 a596b957 2022-07-14 tracey case HEADREF:
605 f8faf9f1 2022-09-01 op free((*qs)->headref);
606 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
607 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
608 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
610 a596b957 2022-07-14 tracey goto done;
613 a596b957 2022-07-14 tracey case INDEX_PAGE:
614 4448825a 2023-06-16 op if (*value == '\0')
616 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
617 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
618 a596b957 2022-07-14 tracey if (errstr) {
619 50f6148a 2023-05-29 op error = got_error_from_errno3(__func__,
620 50f6148a 2023-05-29 op "strtonum", errstr);
621 a596b957 2022-07-14 tracey goto done;
623 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
624 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
626 a596b957 2022-07-14 tracey case PATH:
627 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
628 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
629 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
631 a596b957 2022-07-14 tracey goto done;
634 a596b957 2022-07-14 tracey case PAGE:
635 4448825a 2023-06-16 op if (*value == '\0')
637 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
638 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
639 a596b957 2022-07-14 tracey if (errstr) {
640 50f6148a 2023-05-29 op error = got_error_from_errno3(__func__,
641 50f6148a 2023-05-29 op "strtonum", errstr);
642 a596b957 2022-07-14 tracey goto done;
644 03f6a843 2022-12-17 op if ((*qs)->page < 0)
645 a596b957 2022-07-14 tracey (*qs)->page = 0;
649 03e70dd4 2023-11-16 op /* entry found */
653 a596b957 2022-07-14 tracey return error;
657 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
659 a596b957 2022-07-14 tracey if (rt != NULL) {
660 a596b957 2022-07-14 tracey free(rt->commit_id);
661 625e5896 2022-09-01 op free(rt->tag_name);
662 625e5896 2022-09-01 op free(rt->tag_commit);
663 625e5896 2022-09-01 op free(rt->commit_msg);
664 a596b957 2022-07-14 tracey free(rt->tagger);
666 a596b957 2022-07-14 tracey free(rt);
670 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
672 a596b957 2022-07-14 tracey if (rc != NULL) {
673 a596b957 2022-07-14 tracey free(rc->path);
674 a596b957 2022-07-14 tracey free(rc->refs_str);
675 a596b957 2022-07-14 tracey free(rc->commit_id);
676 a596b957 2022-07-14 tracey free(rc->parent_id);
677 a596b957 2022-07-14 tracey free(rc->tree_id);
678 a596b957 2022-07-14 tracey free(rc->author);
679 a596b957 2022-07-14 tracey free(rc->committer);
680 a596b957 2022-07-14 tracey free(rc->commit_msg);
682 a596b957 2022-07-14 tracey free(rc);
685 a596b957 2022-07-14 tracey static void
686 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
688 a596b957 2022-07-14 tracey if (qs != NULL) {
689 a596b957 2022-07-14 tracey free(qs->commit);
690 a596b957 2022-07-14 tracey free(qs->file);
691 a596b957 2022-07-14 tracey free(qs->folder);
692 a596b957 2022-07-14 tracey free(qs->headref);
693 a596b957 2022-07-14 tracey free(qs->path);
695 a596b957 2022-07-14 tracey free(qs);
698 a596b957 2022-07-14 tracey static void
699 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
701 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
702 a596b957 2022-07-14 tracey free(repo_dir->name);
703 a596b957 2022-07-14 tracey free(repo_dir->owner);
704 a596b957 2022-07-14 tracey free(repo_dir->description);
705 a596b957 2022-07-14 tracey free(repo_dir->url);
706 a596b957 2022-07-14 tracey free(repo_dir->path);
708 a596b957 2022-07-14 tracey free(repo_dir);
712 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
714 df2d3cd2 2023-03-11 op const struct got_error *err;
715 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
716 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
719 df2d3cd2 2023-03-11 op got_ref_list_free(&t->refs);
720 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
721 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
722 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
724 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
725 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
726 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
728 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
729 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
730 e3662697 2023-02-03 op free(t->more_id);
731 341fa7ca 2022-09-01 op free(t->next_id);
732 341fa7ca 2022-09-01 op free(t->prev_id);
734 df2d3cd2 2023-03-11 op got_object_blob_close(t->blob);
736 24a4d801 2023-05-16 op err = got_gotweb_closefile(t->fp);
738 24a4d801 2023-05-16 op log_warnx("%s: got_gotweb_closefile failure: %s",
739 df2d3cd2 2023-03-11 op __func__, err->msg);
741 276bccc4 2023-05-16 op if (t->fd != -1 && close(t->fd) == -1)
742 276bccc4 2023-05-16 op log_warn("%s: close", __func__);
743 df2d3cd2 2023-03-11 op if (t->repos) {
744 df2d3cd2 2023-03-11 op for (i = 0; i < t->nrepos; ++i)
745 df2d3cd2 2023-03-11 op free(t->repos[i]);
746 df2d3cd2 2023-03-11 op free(t->repos);
748 1632f50a 2023-11-17 stsp if (t->repo)
749 1632f50a 2023-11-17 stsp got_repo_close(t->repo);
754 b4c0bd72 2022-12-17 op gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
755 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
757 a596b957 2022-07-14 tracey struct transport *t = c->t;
758 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
759 a596b957 2022-07-14 tracey struct server *srv = c->srv;
761 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
763 a596b957 2022-07-14 tracey switch(qs->action) {
764 a596b957 2022-07-14 tracey case INDEX:
765 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
766 b4c0bd72 2022-12-17 op *have_prev = 1;
767 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
768 8d02314f 2022-09-07 op .action = -1,
769 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
773 b4c0bd72 2022-12-17 op if (t->next_disp == srv->max_repos_display &&
774 b4c0bd72 2022-12-17 op t->repos_total != (qs->index_page + 1) *
775 b4c0bd72 2022-12-17 op srv->max_repos_display) {
776 b4c0bd72 2022-12-17 op *have_next = 1;
777 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
778 b4c0bd72 2022-12-17 op .action = -1,
779 b4c0bd72 2022-12-17 op .index_page = qs->index_page + 1,
784 a596b957 2022-07-14 tracey case TAGS:
785 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
786 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
787 b4c0bd72 2022-12-17 op *have_prev = 1;
788 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
789 b4c0bd72 2022-12-17 op .action = TAGS,
790 b4c0bd72 2022-12-17 op .index_page = -1,
791 b4c0bd72 2022-12-17 op .page = qs->page - 1,
792 b4c0bd72 2022-12-17 op .path = qs->path,
793 b4c0bd72 2022-12-17 op .commit = t->prev_id,
794 b4c0bd72 2022-12-17 op .headref = qs->headref,
797 a596b957 2022-07-14 tracey if (t->next_id) {
798 b4c0bd72 2022-12-17 op *have_next = 1;
799 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
800 8d02314f 2022-09-07 op .action = TAGS,
801 8d02314f 2022-09-07 op .index_page = -1,
802 8d02314f 2022-09-07 op .page = qs->page + 1,
803 8d02314f 2022-09-07 op .path = qs->path,
804 8d02314f 2022-09-07 op .commit = t->next_id,
805 8d02314f 2022-09-07 op .headref = qs->headref,
813 df2d3cd2 2023-03-11 op gotweb_render_index(struct template *tp)
815 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
816 df2d3cd2 2023-03-11 op struct request *c = tp->tp_arg;
817 a596b957 2022-07-14 tracey struct server *srv = c->srv;
818 a596b957 2022-07-14 tracey struct transport *t = c->t;
819 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
820 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
821 df2d3cd2 2023-03-11 op struct dirent **sd_dent = t->repos;
822 df2d3cd2 2023-03-11 op unsigned int d_i, d_disp = 0;
823 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
826 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
829 df2d3cd2 2023-03-11 op for (d_i = 0; d_i < t->nrepos; d_i++) {
830 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
833 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
834 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
839 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
840 525dfdf4 2022-11-22 op sd_dent[d_i]);
843 525dfdf4 2022-11-22 op if (type != DT_DIR) {
845 a596b957 2022-07-14 tracey continue;
848 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
849 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
850 a596b957 2022-07-14 tracey t->prev_disp++;
851 a596b957 2022-07-14 tracey continue;
854 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
855 a596b957 2022-07-14 tracey if (error)
858 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
859 ba55351b 2023-04-28 op if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
860 df2d3cd2 2023-03-11 op if (error->code != GOT_ERR_NOT_GIT_REPO)
861 df2d3cd2 2023-03-11 op log_warnx("%s: %s: %s", __func__,
862 df2d3cd2 2023-03-11 op sd_dent[d_i]->d_name, error->msg);
863 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
864 a596b957 2022-07-14 tracey repo_dir = NULL;
866 a596b957 2022-07-14 tracey continue;
869 a596b957 2022-07-14 tracey d_disp++;
870 a596b957 2022-07-14 tracey t->prev_disp++;
872 df2d3cd2 2023-03-11 op r = gotweb_render_repo_fragment(c->tp, repo_dir);
873 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
874 1632f50a 2023-11-17 stsp repo_dir = NULL;
875 1632f50a 2023-11-17 stsp got_repo_close(t->repo);
876 1632f50a 2023-11-17 stsp t->repo = NULL;
880 a596b957 2022-07-14 tracey t->next_disp++;
881 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
884 df2d3cd2 2023-03-11 op t->repos_total = t->nrepos - d_skipped;
886 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
888 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
890 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
891 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
894 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
900 8d02314f 2022-09-07 op static inline int
901 8d02314f 2022-09-07 op should_urlencode(int c)
903 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
907 8d02314f 2022-09-07 op /* gen-delim */
915 8d02314f 2022-09-07 op /* sub-delims */
927 4a7f5bae 2023-01-05 op /* needed because the URLs are embedded into the HTML */
935 8d02314f 2022-09-07 op static char *
936 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
938 8d02314f 2022-09-07 op const char *s;
939 8d02314f 2022-09-07 op char *escaped;
940 8d02314f 2022-09-07 op size_t i, len;
944 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
946 8d02314f 2022-09-07 op if (should_urlencode(*s))
950 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
951 8d02314f 2022-09-07 op if (escaped == NULL)
955 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
956 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
957 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
958 8d02314f 2022-09-07 op b = (*s & 0x0F);
960 8d02314f 2022-09-07 op escaped[i++] = '%';
961 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
962 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
964 8d02314f 2022-09-07 op escaped[i++] = *s;
967 8d02314f 2022-09-07 op return escaped;
971 ed619ca0 2022-12-14 op gotweb_action_name(int action)
973 8d02314f 2022-09-07 op switch (action) {
975 8d02314f 2022-09-07 op return "blame";
977 8d02314f 2022-09-07 op return "blob";
978 298f95fb 2023-01-05 op case BLOBRAW:
979 298f95fb 2023-01-05 op return "blobraw";
981 8d02314f 2022-09-07 op return "briefs";
982 8d02314f 2022-09-07 op case COMMITS:
983 8d02314f 2022-09-07 op return "commits";
985 8d02314f 2022-09-07 op return "diff";
987 8d02314f 2022-09-07 op return "err";
989 8d02314f 2022-09-07 op return "index";
990 8d02314f 2022-09-07 op case SUMMARY:
991 8d02314f 2022-09-07 op return "summary";
993 8d02314f 2022-09-07 op return "tag";
995 8d02314f 2022-09-07 op return "tags";
997 8d02314f 2022-09-07 op return "tree";
999 1abb18e1 2022-12-20 op return "rss";
1001 8d02314f 2022-09-07 op return NULL;
1006 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1008 8d02314f 2022-09-07 op const char *sep = "?", *action;
1012 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1013 8d02314f 2022-09-07 op if (action != NULL) {
1014 62eab86e 2023-09-13 op if (tp_writef(c->tp, "?action=%s", action) == -1)
1019 8d02314f 2022-09-07 op if (url->commit) {
1020 62eab86e 2023-09-13 op if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1025 8d02314f 2022-09-07 op if (url->previd) {
1026 62eab86e 2023-09-13 op if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
1031 8d02314f 2022-09-07 op if (url->prevset) {
1032 62eab86e 2023-09-13 op if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1037 8d02314f 2022-09-07 op if (url->file) {
1038 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1039 8d02314f 2022-09-07 op if (tmp == NULL)
1041 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1043 8d02314f 2022-09-07 op if (r == -1)
1048 8d02314f 2022-09-07 op if (url->folder) {
1049 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1050 8d02314f 2022-09-07 op if (tmp == NULL)
1052 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1054 8d02314f 2022-09-07 op if (r == -1)
1059 8d02314f 2022-09-07 op if (url->headref) {
1060 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1061 8d02314f 2022-09-07 op if (tmp == NULL)
1063 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1065 8d02314f 2022-09-07 op if (r == -1)
1070 8d02314f 2022-09-07 op if (url->index_page != -1) {
1071 62eab86e 2023-09-13 op if (tp_writef(c->tp, "%sindex_page=%d", sep,
1072 8d02314f 2022-09-07 op url->index_page) == -1)
1077 8d02314f 2022-09-07 op if (url->path) {
1078 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1079 8d02314f 2022-09-07 op if (tmp == NULL)
1081 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1083 8d02314f 2022-09-07 op if (r == -1)
1088 8d02314f 2022-09-07 op if (url->page != -1) {
1089 62eab86e 2023-09-13 op if (tp_writef(c->tp, "%spage=%d", sep, url->page) == -1)
1098 1abb18e1 2022-12-20 op gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1100 1abb18e1 2022-12-20 op struct template *tp = c->tp;
1101 1abb18e1 2022-12-20 op const char *proto = c->https ? "https" : "http";
1103 62eab86e 2023-09-13 op if (tp_writes(tp, proto) == -1 ||
1104 62eab86e 2023-09-13 op tp_writes(tp, "://") == -1 ||
1105 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->server_name) == -1 ||
1106 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->document_uri) == -1)
1109 1abb18e1 2022-12-20 op return gotweb_render_url(c, url);
1112 a596b957 2022-07-14 tracey static const struct got_error *
1113 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1115 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1116 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1117 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1118 a596b957 2022-07-14 tracey struct transport *t = c->t;
1119 a596b957 2022-07-14 tracey DIR *dt;
1120 a596b957 2022-07-14 tracey char *dir_test;
1122 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1123 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1124 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1126 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1127 a596b957 2022-07-14 tracey if (dt == NULL) {
1128 a596b957 2022-07-14 tracey free(dir_test);
1129 260fd73e 2023-12-01 op if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1130 260fd73e 2023-12-01 op repo_dir->name) == -1)
1131 260fd73e 2023-12-01 op return got_error_from_errno("asprintf");
1132 260fd73e 2023-12-01 op dt = opendir(dir_test);
1133 260fd73e 2023-12-01 op if (dt == NULL) {
1134 260fd73e 2023-12-01 op free(dir_test);
1135 260fd73e 2023-12-01 op return got_error_path(repo_dir->name,
1136 260fd73e 2023-12-01 op GOT_ERR_NOT_GIT_REPO);
1140 260fd73e 2023-12-01 op repo_dir->path = dir_test;
1141 260fd73e 2023-12-01 op dir_test = NULL;
1143 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1144 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1145 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1146 d5996b9e 2022-10-31 landry goto err;
1149 1632f50a 2023-11-17 stsp error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1150 1632f50a 2023-11-17 stsp if (error)
1152 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1153 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1154 a596b957 2022-07-14 tracey if (error)
1155 a596b957 2022-07-14 tracey goto err;
1156 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1157 a596b957 2022-07-14 tracey if (error)
1158 a596b957 2022-07-14 tracey goto err;
1159 417c8923 2023-08-11 op if (srv->show_repo_age) {
1160 417c8923 2023-08-11 op error = got_get_repo_age(&repo_dir->age, c, NULL);
1164 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1167 a596b957 2022-07-14 tracey free(dir_test);
1168 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1169 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1170 1632f50a 2023-11-17 stsp if (error && t->repo) {
1171 1632f50a 2023-11-17 stsp got_repo_close(t->repo);
1172 1632f50a 2023-11-17 stsp t->repo = NULL;
1174 a596b957 2022-07-14 tracey return error;
1177 a596b957 2022-07-14 tracey static const struct got_error *
1178 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1180 a596b957 2022-07-14 tracey const struct got_error *error;
1182 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
1183 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
1184 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1186 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1187 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1188 a596b957 2022-07-14 tracey free(*repo_dir);
1189 a596b957 2022-07-14 tracey *repo_dir = NULL;
1190 a596b957 2022-07-14 tracey return error;
1192 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
1193 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
1194 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
1195 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
1197 a596b957 2022-07-14 tracey return NULL;
1200 a596b957 2022-07-14 tracey static const struct got_error *
1201 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1202 3b81530f 2022-11-22 op const char *dirpath, int dir)
1204 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1205 3b81530f 2022-11-22 op struct stat sb;
1206 3b81530f 2022-11-22 op int fd = -1;
1209 a596b957 2022-07-14 tracey *description = NULL;
1210 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1211 a596b957 2022-07-14 tracey return NULL;
1213 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1214 3b81530f 2022-11-22 op if (fd == -1) {
1215 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1216 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1217 3b81530f 2022-11-22 op dirpath, "description");
1219 a596b957 2022-07-14 tracey goto done;
1222 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1223 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1224 3b81530f 2022-11-22 op dirpath, "description");
1225 a596b957 2022-07-14 tracey goto done;
1228 3b81530f 2022-11-22 op len = sb.st_size;
1229 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1230 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1232 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1233 a596b957 2022-07-14 tracey if (*description == NULL) {
1234 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1235 a596b957 2022-07-14 tracey goto done;
1238 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1239 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1241 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1242 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1243 a596b957 2022-07-14 tracey return error;
1246 a596b957 2022-07-14 tracey static const struct got_error *
1247 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1250 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1251 3b81530f 2022-11-22 op struct stat sb;
1252 3b81530f 2022-11-22 op int fd = -1;
1255 a596b957 2022-07-14 tracey *url = NULL;
1256 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1257 a596b957 2022-07-14 tracey return NULL;
1259 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1260 3b81530f 2022-11-22 op if (fd == -1) {
1261 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1262 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1263 3b81530f 2022-11-22 op dirpath, "cloneurl");
1265 a596b957 2022-07-14 tracey goto done;
1268 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1269 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1270 3b81530f 2022-11-22 op dirpath, "cloneurl");
1271 a596b957 2022-07-14 tracey goto done;
1274 3b81530f 2022-11-22 op len = sb.st_size;
1275 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1276 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1278 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1279 a596b957 2022-07-14 tracey if (*url == NULL) {
1280 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1281 a596b957 2022-07-14 tracey goto done;
1284 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1285 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1287 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1288 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1289 a596b957 2022-07-14 tracey return error;
1293 7781b991 2023-10-05 op gotweb_render_age(struct template *tp, time_t committer_time)
1295 cb93ab40 2023-01-22 op struct request *c = tp->tp_arg;
1296 fced5a66 2022-07-20 naddy long long diff_time;
1297 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
1298 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
1299 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
1300 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
1302 7781b991 2023-10-05 op diff_time = time(NULL) - committer_time;
1303 7781b991 2023-10-05 op if (diff_time > 60 * 60 * 24 * 365 * 2) {
1304 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1305 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24 / 365), years) == -1)
1307 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1308 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1309 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24 / (365 / 12)),
1310 7781b991 2023-10-05 op months) == -1)
1312 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1313 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1314 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1316 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 24 * 2) {
1317 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1318 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24), days) == -1)
1320 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 2) {
1321 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1322 7781b991 2023-10-05 op (diff_time / 60 / 60), hours) == -1)
1324 7781b991 2023-10-05 op } else if (diff_time > 60 * 2) {
1325 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1326 7781b991 2023-10-05 op minutes) == -1)
1328 7781b991 2023-10-05 op } else if (diff_time > 2) {
1329 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s", diff_time,
1330 7781b991 2023-10-05 op seconds) == -1)
1333 7781b991 2023-10-05 op if (tp_writes(tp, now) == -1)