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