Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 2ac684a4 2022-09-03 thomas * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 8a35f56c 2022-07-16 thomas *
8 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
9 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
10 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
11 8a35f56c 2022-07-16 thomas *
12 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 8a35f56c 2022-07-16 thomas */
20 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
21 8a35f56c 2022-07-16 thomas
22 8a35f56c 2022-07-16 thomas #include <net/if.h>
23 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
24 3e12c168 2022-07-16 thomas #include <sys/queue.h>
25 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
26 8a35f56c 2022-07-16 thomas #include <sys/types.h>
27 8a35f56c 2022-07-16 thomas
28 2ac684a4 2022-09-03 thomas #include <ctype.h>
29 8a35f56c 2022-07-16 thomas #include <dirent.h>
30 8a35f56c 2022-07-16 thomas #include <errno.h>
31 8a35f56c 2022-07-16 thomas #include <event.h>
32 4606e6d4 2022-11-23 thomas #include <fcntl.h>
33 4606e6d4 2022-11-23 thomas #include <imsg.h>
34 8a35f56c 2022-07-16 thomas #include <stdio.h>
35 8a35f56c 2022-07-16 thomas #include <stdlib.h>
36 8a35f56c 2022-07-16 thomas #include <string.h>
37 8a35f56c 2022-07-16 thomas #include <unistd.h>
38 8a35f56c 2022-07-16 thomas
39 8a35f56c 2022-07-16 thomas #include "got_error.h"
40 8a35f56c 2022-07-16 thomas #include "got_object.h"
41 8a35f56c 2022-07-16 thomas #include "got_reference.h"
42 8a35f56c 2022-07-16 thomas #include "got_repository.h"
43 8a35f56c 2022-07-16 thomas #include "got_path.h"
44 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
45 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
46 8a35f56c 2022-07-16 thomas #include "got_diff.h"
47 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
48 8a35f56c 2022-07-16 thomas #include "got_blame.h"
49 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
50 cb11302c 2022-12-30 thomas
51 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
52 d6795e9f 2022-12-30 thomas #include "tmpl.h"
53 ff36aeea 2022-07-16 thomas
54 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
55 8a35f56c 2022-07-16 thomas { "action", ACTION },
56 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
57 8a35f56c 2022-07-16 thomas { "file", RFILE },
58 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
59 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
60 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
61 8a35f56c 2022-07-16 thomas { "path", PATH },
62 8a35f56c 2022-07-16 thomas { "page", PAGE },
63 8a35f56c 2022-07-16 thomas };
64 8a35f56c 2022-07-16 thomas
65 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
66 8a35f56c 2022-07-16 thomas { "blame", BLAME },
67 8a35f56c 2022-07-16 thomas { "blob", BLOB },
68 b82440e1 2023-01-06 thomas { "blobraw", BLOBRAW },
69 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
70 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
71 8a35f56c 2022-07-16 thomas { "diff", DIFF },
72 8a35f56c 2022-07-16 thomas { "error", ERR },
73 8a35f56c 2022-07-16 thomas { "index", INDEX },
74 feaddee6 2023-12-03 thomas { "patch", PATCH },
75 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
76 8a35f56c 2022-07-16 thomas { "tag", TAG },
77 8a35f56c 2022-07-16 thomas { "tags", TAGS },
78 8a35f56c 2022-07-16 thomas { "tree", TREE },
79 d6795e9f 2022-12-30 thomas { "rss", RSS },
80 8a35f56c 2022-07-16 thomas };
81 8a35f56c 2022-07-16 thomas
82 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
83 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 8a35f56c 2022-07-16 thomas char *);
85 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 8a35f56c 2022-07-16 thomas char *, char *);
87 161663e7 2023-03-11 thomas static int gotweb_render_index(struct template *);
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 8a35f56c 2022-07-16 thomas const char *);
90 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
91 8a35f56c 2022-07-16 thomas struct repo_dir *);
92 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
93 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
94 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 4606e6d4 2022-11-23 thomas const char *, int);
96 e7e5fa49 2022-12-30 thomas
97 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
98 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
99 8a35f56c 2022-07-16 thomas
100 b95d1cf6 2023-06-25 thomas struct server *gotweb_get_server(const char *);
101 8a35f56c 2022-07-16 thomas
102 50237485 2023-01-23 thomas static int
103 50237485 2023-01-23 thomas gotweb_reply(struct request *c, int status, const char *ctype,
104 50237485 2023-01-23 thomas struct gotweb_url *location)
105 50237485 2023-01-23 thomas {
106 50237485 2023-01-23 thomas const char *csp;
107 50237485 2023-01-23 thomas
108 d8bf4f25 2023-09-14 thomas if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
109 50237485 2023-01-23 thomas return -1;
110 50237485 2023-01-23 thomas
111 50237485 2023-01-23 thomas if (location) {
112 d8bf4f25 2023-09-14 thomas if (tp_writes(c->tp, "Location: ") == -1 ||
113 50237485 2023-01-23 thomas gotweb_render_url(c, location) == -1 ||
114 d8bf4f25 2023-09-14 thomas tp_writes(c->tp, "\r\n") == -1)
115 50237485 2023-01-23 thomas return -1;
116 50237485 2023-01-23 thomas }
117 50237485 2023-01-23 thomas
118 50237485 2023-01-23 thomas csp = "Content-Security-Policy: default-src 'self'; "
119 50237485 2023-01-23 thomas "script-src 'none'; object-src 'none';\r\n";
120 d8bf4f25 2023-09-14 thomas if (tp_writes(c->tp, csp) == -1)
121 50237485 2023-01-23 thomas return -1;
122 50237485 2023-01-23 thomas
123 d8bf4f25 2023-09-14 thomas if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
124 50237485 2023-01-23 thomas return -1;
125 50237485 2023-01-23 thomas
126 d8bf4f25 2023-09-14 thomas return tp_writes(c->tp, "\r\n");
127 50237485 2023-01-23 thomas }
128 50237485 2023-01-23 thomas
129 50237485 2023-01-23 thomas static int
130 50237485 2023-01-23 thomas gotweb_reply_file(struct request *c, const char *ctype, const char *file,
131 50237485 2023-01-23 thomas const char *suffix)
132 50237485 2023-01-23 thomas {
133 50237485 2023-01-23 thomas int r;
134 50237485 2023-01-23 thomas
135 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "Content-Disposition: attachment; "
136 50237485 2023-01-23 thomas "filename=%s%s\r\n", file, suffix ? suffix : "");
137 50237485 2023-01-23 thomas if (r == -1)
138 50237485 2023-01-23 thomas return -1;
139 50237485 2023-01-23 thomas return gotweb_reply(c, 200, ctype, NULL);
140 50237485 2023-01-23 thomas }
141 50237485 2023-01-23 thomas
142 8a35f56c 2022-07-16 thomas void
143 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
144 8a35f56c 2022-07-16 thomas {
145 164b5ddc 2023-03-11 thomas const struct got_error *error = NULL;
146 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
147 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
148 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
149 44f2135a 2023-04-01 thomas const char *rss_ctype = "application/rss+xml;charset=utf-8";
150 44f2135a 2023-04-01 thomas const uint8_t *buf;
151 44f2135a 2023-04-01 thomas size_t len;
152 44f2135a 2023-04-01 thomas int r, binary = 0;
153 18e466eb 2023-01-14 thomas
154 8a35f56c 2022-07-16 thomas /* init the transport */
155 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
156 8a35f56c 2022-07-16 thomas if (error) {
157 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
158 46aeda9a 2022-08-27 thomas return;
159 8a35f56c 2022-07-16 thomas }
160 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
161 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
162 8a35f56c 2022-07-16 thomas return;
163 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
164 b95d1cf6 2023-06-25 thomas srv = gotweb_get_server(c->server_name);
165 8a35f56c 2022-07-16 thomas if (srv == NULL) {
166 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
167 8a35f56c 2022-07-16 thomas goto err;
168 8a35f56c 2022-07-16 thomas }
169 8a35f56c 2022-07-16 thomas c->srv = srv;
170 8a35f56c 2022-07-16 thomas /* parse our querystring */
171 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
172 8a35f56c 2022-07-16 thomas if (error) {
173 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
174 8a35f56c 2022-07-16 thomas goto err;
175 8a35f56c 2022-07-16 thomas }
176 8a35f56c 2022-07-16 thomas c->t->qs = qs;
177 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
178 8a35f56c 2022-07-16 thomas if (error) {
179 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
180 8a35f56c 2022-07-16 thomas goto err;
181 8a35f56c 2022-07-16 thomas }
182 8a35f56c 2022-07-16 thomas
183 8a35f56c 2022-07-16 thomas /*
184 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
185 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
186 8a35f56c 2022-07-16 thomas * querystring.
187 8a35f56c 2022-07-16 thomas */
188 8a35f56c 2022-07-16 thomas
189 b82440e1 2023-01-06 thomas if (qs->action == BLAME || qs->action == BLOB ||
190 feaddee6 2023-12-03 thomas qs->action == BLOBRAW || qs->action == DIFF ||
191 feaddee6 2023-12-03 thomas qs->action == PATCH) {
192 b82440e1 2023-01-06 thomas if (qs->commit == NULL) {
193 b2b363b2 2023-06-22 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
194 164b5ddc 2023-03-11 thomas goto err;
195 b82440e1 2023-01-06 thomas }
196 8a35f56c 2022-07-16 thomas }
197 8a35f56c 2022-07-16 thomas
198 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
199 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
200 8a35f56c 2022-07-16 thomas if (error)
201 164b5ddc 2023-03-11 thomas goto err;
202 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
203 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
204 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
205 8a35f56c 2022-07-16 thomas goto err;
206 8a35f56c 2022-07-16 thomas }
207 8a35f56c 2022-07-16 thomas
208 44f2135a 2023-04-01 thomas if (qs->action == BLOBRAW || qs->action == BLOB) {
209 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
210 8a35f56c 2022-07-16 thomas if (error)
211 164b5ddc 2023-03-11 thomas goto err;
212 ea1b5cf5 2023-01-15 thomas
213 164b5ddc 2023-03-11 thomas error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
214 34875d49 2023-12-08 thomas &binary, c, qs->folder, qs->file, qs->commit);
215 164b5ddc 2023-03-11 thomas if (error)
216 164b5ddc 2023-03-11 thomas goto err;
217 44f2135a 2023-04-01 thomas }
218 44f2135a 2023-04-01 thomas
219 aea518b5 2023-11-22 thomas switch (qs->action) {
220 44f2135a 2023-04-01 thomas case BLAME:
221 44f2135a 2023-04-01 thomas error = got_get_repo_commits(c, 1);
222 44f2135a 2023-04-01 thomas if (error) {
223 44f2135a 2023-04-01 thomas log_warnx("%s: %s", __func__, error->msg);
224 44f2135a 2023-04-01 thomas goto err;
225 44f2135a 2023-04-01 thomas }
226 44f2135a 2023-04-01 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
227 44f2135a 2023-04-01 thomas return;
228 44f2135a 2023-04-01 thomas gotweb_render_page(c->tp, gotweb_render_blame);
229 44f2135a 2023-04-01 thomas return;
230 44f2135a 2023-04-01 thomas case BLOB:
231 44f2135a 2023-04-01 thomas if (binary) {
232 44f2135a 2023-04-01 thomas struct gotweb_url url = {
233 44f2135a 2023-04-01 thomas .index_page = -1,
234 44f2135a 2023-04-01 thomas .page = -1,
235 44f2135a 2023-04-01 thomas .action = BLOBRAW,
236 44f2135a 2023-04-01 thomas .path = qs->path,
237 44f2135a 2023-04-01 thomas .commit = qs->commit,
238 44f2135a 2023-04-01 thomas .folder = qs->folder,
239 44f2135a 2023-04-01 thomas .file = qs->file,
240 44f2135a 2023-04-01 thomas };
241 ea1b5cf5 2023-01-15 thomas
242 44f2135a 2023-04-01 thomas gotweb_reply(c, 302, NULL, &url);
243 44f2135a 2023-04-01 thomas return;
244 44f2135a 2023-04-01 thomas }
245 44f2135a 2023-04-01 thomas
246 44f2135a 2023-04-01 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
247 44f2135a 2023-04-01 thomas return;
248 44f2135a 2023-04-01 thomas gotweb_render_page(c->tp, gotweb_render_blob);
249 44f2135a 2023-04-01 thomas return;
250 44f2135a 2023-04-01 thomas case BLOBRAW:
251 ea1b5cf5 2023-01-15 thomas if (binary)
252 50237485 2023-01-23 thomas r = gotweb_reply_file(c, "application/octet-stream",
253 50237485 2023-01-23 thomas qs->file, NULL);
254 ea1b5cf5 2023-01-15 thomas else
255 50237485 2023-01-23 thomas r = gotweb_reply(c, 200, "text/plain", NULL);
256 50237485 2023-01-23 thomas if (r == -1)
257 d8bf4f25 2023-09-14 thomas return;
258 d8bf4f25 2023-09-14 thomas if (template_flush(c->tp) == -1)
259 164b5ddc 2023-03-11 thomas return;
260 ea1b5cf5 2023-01-15 thomas
261 ea1b5cf5 2023-01-15 thomas for (;;) {
262 161663e7 2023-03-11 thomas error = got_object_blob_read_block(&len, c->t->blob);
263 ea1b5cf5 2023-01-15 thomas if (error)
264 164b5ddc 2023-03-11 thomas break;
265 ea1b5cf5 2023-01-15 thomas if (len == 0)
266 ea1b5cf5 2023-01-15 thomas break;
267 161663e7 2023-03-11 thomas buf = got_object_blob_get_read_buf(c->t->blob);
268 d8bf4f25 2023-09-14 thomas if (fcgi_write(c, buf, len) == -1)
269 164b5ddc 2023-03-11 thomas break;
270 b82440e1 2023-01-06 thomas }
271 164b5ddc 2023-03-11 thomas return;
272 8a35f56c 2022-07-16 thomas case BRIEFS:
273 b2251e6e 2023-06-26 thomas error = got_get_repo_commits(c, srv->max_commits_display);
274 b2251e6e 2023-06-26 thomas if (error)
275 b2251e6e 2023-06-26 thomas goto err;
276 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
277 164b5ddc 2023-03-11 thomas return;
278 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_briefs);
279 164b5ddc 2023-03-11 thomas return;
280 8a35f56c 2022-07-16 thomas case COMMITS:
281 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
282 8a35f56c 2022-07-16 thomas if (error) {
283 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
284 8a35f56c 2022-07-16 thomas goto err;
285 8a35f56c 2022-07-16 thomas }
286 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
287 164b5ddc 2023-03-11 thomas return;
288 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_commits);
289 164b5ddc 2023-03-11 thomas return;
290 8a35f56c 2022-07-16 thomas case DIFF:
291 dccd05b4 2023-01-10 thomas error = got_get_repo_commits(c, 1);
292 5d860bce 2023-01-07 thomas if (error) {
293 5d860bce 2023-01-07 thomas log_warnx("%s: %s", __func__, error->msg);
294 5d860bce 2023-01-07 thomas goto err;
295 5d860bce 2023-01-07 thomas }
296 04b2c111 2023-05-16 thomas error = got_open_diff_for_output(&c->t->fp, c);
297 dccd05b4 2023-01-10 thomas if (error) {
298 dccd05b4 2023-01-10 thomas log_warnx("%s: %s", __func__, error->msg);
299 dccd05b4 2023-01-10 thomas goto err;
300 dccd05b4 2023-01-10 thomas }
301 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
302 164b5ddc 2023-03-11 thomas return;
303 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_diff);
304 164b5ddc 2023-03-11 thomas return;
305 8a35f56c 2022-07-16 thomas case INDEX:
306 161663e7 2023-03-11 thomas c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
307 161663e7 2023-03-11 thomas alphasort);
308 161663e7 2023-03-11 thomas if (c->t->nrepos == -1) {
309 161663e7 2023-03-11 thomas c->t->repos = NULL;
310 161663e7 2023-03-11 thomas error = got_error_from_errno2("scandir",
311 161663e7 2023-03-11 thomas srv->repos_path);
312 8a35f56c 2022-07-16 thomas goto err;
313 8a35f56c 2022-07-16 thomas }
314 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
315 164b5ddc 2023-03-11 thomas return;
316 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_index);
317 164b5ddc 2023-03-11 thomas return;
318 feaddee6 2023-12-03 thomas case PATCH:
319 feaddee6 2023-12-03 thomas error = got_get_repo_commits(c, 1);
320 feaddee6 2023-12-03 thomas if (error) {
321 feaddee6 2023-12-03 thomas log_warnx("%s: %s", __func__, error->msg);
322 feaddee6 2023-12-03 thomas goto err;
323 feaddee6 2023-12-03 thomas }
324 feaddee6 2023-12-03 thomas error = got_open_diff_for_output(&c->t->fp, c);
325 feaddee6 2023-12-03 thomas if (error) {
326 feaddee6 2023-12-03 thomas log_warnx("%s: %s", __func__, error->msg);
327 feaddee6 2023-12-03 thomas goto err;
328 feaddee6 2023-12-03 thomas }
329 feaddee6 2023-12-03 thomas if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
330 feaddee6 2023-12-03 thomas return;
331 feaddee6 2023-12-03 thomas gotweb_render_patch(c->tp);
332 feaddee6 2023-12-03 thomas return;
333 44f2135a 2023-04-01 thomas case RSS:
334 44f2135a 2023-04-01 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
335 44f2135a 2023-04-01 thomas if (error)
336 44f2135a 2023-04-01 thomas goto err;
337 44f2135a 2023-04-01 thomas if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
338 44f2135a 2023-04-01 thomas == -1)
339 44f2135a 2023-04-01 thomas return;
340 44f2135a 2023-04-01 thomas gotweb_render_rss(c->tp);
341 44f2135a 2023-04-01 thomas return;
342 8a35f56c 2022-07-16 thomas case SUMMARY:
343 161663e7 2023-03-11 thomas error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
344 18e466eb 2023-01-14 thomas got_ref_cmp_by_name, NULL);
345 8a35f56c 2022-07-16 thomas if (error) {
346 18e466eb 2023-01-14 thomas log_warnx("%s: got_ref_list: %s", __func__,
347 18e466eb 2023-01-14 thomas error->msg);
348 8a35f56c 2022-07-16 thomas goto err;
349 8a35f56c 2022-07-16 thomas }
350 b2251e6e 2023-06-26 thomas error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
351 b2251e6e 2023-06-26 thomas if (error)
352 b2251e6e 2023-06-26 thomas goto err;
353 18e466eb 2023-01-14 thomas qs->action = TAGS;
354 c328bece 2023-12-08 thomas error = got_get_repo_tags(c, D_MAXSLTAGDISP);
355 18e466eb 2023-01-14 thomas if (error) {
356 18e466eb 2023-01-14 thomas log_warnx("%s: got_get_repo_tags: %s", __func__,
357 18e466eb 2023-01-14 thomas error->msg);
358 18e466eb 2023-01-14 thomas goto err;
359 18e466eb 2023-01-14 thomas }
360 18e466eb 2023-01-14 thomas qs->action = SUMMARY;
361 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
362 164b5ddc 2023-03-11 thomas return;
363 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_summary);
364 164b5ddc 2023-03-11 thomas return;
365 8a35f56c 2022-07-16 thomas case TAG:
366 145ca42a 2023-01-09 thomas error = got_get_repo_tags(c, 1);
367 8a35f56c 2022-07-16 thomas if (error) {
368 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
369 145ca42a 2023-01-09 thomas goto err;
370 145ca42a 2023-01-09 thomas }
371 145ca42a 2023-01-09 thomas if (c->t->tag_count == 0) {
372 145ca42a 2023-01-09 thomas error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
373 145ca42a 2023-01-09 thomas "bad commit id");
374 8a35f56c 2022-07-16 thomas goto err;
375 8a35f56c 2022-07-16 thomas }
376 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
377 164b5ddc 2023-03-11 thomas return;
378 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tag);
379 164b5ddc 2023-03-11 thomas return;
380 8a35f56c 2022-07-16 thomas case TAGS:
381 b3ba36c3 2023-01-14 thomas error = got_get_repo_tags(c, srv->max_commits_display);
382 8a35f56c 2022-07-16 thomas if (error) {
383 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
384 8a35f56c 2022-07-16 thomas goto err;
385 8a35f56c 2022-07-16 thomas }
386 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
387 164b5ddc 2023-03-11 thomas return;
388 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tags);
389 164b5ddc 2023-03-11 thomas return;
390 8a35f56c 2022-07-16 thomas case TREE:
391 3c14c1f2 2023-01-06 thomas error = got_get_repo_commits(c, 1);
392 8a35f56c 2022-07-16 thomas if (error) {
393 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
394 8a35f56c 2022-07-16 thomas goto err;
395 8a35f56c 2022-07-16 thomas }
396 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
397 164b5ddc 2023-03-11 thomas return;
398 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tree);
399 164b5ddc 2023-03-11 thomas return;
400 8a35f56c 2022-07-16 thomas case ERR:
401 8a35f56c 2022-07-16 thomas default:
402 164b5ddc 2023-03-11 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
403 8a35f56c 2022-07-16 thomas }
404 8a35f56c 2022-07-16 thomas
405 8a35f56c 2022-07-16 thomas err:
406 164b5ddc 2023-03-11 thomas c->t->error = error;
407 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 400, "text/html", NULL) == -1)
408 8a35f56c 2022-07-16 thomas return;
409 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_error);
410 8a35f56c 2022-07-16 thomas }
411 8a35f56c 2022-07-16 thomas
412 8a35f56c 2022-07-16 thomas struct server *
413 b95d1cf6 2023-06-25 thomas gotweb_get_server(const char *server_name)
414 8a35f56c 2022-07-16 thomas {
415 b95d1cf6 2023-06-25 thomas struct server *srv;
416 8a35f56c 2022-07-16 thomas
417 3f0158b4 2022-08-30 thomas /* check against the server name first */
418 102d840d 2023-06-22 thomas if (*server_name != '\0')
419 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
420 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
421 b95d1cf6 2023-06-25 thomas return srv;
422 8a35f56c 2022-07-16 thomas
423 b95d1cf6 2023-06-25 thomas /* otherwise, use the first server */
424 b95d1cf6 2023-06-25 thomas return TAILQ_FIRST(&gotwebd_env->servers);
425 8a35f56c 2022-07-16 thomas };
426 8a35f56c 2022-07-16 thomas
427 8a35f56c 2022-07-16 thomas const struct got_error *
428 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
429 8a35f56c 2022-07-16 thomas {
430 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
431 8a35f56c 2022-07-16 thomas
432 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
433 8a35f56c 2022-07-16 thomas if (*t == NULL)
434 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
435 8a35f56c 2022-07-16 thomas
436 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
437 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
438 161663e7 2023-03-11 thomas TAILQ_INIT(&(*t)->refs);
439 161663e7 2023-03-11 thomas
440 161663e7 2023-03-11 thomas (*t)->fd = -1;
441 8a35f56c 2022-07-16 thomas
442 8a35f56c 2022-07-16 thomas return error;
443 8a35f56c 2022-07-16 thomas }
444 8a35f56c 2022-07-16 thomas
445 8a35f56c 2022-07-16 thomas static const struct got_error *
446 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
447 8a35f56c 2022-07-16 thomas {
448 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
449 8a35f56c 2022-07-16 thomas
450 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
451 8a35f56c 2022-07-16 thomas if (*qs == NULL)
452 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
453 8a35f56c 2022-07-16 thomas
454 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
455 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
456 84630254 2022-09-02 thomas free(*qs);
457 84630254 2022-09-02 thomas *qs = NULL;
458 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
459 8a35f56c 2022-07-16 thomas }
460 84630254 2022-09-02 thomas
461 84630254 2022-09-02 thomas (*qs)->action = INDEX;
462 84630254 2022-09-02 thomas (*qs)->commit = NULL;
463 84630254 2022-09-02 thomas (*qs)->file = NULL;
464 84630254 2022-09-02 thomas (*qs)->folder = NULL;
465 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
466 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
467 8a35f56c 2022-07-16 thomas
468 8a35f56c 2022-07-16 thomas return error;
469 8a35f56c 2022-07-16 thomas }
470 8a35f56c 2022-07-16 thomas
471 8a35f56c 2022-07-16 thomas static const struct got_error *
472 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
473 8a35f56c 2022-07-16 thomas {
474 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
475 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
476 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
477 8a35f56c 2022-07-16 thomas
478 8a35f56c 2022-07-16 thomas if (qst == NULL)
479 8a35f56c 2022-07-16 thomas return error;
480 8a35f56c 2022-07-16 thomas
481 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
482 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
483 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
484 8a35f56c 2022-07-16 thomas
485 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
486 8a35f56c 2022-07-16 thomas tok1_end = tok1;
487 8a35f56c 2022-07-16 thomas
488 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
489 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
490 8a35f56c 2022-07-16 thomas
491 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
492 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
493 8a35f56c 2022-07-16 thomas free(tok1);
494 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
495 8a35f56c 2022-07-16 thomas }
496 8a35f56c 2022-07-16 thomas
497 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
498 8a35f56c 2022-07-16 thomas tok2_end = tok2;
499 8a35f56c 2022-07-16 thomas
500 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
501 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
502 8a35f56c 2022-07-16 thomas if (tok2_end) {
503 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
504 8a35f56c 2022-07-16 thomas tok2_end);
505 8a35f56c 2022-07-16 thomas if (error)
506 8a35f56c 2022-07-16 thomas goto err;
507 8a35f56c 2022-07-16 thomas }
508 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
509 8a35f56c 2022-07-16 thomas }
510 8a35f56c 2022-07-16 thomas free(tok2);
511 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
512 8a35f56c 2022-07-16 thomas }
513 8a35f56c 2022-07-16 thomas free(tok1);
514 8a35f56c 2022-07-16 thomas return error;
515 8a35f56c 2022-07-16 thomas err:
516 8a35f56c 2022-07-16 thomas free(tok2);
517 8a35f56c 2022-07-16 thomas free(tok1);
518 8a35f56c 2022-07-16 thomas return error;
519 8a35f56c 2022-07-16 thomas }
520 8a35f56c 2022-07-16 thomas
521 2ac684a4 2022-09-03 thomas /*
522 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
523 2ac684a4 2022-09-03 thomas */
524 8a35f56c 2022-07-16 thomas static const struct got_error *
525 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
526 2ac684a4 2022-09-03 thomas {
527 2ac684a4 2022-09-03 thomas char *p, *q;
528 2ac684a4 2022-09-03 thomas char hex[3];
529 2ac684a4 2022-09-03 thomas unsigned long x;
530 2ac684a4 2022-09-03 thomas
531 2ac684a4 2022-09-03 thomas hex[2] = '\0';
532 2ac684a4 2022-09-03 thomas p = q = url;
533 2ac684a4 2022-09-03 thomas
534 2ac684a4 2022-09-03 thomas while (*p != '\0') {
535 2ac684a4 2022-09-03 thomas switch (*p) {
536 2ac684a4 2022-09-03 thomas case '%':
537 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
538 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
539 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
540 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
541 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
542 2ac684a4 2022-09-03 thomas
543 2ac684a4 2022-09-03 thomas hex[0] = p[1];
544 2ac684a4 2022-09-03 thomas hex[1] = p[2];
545 2ac684a4 2022-09-03 thomas
546 2ac684a4 2022-09-03 thomas /*
547 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
548 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
549 2ac684a4 2022-09-03 thomas */
550 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
551 2ac684a4 2022-09-03 thomas *q = (char)x;
552 2ac684a4 2022-09-03 thomas p += 2;
553 2ac684a4 2022-09-03 thomas break;
554 2ac684a4 2022-09-03 thomas default:
555 2ac684a4 2022-09-03 thomas *q = *p;
556 2ac684a4 2022-09-03 thomas break;
557 2ac684a4 2022-09-03 thomas }
558 2ac684a4 2022-09-03 thomas p++;
559 2ac684a4 2022-09-03 thomas q++;
560 2ac684a4 2022-09-03 thomas }
561 2ac684a4 2022-09-03 thomas *q = '\0';
562 2ac684a4 2022-09-03 thomas
563 2ac684a4 2022-09-03 thomas return NULL;
564 2ac684a4 2022-09-03 thomas }
565 2ac684a4 2022-09-03 thomas
566 2ac684a4 2022-09-03 thomas static const struct got_error *
567 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
568 8a35f56c 2022-07-16 thomas {
569 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
570 8a35f56c 2022-07-16 thomas const char *errstr;
571 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
572 8a35f56c 2022-07-16 thomas
573 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
574 2ac684a4 2022-09-03 thomas if (error)
575 2ac684a4 2022-09-03 thomas return error;
576 2ac684a4 2022-09-03 thomas
577 b38e9a2a 2023-11-22 thomas for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
578 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
579 8a35f56c 2022-07-16 thomas continue;
580 8a35f56c 2022-07-16 thomas
581 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
582 8a35f56c 2022-07-16 thomas case ACTION:
583 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
584 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
585 8a35f56c 2022-07-16 thomas continue;
586 8a35f56c 2022-07-16 thomas else if (strcmp(value,
587 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
588 8a35f56c 2022-07-16 thomas (*qs)->action =
589 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
590 8a35f56c 2022-07-16 thomas goto qa_found;
591 8a35f56c 2022-07-16 thomas }
592 8a35f56c 2022-07-16 thomas }
593 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
594 8a35f56c 2022-07-16 thomas qa_found:
595 8a35f56c 2022-07-16 thomas break;
596 8a35f56c 2022-07-16 thomas case COMMIT:
597 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
598 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
599 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
600 d8edcc94 2023-06-01 thomas "strdup");
601 8a35f56c 2022-07-16 thomas goto done;
602 8a35f56c 2022-07-16 thomas }
603 8a35f56c 2022-07-16 thomas break;
604 8a35f56c 2022-07-16 thomas case RFILE:
605 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
606 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
607 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
608 d8edcc94 2023-06-01 thomas "strdup");
609 8a35f56c 2022-07-16 thomas goto done;
610 8a35f56c 2022-07-16 thomas }
611 8a35f56c 2022-07-16 thomas break;
612 8a35f56c 2022-07-16 thomas case FOLDER:
613 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
614 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
615 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
616 d8edcc94 2023-06-01 thomas "strdup");
617 8a35f56c 2022-07-16 thomas goto done;
618 8a35f56c 2022-07-16 thomas }
619 8a35f56c 2022-07-16 thomas break;
620 8a35f56c 2022-07-16 thomas case HEADREF:
621 b0764984 2022-09-02 thomas free((*qs)->headref);
622 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
623 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
624 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
625 d8edcc94 2023-06-01 thomas "strdup");
626 8a35f56c 2022-07-16 thomas goto done;
627 8a35f56c 2022-07-16 thomas }
628 8a35f56c 2022-07-16 thomas break;
629 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
630 102d840d 2023-06-22 thomas if (*value == '\0')
631 8a35f56c 2022-07-16 thomas break;
632 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
633 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
634 8a35f56c 2022-07-16 thomas if (errstr) {
635 d8edcc94 2023-06-01 thomas error = got_error_from_errno3(__func__,
636 d8edcc94 2023-06-01 thomas "strtonum", errstr);
637 8a35f56c 2022-07-16 thomas goto done;
638 8a35f56c 2022-07-16 thomas }
639 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
640 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
641 8a35f56c 2022-07-16 thomas break;
642 8a35f56c 2022-07-16 thomas case PATH:
643 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
644 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
645 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
646 d8edcc94 2023-06-01 thomas "strdup");
647 8a35f56c 2022-07-16 thomas goto done;
648 8a35f56c 2022-07-16 thomas }
649 8a35f56c 2022-07-16 thomas break;
650 8a35f56c 2022-07-16 thomas case PAGE:
651 102d840d 2023-06-22 thomas if (*value == '\0')
652 8a35f56c 2022-07-16 thomas break;
653 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
654 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
655 8a35f56c 2022-07-16 thomas if (errstr) {
656 d8edcc94 2023-06-01 thomas error = got_error_from_errno3(__func__,
657 d8edcc94 2023-06-01 thomas "strtonum", errstr);
658 8a35f56c 2022-07-16 thomas goto done;
659 8a35f56c 2022-07-16 thomas }
660 3d6d1fb0 2022-12-30 thomas if ((*qs)->page < 0)
661 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
662 8a35f56c 2022-07-16 thomas break;
663 8a35f56c 2022-07-16 thomas }
664 b38e9a2a 2023-11-22 thomas
665 b38e9a2a 2023-11-22 thomas /* entry found */
666 b38e9a2a 2023-11-22 thomas break;
667 8a35f56c 2022-07-16 thomas }
668 8a35f56c 2022-07-16 thomas done:
669 8a35f56c 2022-07-16 thomas return error;
670 8a35f56c 2022-07-16 thomas }
671 8a35f56c 2022-07-16 thomas
672 8a35f56c 2022-07-16 thomas void
673 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
674 8a35f56c 2022-07-16 thomas {
675 8a35f56c 2022-07-16 thomas if (rt != NULL) {
676 8a35f56c 2022-07-16 thomas free(rt->commit_id);
677 217813df 2022-09-02 thomas free(rt->tag_name);
678 217813df 2022-09-02 thomas free(rt->tag_commit);
679 217813df 2022-09-02 thomas free(rt->commit_msg);
680 8a35f56c 2022-07-16 thomas free(rt->tagger);
681 8a35f56c 2022-07-16 thomas }
682 8a35f56c 2022-07-16 thomas free(rt);
683 8a35f56c 2022-07-16 thomas }
684 8a35f56c 2022-07-16 thomas
685 8a35f56c 2022-07-16 thomas void
686 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
687 8a35f56c 2022-07-16 thomas {
688 8a35f56c 2022-07-16 thomas if (rc != NULL) {
689 8a35f56c 2022-07-16 thomas free(rc->path);
690 8a35f56c 2022-07-16 thomas free(rc->refs_str);
691 8a35f56c 2022-07-16 thomas free(rc->commit_id);
692 8a35f56c 2022-07-16 thomas free(rc->parent_id);
693 8a35f56c 2022-07-16 thomas free(rc->tree_id);
694 8a35f56c 2022-07-16 thomas free(rc->author);
695 8a35f56c 2022-07-16 thomas free(rc->committer);
696 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
697 8a35f56c 2022-07-16 thomas }
698 8a35f56c 2022-07-16 thomas free(rc);
699 8a35f56c 2022-07-16 thomas }
700 8a35f56c 2022-07-16 thomas
701 8a35f56c 2022-07-16 thomas static void
702 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
703 8a35f56c 2022-07-16 thomas {
704 8a35f56c 2022-07-16 thomas if (qs != NULL) {
705 8a35f56c 2022-07-16 thomas free(qs->commit);
706 8a35f56c 2022-07-16 thomas free(qs->file);
707 8a35f56c 2022-07-16 thomas free(qs->folder);
708 8a35f56c 2022-07-16 thomas free(qs->headref);
709 8a35f56c 2022-07-16 thomas free(qs->path);
710 8a35f56c 2022-07-16 thomas }
711 8a35f56c 2022-07-16 thomas free(qs);
712 8a35f56c 2022-07-16 thomas }
713 8a35f56c 2022-07-16 thomas
714 8a35f56c 2022-07-16 thomas static void
715 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
716 8a35f56c 2022-07-16 thomas {
717 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
718 8a35f56c 2022-07-16 thomas free(repo_dir->name);
719 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
720 8a35f56c 2022-07-16 thomas free(repo_dir->description);
721 8a35f56c 2022-07-16 thomas free(repo_dir->url);
722 8a35f56c 2022-07-16 thomas free(repo_dir->path);
723 8a35f56c 2022-07-16 thomas }
724 8a35f56c 2022-07-16 thomas free(repo_dir);
725 8a35f56c 2022-07-16 thomas }
726 8a35f56c 2022-07-16 thomas
727 8a35f56c 2022-07-16 thomas void
728 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
729 8a35f56c 2022-07-16 thomas {
730 161663e7 2023-03-11 thomas const struct got_error *err;
731 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
732 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
733 161663e7 2023-03-11 thomas int i;
734 8a35f56c 2022-07-16 thomas
735 161663e7 2023-03-11 thomas got_ref_list_free(&t->refs);
736 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
737 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
738 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
739 8a35f56c 2022-07-16 thomas }
740 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
741 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
742 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
743 8a35f56c 2022-07-16 thomas }
744 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
745 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
746 20bab626 2023-02-03 thomas free(t->more_id);
747 978394e7 2023-12-08 thomas free(t->tags_more_id);
748 161663e7 2023-03-11 thomas if (t->blob)
749 161663e7 2023-03-11 thomas got_object_blob_close(t->blob);
750 161663e7 2023-03-11 thomas if (t->fp) {
751 d00235d8 2023-05-17 thomas err = got_gotweb_closefile(t->fp);
752 161663e7 2023-03-11 thomas if (err)
753 d00235d8 2023-05-17 thomas log_warnx("%s: got_gotweb_closefile failure: %s",
754 161663e7 2023-03-11 thomas __func__, err->msg);
755 161663e7 2023-03-11 thomas }
756 54ca6e2b 2023-05-16 thomas if (t->fd != -1 && close(t->fd) == -1)
757 54ca6e2b 2023-05-16 thomas log_warn("%s: close", __func__);
758 161663e7 2023-03-11 thomas if (t->repos) {
759 161663e7 2023-03-11 thomas for (i = 0; i < t->nrepos; ++i)
760 161663e7 2023-03-11 thomas free(t->repos[i]);
761 161663e7 2023-03-11 thomas free(t->repos);
762 161663e7 2023-03-11 thomas }
763 ab5bda7e 2023-11-22 thomas if (t->repo)
764 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
765 8a35f56c 2022-07-16 thomas free(t);
766 8a35f56c 2022-07-16 thomas }
767 8a35f56c 2022-07-16 thomas
768 2f4f0731 2022-12-30 thomas void
769 978394e7 2023-12-08 thomas gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
770 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
771 8a35f56c 2022-07-16 thomas {
772 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
773 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
774 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
775 8a35f56c 2022-07-16 thomas
776 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
777 8a35f56c 2022-07-16 thomas
778 978394e7 2023-12-08 thomas if (qs->index_page > 0) {
779 978394e7 2023-12-08 thomas *have_prev = 1;
780 978394e7 2023-12-08 thomas *prev = (struct gotweb_url){
781 978394e7 2023-12-08 thomas .action = -1,
782 978394e7 2023-12-08 thomas .index_page = qs->index_page - 1,
783 978394e7 2023-12-08 thomas .page = -1,
784 978394e7 2023-12-08 thomas };
785 978394e7 2023-12-08 thomas }
786 978394e7 2023-12-08 thomas if (t->next_disp == srv->max_repos_display &&
787 978394e7 2023-12-08 thomas t->repos_total != (qs->index_page + 1) *
788 978394e7 2023-12-08 thomas srv->max_repos_display) {
789 978394e7 2023-12-08 thomas *have_next = 1;
790 978394e7 2023-12-08 thomas *next = (struct gotweb_url){
791 978394e7 2023-12-08 thomas .action = -1,
792 978394e7 2023-12-08 thomas .index_page = qs->index_page + 1,
793 978394e7 2023-12-08 thomas .page = -1,
794 978394e7 2023-12-08 thomas };
795 8a35f56c 2022-07-16 thomas }
796 8a35f56c 2022-07-16 thomas }
797 8a35f56c 2022-07-16 thomas
798 161663e7 2023-03-11 thomas static int
799 161663e7 2023-03-11 thomas gotweb_render_index(struct template *tp)
800 8a35f56c 2022-07-16 thomas {
801 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
802 161663e7 2023-03-11 thomas struct request *c = tp->tp_arg;
803 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
804 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
805 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
806 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
807 161663e7 2023-03-11 thomas struct dirent **sd_dent = t->repos;
808 161663e7 2023-03-11 thomas unsigned int d_i, d_disp = 0;
809 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
810 161663e7 2023-03-11 thomas int type, r;
811 8a35f56c 2022-07-16 thomas
812 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
813 161663e7 2023-03-11 thomas return -1;
814 79393471 2022-08-27 thomas
815 161663e7 2023-03-11 thomas for (d_i = 0; d_i < t->nrepos; d_i++) {
816 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
817 57e88d7c 2022-11-23 thomas break;
818 8a35f56c 2022-07-16 thomas
819 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
820 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
821 24240f6a 2022-11-23 thomas d_skipped++;
822 24240f6a 2022-11-23 thomas continue;
823 24240f6a 2022-11-23 thomas }
824 24240f6a 2022-11-23 thomas
825 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
826 24240f6a 2022-11-23 thomas sd_dent[d_i]);
827 24240f6a 2022-11-23 thomas if (error)
828 161663e7 2023-03-11 thomas continue;
829 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
830 24240f6a 2022-11-23 thomas d_skipped++;
831 8a35f56c 2022-07-16 thomas continue;
832 24240f6a 2022-11-23 thomas }
833 8a35f56c 2022-07-16 thomas
834 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
835 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
836 8a35f56c 2022-07-16 thomas t->prev_disp++;
837 8a35f56c 2022-07-16 thomas continue;
838 8a35f56c 2022-07-16 thomas }
839 8a35f56c 2022-07-16 thomas
840 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
841 8a35f56c 2022-07-16 thomas if (error)
842 161663e7 2023-03-11 thomas continue;
843 8a35f56c 2022-07-16 thomas
844 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
845 f5af6dbd 2023-04-29 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
846 161663e7 2023-03-11 thomas if (error->code != GOT_ERR_NOT_GIT_REPO)
847 161663e7 2023-03-11 thomas log_warnx("%s: %s: %s", __func__,
848 161663e7 2023-03-11 thomas sd_dent[d_i]->d_name, error->msg);
849 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
850 8a35f56c 2022-07-16 thomas repo_dir = NULL;
851 24240f6a 2022-11-23 thomas d_skipped++;
852 8a35f56c 2022-07-16 thomas continue;
853 8a35f56c 2022-07-16 thomas }
854 24240f6a 2022-11-23 thomas
855 8a35f56c 2022-07-16 thomas d_disp++;
856 8a35f56c 2022-07-16 thomas t->prev_disp++;
857 8a35f56c 2022-07-16 thomas
858 161663e7 2023-03-11 thomas r = gotweb_render_repo_fragment(c->tp, repo_dir);
859 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
860 ab5bda7e 2023-11-22 thomas repo_dir = NULL;
861 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
862 ab5bda7e 2023-11-22 thomas t->repo = NULL;
863 161663e7 2023-03-11 thomas if (r == -1)
864 161663e7 2023-03-11 thomas return -1;
865 161663e7 2023-03-11 thomas
866 8a35f56c 2022-07-16 thomas t->next_disp++;
867 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
868 8a35f56c 2022-07-16 thomas break;
869 8a35f56c 2022-07-16 thomas }
870 161663e7 2023-03-11 thomas t->repos_total = t->nrepos - d_skipped;
871 24240f6a 2022-11-23 thomas
872 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
873 161663e7 2023-03-11 thomas return 0;
874 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
875 161663e7 2023-03-11 thomas return 0;
876 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
877 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
878 161663e7 2023-03-11 thomas return 0;
879 8a35f56c 2022-07-16 thomas
880 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
881 161663e7 2023-03-11 thomas return -1;
882 161663e7 2023-03-11 thomas
883 161663e7 2023-03-11 thomas return 0;
884 8a35f56c 2022-07-16 thomas }
885 8a35f56c 2022-07-16 thomas
886 55144267 2022-09-07 thomas static inline int
887 55144267 2022-09-07 thomas should_urlencode(int c)
888 55144267 2022-09-07 thomas {
889 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
890 55144267 2022-09-07 thomas return 1;
891 55144267 2022-09-07 thomas
892 55144267 2022-09-07 thomas switch (c) {
893 55144267 2022-09-07 thomas /* gen-delim */
894 55144267 2022-09-07 thomas case ':':
895 55144267 2022-09-07 thomas case '/':
896 55144267 2022-09-07 thomas case '?':
897 55144267 2022-09-07 thomas case '#':
898 55144267 2022-09-07 thomas case '[':
899 55144267 2022-09-07 thomas case ']':
900 55144267 2022-09-07 thomas case '@':
901 55144267 2022-09-07 thomas /* sub-delims */
902 55144267 2022-09-07 thomas case '!':
903 55144267 2022-09-07 thomas case '$':
904 55144267 2022-09-07 thomas case '&':
905 55144267 2022-09-07 thomas case '\'':
906 55144267 2022-09-07 thomas case '(':
907 55144267 2022-09-07 thomas case ')':
908 55144267 2022-09-07 thomas case '*':
909 55144267 2022-09-07 thomas case '+':
910 55144267 2022-09-07 thomas case ',':
911 55144267 2022-09-07 thomas case ';':
912 55144267 2022-09-07 thomas case '=':
913 e1a9403a 2023-01-06 thomas /* needed because the URLs are embedded into the HTML */
914 e1a9403a 2023-01-06 thomas case '\"':
915 55144267 2022-09-07 thomas return 1;
916 55144267 2022-09-07 thomas default:
917 55144267 2022-09-07 thomas return 0;
918 55144267 2022-09-07 thomas }
919 55144267 2022-09-07 thomas }
920 55144267 2022-09-07 thomas
921 55144267 2022-09-07 thomas static char *
922 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
923 55144267 2022-09-07 thomas {
924 55144267 2022-09-07 thomas const char *s;
925 55144267 2022-09-07 thomas char *escaped;
926 55144267 2022-09-07 thomas size_t i, len;
927 55144267 2022-09-07 thomas int a, b;
928 55144267 2022-09-07 thomas
929 55144267 2022-09-07 thomas len = 0;
930 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
931 55144267 2022-09-07 thomas len++;
932 55144267 2022-09-07 thomas if (should_urlencode(*s))
933 55144267 2022-09-07 thomas len += 2;
934 55144267 2022-09-07 thomas }
935 55144267 2022-09-07 thomas
936 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
937 55144267 2022-09-07 thomas if (escaped == NULL)
938 55144267 2022-09-07 thomas return NULL;
939 55144267 2022-09-07 thomas
940 55144267 2022-09-07 thomas i = 0;
941 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
942 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
943 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
944 55144267 2022-09-07 thomas b = (*s & 0x0F);
945 55144267 2022-09-07 thomas
946 55144267 2022-09-07 thomas escaped[i++] = '%';
947 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
948 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
949 55144267 2022-09-07 thomas } else
950 55144267 2022-09-07 thomas escaped[i++] = *s;
951 55144267 2022-09-07 thomas }
952 55144267 2022-09-07 thomas
953 55144267 2022-09-07 thomas return escaped;
954 55144267 2022-09-07 thomas }
955 55144267 2022-09-07 thomas
956 e7e5fa49 2022-12-30 thomas const char *
957 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
958 55144267 2022-09-07 thomas {
959 55144267 2022-09-07 thomas switch (action) {
960 55144267 2022-09-07 thomas case BLAME:
961 55144267 2022-09-07 thomas return "blame";
962 55144267 2022-09-07 thomas case BLOB:
963 55144267 2022-09-07 thomas return "blob";
964 b82440e1 2023-01-06 thomas case BLOBRAW:
965 b82440e1 2023-01-06 thomas return "blobraw";
966 55144267 2022-09-07 thomas case BRIEFS:
967 55144267 2022-09-07 thomas return "briefs";
968 55144267 2022-09-07 thomas case COMMITS:
969 55144267 2022-09-07 thomas return "commits";
970 55144267 2022-09-07 thomas case DIFF:
971 55144267 2022-09-07 thomas return "diff";
972 55144267 2022-09-07 thomas case ERR:
973 55144267 2022-09-07 thomas return "err";
974 55144267 2022-09-07 thomas case INDEX:
975 55144267 2022-09-07 thomas return "index";
976 feaddee6 2023-12-03 thomas case PATCH:
977 feaddee6 2023-12-03 thomas return "patch";
978 55144267 2022-09-07 thomas case SUMMARY:
979 55144267 2022-09-07 thomas return "summary";
980 55144267 2022-09-07 thomas case TAG:
981 55144267 2022-09-07 thomas return "tag";
982 55144267 2022-09-07 thomas case TAGS:
983 55144267 2022-09-07 thomas return "tags";
984 55144267 2022-09-07 thomas case TREE:
985 55144267 2022-09-07 thomas return "tree";
986 d6795e9f 2022-12-30 thomas case RSS:
987 d6795e9f 2022-12-30 thomas return "rss";
988 55144267 2022-09-07 thomas default:
989 55144267 2022-09-07 thomas return NULL;
990 55144267 2022-09-07 thomas }
991 55144267 2022-09-07 thomas }
992 55144267 2022-09-07 thomas
993 e7e5fa49 2022-12-30 thomas int
994 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
995 55144267 2022-09-07 thomas {
996 55144267 2022-09-07 thomas const char *sep = "?", *action;
997 55144267 2022-09-07 thomas char *tmp;
998 55144267 2022-09-07 thomas int r;
999 55144267 2022-09-07 thomas
1000 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
1001 55144267 2022-09-07 thomas if (action != NULL) {
1002 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "?action=%s", action) == -1)
1003 55144267 2022-09-07 thomas return -1;
1004 55144267 2022-09-07 thomas sep = "&";
1005 55144267 2022-09-07 thomas }
1006 55144267 2022-09-07 thomas
1007 55144267 2022-09-07 thomas if (url->commit) {
1008 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1009 55144267 2022-09-07 thomas return -1;
1010 55144267 2022-09-07 thomas sep = "&";
1011 55144267 2022-09-07 thomas }
1012 55144267 2022-09-07 thomas
1013 55144267 2022-09-07 thomas if (url->previd) {
1014 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
1015 55144267 2022-09-07 thomas return -1;
1016 55144267 2022-09-07 thomas sep = "&";
1017 55144267 2022-09-07 thomas }
1018 55144267 2022-09-07 thomas
1019 55144267 2022-09-07 thomas if (url->prevset) {
1020 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1021 55144267 2022-09-07 thomas return -1;
1022 55144267 2022-09-07 thomas sep = "&";
1023 55144267 2022-09-07 thomas }
1024 55144267 2022-09-07 thomas
1025 55144267 2022-09-07 thomas if (url->file) {
1026 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1027 55144267 2022-09-07 thomas if (tmp == NULL)
1028 55144267 2022-09-07 thomas return -1;
1029 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1030 55144267 2022-09-07 thomas free(tmp);
1031 55144267 2022-09-07 thomas if (r == -1)
1032 55144267 2022-09-07 thomas return -1;
1033 55144267 2022-09-07 thomas sep = "&";
1034 55144267 2022-09-07 thomas }
1035 55144267 2022-09-07 thomas
1036 55144267 2022-09-07 thomas if (url->folder) {
1037 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1038 55144267 2022-09-07 thomas if (tmp == NULL)
1039 55144267 2022-09-07 thomas return -1;
1040 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1041 55144267 2022-09-07 thomas free(tmp);
1042 55144267 2022-09-07 thomas if (r == -1)
1043 55144267 2022-09-07 thomas return -1;
1044 55144267 2022-09-07 thomas sep = "&";
1045 55144267 2022-09-07 thomas }
1046 55144267 2022-09-07 thomas
1047 55144267 2022-09-07 thomas if (url->headref) {
1048 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1049 55144267 2022-09-07 thomas if (tmp == NULL)
1050 55144267 2022-09-07 thomas return -1;
1051 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1052 55144267 2022-09-07 thomas free(tmp);
1053 55144267 2022-09-07 thomas if (r == -1)
1054 55144267 2022-09-07 thomas return -1;
1055 55144267 2022-09-07 thomas sep = "&";
1056 55144267 2022-09-07 thomas }
1057 55144267 2022-09-07 thomas
1058 55144267 2022-09-07 thomas if (url->index_page != -1) {
1059 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sindex_page=%d", sep,
1060 55144267 2022-09-07 thomas url->index_page) == -1)
1061 55144267 2022-09-07 thomas return -1;
1062 55144267 2022-09-07 thomas sep = "&";
1063 55144267 2022-09-07 thomas }
1064 55144267 2022-09-07 thomas
1065 55144267 2022-09-07 thomas if (url->path) {
1066 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1067 55144267 2022-09-07 thomas if (tmp == NULL)
1068 55144267 2022-09-07 thomas return -1;
1069 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1070 55144267 2022-09-07 thomas free(tmp);
1071 55144267 2022-09-07 thomas if (r == -1)
1072 55144267 2022-09-07 thomas return -1;
1073 55144267 2022-09-07 thomas sep = "&";
1074 55144267 2022-09-07 thomas }
1075 55144267 2022-09-07 thomas
1076 55144267 2022-09-07 thomas if (url->page != -1) {
1077 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%spage=%d", sep, url->page) == -1)
1078 55144267 2022-09-07 thomas return -1;
1079 55144267 2022-09-07 thomas sep = "&";
1080 55144267 2022-09-07 thomas }
1081 55144267 2022-09-07 thomas
1082 55144267 2022-09-07 thomas return 0;
1083 55144267 2022-09-07 thomas }
1084 55144267 2022-09-07 thomas
1085 55144267 2022-09-07 thomas int
1086 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1087 d6795e9f 2022-12-30 thomas {
1088 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1089 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1090 d6795e9f 2022-12-30 thomas
1091 d8bf4f25 2023-09-14 thomas if (tp_writes(tp, proto) == -1 ||
1092 d8bf4f25 2023-09-14 thomas tp_writes(tp, "://") == -1 ||
1093 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1094 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1095 d6795e9f 2022-12-30 thomas return -1;
1096 d6795e9f 2022-12-30 thomas
1097 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1098 55e6cffd 2022-09-01 thomas }
1099 55e6cffd 2022-09-01 thomas
1100 8a35f56c 2022-07-16 thomas static const struct got_error *
1101 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1102 8a35f56c 2022-07-16 thomas {
1103 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1104 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1105 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1106 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1107 8a35f56c 2022-07-16 thomas DIR *dt;
1108 8a35f56c 2022-07-16 thomas char *dir_test;
1109 8a35f56c 2022-07-16 thomas
1110 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1111 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1112 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1113 8a35f56c 2022-07-16 thomas
1114 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1115 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1116 8a35f56c 2022-07-16 thomas free(dir_test);
1117 c13168e4 2023-12-01 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1118 c13168e4 2023-12-01 thomas repo_dir->name) == -1)
1119 c13168e4 2023-12-01 thomas return got_error_from_errno("asprintf");
1120 c13168e4 2023-12-01 thomas dt = opendir(dir_test);
1121 c13168e4 2023-12-01 thomas if (dt == NULL) {
1122 c13168e4 2023-12-01 thomas free(dir_test);
1123 c13168e4 2023-12-01 thomas return got_error_path(repo_dir->name,
1124 c13168e4 2023-12-01 thomas GOT_ERR_NOT_GIT_REPO);
1125 c13168e4 2023-12-01 thomas }
1126 ab5bda7e 2023-11-22 thomas }
1127 8a35f56c 2022-07-16 thomas
1128 c13168e4 2023-12-01 thomas repo_dir->path = dir_test;
1129 c13168e4 2023-12-01 thomas dir_test = NULL;
1130 c2d3d9a0 2022-09-01 thomas
1131 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1132 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1133 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1134 3991b2a5 2022-10-31 thomas goto err;
1135 3991b2a5 2022-10-31 thomas }
1136 3991b2a5 2022-10-31 thomas
1137 ab5bda7e 2023-11-22 thomas error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1138 ab5bda7e 2023-11-22 thomas if (error)
1139 ab5bda7e 2023-11-22 thomas goto err;
1140 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1141 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1142 8a35f56c 2022-07-16 thomas if (error)
1143 8a35f56c 2022-07-16 thomas goto err;
1144 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1145 8a35f56c 2022-07-16 thomas if (error)
1146 8a35f56c 2022-07-16 thomas goto err;
1147 b38bef13 2023-08-12 thomas if (srv->show_repo_age) {
1148 b38bef13 2023-08-12 thomas error = got_get_repo_age(&repo_dir->age, c, NULL);
1149 b38bef13 2023-08-12 thomas if (error)
1150 b38bef13 2023-08-12 thomas goto err;
1151 b38bef13 2023-08-12 thomas }
1152 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1153 4606e6d4 2022-11-23 thomas dirfd(dt));
1154 8a35f56c 2022-07-16 thomas err:
1155 8a35f56c 2022-07-16 thomas free(dir_test);
1156 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1157 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1158 ab5bda7e 2023-11-22 thomas if (error && t->repo) {
1159 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
1160 ab5bda7e 2023-11-22 thomas t->repo = NULL;
1161 ab5bda7e 2023-11-22 thomas }
1162 8a35f56c 2022-07-16 thomas return error;
1163 8a35f56c 2022-07-16 thomas }
1164 8a35f56c 2022-07-16 thomas
1165 8a35f56c 2022-07-16 thomas static const struct got_error *
1166 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1167 8a35f56c 2022-07-16 thomas {
1168 8a35f56c 2022-07-16 thomas const struct got_error *error;
1169 8a35f56c 2022-07-16 thomas
1170 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1171 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1172 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1173 8a35f56c 2022-07-16 thomas
1174 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1175 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1176 8a35f56c 2022-07-16 thomas free(*repo_dir);
1177 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1178 8a35f56c 2022-07-16 thomas return error;
1179 8a35f56c 2022-07-16 thomas }
1180 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1181 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1182 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1183 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1184 8a35f56c 2022-07-16 thomas
1185 8a35f56c 2022-07-16 thomas return NULL;
1186 8a35f56c 2022-07-16 thomas }
1187 8a35f56c 2022-07-16 thomas
1188 8a35f56c 2022-07-16 thomas static const struct got_error *
1189 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1190 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1191 8a35f56c 2022-07-16 thomas {
1192 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1193 4606e6d4 2022-11-23 thomas struct stat sb;
1194 4606e6d4 2022-11-23 thomas int fd = -1;
1195 4606e6d4 2022-11-23 thomas off_t len;
1196 8a35f56c 2022-07-16 thomas
1197 8a35f56c 2022-07-16 thomas *description = NULL;
1198 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1199 8a35f56c 2022-07-16 thomas return NULL;
1200 8a35f56c 2022-07-16 thomas
1201 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1202 4606e6d4 2022-11-23 thomas if (fd == -1) {
1203 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1204 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1205 4606e6d4 2022-11-23 thomas dirpath, "description");
1206 4606e6d4 2022-11-23 thomas }
1207 8a35f56c 2022-07-16 thomas goto done;
1208 8a35f56c 2022-07-16 thomas }
1209 8a35f56c 2022-07-16 thomas
1210 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1211 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1212 4606e6d4 2022-11-23 thomas dirpath, "description");
1213 8a35f56c 2022-07-16 thomas goto done;
1214 8a35f56c 2022-07-16 thomas }
1215 8a35f56c 2022-07-16 thomas
1216 4606e6d4 2022-11-23 thomas len = sb.st_size;
1217 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1218 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1219 8a35f56c 2022-07-16 thomas
1220 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1221 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1222 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1223 8a35f56c 2022-07-16 thomas goto done;
1224 8a35f56c 2022-07-16 thomas }
1225 8a35f56c 2022-07-16 thomas
1226 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1227 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1228 8a35f56c 2022-07-16 thomas done:
1229 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1230 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1231 8a35f56c 2022-07-16 thomas return error;
1232 8a35f56c 2022-07-16 thomas }
1233 8a35f56c 2022-07-16 thomas
1234 8a35f56c 2022-07-16 thomas static const struct got_error *
1235 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1236 4606e6d4 2022-11-23 thomas int dir)
1237 8a35f56c 2022-07-16 thomas {
1238 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1239 4606e6d4 2022-11-23 thomas struct stat sb;
1240 4606e6d4 2022-11-23 thomas int fd = -1;
1241 4606e6d4 2022-11-23 thomas off_t len;
1242 8a35f56c 2022-07-16 thomas
1243 8a35f56c 2022-07-16 thomas *url = NULL;
1244 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1245 8a35f56c 2022-07-16 thomas return NULL;
1246 8a35f56c 2022-07-16 thomas
1247 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
1248 4606e6d4 2022-11-23 thomas if (fd == -1) {
1249 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1250 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1251 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1252 4606e6d4 2022-11-23 thomas }
1253 8a35f56c 2022-07-16 thomas goto done;
1254 8a35f56c 2022-07-16 thomas }
1255 8a35f56c 2022-07-16 thomas
1256 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1257 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1258 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1259 8a35f56c 2022-07-16 thomas goto done;
1260 8a35f56c 2022-07-16 thomas }
1261 8a35f56c 2022-07-16 thomas
1262 4606e6d4 2022-11-23 thomas len = sb.st_size;
1263 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1264 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
1265 8a35f56c 2022-07-16 thomas
1266 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
1267 8a35f56c 2022-07-16 thomas if (*url == NULL) {
1268 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1269 8a35f56c 2022-07-16 thomas goto done;
1270 8a35f56c 2022-07-16 thomas }
1271 8a35f56c 2022-07-16 thomas
1272 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
1273 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1274 8a35f56c 2022-07-16 thomas done:
1275 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1276 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1277 8a35f56c 2022-07-16 thomas return error;
1278 8a35f56c 2022-07-16 thomas }
1279 8a35f56c 2022-07-16 thomas
1280 53bf32b8 2023-01-23 thomas int
1281 4cc0851e 2023-10-08 thomas gotweb_render_age(struct template *tp, time_t committer_time)
1282 8a35f56c 2022-07-16 thomas {
1283 53bf32b8 2023-01-23 thomas struct request *c = tp->tp_arg;
1284 399ea8e4 2022-07-20 thomas long long diff_time;
1285 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
1286 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
1287 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
1288 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
1289 8a35f56c 2022-07-16 thomas
1290 4cc0851e 2023-10-08 thomas diff_time = time(NULL) - committer_time;
1291 4cc0851e 2023-10-08 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
1292 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1293 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
1294 53bf32b8 2023-01-23 thomas return -1;
1295 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1296 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1297 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
1298 4cc0851e 2023-10-08 thomas months) == -1)
1299 53bf32b8 2023-01-23 thomas return -1;
1300 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1301 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1302 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1303 53bf32b8 2023-01-23 thomas return -1;
1304 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
1305 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1306 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24), days) == -1)
1307 4cc0851e 2023-10-08 thomas return -1;
1308 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 2) {
1309 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1310 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60), hours) == -1)
1311 4cc0851e 2023-10-08 thomas return -1;
1312 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 2) {
1313 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1314 4cc0851e 2023-10-08 thomas minutes) == -1)
1315 4cc0851e 2023-10-08 thomas return -1;
1316 4cc0851e 2023-10-08 thomas } else if (diff_time > 2) {
1317 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s", diff_time,
1318 4cc0851e 2023-10-08 thomas seconds) == -1)
1319 4cc0851e 2023-10-08 thomas return -1;
1320 4cc0851e 2023-10-08 thomas } else {
1321 4cc0851e 2023-10-08 thomas if (tp_writes(tp, now) == -1)
1322 4cc0851e 2023-10-08 thomas return -1;
1323 8a35f56c 2022-07-16 thomas }
1324 53bf32b8 2023-01-23 thomas return 0;
1325 3e12c168 2022-07-16 thomas }