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