Blame


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