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