Blame


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