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 8a35f56c 2022-07-16 thomas
21 8a35f56c 2022-07-16 thomas #include <net/if.h>
22 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
23 3e12c168 2022-07-16 thomas #include <sys/queue.h>
24 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
26 8a35f56c 2022-07-16 thomas
27 2ac684a4 2022-09-03 thomas #include <ctype.h>
28 8a35f56c 2022-07-16 thomas #include <dirent.h>
29 8a35f56c 2022-07-16 thomas #include <errno.h>
30 8a35f56c 2022-07-16 thomas #include <event.h>
31 4606e6d4 2022-11-23 thomas #include <fcntl.h>
32 4606e6d4 2022-11-23 thomas #include <imsg.h>
33 8a35f56c 2022-07-16 thomas #include <stdio.h>
34 8a35f56c 2022-07-16 thomas #include <stdlib.h>
35 8a35f56c 2022-07-16 thomas #include <string.h>
36 8a35f56c 2022-07-16 thomas #include <unistd.h>
37 8a35f56c 2022-07-16 thomas
38 8a35f56c 2022-07-16 thomas #include "got_error.h"
39 8a35f56c 2022-07-16 thomas #include "got_object.h"
40 8a35f56c 2022-07-16 thomas #include "got_reference.h"
41 8a35f56c 2022-07-16 thomas #include "got_repository.h"
42 8a35f56c 2022-07-16 thomas #include "got_path.h"
43 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
44 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
45 8a35f56c 2022-07-16 thomas #include "got_diff.h"
46 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
47 8a35f56c 2022-07-16 thomas #include "got_blame.h"
48 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
49 cb11302c 2022-12-30 thomas
50 cb11302c 2022-12-30 thomas #include "got_compat.h"
51 8a35f56c 2022-07-16 thomas
52 8a35f56c 2022-07-16 thomas #include "proc.h"
53 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
54 d6795e9f 2022-12-30 thomas #include "tmpl.h"
55 ff36aeea 2022-07-16 thomas
56 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
57 8a35f56c 2022-07-16 thomas { "action", ACTION },
58 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
59 8a35f56c 2022-07-16 thomas { "file", RFILE },
60 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
61 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
62 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
63 8a35f56c 2022-07-16 thomas { "path", PATH },
64 8a35f56c 2022-07-16 thomas { "page", PAGE },
65 8a35f56c 2022-07-16 thomas };
66 8a35f56c 2022-07-16 thomas
67 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
68 8a35f56c 2022-07-16 thomas { "blame", BLAME },
69 8a35f56c 2022-07-16 thomas { "blob", BLOB },
70 b82440e1 2023-01-06 thomas { "blobraw", BLOBRAW },
71 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
72 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
73 8a35f56c 2022-07-16 thomas { "diff", DIFF },
74 8a35f56c 2022-07-16 thomas { "error", ERR },
75 8a35f56c 2022-07-16 thomas { "index", INDEX },
76 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
77 8a35f56c 2022-07-16 thomas { "tag", TAG },
78 8a35f56c 2022-07-16 thomas { "tags", TAGS },
79 8a35f56c 2022-07-16 thomas { "tree", TREE },
80 d6795e9f 2022-12-30 thomas { "rss", RSS },
81 8a35f56c 2022-07-16 thomas };
82 8a35f56c 2022-07-16 thomas
83 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
84 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 8a35f56c 2022-07-16 thomas char *);
86 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 8a35f56c 2022-07-16 thomas char *, char *);
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_index(struct request *);
89 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 8a35f56c 2022-07-16 thomas const char *);
91 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
92 8a35f56c 2022-07-16 thomas struct repo_dir *);
93 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
94 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
95 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 4606e6d4 2022-11-23 thomas const char *, int);
97 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_blame(struct request *);
98 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_summary(struct request *);
99 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tag(struct request *);
100 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tags(struct request *);
101 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_branches(struct request *);
102 e7e5fa49 2022-12-30 thomas
103 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
104 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
105 8a35f56c 2022-07-16 thomas
106 3f0158b4 2022-08-30 thomas struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 8a35f56c 2022-07-16 thomas
108 8a35f56c 2022-07-16 thomas void
109 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
110 8a35f56c 2022-07-16 thomas {
111 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL, *error2 = NULL;
112 b82440e1 2023-01-06 thomas struct got_blob_object *blob = NULL;
113 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
114 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
115 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
116 5d860bce 2023-01-07 thomas FILE *fp = NULL;
117 8a35f56c 2022-07-16 thomas uint8_t err[] = "gotwebd experienced an error: ";
118 b82440e1 2023-01-06 thomas int r, html = 0, fd = -1;
119 8a35f56c 2022-07-16 thomas
120 8a35f56c 2022-07-16 thomas /* init the transport */
121 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
122 8a35f56c 2022-07-16 thomas if (error) {
123 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
124 46aeda9a 2022-08-27 thomas return;
125 8a35f56c 2022-07-16 thomas }
126 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
127 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
128 8a35f56c 2022-07-16 thomas return;
129 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
130 3f0158b4 2022-08-30 thomas srv = gotweb_get_server(c->server_name, c->http_host);
131 8a35f56c 2022-07-16 thomas if (srv == NULL) {
132 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
133 8a35f56c 2022-07-16 thomas goto err;
134 8a35f56c 2022-07-16 thomas }
135 8a35f56c 2022-07-16 thomas c->srv = srv;
136 8a35f56c 2022-07-16 thomas /* parse our querystring */
137 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
138 8a35f56c 2022-07-16 thomas if (error) {
139 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
140 8a35f56c 2022-07-16 thomas goto err;
141 8a35f56c 2022-07-16 thomas }
142 8a35f56c 2022-07-16 thomas c->t->qs = qs;
143 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
144 8a35f56c 2022-07-16 thomas if (error) {
145 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
146 8a35f56c 2022-07-16 thomas goto err;
147 8a35f56c 2022-07-16 thomas }
148 8a35f56c 2022-07-16 thomas
149 8a35f56c 2022-07-16 thomas /*
150 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
151 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
152 8a35f56c 2022-07-16 thomas * querystring.
153 8a35f56c 2022-07-16 thomas */
154 8a35f56c 2022-07-16 thomas
155 b82440e1 2023-01-06 thomas if (qs->action == BLAME || qs->action == BLOB ||
156 b82440e1 2023-01-06 thomas qs->action == BLOBRAW || qs->action == DIFF) {
157 b82440e1 2023-01-06 thomas if (qs->commit == NULL) {
158 b82440e1 2023-01-06 thomas error2 = got_error(GOT_ERR_QUERYSTRING);
159 b82440e1 2023-01-06 thomas goto render;
160 b82440e1 2023-01-06 thomas }
161 8a35f56c 2022-07-16 thomas }
162 8a35f56c 2022-07-16 thomas
163 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
164 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
165 8a35f56c 2022-07-16 thomas if (error)
166 8a35f56c 2022-07-16 thomas goto done;
167 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
168 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
169 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
170 8a35f56c 2022-07-16 thomas goto err;
171 8a35f56c 2022-07-16 thomas }
172 8a35f56c 2022-07-16 thomas
173 b82440e1 2023-01-06 thomas if (qs->action == BLOBRAW) {
174 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
175 8a35f56c 2022-07-16 thomas if (error)
176 8a35f56c 2022-07-16 thomas goto done;
177 8a35f56c 2022-07-16 thomas error = got_output_file_blob(c);
178 d6795e9f 2022-12-30 thomas if (error) {
179 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
180 d6795e9f 2022-12-30 thomas goto err;
181 d6795e9f 2022-12-30 thomas }
182 d6795e9f 2022-12-30 thomas goto done;
183 b82440e1 2023-01-06 thomas }
184 b82440e1 2023-01-06 thomas
185 b82440e1 2023-01-06 thomas if (qs->action == BLOB) {
186 b82440e1 2023-01-06 thomas int binary;
187 b82440e1 2023-01-06 thomas struct gotweb_url url = {
188 b82440e1 2023-01-06 thomas .index_page = -1,
189 b82440e1 2023-01-06 thomas .page = -1,
190 b82440e1 2023-01-06 thomas .action = BLOBRAW,
191 b82440e1 2023-01-06 thomas .path = qs->path,
192 b82440e1 2023-01-06 thomas .commit = qs->commit,
193 b82440e1 2023-01-06 thomas .folder = qs->folder,
194 b82440e1 2023-01-06 thomas .file = qs->file,
195 b82440e1 2023-01-06 thomas };
196 b82440e1 2023-01-06 thomas
197 b82440e1 2023-01-06 thomas error = got_get_repo_commits(c, 1);
198 b82440e1 2023-01-06 thomas if (error)
199 b82440e1 2023-01-06 thomas goto done;
200 b82440e1 2023-01-06 thomas
201 b82440e1 2023-01-06 thomas error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
202 b82440e1 2023-01-06 thomas if (error2)
203 b82440e1 2023-01-06 thomas goto render;
204 b82440e1 2023-01-06 thomas if (binary) {
205 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "Status: 302\r\n");
206 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "Location: ");
207 b82440e1 2023-01-06 thomas gotweb_render_url(c, &url);
208 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "\r\n\r\n");
209 b82440e1 2023-01-06 thomas goto done;
210 b82440e1 2023-01-06 thomas }
211 d6795e9f 2022-12-30 thomas }
212 d6795e9f 2022-12-30 thomas
213 d6795e9f 2022-12-30 thomas if (qs->action == RSS) {
214 d7034a4e 2023-01-06 thomas error = gotweb_render_content_type_file(c,
215 d7034a4e 2023-01-06 thomas "application/rss+xml;charset=utf-8",
216 d7034a4e 2023-01-06 thomas repo_dir->name, ".rss");
217 d6795e9f 2022-12-30 thomas if (error) {
218 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
219 d6795e9f 2022-12-30 thomas goto err;
220 d6795e9f 2022-12-30 thomas }
221 d6795e9f 2022-12-30 thomas
222 d6795e9f 2022-12-30 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
223 8a35f56c 2022-07-16 thomas if (error) {
224 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
225 8a35f56c 2022-07-16 thomas goto err;
226 8a35f56c 2022-07-16 thomas }
227 d6795e9f 2022-12-30 thomas if (gotweb_render_rss(c->tp) == -1)
228 d6795e9f 2022-12-30 thomas goto err;
229 8a35f56c 2022-07-16 thomas goto done;
230 43a44bce 2022-12-04 thomas }
231 43a44bce 2022-12-04 thomas
232 43a44bce 2022-12-04 thomas render:
233 43a44bce 2022-12-04 thomas error = gotweb_render_content_type(c, "text/html");
234 43a44bce 2022-12-04 thomas if (error) {
235 43a44bce 2022-12-04 thomas log_warnx("%s: %s", __func__, error->msg);
236 43a44bce 2022-12-04 thomas goto err;
237 8a35f56c 2022-07-16 thomas }
238 43a44bce 2022-12-04 thomas html = 1;
239 8a35f56c 2022-07-16 thomas
240 e7e5fa49 2022-12-30 thomas if (gotweb_render_header(c->tp) == -1)
241 8a35f56c 2022-07-16 thomas goto err;
242 8a35f56c 2022-07-16 thomas
243 8a35f56c 2022-07-16 thomas if (error2) {
244 8a35f56c 2022-07-16 thomas error = error2;
245 8a35f56c 2022-07-16 thomas goto err;
246 8a35f56c 2022-07-16 thomas }
247 8a35f56c 2022-07-16 thomas
248 8a35f56c 2022-07-16 thomas switch(qs->action) {
249 8a35f56c 2022-07-16 thomas case BLAME:
250 8a35f56c 2022-07-16 thomas error = gotweb_render_blame(c);
251 8a35f56c 2022-07-16 thomas if (error) {
252 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
253 8a35f56c 2022-07-16 thomas goto err;
254 8a35f56c 2022-07-16 thomas }
255 b82440e1 2023-01-06 thomas break;
256 b82440e1 2023-01-06 thomas case BLOB:
257 b82440e1 2023-01-06 thomas if (gotweb_render_blob(c->tp, blob) == -1)
258 b82440e1 2023-01-06 thomas goto err;
259 8a35f56c 2022-07-16 thomas break;
260 8a35f56c 2022-07-16 thomas case BRIEFS:
261 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
262 8a35f56c 2022-07-16 thomas goto err;
263 8a35f56c 2022-07-16 thomas break;
264 8a35f56c 2022-07-16 thomas case COMMITS:
265 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
266 8a35f56c 2022-07-16 thomas if (error) {
267 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
268 8a35f56c 2022-07-16 thomas goto err;
269 8a35f56c 2022-07-16 thomas }
270 7ade8b27 2022-12-30 thomas if (gotweb_render_commits(c->tp) == -1)
271 7ade8b27 2022-12-30 thomas goto err;
272 8a35f56c 2022-07-16 thomas break;
273 8a35f56c 2022-07-16 thomas case DIFF:
274 5d860bce 2023-01-07 thomas error = got_get_repo_commits(c, 1);
275 8a35f56c 2022-07-16 thomas if (error) {
276 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
277 8a35f56c 2022-07-16 thomas goto err;
278 8a35f56c 2022-07-16 thomas }
279 5d860bce 2023-01-07 thomas error = got_open_diff_for_output(&fp, &fd, c);
280 5d860bce 2023-01-07 thomas if (error) {
281 5d860bce 2023-01-07 thomas log_warnx("%s: %s", __func__, error->msg);
282 5d860bce 2023-01-07 thomas goto err;
283 5d860bce 2023-01-07 thomas }
284 5d860bce 2023-01-07 thomas if (gotweb_render_diff(c->tp, fp) == -1)
285 5d860bce 2023-01-07 thomas goto err;
286 8a35f56c 2022-07-16 thomas break;
287 8a35f56c 2022-07-16 thomas case INDEX:
288 8a35f56c 2022-07-16 thomas error = gotweb_render_index(c);
289 8a35f56c 2022-07-16 thomas if (error) {
290 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
291 8a35f56c 2022-07-16 thomas goto err;
292 8a35f56c 2022-07-16 thomas }
293 8a35f56c 2022-07-16 thomas break;
294 8a35f56c 2022-07-16 thomas case SUMMARY:
295 8a35f56c 2022-07-16 thomas error = gotweb_render_summary(c);
296 8a35f56c 2022-07-16 thomas if (error) {
297 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
298 8a35f56c 2022-07-16 thomas goto err;
299 8a35f56c 2022-07-16 thomas }
300 8a35f56c 2022-07-16 thomas break;
301 8a35f56c 2022-07-16 thomas case TAG:
302 8a35f56c 2022-07-16 thomas error = gotweb_render_tag(c);
303 8a35f56c 2022-07-16 thomas if (error) {
304 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
305 8a35f56c 2022-07-16 thomas goto err;
306 8a35f56c 2022-07-16 thomas }
307 8a35f56c 2022-07-16 thomas break;
308 8a35f56c 2022-07-16 thomas case TAGS:
309 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
310 8a35f56c 2022-07-16 thomas if (error) {
311 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
312 8a35f56c 2022-07-16 thomas goto err;
313 8a35f56c 2022-07-16 thomas }
314 8a35f56c 2022-07-16 thomas break;
315 8a35f56c 2022-07-16 thomas case TREE:
316 3c14c1f2 2023-01-06 thomas error = got_get_repo_commits(c, 1);
317 8a35f56c 2022-07-16 thomas if (error) {
318 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
319 8a35f56c 2022-07-16 thomas goto err;
320 8a35f56c 2022-07-16 thomas }
321 3c14c1f2 2023-01-06 thomas if (gotweb_render_tree(c->tp) == -1)
322 3c14c1f2 2023-01-06 thomas goto err;
323 8a35f56c 2022-07-16 thomas break;
324 8a35f56c 2022-07-16 thomas case ERR:
325 8a35f56c 2022-07-16 thomas default:
326 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
327 79393471 2022-08-27 thomas "Erorr: Bad Querystring");
328 79393471 2022-08-27 thomas if (r == -1)
329 8a35f56c 2022-07-16 thomas goto err;
330 8a35f56c 2022-07-16 thomas break;
331 8a35f56c 2022-07-16 thomas }
332 8a35f56c 2022-07-16 thomas
333 8a35f56c 2022-07-16 thomas goto done;
334 8a35f56c 2022-07-16 thomas err:
335 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
336 8a35f56c 2022-07-16 thomas return;
337 a5f25a12 2022-10-31 thomas if (fcgi_printf(c, "\n%s", err) == -1)
338 8a35f56c 2022-07-16 thomas return;
339 8a35f56c 2022-07-16 thomas if (error) {
340 79393471 2022-08-27 thomas if (fcgi_printf(c, "%s", error->msg) == -1)
341 8a35f56c 2022-07-16 thomas return;
342 8a35f56c 2022-07-16 thomas } else {
343 79393471 2022-08-27 thomas if (fcgi_printf(c, "see daemon logs for details") == -1)
344 8a35f56c 2022-07-16 thomas return;
345 8a35f56c 2022-07-16 thomas }
346 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "</div>\n") == -1)
347 8a35f56c 2022-07-16 thomas return;
348 8a35f56c 2022-07-16 thomas done:
349 b82440e1 2023-01-06 thomas if (blob)
350 b82440e1 2023-01-06 thomas got_object_blob_close(blob);
351 5d860bce 2023-01-07 thomas if (fp) {
352 5d860bce 2023-01-07 thomas error = got_gotweb_flushfile(fp, fd);
353 5d860bce 2023-01-07 thomas if (error)
354 5d860bce 2023-01-07 thomas log_warnx("%s: got_gotweb_flushfile failure: %s",
355 5d860bce 2023-01-07 thomas __func__, error->msg);
356 5d860bce 2023-01-07 thomas fd = -1;
357 5d860bce 2023-01-07 thomas }
358 b82440e1 2023-01-06 thomas if (fd != -1)
359 b82440e1 2023-01-06 thomas close(fd);
360 8a35f56c 2022-07-16 thomas if (html && srv != NULL)
361 e7e5fa49 2022-12-30 thomas gotweb_render_footer(c->tp);
362 8a35f56c 2022-07-16 thomas }
363 8a35f56c 2022-07-16 thomas
364 8a35f56c 2022-07-16 thomas struct server *
365 3f0158b4 2022-08-30 thomas gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
366 8a35f56c 2022-07-16 thomas {
367 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
368 8a35f56c 2022-07-16 thomas
369 3f0158b4 2022-08-30 thomas /* check against the server name first */
370 8a35f56c 2022-07-16 thomas if (strlen(server_name) > 0)
371 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
372 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
373 8a35f56c 2022-07-16 thomas goto done;
374 8a35f56c 2022-07-16 thomas
375 3f0158b4 2022-08-30 thomas /* check against subdomain second */
376 8a35f56c 2022-07-16 thomas if (strlen(subdomain) > 0)
377 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
378 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, subdomain) == 0)
379 8a35f56c 2022-07-16 thomas goto done;
380 8a35f56c 2022-07-16 thomas
381 8a35f56c 2022-07-16 thomas /* if those fail, send first server */
382 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
383 8a35f56c 2022-07-16 thomas if (srv != NULL)
384 8a35f56c 2022-07-16 thomas break;
385 8a35f56c 2022-07-16 thomas done:
386 8a35f56c 2022-07-16 thomas return srv;
387 8a35f56c 2022-07-16 thomas };
388 8a35f56c 2022-07-16 thomas
389 8a35f56c 2022-07-16 thomas const struct got_error *
390 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
391 8a35f56c 2022-07-16 thomas {
392 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
393 8a35f56c 2022-07-16 thomas
394 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
395 8a35f56c 2022-07-16 thomas if (*t == NULL)
396 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
397 8a35f56c 2022-07-16 thomas
398 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
399 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
400 8a35f56c 2022-07-16 thomas
401 8a35f56c 2022-07-16 thomas (*t)->repo = NULL;
402 8a35f56c 2022-07-16 thomas (*t)->repo_dir = NULL;
403 8a35f56c 2022-07-16 thomas (*t)->qs = NULL;
404 8a35f56c 2022-07-16 thomas (*t)->next_id = NULL;
405 8a35f56c 2022-07-16 thomas (*t)->prev_id = NULL;
406 8a35f56c 2022-07-16 thomas (*t)->next_disp = 0;
407 8a35f56c 2022-07-16 thomas (*t)->prev_disp = 0;
408 8a35f56c 2022-07-16 thomas
409 8a35f56c 2022-07-16 thomas return error;
410 8a35f56c 2022-07-16 thomas }
411 8a35f56c 2022-07-16 thomas
412 8a35f56c 2022-07-16 thomas static const struct got_error *
413 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
414 8a35f56c 2022-07-16 thomas {
415 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
416 8a35f56c 2022-07-16 thomas
417 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
418 8a35f56c 2022-07-16 thomas if (*qs == NULL)
419 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
420 8a35f56c 2022-07-16 thomas
421 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
422 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
423 84630254 2022-09-02 thomas free(*qs);
424 84630254 2022-09-02 thomas *qs = NULL;
425 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
426 8a35f56c 2022-07-16 thomas }
427 84630254 2022-09-02 thomas
428 84630254 2022-09-02 thomas (*qs)->action = INDEX;
429 84630254 2022-09-02 thomas (*qs)->commit = NULL;
430 84630254 2022-09-02 thomas (*qs)->file = NULL;
431 84630254 2022-09-02 thomas (*qs)->folder = NULL;
432 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
433 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
434 8a35f56c 2022-07-16 thomas
435 8a35f56c 2022-07-16 thomas return error;
436 8a35f56c 2022-07-16 thomas }
437 8a35f56c 2022-07-16 thomas
438 8a35f56c 2022-07-16 thomas static const struct got_error *
439 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
440 8a35f56c 2022-07-16 thomas {
441 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
442 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
443 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
444 8a35f56c 2022-07-16 thomas
445 8a35f56c 2022-07-16 thomas if (qst == NULL)
446 8a35f56c 2022-07-16 thomas return error;
447 8a35f56c 2022-07-16 thomas
448 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
449 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
450 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
451 8a35f56c 2022-07-16 thomas
452 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
453 8a35f56c 2022-07-16 thomas tok1_end = tok1;
454 8a35f56c 2022-07-16 thomas
455 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
456 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
457 8a35f56c 2022-07-16 thomas
458 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
459 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
460 8a35f56c 2022-07-16 thomas free(tok1);
461 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
462 8a35f56c 2022-07-16 thomas }
463 8a35f56c 2022-07-16 thomas
464 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
465 8a35f56c 2022-07-16 thomas tok2_end = tok2;
466 8a35f56c 2022-07-16 thomas
467 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
468 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
469 8a35f56c 2022-07-16 thomas if (tok2_end) {
470 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
471 8a35f56c 2022-07-16 thomas tok2_end);
472 8a35f56c 2022-07-16 thomas if (error)
473 8a35f56c 2022-07-16 thomas goto err;
474 8a35f56c 2022-07-16 thomas }
475 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
476 8a35f56c 2022-07-16 thomas }
477 8a35f56c 2022-07-16 thomas free(tok2);
478 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
479 8a35f56c 2022-07-16 thomas }
480 8a35f56c 2022-07-16 thomas free(tok1);
481 8a35f56c 2022-07-16 thomas return error;
482 8a35f56c 2022-07-16 thomas err:
483 8a35f56c 2022-07-16 thomas free(tok2);
484 8a35f56c 2022-07-16 thomas free(tok1);
485 8a35f56c 2022-07-16 thomas return error;
486 8a35f56c 2022-07-16 thomas }
487 8a35f56c 2022-07-16 thomas
488 2ac684a4 2022-09-03 thomas /*
489 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
490 2ac684a4 2022-09-03 thomas */
491 8a35f56c 2022-07-16 thomas static const struct got_error *
492 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
493 2ac684a4 2022-09-03 thomas {
494 2ac684a4 2022-09-03 thomas char *p, *q;
495 2ac684a4 2022-09-03 thomas char hex[3];
496 2ac684a4 2022-09-03 thomas unsigned long x;
497 2ac684a4 2022-09-03 thomas
498 2ac684a4 2022-09-03 thomas hex[2] = '\0';
499 2ac684a4 2022-09-03 thomas p = q = url;
500 2ac684a4 2022-09-03 thomas
501 2ac684a4 2022-09-03 thomas while (*p != '\0') {
502 2ac684a4 2022-09-03 thomas switch (*p) {
503 2ac684a4 2022-09-03 thomas case '%':
504 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
505 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
506 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
507 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
508 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
509 2ac684a4 2022-09-03 thomas
510 2ac684a4 2022-09-03 thomas hex[0] = p[1];
511 2ac684a4 2022-09-03 thomas hex[1] = p[2];
512 2ac684a4 2022-09-03 thomas
513 2ac684a4 2022-09-03 thomas /*
514 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
515 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
516 2ac684a4 2022-09-03 thomas */
517 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
518 2ac684a4 2022-09-03 thomas *q = (char)x;
519 2ac684a4 2022-09-03 thomas p += 2;
520 2ac684a4 2022-09-03 thomas break;
521 2ac684a4 2022-09-03 thomas default:
522 2ac684a4 2022-09-03 thomas *q = *p;
523 2ac684a4 2022-09-03 thomas break;
524 2ac684a4 2022-09-03 thomas }
525 2ac684a4 2022-09-03 thomas p++;
526 2ac684a4 2022-09-03 thomas q++;
527 2ac684a4 2022-09-03 thomas }
528 2ac684a4 2022-09-03 thomas *q = '\0';
529 2ac684a4 2022-09-03 thomas
530 2ac684a4 2022-09-03 thomas return NULL;
531 2ac684a4 2022-09-03 thomas }
532 2ac684a4 2022-09-03 thomas
533 2ac684a4 2022-09-03 thomas static const struct got_error *
534 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
535 8a35f56c 2022-07-16 thomas {
536 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
537 8a35f56c 2022-07-16 thomas const char *errstr;
538 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
539 8a35f56c 2022-07-16 thomas
540 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
541 2ac684a4 2022-09-03 thomas if (error)
542 2ac684a4 2022-09-03 thomas return error;
543 2ac684a4 2022-09-03 thomas
544 8a35f56c 2022-07-16 thomas for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
545 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
546 8a35f56c 2022-07-16 thomas continue;
547 8a35f56c 2022-07-16 thomas
548 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
549 8a35f56c 2022-07-16 thomas case ACTION:
550 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
551 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
552 8a35f56c 2022-07-16 thomas continue;
553 8a35f56c 2022-07-16 thomas else if (strcmp(value,
554 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
555 8a35f56c 2022-07-16 thomas (*qs)->action =
556 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
557 8a35f56c 2022-07-16 thomas goto qa_found;
558 8a35f56c 2022-07-16 thomas }
559 8a35f56c 2022-07-16 thomas }
560 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
561 8a35f56c 2022-07-16 thomas qa_found:
562 8a35f56c 2022-07-16 thomas break;
563 8a35f56c 2022-07-16 thomas case COMMIT:
564 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
565 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
566 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
567 8a35f56c 2022-07-16 thomas __func__);
568 8a35f56c 2022-07-16 thomas goto done;
569 8a35f56c 2022-07-16 thomas }
570 8a35f56c 2022-07-16 thomas break;
571 8a35f56c 2022-07-16 thomas case RFILE:
572 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
573 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
574 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
575 8a35f56c 2022-07-16 thomas __func__);
576 8a35f56c 2022-07-16 thomas goto done;
577 8a35f56c 2022-07-16 thomas }
578 8a35f56c 2022-07-16 thomas break;
579 8a35f56c 2022-07-16 thomas case FOLDER:
580 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
581 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
582 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
583 8a35f56c 2022-07-16 thomas __func__);
584 8a35f56c 2022-07-16 thomas goto done;
585 8a35f56c 2022-07-16 thomas }
586 8a35f56c 2022-07-16 thomas break;
587 8a35f56c 2022-07-16 thomas case HEADREF:
588 b0764984 2022-09-02 thomas free((*qs)->headref);
589 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
590 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
591 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
592 8a35f56c 2022-07-16 thomas __func__);
593 8a35f56c 2022-07-16 thomas goto done;
594 8a35f56c 2022-07-16 thomas }
595 8a35f56c 2022-07-16 thomas break;
596 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
597 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
598 8a35f56c 2022-07-16 thomas break;
599 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
600 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
601 8a35f56c 2022-07-16 thomas if (errstr) {
602 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
603 8a35f56c 2022-07-16 thomas __func__, errstr);
604 8a35f56c 2022-07-16 thomas goto done;
605 8a35f56c 2022-07-16 thomas }
606 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
607 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
608 8a35f56c 2022-07-16 thomas break;
609 8a35f56c 2022-07-16 thomas case PATH:
610 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
611 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
612 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
613 8a35f56c 2022-07-16 thomas __func__);
614 8a35f56c 2022-07-16 thomas goto done;
615 8a35f56c 2022-07-16 thomas }
616 8a35f56c 2022-07-16 thomas break;
617 8a35f56c 2022-07-16 thomas case PAGE:
618 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
619 8a35f56c 2022-07-16 thomas break;
620 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
621 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
622 8a35f56c 2022-07-16 thomas if (errstr) {
623 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
624 8a35f56c 2022-07-16 thomas __func__, errstr);
625 8a35f56c 2022-07-16 thomas goto done;
626 8a35f56c 2022-07-16 thomas }
627 3d6d1fb0 2022-12-30 thomas if ((*qs)->page < 0)
628 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
629 8a35f56c 2022-07-16 thomas break;
630 8a35f56c 2022-07-16 thomas default:
631 8a35f56c 2022-07-16 thomas break;
632 8a35f56c 2022-07-16 thomas }
633 8a35f56c 2022-07-16 thomas }
634 8a35f56c 2022-07-16 thomas done:
635 8a35f56c 2022-07-16 thomas return error;
636 8a35f56c 2022-07-16 thomas }
637 8a35f56c 2022-07-16 thomas
638 8a35f56c 2022-07-16 thomas void
639 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
640 8a35f56c 2022-07-16 thomas {
641 8a35f56c 2022-07-16 thomas if (rt != NULL) {
642 8a35f56c 2022-07-16 thomas free(rt->commit_id);
643 217813df 2022-09-02 thomas free(rt->tag_name);
644 217813df 2022-09-02 thomas free(rt->tag_commit);
645 217813df 2022-09-02 thomas free(rt->commit_msg);
646 8a35f56c 2022-07-16 thomas free(rt->tagger);
647 8a35f56c 2022-07-16 thomas }
648 8a35f56c 2022-07-16 thomas free(rt);
649 8a35f56c 2022-07-16 thomas }
650 8a35f56c 2022-07-16 thomas
651 8a35f56c 2022-07-16 thomas void
652 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
653 8a35f56c 2022-07-16 thomas {
654 8a35f56c 2022-07-16 thomas if (rc != NULL) {
655 8a35f56c 2022-07-16 thomas free(rc->path);
656 8a35f56c 2022-07-16 thomas free(rc->refs_str);
657 8a35f56c 2022-07-16 thomas free(rc->commit_id);
658 8a35f56c 2022-07-16 thomas free(rc->parent_id);
659 8a35f56c 2022-07-16 thomas free(rc->tree_id);
660 8a35f56c 2022-07-16 thomas free(rc->author);
661 8a35f56c 2022-07-16 thomas free(rc->committer);
662 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
663 8a35f56c 2022-07-16 thomas }
664 8a35f56c 2022-07-16 thomas free(rc);
665 8a35f56c 2022-07-16 thomas }
666 8a35f56c 2022-07-16 thomas
667 8a35f56c 2022-07-16 thomas static void
668 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
669 8a35f56c 2022-07-16 thomas {
670 8a35f56c 2022-07-16 thomas if (qs != NULL) {
671 8a35f56c 2022-07-16 thomas free(qs->commit);
672 8a35f56c 2022-07-16 thomas free(qs->file);
673 8a35f56c 2022-07-16 thomas free(qs->folder);
674 8a35f56c 2022-07-16 thomas free(qs->headref);
675 8a35f56c 2022-07-16 thomas free(qs->path);
676 8a35f56c 2022-07-16 thomas }
677 8a35f56c 2022-07-16 thomas free(qs);
678 8a35f56c 2022-07-16 thomas }
679 8a35f56c 2022-07-16 thomas
680 8a35f56c 2022-07-16 thomas static void
681 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
682 8a35f56c 2022-07-16 thomas {
683 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
684 8a35f56c 2022-07-16 thomas free(repo_dir->name);
685 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
686 8a35f56c 2022-07-16 thomas free(repo_dir->description);
687 8a35f56c 2022-07-16 thomas free(repo_dir->url);
688 8a35f56c 2022-07-16 thomas free(repo_dir->age);
689 8a35f56c 2022-07-16 thomas free(repo_dir->path);
690 8a35f56c 2022-07-16 thomas }
691 8a35f56c 2022-07-16 thomas free(repo_dir);
692 8a35f56c 2022-07-16 thomas }
693 8a35f56c 2022-07-16 thomas
694 8a35f56c 2022-07-16 thomas void
695 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
696 8a35f56c 2022-07-16 thomas {
697 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
698 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
699 8a35f56c 2022-07-16 thomas
700 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
701 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
702 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
703 8a35f56c 2022-07-16 thomas }
704 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
705 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
706 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
707 8a35f56c 2022-07-16 thomas }
708 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
709 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
710 6b42af1e 2022-09-02 thomas free(t->next_id);
711 6b42af1e 2022-09-02 thomas free(t->prev_id);
712 8a35f56c 2022-07-16 thomas free(t);
713 8a35f56c 2022-07-16 thomas }
714 8a35f56c 2022-07-16 thomas
715 8a35f56c 2022-07-16 thomas const struct got_error *
716 0cef9478 2023-01-06 thomas gotweb_render_content_type(struct request *c, const char *type)
717 8a35f56c 2022-07-16 thomas {
718 0b75e088 2022-08-27 thomas const char *csp = "default-src 'self'; script-src 'none'; "
719 0b75e088 2022-08-27 thomas "object-src 'none';";
720 0b75e088 2022-08-27 thomas
721 0b75e088 2022-08-27 thomas fcgi_printf(c,
722 0b75e088 2022-08-27 thomas "Content-Security-Policy: %s\r\n"
723 0b75e088 2022-08-27 thomas "Content-Type: %s\r\n\r\n",
724 0b75e088 2022-08-27 thomas csp, type);
725 79393471 2022-08-27 thomas return NULL;
726 8a35f56c 2022-07-16 thomas }
727 8a35f56c 2022-07-16 thomas
728 8a35f56c 2022-07-16 thomas const struct got_error *
729 c7c38295 2023-01-06 thomas gotweb_render_content_type_file(struct request *c, const char *type,
730 d7034a4e 2023-01-06 thomas const char *file, const char *suffix)
731 8a35f56c 2022-07-16 thomas {
732 79393471 2022-08-27 thomas fcgi_printf(c, "Content-type: %s\r\n"
733 d7034a4e 2023-01-06 thomas "Content-disposition: attachment; filename=%s%s\r\n\r\n",
734 d7034a4e 2023-01-06 thomas type, file, suffix ? suffix : "");
735 79393471 2022-08-27 thomas return NULL;
736 8a35f56c 2022-07-16 thomas }
737 8a35f56c 2022-07-16 thomas
738 2f4f0731 2022-12-30 thomas void
739 2f4f0731 2022-12-30 thomas gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
740 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
741 8a35f56c 2022-07-16 thomas {
742 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
743 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
744 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
745 8a35f56c 2022-07-16 thomas
746 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
747 8a35f56c 2022-07-16 thomas
748 8a35f56c 2022-07-16 thomas switch(qs->action) {
749 8a35f56c 2022-07-16 thomas case INDEX:
750 8a35f56c 2022-07-16 thomas if (qs->index_page > 0) {
751 2f4f0731 2022-12-30 thomas *have_prev = 1;
752 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
753 55144267 2022-09-07 thomas .action = -1,
754 55144267 2022-09-07 thomas .index_page = qs->index_page - 1,
755 55144267 2022-09-07 thomas .page = -1,
756 55144267 2022-09-07 thomas };
757 8a35f56c 2022-07-16 thomas }
758 2f4f0731 2022-12-30 thomas if (t->next_disp == srv->max_repos_display &&
759 2f4f0731 2022-12-30 thomas t->repos_total != (qs->index_page + 1) *
760 2f4f0731 2022-12-30 thomas srv->max_repos_display) {
761 2f4f0731 2022-12-30 thomas *have_next = 1;
762 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
763 2f4f0731 2022-12-30 thomas .action = -1,
764 2f4f0731 2022-12-30 thomas .index_page = qs->index_page + 1,
765 2f4f0731 2022-12-30 thomas .page = -1,
766 2f4f0731 2022-12-30 thomas };
767 2f4f0731 2022-12-30 thomas }
768 8a35f56c 2022-07-16 thomas break;
769 8a35f56c 2022-07-16 thomas case BRIEFS:
770 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
771 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
772 2f4f0731 2022-12-30 thomas *have_prev = 1;
773 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
774 55144267 2022-09-07 thomas .action = BRIEFS,
775 55144267 2022-09-07 thomas .index_page = -1,
776 55144267 2022-09-07 thomas .page = qs->page - 1,
777 55144267 2022-09-07 thomas .path = qs->path,
778 55144267 2022-09-07 thomas .commit = t->prev_id,
779 55144267 2022-09-07 thomas .headref = qs->headref,
780 55144267 2022-09-07 thomas };
781 8a35f56c 2022-07-16 thomas }
782 2f4f0731 2022-12-30 thomas if (t->next_id) {
783 2f4f0731 2022-12-30 thomas *have_next = 1;
784 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
785 2f4f0731 2022-12-30 thomas .action = BRIEFS,
786 2f4f0731 2022-12-30 thomas .index_page = -1,
787 2f4f0731 2022-12-30 thomas .page = qs->page + 1,
788 2f4f0731 2022-12-30 thomas .path = qs->path,
789 2f4f0731 2022-12-30 thomas .commit = t->next_id,
790 2f4f0731 2022-12-30 thomas .headref = qs->headref,
791 2f4f0731 2022-12-30 thomas };
792 2f4f0731 2022-12-30 thomas }
793 8a35f56c 2022-07-16 thomas break;
794 8a35f56c 2022-07-16 thomas case COMMITS:
795 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
796 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
797 2f4f0731 2022-12-30 thomas *have_prev = 1;
798 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
799 8ea2e76e 2022-12-30 thomas .action = COMMITS,
800 55144267 2022-09-07 thomas .index_page = -1,
801 55144267 2022-09-07 thomas .page = qs->page - 1,
802 55144267 2022-09-07 thomas .path = qs->path,
803 55144267 2022-09-07 thomas .commit = t->prev_id,
804 55144267 2022-09-07 thomas .headref = qs->headref,
805 55144267 2022-09-07 thomas .folder = qs->folder,
806 55144267 2022-09-07 thomas .file = qs->file,
807 55144267 2022-09-07 thomas };
808 8a35f56c 2022-07-16 thomas }
809 2f4f0731 2022-12-30 thomas if (t->next_id) {
810 2f4f0731 2022-12-30 thomas *have_next = 1;
811 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
812 8ea2e76e 2022-12-30 thomas .action = COMMITS,
813 55144267 2022-09-07 thomas .index_page = -1,
814 55144267 2022-09-07 thomas .page = qs->page + 1,
815 55144267 2022-09-07 thomas .path = qs->path,
816 55144267 2022-09-07 thomas .commit = t->next_id,
817 55144267 2022-09-07 thomas .headref = qs->headref,
818 55144267 2022-09-07 thomas .folder = qs->folder,
819 55144267 2022-09-07 thomas .file = qs->file,
820 55144267 2022-09-07 thomas };
821 8a35f56c 2022-07-16 thomas }
822 8a35f56c 2022-07-16 thomas break;
823 8a35f56c 2022-07-16 thomas case TAGS:
824 2f4f0731 2022-12-30 thomas if (t->prev_id && qs->commit != NULL &&
825 2f4f0731 2022-12-30 thomas strcmp(qs->commit, t->prev_id) != 0) {
826 2f4f0731 2022-12-30 thomas *have_prev = 1;
827 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
828 2f4f0731 2022-12-30 thomas .action = TAGS,
829 2f4f0731 2022-12-30 thomas .index_page = -1,
830 2f4f0731 2022-12-30 thomas .page = qs->page - 1,
831 2f4f0731 2022-12-30 thomas .path = qs->path,
832 2f4f0731 2022-12-30 thomas .commit = t->prev_id,
833 2f4f0731 2022-12-30 thomas .headref = qs->headref,
834 2f4f0731 2022-12-30 thomas };
835 2f4f0731 2022-12-30 thomas }
836 8a35f56c 2022-07-16 thomas if (t->next_id) {
837 2f4f0731 2022-12-30 thomas *have_next = 1;
838 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
839 55144267 2022-09-07 thomas .action = TAGS,
840 55144267 2022-09-07 thomas .index_page = -1,
841 55144267 2022-09-07 thomas .page = qs->page + 1,
842 55144267 2022-09-07 thomas .path = qs->path,
843 55144267 2022-09-07 thomas .commit = t->next_id,
844 55144267 2022-09-07 thomas .headref = qs->headref,
845 55144267 2022-09-07 thomas };
846 8a35f56c 2022-07-16 thomas }
847 8a35f56c 2022-07-16 thomas break;
848 8a35f56c 2022-07-16 thomas }
849 8a35f56c 2022-07-16 thomas }
850 8a35f56c 2022-07-16 thomas
851 8a35f56c 2022-07-16 thomas static const struct got_error *
852 8a35f56c 2022-07-16 thomas gotweb_render_index(struct request *c)
853 8a35f56c 2022-07-16 thomas {
854 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
855 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
856 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
857 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
858 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
859 8a35f56c 2022-07-16 thomas DIR *d;
860 f530ab0e 2022-09-02 thomas struct dirent **sd_dent = NULL;
861 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
862 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
863 e7e5fa49 2022-12-30 thomas int type;
864 8a35f56c 2022-07-16 thomas
865 8a35f56c 2022-07-16 thomas d = opendir(srv->repos_path);
866 8a35f56c 2022-07-16 thomas if (d == NULL) {
867 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("opendir", srv->repos_path);
868 8a35f56c 2022-07-16 thomas return error;
869 8a35f56c 2022-07-16 thomas }
870 8a35f56c 2022-07-16 thomas
871 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
872 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
873 f530ab0e 2022-09-02 thomas sd_dent = NULL;
874 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
875 8a35f56c 2022-07-16 thomas goto done;
876 8a35f56c 2022-07-16 thomas }
877 8a35f56c 2022-07-16 thomas
878 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
879 8a35f56c 2022-07-16 thomas goto done;
880 79393471 2022-08-27 thomas
881 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
882 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
883 57e88d7c 2022-11-23 thomas break;
884 8a35f56c 2022-07-16 thomas
885 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
886 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
887 24240f6a 2022-11-23 thomas d_skipped++;
888 24240f6a 2022-11-23 thomas continue;
889 24240f6a 2022-11-23 thomas }
890 24240f6a 2022-11-23 thomas
891 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
892 24240f6a 2022-11-23 thomas sd_dent[d_i]);
893 24240f6a 2022-11-23 thomas if (error)
894 24240f6a 2022-11-23 thomas goto done;
895 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
896 24240f6a 2022-11-23 thomas d_skipped++;
897 8a35f56c 2022-07-16 thomas continue;
898 24240f6a 2022-11-23 thomas }
899 8a35f56c 2022-07-16 thomas
900 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
901 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
902 8a35f56c 2022-07-16 thomas t->prev_disp++;
903 8a35f56c 2022-07-16 thomas continue;
904 8a35f56c 2022-07-16 thomas }
905 8a35f56c 2022-07-16 thomas
906 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
907 8a35f56c 2022-07-16 thomas if (error)
908 8a35f56c 2022-07-16 thomas goto done;
909 8a35f56c 2022-07-16 thomas
910 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
911 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
912 8a35f56c 2022-07-16 thomas error = NULL;
913 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
914 8a35f56c 2022-07-16 thomas repo_dir = NULL;
915 24240f6a 2022-11-23 thomas d_skipped++;
916 8a35f56c 2022-07-16 thomas continue;
917 8a35f56c 2022-07-16 thomas }
918 24240f6a 2022-11-23 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
919 24240f6a 2022-11-23 thomas goto done;
920 24240f6a 2022-11-23 thomas
921 8a35f56c 2022-07-16 thomas d_disp++;
922 8a35f56c 2022-07-16 thomas t->prev_disp++;
923 8a35f56c 2022-07-16 thomas
924 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
925 55144267 2022-09-07 thomas goto done;
926 55144267 2022-09-07 thomas
927 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
928 8a35f56c 2022-07-16 thomas repo_dir = NULL;
929 8a35f56c 2022-07-16 thomas t->next_disp++;
930 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
931 8a35f56c 2022-07-16 thomas break;
932 8a35f56c 2022-07-16 thomas }
933 24240f6a 2022-11-23 thomas t->repos_total = d_cnt - d_skipped;
934 24240f6a 2022-11-23 thomas
935 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
936 79393471 2022-08-27 thomas goto done;
937 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
938 79393471 2022-08-27 thomas goto done;
939 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
940 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
941 79393471 2022-08-27 thomas goto done;
942 8a35f56c 2022-07-16 thomas
943 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
944 8a35f56c 2022-07-16 thomas goto done;
945 8a35f56c 2022-07-16 thomas done:
946 f530ab0e 2022-09-02 thomas if (sd_dent) {
947 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
948 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
949 f530ab0e 2022-09-02 thomas free(sd_dent);
950 f530ab0e 2022-09-02 thomas }
951 8a35f56c 2022-07-16 thomas if (d != NULL && closedir(d) == EOF && error == NULL)
952 8a35f56c 2022-07-16 thomas error = got_error_from_errno("closedir");
953 8a35f56c 2022-07-16 thomas return error;
954 8a35f56c 2022-07-16 thomas }
955 8a35f56c 2022-07-16 thomas
956 8a35f56c 2022-07-16 thomas static const struct got_error *
957 8a35f56c 2022-07-16 thomas gotweb_render_blame(struct request *c)
958 8a35f56c 2022-07-16 thomas {
959 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
960 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
961 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
962 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
963 79393471 2022-08-27 thomas int r;
964 8a35f56c 2022-07-16 thomas
965 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
966 8a35f56c 2022-07-16 thomas if (error)
967 8a35f56c 2022-07-16 thomas return error;
968 8a35f56c 2022-07-16 thomas
969 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
970 8a35f56c 2022-07-16 thomas
971 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
972 255f4022 2022-08-27 thomas if (error)
973 255f4022 2022-08-27 thomas goto done;
974 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
975 8a35f56c 2022-07-16 thomas if (error)
976 8a35f56c 2022-07-16 thomas goto done;
977 8a35f56c 2022-07-16 thomas
978 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
979 79393471 2022-08-27 thomas "<div id='blame_title'>Blame</div>\n"
980 79393471 2022-08-27 thomas "</div>\n" /* #blame_title_wrapper */
981 79393471 2022-08-27 thomas "<div id='blame_content'>\n"
982 79393471 2022-08-27 thomas "<div id='blame_header_wrapper'>\n"
983 79393471 2022-08-27 thomas "<div id='blame_header'>\n"
984 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
985 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
986 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
987 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
988 79393471 2022-08-27 thomas "</div>\n" /* #blame_header */
989 79393471 2022-08-27 thomas "</div>\n" /* #blame_header_wrapper */
990 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
991 79393471 2022-08-27 thomas "<div id='blame'>\n",
992 2ad628aa 2022-08-31 thomas age,
993 255f4022 2022-08-27 thomas msg);
994 79393471 2022-08-27 thomas if (r == -1)
995 8a35f56c 2022-07-16 thomas goto done;
996 8a35f56c 2022-07-16 thomas
997 8a35f56c 2022-07-16 thomas error = got_output_file_blame(c);
998 8a35f56c 2022-07-16 thomas if (error)
999 8a35f56c 2022-07-16 thomas goto done;
1000 8a35f56c 2022-07-16 thomas
1001 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n" /* #blame */
1002 79393471 2022-08-27 thomas "</div>\n"); /* #blame_content */
1003 8a35f56c 2022-07-16 thomas done:
1004 8a35f56c 2022-07-16 thomas free(age);
1005 255f4022 2022-08-27 thomas free(msg);
1006 8a35f56c 2022-07-16 thomas return error;
1007 8a35f56c 2022-07-16 thomas }
1008 8a35f56c 2022-07-16 thomas
1009 8a35f56c 2022-07-16 thomas static const struct got_error *
1010 8a35f56c 2022-07-16 thomas gotweb_render_branches(struct request *c)
1011 8a35f56c 2022-07-16 thomas {
1012 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1013 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1014 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
1015 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1016 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1017 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1018 55144267 2022-09-07 thomas char *escaped_refname = NULL;
1019 8a35f56c 2022-07-16 thomas char *age = NULL;
1020 79393471 2022-08-27 thomas int r;
1021 8a35f56c 2022-07-16 thomas
1022 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1023 8a35f56c 2022-07-16 thomas
1024 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
1025 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
1026 8a35f56c 2022-07-16 thomas if (error)
1027 8a35f56c 2022-07-16 thomas goto done;
1028 8a35f56c 2022-07-16 thomas
1029 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1030 79393471 2022-08-27 thomas "<div id='branches_title'>Branches</div>\n"
1031 79393471 2022-08-27 thomas "</div>\n" /* #branches_title_wrapper */
1032 79393471 2022-08-27 thomas "<div id='branches_content'>\n");
1033 79393471 2022-08-27 thomas if (r == -1)
1034 8a35f56c 2022-07-16 thomas goto done;
1035 8a35f56c 2022-07-16 thomas
1036 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
1037 255f4022 2022-08-27 thomas const char *refname = NULL;
1038 8a35f56c 2022-07-16 thomas
1039 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
1040 8a35f56c 2022-07-16 thomas continue;
1041 8a35f56c 2022-07-16 thomas
1042 255f4022 2022-08-27 thomas refname = got_ref_get_name(re->ref);
1043 8a35f56c 2022-07-16 thomas if (refname == NULL) {
1044 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
1045 8a35f56c 2022-07-16 thomas goto done;
1046 8a35f56c 2022-07-16 thomas }
1047 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) != 0)
1048 8a35f56c 2022-07-16 thomas continue;
1049 8a35f56c 2022-07-16 thomas
1050 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&age, c, refname, TM_DIFF);
1051 8a35f56c 2022-07-16 thomas if (error)
1052 8a35f56c 2022-07-16 thomas goto done;
1053 8a35f56c 2022-07-16 thomas
1054 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
1055 8a35f56c 2022-07-16 thomas refname += 11;
1056 255f4022 2022-08-27 thomas error = gotweb_escape_html(&escaped_refname, refname);
1057 255f4022 2022-08-27 thomas if (error)
1058 255f4022 2022-08-27 thomas goto done;
1059 8a35f56c 2022-07-16 thomas
1060 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1061 79393471 2022-08-27 thomas "<div class='branches_age'>%s</div>\n"
1062 79393471 2022-08-27 thomas "<div class='branches_space'>&nbsp;</div>\n"
1063 55144267 2022-09-07 thomas "<div class='branch'>", age);
1064 55144267 2022-09-07 thomas if (r == -1)
1065 55144267 2022-09-07 thomas goto done;
1066 55144267 2022-09-07 thomas
1067 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1068 55144267 2022-09-07 thomas .action = SUMMARY,
1069 55144267 2022-09-07 thomas .index_page = -1,
1070 55144267 2022-09-07 thomas .page = -1,
1071 55144267 2022-09-07 thomas .path = qs->path,
1072 55144267 2022-09-07 thomas .headref = refname,
1073 55144267 2022-09-07 thomas }, "%s", escaped_refname);
1074 55144267 2022-09-07 thomas if (r == -1)
1075 55144267 2022-09-07 thomas goto done;
1076 55144267 2022-09-07 thomas
1077 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .branch */
1078 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1079 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1080 55144267 2022-09-07 thomas goto done;
1081 55144267 2022-09-07 thomas
1082 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1083 55144267 2022-09-07 thomas .action = SUMMARY,
1084 55144267 2022-09-07 thomas .index_page = -1,
1085 55144267 2022-09-07 thomas .page = -1,
1086 55144267 2022-09-07 thomas .path = qs->path,
1087 55144267 2022-09-07 thomas .headref = refname,
1088 55144267 2022-09-07 thomas }, "summary");
1089 55144267 2022-09-07 thomas if (r == -1)
1090 55144267 2022-09-07 thomas goto done;
1091 55144267 2022-09-07 thomas
1092 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1093 55144267 2022-09-07 thomas goto done;
1094 55144267 2022-09-07 thomas
1095 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1096 55144267 2022-09-07 thomas .action = BRIEFS,
1097 55144267 2022-09-07 thomas .index_page = -1,
1098 55144267 2022-09-07 thomas .page = -1,
1099 55144267 2022-09-07 thomas .path = qs->path,
1100 55144267 2022-09-07 thomas .headref = refname,
1101 55144267 2022-09-07 thomas }, "commit briefs");
1102 55144267 2022-09-07 thomas if (r == -1)
1103 55144267 2022-09-07 thomas goto done;
1104 55144267 2022-09-07 thomas
1105 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1106 55144267 2022-09-07 thomas goto done;
1107 55144267 2022-09-07 thomas
1108 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1109 55144267 2022-09-07 thomas .action = COMMITS,
1110 55144267 2022-09-07 thomas .index_page = -1,
1111 55144267 2022-09-07 thomas .page = -1,
1112 55144267 2022-09-07 thomas .path = qs->path,
1113 55144267 2022-09-07 thomas .headref = refname,
1114 55144267 2022-09-07 thomas }, "commits");
1115 55144267 2022-09-07 thomas if (r == -1)
1116 55144267 2022-09-07 thomas goto done;
1117 55144267 2022-09-07 thomas
1118 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>\n" /* .navs */
1119 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1120 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1121 55144267 2022-09-07 thomas "</div>\n"); /* .branches_wrapper */
1122 79393471 2022-08-27 thomas if (r == -1)
1123 8a35f56c 2022-07-16 thomas goto done;
1124 8a35f56c 2022-07-16 thomas
1125 8a35f56c 2022-07-16 thomas free(age);
1126 8a35f56c 2022-07-16 thomas age = NULL;
1127 55144267 2022-09-07 thomas free(escaped_refname);
1128 55144267 2022-09-07 thomas escaped_refname = NULL;
1129 8a35f56c 2022-07-16 thomas }
1130 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #branches_content */
1131 8a35f56c 2022-07-16 thomas done:
1132 f95e65a1 2022-09-02 thomas free(age);
1133 55144267 2022-09-07 thomas free(escaped_refname);
1134 f95e65a1 2022-09-02 thomas got_ref_list_free(&refs);
1135 8a35f56c 2022-07-16 thomas return error;
1136 8a35f56c 2022-07-16 thomas }
1137 8a35f56c 2022-07-16 thomas
1138 8a35f56c 2022-07-16 thomas static const struct got_error *
1139 8a35f56c 2022-07-16 thomas gotweb_render_summary(struct request *c)
1140 8a35f56c 2022-07-16 thomas {
1141 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1142 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1143 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1144 79393471 2022-08-27 thomas int r;
1145 8a35f56c 2022-07-16 thomas
1146 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1147 8a35f56c 2022-07-16 thomas goto done;
1148 8a35f56c 2022-07-16 thomas
1149 79393471 2022-08-27 thomas if (srv->show_repo_description) {
1150 79393471 2022-08-27 thomas r = fcgi_printf(c,
1151 79393471 2022-08-27 thomas "<div id='description_title'>Description:</div>\n"
1152 79393471 2022-08-27 thomas "<div id='description'>%s</div>\n",
1153 ddf2e5c2 2022-08-27 thomas t->repo_dir->description ? t->repo_dir->description : "");
1154 79393471 2022-08-27 thomas if (r == -1)
1155 79393471 2022-08-27 thomas goto done;
1156 79393471 2022-08-27 thomas }
1157 8a35f56c 2022-07-16 thomas
1158 79393471 2022-08-27 thomas if (srv->show_repo_owner) {
1159 79393471 2022-08-27 thomas r = fcgi_printf(c,
1160 79393471 2022-08-27 thomas "<div id='repo_owner_title'>Owner:</div>\n"
1161 79393471 2022-08-27 thomas "<div id='repo_owner'>%s</div>\n",
1162 ddf2e5c2 2022-08-27 thomas t->repo_dir->owner ? t->repo_dir->owner : "");
1163 79393471 2022-08-27 thomas if (r == -1)
1164 79393471 2022-08-27 thomas goto done;
1165 79393471 2022-08-27 thomas }
1166 8a35f56c 2022-07-16 thomas
1167 79393471 2022-08-27 thomas if (srv->show_repo_age) {
1168 79393471 2022-08-27 thomas r = fcgi_printf(c,
1169 79393471 2022-08-27 thomas "<div id='last_change_title'>Last Change:</div>\n"
1170 79393471 2022-08-27 thomas "<div id='last_change'>%s</div>\n",
1171 79393471 2022-08-27 thomas t->repo_dir->age);
1172 79393471 2022-08-27 thomas if (r == -1)
1173 79393471 2022-08-27 thomas goto done;
1174 79393471 2022-08-27 thomas }
1175 8a35f56c 2022-07-16 thomas
1176 79393471 2022-08-27 thomas if (srv->show_repo_cloneurl) {
1177 79393471 2022-08-27 thomas r = fcgi_printf(c,
1178 79393471 2022-08-27 thomas "<div id='cloneurl_title'>Clone URL:</div>\n"
1179 79393471 2022-08-27 thomas "<div id='cloneurl'>%s</div>\n",
1180 79393471 2022-08-27 thomas t->repo_dir->url ? t->repo_dir->url : "");
1181 79393471 2022-08-27 thomas if (r == -1)
1182 79393471 2022-08-27 thomas goto done;
1183 79393471 2022-08-27 thomas }
1184 8a35f56c 2022-07-16 thomas
1185 79393471 2022-08-27 thomas r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1186 79393471 2022-08-27 thomas if (r == -1)
1187 8a35f56c 2022-07-16 thomas goto done;
1188 8a35f56c 2022-07-16 thomas
1189 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
1190 8a35f56c 2022-07-16 thomas goto done;
1191 8a35f56c 2022-07-16 thomas
1192 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
1193 8a35f56c 2022-07-16 thomas if (error) {
1194 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1195 8a35f56c 2022-07-16 thomas goto done;
1196 8a35f56c 2022-07-16 thomas }
1197 8a35f56c 2022-07-16 thomas
1198 8a35f56c 2022-07-16 thomas error = gotweb_render_branches(c);
1199 8a35f56c 2022-07-16 thomas if (error)
1200 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1201 8a35f56c 2022-07-16 thomas done:
1202 8a35f56c 2022-07-16 thomas return error;
1203 8a35f56c 2022-07-16 thomas }
1204 8a35f56c 2022-07-16 thomas
1205 8a35f56c 2022-07-16 thomas static const struct got_error *
1206 8a35f56c 2022-07-16 thomas gotweb_render_tag(struct request *c)
1207 8a35f56c 2022-07-16 thomas {
1208 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1209 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1210 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1211 255f4022 2022-08-27 thomas char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1212 8a35f56c 2022-07-16 thomas
1213 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, 1);
1214 8a35f56c 2022-07-16 thomas if (error)
1215 8a35f56c 2022-07-16 thomas goto done;
1216 8a35f56c 2022-07-16 thomas
1217 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1218 8a35f56c 2022-07-16 thomas error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1219 8a35f56c 2022-07-16 thomas "bad commit id");
1220 8a35f56c 2022-07-16 thomas goto done;
1221 8a35f56c 2022-07-16 thomas }
1222 8a35f56c 2022-07-16 thomas
1223 8a35f56c 2022-07-16 thomas rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1224 8a35f56c 2022-07-16 thomas
1225 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1226 8a35f56c 2022-07-16 thomas if (error)
1227 8a35f56c 2022-07-16 thomas goto done;
1228 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rt->tagger);
1229 8a35f56c 2022-07-16 thomas if (error)
1230 8a35f56c 2022-07-16 thomas goto done;
1231 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->commit_msg);
1232 255f4022 2022-08-27 thomas if (error)
1233 255f4022 2022-08-27 thomas goto done;
1234 8a35f56c 2022-07-16 thomas
1235 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1236 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/", 5) == 0)
1237 c8d0196f 2022-09-02 thomas tagname += 5;
1238 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1239 255f4022 2022-08-27 thomas if (error)
1240 255f4022 2022-08-27 thomas goto done;
1241 8a35f56c 2022-07-16 thomas
1242 79393471 2022-08-27 thomas fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1243 79393471 2022-08-27 thomas "<div id='tags_title'>Tag</div>\n"
1244 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1245 79393471 2022-08-27 thomas "<div id='tags_content'>\n"
1246 79393471 2022-08-27 thomas "<div id='tag_header_wrapper'>\n"
1247 79393471 2022-08-27 thomas "<div id='tag_header'>\n"
1248 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1249 79393471 2022-08-27 thomas "<div class='header_commit'>%s"
1250 79393471 2022-08-27 thomas " <span class='refs_str'>(%s)</span></div>\n"
1251 79393471 2022-08-27 thomas "<div class='header_author_title'>Tagger:</div>\n"
1252 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1253 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1254 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1255 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1256 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1257 79393471 2022-08-27 thomas "</div>\n" /* #tag_header */
1258 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1259 79393471 2022-08-27 thomas "<div id='tag_commit'>\n%s</div>"
1260 80ca8b0f 2022-09-06 thomas "</div>" /* #tag_header_wrapper */
1261 80ca8b0f 2022-09-06 thomas "</div>", /* #tags_content */
1262 79393471 2022-08-27 thomas rt->commit_id,
1263 255f4022 2022-08-27 thomas tagname,
1264 255f4022 2022-08-27 thomas author,
1265 2ad628aa 2022-08-31 thomas age,
1266 255f4022 2022-08-27 thomas msg,
1267 79393471 2022-08-27 thomas rt->tag_commit);
1268 8a35f56c 2022-07-16 thomas
1269 8a35f56c 2022-07-16 thomas done:
1270 8a35f56c 2022-07-16 thomas free(age);
1271 8a35f56c 2022-07-16 thomas free(author);
1272 255f4022 2022-08-27 thomas free(msg);
1273 8a35f56c 2022-07-16 thomas return error;
1274 8a35f56c 2022-07-16 thomas }
1275 8a35f56c 2022-07-16 thomas
1276 8a35f56c 2022-07-16 thomas static const struct got_error *
1277 8a35f56c 2022-07-16 thomas gotweb_render_tags(struct request *c)
1278 8a35f56c 2022-07-16 thomas {
1279 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1280 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1281 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1282 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1283 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1284 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1285 255f4022 2022-08-27 thomas char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1286 79393471 2022-08-27 thomas int r, commit_found = 0;
1287 8a35f56c 2022-07-16 thomas
1288 8a35f56c 2022-07-16 thomas if (qs->action == BRIEFS) {
1289 8a35f56c 2022-07-16 thomas qs->action = TAGS;
1290 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1291 8a35f56c 2022-07-16 thomas } else
1292 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, srv->max_commits_display);
1293 8a35f56c 2022-07-16 thomas if (error)
1294 8a35f56c 2022-07-16 thomas goto done;
1295 8a35f56c 2022-07-16 thomas
1296 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1297 79393471 2022-08-27 thomas "<div id='tags_title'>Tags</div>\n"
1298 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1299 79393471 2022-08-27 thomas "<div id='tags_content'>\n");
1300 79393471 2022-08-27 thomas if (r == -1)
1301 8a35f56c 2022-07-16 thomas goto done;
1302 8a35f56c 2022-07-16 thomas
1303 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1304 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1305 79393471 2022-08-27 thomas "This repository contains no tags");
1306 79393471 2022-08-27 thomas if (r == -1)
1307 8a35f56c 2022-07-16 thomas goto done;
1308 8a35f56c 2022-07-16 thomas }
1309 8a35f56c 2022-07-16 thomas
1310 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1311 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL) {
1312 8a35f56c 2022-07-16 thomas if (strcmp(qs->commit, rt->commit_id) != 0)
1313 8a35f56c 2022-07-16 thomas continue;
1314 8a35f56c 2022-07-16 thomas else
1315 8a35f56c 2022-07-16 thomas commit_found = 1;
1316 8a35f56c 2022-07-16 thomas }
1317 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1318 8a35f56c 2022-07-16 thomas if (error)
1319 8a35f56c 2022-07-16 thomas goto done;
1320 8a35f56c 2022-07-16 thomas
1321 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1322 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/tags/", 10) == 0)
1323 c8d0196f 2022-09-02 thomas tagname += 10;
1324 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1325 255f4022 2022-08-27 thomas if (error)
1326 255f4022 2022-08-27 thomas goto done;
1327 8a35f56c 2022-07-16 thomas
1328 8a35f56c 2022-07-16 thomas if (rt->tag_commit != NULL) {
1329 8a35f56c 2022-07-16 thomas newline = strchr(rt->tag_commit, '\n');
1330 8a35f56c 2022-07-16 thomas if (newline)
1331 8a35f56c 2022-07-16 thomas *newline = '\0';
1332 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->tag_commit);
1333 255f4022 2022-08-27 thomas if (error)
1334 255f4022 2022-08-27 thomas goto done;
1335 8a35f56c 2022-07-16 thomas }
1336 8a35f56c 2022-07-16 thomas
1337 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1338 79393471 2022-08-27 thomas "<div class='tag'>%s</div>\n"
1339 55144267 2022-09-07 thomas "<div class='tag_log'>", age, tagname) == -1)
1340 55144267 2022-09-07 thomas goto done;
1341 55144267 2022-09-07 thomas
1342 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1343 55144267 2022-09-07 thomas .action = TAG,
1344 55144267 2022-09-07 thomas .index_page = -1,
1345 55144267 2022-09-07 thomas .page = -1,
1346 55144267 2022-09-07 thomas .path = repo_dir->name,
1347 55144267 2022-09-07 thomas .commit = rt->commit_id,
1348 55144267 2022-09-07 thomas }, "%s", msg ? msg : "");
1349 55144267 2022-09-07 thomas if (r == -1)
1350 55144267 2022-09-07 thomas goto done;
1351 55144267 2022-09-07 thomas
1352 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .tag_log */
1353 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1354 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1355 55144267 2022-09-07 thomas goto done;
1356 55144267 2022-09-07 thomas
1357 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1358 55144267 2022-09-07 thomas .action = TAG,
1359 55144267 2022-09-07 thomas .index_page = -1,
1360 55144267 2022-09-07 thomas .page = -1,
1361 55144267 2022-09-07 thomas .path = repo_dir->name,
1362 55144267 2022-09-07 thomas .commit = rt->commit_id,
1363 55144267 2022-09-07 thomas }, "tag");
1364 55144267 2022-09-07 thomas if (r == -1)
1365 55144267 2022-09-07 thomas goto done;
1366 55144267 2022-09-07 thomas
1367 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1368 55144267 2022-09-07 thomas goto done;
1369 55144267 2022-09-07 thomas
1370 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1371 55144267 2022-09-07 thomas .action = BRIEFS,
1372 55144267 2022-09-07 thomas .index_page = -1,
1373 55144267 2022-09-07 thomas .page = -1,
1374 55144267 2022-09-07 thomas .path = repo_dir->name,
1375 55144267 2022-09-07 thomas .commit = rt->commit_id,
1376 55144267 2022-09-07 thomas }, "commit briefs");
1377 55144267 2022-09-07 thomas if (r == -1)
1378 55144267 2022-09-07 thomas goto done;
1379 55144267 2022-09-07 thomas
1380 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1381 55144267 2022-09-07 thomas goto done;
1382 55144267 2022-09-07 thomas
1383 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1384 55144267 2022-09-07 thomas .action = COMMITS,
1385 55144267 2022-09-07 thomas .index_page = -1,
1386 55144267 2022-09-07 thomas .page = -1,
1387 55144267 2022-09-07 thomas .path = repo_dir->name,
1388 55144267 2022-09-07 thomas .commit = rt->commit_id,
1389 55144267 2022-09-07 thomas }, "commits");
1390 55144267 2022-09-07 thomas if (r == -1)
1391 55144267 2022-09-07 thomas goto done;
1392 55144267 2022-09-07 thomas
1393 55144267 2022-09-07 thomas r = fcgi_printf(c,
1394 79393471 2022-08-27 thomas "</div>\n" /* .navs */
1395 79393471 2022-08-27 thomas "</div>\n" /* .navs_wrapper */
1396 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n");
1397 79393471 2022-08-27 thomas if (r == -1)
1398 8a35f56c 2022-07-16 thomas goto done;
1399 8a35f56c 2022-07-16 thomas
1400 8a35f56c 2022-07-16 thomas free(age);
1401 8a35f56c 2022-07-16 thomas age = NULL;
1402 255f4022 2022-08-27 thomas free(tagname);
1403 255f4022 2022-08-27 thomas tagname = NULL;
1404 255f4022 2022-08-27 thomas free(msg);
1405 255f4022 2022-08-27 thomas msg = NULL;
1406 8a35f56c 2022-07-16 thomas }
1407 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1408 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
1409 8a35f56c 2022-07-16 thomas goto done;
1410 8a35f56c 2022-07-16 thomas }
1411 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tags_content */
1412 8a35f56c 2022-07-16 thomas done:
1413 8a35f56c 2022-07-16 thomas free(age);
1414 255f4022 2022-08-27 thomas free(tagname);
1415 255f4022 2022-08-27 thomas free(msg);
1416 8a35f56c 2022-07-16 thomas return error;
1417 8a35f56c 2022-07-16 thomas }
1418 8a35f56c 2022-07-16 thomas
1419 8a35f56c 2022-07-16 thomas const struct got_error *
1420 8a35f56c 2022-07-16 thomas gotweb_escape_html(char **escaped_html, const char *orig_html)
1421 8a35f56c 2022-07-16 thomas {
1422 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1423 8a35f56c 2022-07-16 thomas struct escape_pair {
1424 8a35f56c 2022-07-16 thomas char c;
1425 8a35f56c 2022-07-16 thomas const char *s;
1426 8a35f56c 2022-07-16 thomas } esc[] = {
1427 8a35f56c 2022-07-16 thomas { '>', "&gt;" },
1428 8a35f56c 2022-07-16 thomas { '<', "&lt;" },
1429 8a35f56c 2022-07-16 thomas { '&', "&amp;" },
1430 8a35f56c 2022-07-16 thomas { '"', "&quot;" },
1431 8a35f56c 2022-07-16 thomas { '\'', "&apos;" },
1432 8a35f56c 2022-07-16 thomas { '\n', "<br />" },
1433 8a35f56c 2022-07-16 thomas };
1434 8a35f56c 2022-07-16 thomas size_t orig_len, len;
1435 8a35f56c 2022-07-16 thomas int i, j, x;
1436 8a35f56c 2022-07-16 thomas
1437 8a35f56c 2022-07-16 thomas orig_len = strlen(orig_html);
1438 8a35f56c 2022-07-16 thomas len = orig_len;
1439 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1440 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1441 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1442 8a35f56c 2022-07-16 thomas continue;
1443 8a35f56c 2022-07-16 thomas len += strlen(esc[j].s) - 1 /* escaped char */;
1444 8a35f56c 2022-07-16 thomas }
1445 8a35f56c 2022-07-16 thomas }
1446 8a35f56c 2022-07-16 thomas
1447 8a35f56c 2022-07-16 thomas *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1448 8a35f56c 2022-07-16 thomas if (*escaped_html == NULL)
1449 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1450 8a35f56c 2022-07-16 thomas
1451 8a35f56c 2022-07-16 thomas x = 0;
1452 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1453 8a35f56c 2022-07-16 thomas int escaped = 0;
1454 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1455 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1456 8a35f56c 2022-07-16 thomas continue;
1457 8a35f56c 2022-07-16 thomas
1458 8a35f56c 2022-07-16 thomas if (strlcat(*escaped_html, esc[j].s, len + 1)
1459 8a35f56c 2022-07-16 thomas >= len + 1) {
1460 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_SPACE);
1461 8a35f56c 2022-07-16 thomas goto done;
1462 8a35f56c 2022-07-16 thomas }
1463 8a35f56c 2022-07-16 thomas x += strlen(esc[j].s);
1464 8a35f56c 2022-07-16 thomas escaped = 1;
1465 8a35f56c 2022-07-16 thomas break;
1466 8a35f56c 2022-07-16 thomas }
1467 8a35f56c 2022-07-16 thomas if (!escaped) {
1468 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = orig_html[i];
1469 8a35f56c 2022-07-16 thomas x++;
1470 8a35f56c 2022-07-16 thomas }
1471 8a35f56c 2022-07-16 thomas }
1472 8a35f56c 2022-07-16 thomas done:
1473 8a35f56c 2022-07-16 thomas if (error) {
1474 8a35f56c 2022-07-16 thomas free(*escaped_html);
1475 8a35f56c 2022-07-16 thomas *escaped_html = NULL;
1476 8a35f56c 2022-07-16 thomas } else {
1477 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = '\0';
1478 8a35f56c 2022-07-16 thomas }
1479 8a35f56c 2022-07-16 thomas
1480 8a35f56c 2022-07-16 thomas return error;
1481 8a35f56c 2022-07-16 thomas }
1482 8a35f56c 2022-07-16 thomas
1483 55144267 2022-09-07 thomas static inline int
1484 55144267 2022-09-07 thomas should_urlencode(int c)
1485 55144267 2022-09-07 thomas {
1486 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
1487 55144267 2022-09-07 thomas return 1;
1488 55144267 2022-09-07 thomas
1489 55144267 2022-09-07 thomas switch (c) {
1490 55144267 2022-09-07 thomas /* gen-delim */
1491 55144267 2022-09-07 thomas case ':':
1492 55144267 2022-09-07 thomas case '/':
1493 55144267 2022-09-07 thomas case '?':
1494 55144267 2022-09-07 thomas case '#':
1495 55144267 2022-09-07 thomas case '[':
1496 55144267 2022-09-07 thomas case ']':
1497 55144267 2022-09-07 thomas case '@':
1498 55144267 2022-09-07 thomas /* sub-delims */
1499 55144267 2022-09-07 thomas case '!':
1500 55144267 2022-09-07 thomas case '$':
1501 55144267 2022-09-07 thomas case '&':
1502 55144267 2022-09-07 thomas case '\'':
1503 55144267 2022-09-07 thomas case '(':
1504 55144267 2022-09-07 thomas case ')':
1505 55144267 2022-09-07 thomas case '*':
1506 55144267 2022-09-07 thomas case '+':
1507 55144267 2022-09-07 thomas case ',':
1508 55144267 2022-09-07 thomas case ';':
1509 55144267 2022-09-07 thomas case '=':
1510 e1a9403a 2023-01-06 thomas /* needed because the URLs are embedded into the HTML */
1511 e1a9403a 2023-01-06 thomas case '\"':
1512 55144267 2022-09-07 thomas return 1;
1513 55144267 2022-09-07 thomas default:
1514 55144267 2022-09-07 thomas return 0;
1515 55144267 2022-09-07 thomas }
1516 55144267 2022-09-07 thomas }
1517 55144267 2022-09-07 thomas
1518 55144267 2022-09-07 thomas static char *
1519 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
1520 55144267 2022-09-07 thomas {
1521 55144267 2022-09-07 thomas const char *s;
1522 55144267 2022-09-07 thomas char *escaped;
1523 55144267 2022-09-07 thomas size_t i, len;
1524 55144267 2022-09-07 thomas int a, b;
1525 55144267 2022-09-07 thomas
1526 55144267 2022-09-07 thomas len = 0;
1527 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1528 55144267 2022-09-07 thomas len++;
1529 55144267 2022-09-07 thomas if (should_urlencode(*s))
1530 55144267 2022-09-07 thomas len += 2;
1531 55144267 2022-09-07 thomas }
1532 55144267 2022-09-07 thomas
1533 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
1534 55144267 2022-09-07 thomas if (escaped == NULL)
1535 55144267 2022-09-07 thomas return NULL;
1536 55144267 2022-09-07 thomas
1537 55144267 2022-09-07 thomas i = 0;
1538 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1539 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
1540 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
1541 55144267 2022-09-07 thomas b = (*s & 0x0F);
1542 55144267 2022-09-07 thomas
1543 55144267 2022-09-07 thomas escaped[i++] = '%';
1544 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1545 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1546 55144267 2022-09-07 thomas } else
1547 55144267 2022-09-07 thomas escaped[i++] = *s;
1548 55144267 2022-09-07 thomas }
1549 55144267 2022-09-07 thomas
1550 55144267 2022-09-07 thomas return escaped;
1551 55144267 2022-09-07 thomas }
1552 55144267 2022-09-07 thomas
1553 e7e5fa49 2022-12-30 thomas const char *
1554 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
1555 55144267 2022-09-07 thomas {
1556 55144267 2022-09-07 thomas switch (action) {
1557 55144267 2022-09-07 thomas case BLAME:
1558 55144267 2022-09-07 thomas return "blame";
1559 55144267 2022-09-07 thomas case BLOB:
1560 55144267 2022-09-07 thomas return "blob";
1561 b82440e1 2023-01-06 thomas case BLOBRAW:
1562 b82440e1 2023-01-06 thomas return "blobraw";
1563 55144267 2022-09-07 thomas case BRIEFS:
1564 55144267 2022-09-07 thomas return "briefs";
1565 55144267 2022-09-07 thomas case COMMITS:
1566 55144267 2022-09-07 thomas return "commits";
1567 55144267 2022-09-07 thomas case DIFF:
1568 55144267 2022-09-07 thomas return "diff";
1569 55144267 2022-09-07 thomas case ERR:
1570 55144267 2022-09-07 thomas return "err";
1571 55144267 2022-09-07 thomas case INDEX:
1572 55144267 2022-09-07 thomas return "index";
1573 55144267 2022-09-07 thomas case SUMMARY:
1574 55144267 2022-09-07 thomas return "summary";
1575 55144267 2022-09-07 thomas case TAG:
1576 55144267 2022-09-07 thomas return "tag";
1577 55144267 2022-09-07 thomas case TAGS:
1578 55144267 2022-09-07 thomas return "tags";
1579 55144267 2022-09-07 thomas case TREE:
1580 55144267 2022-09-07 thomas return "tree";
1581 d6795e9f 2022-12-30 thomas case RSS:
1582 d6795e9f 2022-12-30 thomas return "rss";
1583 55144267 2022-09-07 thomas default:
1584 55144267 2022-09-07 thomas return NULL;
1585 55144267 2022-09-07 thomas }
1586 55144267 2022-09-07 thomas }
1587 55144267 2022-09-07 thomas
1588 e7e5fa49 2022-12-30 thomas int
1589 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
1590 55144267 2022-09-07 thomas {
1591 55144267 2022-09-07 thomas const char *sep = "?", *action;
1592 55144267 2022-09-07 thomas char *tmp;
1593 55144267 2022-09-07 thomas int r;
1594 55144267 2022-09-07 thomas
1595 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
1596 55144267 2022-09-07 thomas if (action != NULL) {
1597 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
1598 55144267 2022-09-07 thomas return -1;
1599 55144267 2022-09-07 thomas sep = "&";
1600 55144267 2022-09-07 thomas }
1601 55144267 2022-09-07 thomas
1602 55144267 2022-09-07 thomas if (url->commit) {
1603 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1604 55144267 2022-09-07 thomas return -1;
1605 55144267 2022-09-07 thomas sep = "&";
1606 55144267 2022-09-07 thomas }
1607 55144267 2022-09-07 thomas
1608 55144267 2022-09-07 thomas if (url->previd) {
1609 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1610 55144267 2022-09-07 thomas return -1;
1611 55144267 2022-09-07 thomas sep = "&";
1612 55144267 2022-09-07 thomas }
1613 55144267 2022-09-07 thomas
1614 55144267 2022-09-07 thomas if (url->prevset) {
1615 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1616 55144267 2022-09-07 thomas return -1;
1617 55144267 2022-09-07 thomas sep = "&";
1618 55144267 2022-09-07 thomas }
1619 55144267 2022-09-07 thomas
1620 55144267 2022-09-07 thomas if (url->file) {
1621 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1622 55144267 2022-09-07 thomas if (tmp == NULL)
1623 55144267 2022-09-07 thomas return -1;
1624 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1625 55144267 2022-09-07 thomas free(tmp);
1626 55144267 2022-09-07 thomas if (r == -1)
1627 55144267 2022-09-07 thomas return -1;
1628 55144267 2022-09-07 thomas sep = "&";
1629 55144267 2022-09-07 thomas }
1630 55144267 2022-09-07 thomas
1631 55144267 2022-09-07 thomas if (url->folder) {
1632 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1633 55144267 2022-09-07 thomas if (tmp == NULL)
1634 55144267 2022-09-07 thomas return -1;
1635 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1636 55144267 2022-09-07 thomas free(tmp);
1637 55144267 2022-09-07 thomas if (r == -1)
1638 55144267 2022-09-07 thomas return -1;
1639 55144267 2022-09-07 thomas sep = "&";
1640 55144267 2022-09-07 thomas }
1641 55144267 2022-09-07 thomas
1642 55144267 2022-09-07 thomas if (url->headref) {
1643 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1644 55144267 2022-09-07 thomas if (tmp == NULL)
1645 55144267 2022-09-07 thomas return -1;
1646 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1647 55144267 2022-09-07 thomas free(tmp);
1648 55144267 2022-09-07 thomas if (r == -1)
1649 55144267 2022-09-07 thomas return -1;
1650 55144267 2022-09-07 thomas sep = "&";
1651 55144267 2022-09-07 thomas }
1652 55144267 2022-09-07 thomas
1653 55144267 2022-09-07 thomas if (url->index_page != -1) {
1654 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
1655 55144267 2022-09-07 thomas url->index_page) == -1)
1656 55144267 2022-09-07 thomas return -1;
1657 55144267 2022-09-07 thomas sep = "&";
1658 55144267 2022-09-07 thomas }
1659 55144267 2022-09-07 thomas
1660 55144267 2022-09-07 thomas if (url->path) {
1661 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1662 55144267 2022-09-07 thomas if (tmp == NULL)
1663 55144267 2022-09-07 thomas return -1;
1664 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
1665 55144267 2022-09-07 thomas free(tmp);
1666 55144267 2022-09-07 thomas if (r == -1)
1667 55144267 2022-09-07 thomas return -1;
1668 55144267 2022-09-07 thomas sep = "&";
1669 55144267 2022-09-07 thomas }
1670 55144267 2022-09-07 thomas
1671 55144267 2022-09-07 thomas if (url->page != -1) {
1672 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1673 55144267 2022-09-07 thomas return -1;
1674 55144267 2022-09-07 thomas sep = "&";
1675 55144267 2022-09-07 thomas }
1676 55144267 2022-09-07 thomas
1677 55144267 2022-09-07 thomas return 0;
1678 55144267 2022-09-07 thomas }
1679 55144267 2022-09-07 thomas
1680 55144267 2022-09-07 thomas int
1681 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1682 d6795e9f 2022-12-30 thomas {
1683 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1684 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1685 d6795e9f 2022-12-30 thomas
1686 d6795e9f 2022-12-30 thomas if (fcgi_puts(tp, proto) == -1 ||
1687 d6795e9f 2022-12-30 thomas fcgi_puts(tp, "://") == -1 ||
1688 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1689 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1690 d6795e9f 2022-12-30 thomas return -1;
1691 d6795e9f 2022-12-30 thomas
1692 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1693 d6795e9f 2022-12-30 thomas }
1694 d6795e9f 2022-12-30 thomas
1695 d6795e9f 2022-12-30 thomas int
1696 55144267 2022-09-07 thomas gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1697 55144267 2022-09-07 thomas {
1698 55144267 2022-09-07 thomas va_list ap;
1699 55144267 2022-09-07 thomas int r;
1700 55144267 2022-09-07 thomas
1701 55144267 2022-09-07 thomas if (fcgi_printf(c, "<a href='") == -1)
1702 55144267 2022-09-07 thomas return -1;
1703 55144267 2022-09-07 thomas
1704 e7e5fa49 2022-12-30 thomas if (gotweb_render_url(c, url) == -1)
1705 55144267 2022-09-07 thomas return -1;
1706 55144267 2022-09-07 thomas
1707 55144267 2022-09-07 thomas if (fcgi_printf(c, "'>") == -1)
1708 55144267 2022-09-07 thomas return -1;
1709 55144267 2022-09-07 thomas
1710 55144267 2022-09-07 thomas va_start(ap, fmt);
1711 55144267 2022-09-07 thomas r = fcgi_vprintf(c, fmt, ap);
1712 55144267 2022-09-07 thomas va_end(ap);
1713 55144267 2022-09-07 thomas if (r == -1)
1714 55144267 2022-09-07 thomas return -1;
1715 55144267 2022-09-07 thomas
1716 55144267 2022-09-07 thomas if (fcgi_printf(c, "</a>"))
1717 55144267 2022-09-07 thomas return -1;
1718 55144267 2022-09-07 thomas return 0;
1719 55144267 2022-09-07 thomas }
1720 55144267 2022-09-07 thomas
1721 55e6cffd 2022-09-01 thomas static struct got_repository *
1722 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
1723 55e6cffd 2022-09-01 thomas {
1724 55e6cffd 2022-09-01 thomas int i;
1725 55e6cffd 2022-09-01 thomas
1726 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
1727 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
1728 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
1729 55e6cffd 2022-09-01 thomas }
1730 55e6cffd 2022-09-01 thomas
1731 55e6cffd 2022-09-01 thomas return NULL;
1732 55e6cffd 2022-09-01 thomas }
1733 55e6cffd 2022-09-01 thomas
1734 8a35f56c 2022-07-16 thomas static const struct got_error *
1735 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
1736 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
1737 55e6cffd 2022-09-01 thomas {
1738 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
1739 55e6cffd 2022-09-01 thomas struct got_repository *repo;
1740 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
1741 55e6cffd 2022-09-01 thomas int evicted = 0;
1742 55e6cffd 2022-09-01 thomas
1743 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1744 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
1745 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
1746 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
1747 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
1748 55e6cffd 2022-09-01 thomas if (error)
1749 55e6cffd 2022-09-01 thomas return error;
1750 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1751 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1752 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
1753 55e6cffd 2022-09-01 thomas evicted = 1;
1754 55e6cffd 2022-09-01 thomas } else {
1755 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
1756 55e6cffd 2022-09-01 thomas }
1757 55e6cffd 2022-09-01 thomas
1758 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1759 55e6cffd 2022-09-01 thomas if (error) {
1760 55e6cffd 2022-09-01 thomas if (evicted) {
1761 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1762 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1763 55e6cffd 2022-09-01 thomas }
1764 55e6cffd 2022-09-01 thomas return error;
1765 55e6cffd 2022-09-01 thomas }
1766 55e6cffd 2022-09-01 thomas
1767 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1768 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
1769 55e6cffd 2022-09-01 thomas if (evicted) {
1770 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1771 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1772 55e6cffd 2022-09-01 thomas }
1773 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
1774 55e6cffd 2022-09-01 thomas }
1775 55e6cffd 2022-09-01 thomas
1776 55e6cffd 2022-09-01 thomas cr->repo = repo;
1777 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
1778 55e6cffd 2022-09-01 thomas *new = repo;
1779 55e6cffd 2022-09-01 thomas return NULL;
1780 55e6cffd 2022-09-01 thomas }
1781 55e6cffd 2022-09-01 thomas
1782 55e6cffd 2022-09-01 thomas static const struct got_error *
1783 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1784 8a35f56c 2022-07-16 thomas {
1785 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1786 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1787 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1788 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1789 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
1790 8a35f56c 2022-07-16 thomas DIR *dt;
1791 8a35f56c 2022-07-16 thomas char *dir_test;
1792 8a35f56c 2022-07-16 thomas
1793 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1794 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1795 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1796 8a35f56c 2022-07-16 thomas
1797 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1798 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1799 8a35f56c 2022-07-16 thomas free(dir_test);
1800 8a35f56c 2022-07-16 thomas } else {
1801 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1802 8a35f56c 2022-07-16 thomas dir_test = NULL;
1803 c2d3d9a0 2022-09-01 thomas goto done;
1804 8a35f56c 2022-07-16 thomas }
1805 8a35f56c 2022-07-16 thomas
1806 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1807 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
1808 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
1809 8a35f56c 2022-07-16 thomas
1810 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1811 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1812 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1813 8a35f56c 2022-07-16 thomas goto err;
1814 c2d3d9a0 2022-09-01 thomas } else {
1815 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1816 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
1817 c2d3d9a0 2022-09-01 thomas }
1818 c2d3d9a0 2022-09-01 thomas
1819 8a35f56c 2022-07-16 thomas done:
1820 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1821 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1822 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1823 3991b2a5 2022-10-31 thomas goto err;
1824 3991b2a5 2022-10-31 thomas }
1825 3991b2a5 2022-10-31 thomas
1826 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
1827 55e6cffd 2022-09-01 thomas if (repo == NULL) {
1828 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
1829 55e6cffd 2022-09-01 thomas if (error)
1830 55e6cffd 2022-09-01 thomas goto err;
1831 55e6cffd 2022-09-01 thomas }
1832 55e6cffd 2022-09-01 thomas t->repo = repo;
1833 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1834 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1835 8a35f56c 2022-07-16 thomas if (error)
1836 8a35f56c 2022-07-16 thomas goto err;
1837 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1838 8a35f56c 2022-07-16 thomas if (error)
1839 8a35f56c 2022-07-16 thomas goto err;
1840 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1841 8a35f56c 2022-07-16 thomas if (error)
1842 8a35f56c 2022-07-16 thomas goto err;
1843 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1844 4606e6d4 2022-11-23 thomas dirfd(dt));
1845 8a35f56c 2022-07-16 thomas err:
1846 8a35f56c 2022-07-16 thomas free(dir_test);
1847 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1848 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1849 8a35f56c 2022-07-16 thomas return error;
1850 8a35f56c 2022-07-16 thomas }
1851 8a35f56c 2022-07-16 thomas
1852 8a35f56c 2022-07-16 thomas static const struct got_error *
1853 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1854 8a35f56c 2022-07-16 thomas {
1855 8a35f56c 2022-07-16 thomas const struct got_error *error;
1856 8a35f56c 2022-07-16 thomas
1857 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1858 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1859 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1860 8a35f56c 2022-07-16 thomas
1861 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1862 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1863 8a35f56c 2022-07-16 thomas free(*repo_dir);
1864 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1865 8a35f56c 2022-07-16 thomas return error;
1866 8a35f56c 2022-07-16 thomas }
1867 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1868 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1869 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1870 8a35f56c 2022-07-16 thomas (*repo_dir)->age = NULL;
1871 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1872 8a35f56c 2022-07-16 thomas
1873 8a35f56c 2022-07-16 thomas return NULL;
1874 8a35f56c 2022-07-16 thomas }
1875 8a35f56c 2022-07-16 thomas
1876 8a35f56c 2022-07-16 thomas static const struct got_error *
1877 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1878 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1879 8a35f56c 2022-07-16 thomas {
1880 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1881 4606e6d4 2022-11-23 thomas struct stat sb;
1882 4606e6d4 2022-11-23 thomas int fd = -1;
1883 4606e6d4 2022-11-23 thomas off_t len;
1884 8a35f56c 2022-07-16 thomas
1885 8a35f56c 2022-07-16 thomas *description = NULL;
1886 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1887 8a35f56c 2022-07-16 thomas return NULL;
1888 8a35f56c 2022-07-16 thomas
1889 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1890 4606e6d4 2022-11-23 thomas if (fd == -1) {
1891 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1892 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1893 4606e6d4 2022-11-23 thomas dirpath, "description");
1894 4606e6d4 2022-11-23 thomas }
1895 8a35f56c 2022-07-16 thomas goto done;
1896 8a35f56c 2022-07-16 thomas }
1897 8a35f56c 2022-07-16 thomas
1898 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1899 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1900 4606e6d4 2022-11-23 thomas dirpath, "description");
1901 8a35f56c 2022-07-16 thomas goto done;
1902 8a35f56c 2022-07-16 thomas }
1903 8a35f56c 2022-07-16 thomas
1904 4606e6d4 2022-11-23 thomas len = sb.st_size;
1905 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1906 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1907 8a35f56c 2022-07-16 thomas
1908 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1909 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1910 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1911 8a35f56c 2022-07-16 thomas goto done;
1912 8a35f56c 2022-07-16 thomas }
1913 8a35f56c 2022-07-16 thomas
1914 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1915 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1916 8a35f56c 2022-07-16 thomas done:
1917 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1918 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1919 8a35f56c 2022-07-16 thomas return error;
1920 8a35f56c 2022-07-16 thomas }
1921 8a35f56c 2022-07-16 thomas
1922 8a35f56c 2022-07-16 thomas static const struct got_error *
1923 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1924 4606e6d4 2022-11-23 thomas int dir)
1925 8a35f56c 2022-07-16 thomas {
1926 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1927 4606e6d4 2022-11-23 thomas struct stat sb;
1928 4606e6d4 2022-11-23 thomas int fd = -1;
1929 4606e6d4 2022-11-23 thomas off_t len;
1930 8a35f56c 2022-07-16 thomas
1931 8a35f56c 2022-07-16 thomas *url = NULL;
1932 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1933 8a35f56c 2022-07-16 thomas return NULL;
1934 8a35f56c 2022-07-16 thomas
1935 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
1936 4606e6d4 2022-11-23 thomas if (fd == -1) {
1937 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1938 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1939 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1940 4606e6d4 2022-11-23 thomas }
1941 8a35f56c 2022-07-16 thomas goto done;
1942 8a35f56c 2022-07-16 thomas }
1943 8a35f56c 2022-07-16 thomas
1944 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1945 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1946 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1947 8a35f56c 2022-07-16 thomas goto done;
1948 8a35f56c 2022-07-16 thomas }
1949 8a35f56c 2022-07-16 thomas
1950 4606e6d4 2022-11-23 thomas len = sb.st_size;
1951 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1952 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
1953 8a35f56c 2022-07-16 thomas
1954 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
1955 8a35f56c 2022-07-16 thomas if (*url == NULL) {
1956 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1957 8a35f56c 2022-07-16 thomas goto done;
1958 8a35f56c 2022-07-16 thomas }
1959 8a35f56c 2022-07-16 thomas
1960 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
1961 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1962 8a35f56c 2022-07-16 thomas done:
1963 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1964 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1965 8a35f56c 2022-07-16 thomas return error;
1966 8a35f56c 2022-07-16 thomas }
1967 8a35f56c 2022-07-16 thomas
1968 8a35f56c 2022-07-16 thomas const struct got_error *
1969 8a35f56c 2022-07-16 thomas gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1970 8a35f56c 2022-07-16 thomas {
1971 8a35f56c 2022-07-16 thomas struct tm tm;
1972 399ea8e4 2022-07-20 thomas long long diff_time;
1973 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
1974 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
1975 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
1976 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
1977 8a35f56c 2022-07-16 thomas char *s;
1978 d6795e9f 2022-12-30 thomas char datebuf[64];
1979 d6795e9f 2022-12-30 thomas size_t r;
1980 8a35f56c 2022-07-16 thomas
1981 8a35f56c 2022-07-16 thomas *repo_age = NULL;
1982 8a35f56c 2022-07-16 thomas
1983 8a35f56c 2022-07-16 thomas switch (ref_tm) {
1984 8a35f56c 2022-07-16 thomas case TM_DIFF:
1985 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
1986 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
1987 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1988 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
1989 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1990 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1991 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1992 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
1993 8a35f56c 2022-07-16 thomas months) == -1)
1994 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1995 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1996 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1997 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1998 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1999 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
2000 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2001 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
2002 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2003 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
2004 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2005 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
2006 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2007 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
2008 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2009 8a35f56c 2022-07-16 thomas minutes) == -1)
2010 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2011 8a35f56c 2022-07-16 thomas } else if (diff_time > 2) {
2012 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", diff_time,
2013 8a35f56c 2022-07-16 thomas seconds) == -1)
2014 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2015 8a35f56c 2022-07-16 thomas } else {
2016 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s", now) == -1)
2017 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2018 8a35f56c 2022-07-16 thomas }
2019 8a35f56c 2022-07-16 thomas break;
2020 8a35f56c 2022-07-16 thomas case TM_LONG:
2021 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2022 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
2023 8a35f56c 2022-07-16 thomas
2024 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
2025 8a35f56c 2022-07-16 thomas if (s == NULL)
2026 8a35f56c 2022-07-16 thomas return got_error_from_errno("asctime_r");
2027 8a35f56c 2022-07-16 thomas
2028 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2029 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2030 8a35f56c 2022-07-16 thomas break;
2031 d6795e9f 2022-12-30 thomas case TM_RFC822:
2032 d6795e9f 2022-12-30 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2033 d6795e9f 2022-12-30 thomas return got_error_from_errno("gmtime_r");
2034 d6795e9f 2022-12-30 thomas
2035 d6795e9f 2022-12-30 thomas r = strftime(datebuf, sizeof(datebuf),
2036 d6795e9f 2022-12-30 thomas "%a, %d %b %Y %H:%M:%S GMT", &tm);
2037 d6795e9f 2022-12-30 thomas if (r == 0)
2038 d6795e9f 2022-12-30 thomas return got_error(GOT_ERR_NO_SPACE);
2039 d6795e9f 2022-12-30 thomas
2040 d6795e9f 2022-12-30 thomas *repo_age = strdup(datebuf);
2041 d6795e9f 2022-12-30 thomas if (*repo_age == NULL)
2042 d6795e9f 2022-12-30 thomas return got_error_from_errno("asprintf");
2043 d6795e9f 2022-12-30 thomas break;
2044 8a35f56c 2022-07-16 thomas }
2045 8a35f56c 2022-07-16 thomas return NULL;
2046 3e12c168 2022-07-16 thomas }