Blame


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