Blame


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