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 8a35f56c 2022-07-16 thomas #include <stdio.h>
32 8a35f56c 2022-07-16 thomas #include <stdlib.h>
33 8a35f56c 2022-07-16 thomas #include <string.h>
34 8a35f56c 2022-07-16 thomas #include <unistd.h>
35 8a35f56c 2022-07-16 thomas
36 8a35f56c 2022-07-16 thomas #include "got_error.h"
37 8a35f56c 2022-07-16 thomas #include "got_object.h"
38 8a35f56c 2022-07-16 thomas #include "got_reference.h"
39 8a35f56c 2022-07-16 thomas #include "got_repository.h"
40 8a35f56c 2022-07-16 thomas #include "got_path.h"
41 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
42 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
43 8a35f56c 2022-07-16 thomas #include "got_diff.h"
44 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
45 8a35f56c 2022-07-16 thomas #include "got_blame.h"
46 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
47 8a35f56c 2022-07-16 thomas
48 8a35f56c 2022-07-16 thomas #include "proc.h"
49 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
50 ff36aeea 2022-07-16 thomas
51 ff36aeea 2022-07-16 thomas #include "got_compat.h"
52 8a35f56c 2022-07-16 thomas
53 8a35f56c 2022-07-16 thomas enum gotweb_ref_tm {
54 8a35f56c 2022-07-16 thomas TM_DIFF,
55 8a35f56c 2022-07-16 thomas TM_LONG,
56 8a35f56c 2022-07-16 thomas };
57 8a35f56c 2022-07-16 thomas
58 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
59 8a35f56c 2022-07-16 thomas { "action", ACTION },
60 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
61 8a35f56c 2022-07-16 thomas { "file", RFILE },
62 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
63 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
64 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
65 8a35f56c 2022-07-16 thomas { "path", PATH },
66 8a35f56c 2022-07-16 thomas { "page", PAGE },
67 8a35f56c 2022-07-16 thomas };
68 8a35f56c 2022-07-16 thomas
69 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
70 8a35f56c 2022-07-16 thomas { "blame", BLAME },
71 8a35f56c 2022-07-16 thomas { "blob", BLOB },
72 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
73 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
74 8a35f56c 2022-07-16 thomas { "diff", DIFF },
75 8a35f56c 2022-07-16 thomas { "error", ERR },
76 8a35f56c 2022-07-16 thomas { "index", INDEX },
77 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
78 8a35f56c 2022-07-16 thomas { "tag", TAG },
79 8a35f56c 2022-07-16 thomas { "tags", TAGS },
80 8a35f56c 2022-07-16 thomas { "tree", TREE },
81 8a35f56c 2022-07-16 thomas };
82 8a35f56c 2022-07-16 thomas
83 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
84 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 8a35f56c 2022-07-16 thomas char *);
86 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 8a35f56c 2022-07-16 thomas char *, char *);
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_header(struct request *);
89 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_footer(struct request *);
90 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_index(struct request *);
91 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
92 8a35f56c 2022-07-16 thomas const char *);
93 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
94 8a35f56c 2022-07-16 thomas struct repo_dir *);
95 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
96 8a35f56c 2022-07-16 thomas struct server *, char *);
97 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
98 8a35f56c 2022-07-16 thomas char *);
99 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_navs(struct request *);
100 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_blame(struct request *);
101 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_briefs(struct request *);
102 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_commits(struct request *);
103 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_diff(struct request *);
104 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_summary(struct request *);
105 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tag(struct request *);
106 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tags(struct request *);
107 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tree(struct request *);
108 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_branches(struct request *);
109 8a35f56c 2022-07-16 thomas
110 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
111 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
112 8a35f56c 2022-07-16 thomas
113 3f0158b4 2022-08-30 thomas struct server *gotweb_get_server(uint8_t *, uint8_t *);
114 8a35f56c 2022-07-16 thomas
115 8a35f56c 2022-07-16 thomas void
116 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
117 8a35f56c 2022-07-16 thomas {
118 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL, *error2 = NULL;
119 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
120 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
121 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
122 8a35f56c 2022-07-16 thomas uint8_t err[] = "gotwebd experienced an error: ";
123 79393471 2022-08-27 thomas int r, html = 0;
124 8a35f56c 2022-07-16 thomas
125 8a35f56c 2022-07-16 thomas /* init the transport */
126 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
127 8a35f56c 2022-07-16 thomas if (error) {
128 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
129 46aeda9a 2022-08-27 thomas return;
130 8a35f56c 2022-07-16 thomas }
131 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
132 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
133 8a35f56c 2022-07-16 thomas return;
134 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
135 3f0158b4 2022-08-30 thomas srv = gotweb_get_server(c->server_name, c->http_host);
136 8a35f56c 2022-07-16 thomas if (srv == NULL) {
137 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
138 8a35f56c 2022-07-16 thomas goto err;
139 8a35f56c 2022-07-16 thomas }
140 8a35f56c 2022-07-16 thomas c->srv = srv;
141 8a35f56c 2022-07-16 thomas /* parse our querystring */
142 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
143 8a35f56c 2022-07-16 thomas if (error) {
144 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
145 8a35f56c 2022-07-16 thomas goto err;
146 8a35f56c 2022-07-16 thomas }
147 8a35f56c 2022-07-16 thomas c->t->qs = qs;
148 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
149 8a35f56c 2022-07-16 thomas if (error) {
150 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
151 8a35f56c 2022-07-16 thomas goto err;
152 8a35f56c 2022-07-16 thomas }
153 8a35f56c 2022-07-16 thomas
154 8a35f56c 2022-07-16 thomas /*
155 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
156 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
157 8a35f56c 2022-07-16 thomas * querystring.
158 8a35f56c 2022-07-16 thomas */
159 8a35f56c 2022-07-16 thomas
160 8a35f56c 2022-07-16 thomas if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
161 8a35f56c 2022-07-16 thomas qs->action == DIFF)) {
162 8a35f56c 2022-07-16 thomas error2 = got_error(GOT_ERR_QUERYSTRING);
163 8a35f56c 2022-07-16 thomas goto render;
164 8a35f56c 2022-07-16 thomas }
165 8a35f56c 2022-07-16 thomas
166 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
167 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
168 8a35f56c 2022-07-16 thomas if (error)
169 8a35f56c 2022-07-16 thomas goto done;
170 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
171 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
172 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
173 8a35f56c 2022-07-16 thomas goto err;
174 8a35f56c 2022-07-16 thomas }
175 8a35f56c 2022-07-16 thomas
176 8a35f56c 2022-07-16 thomas /* render top of page */
177 8a35f56c 2022-07-16 thomas if (qs != NULL && qs->action == BLOB) {
178 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
179 8a35f56c 2022-07-16 thomas if (error)
180 8a35f56c 2022-07-16 thomas goto done;
181 8a35f56c 2022-07-16 thomas error = got_output_file_blob(c);
182 8a35f56c 2022-07-16 thomas if (error) {
183 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
184 8a35f56c 2022-07-16 thomas goto err;
185 8a35f56c 2022-07-16 thomas }
186 8a35f56c 2022-07-16 thomas goto done;
187 8a35f56c 2022-07-16 thomas } else {
188 8a35f56c 2022-07-16 thomas render:
189 8a35f56c 2022-07-16 thomas error = gotweb_render_content_type(c, "text/html");
190 8a35f56c 2022-07-16 thomas if (error) {
191 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
192 8a35f56c 2022-07-16 thomas goto err;
193 8a35f56c 2022-07-16 thomas }
194 8a35f56c 2022-07-16 thomas html = 1;
195 8a35f56c 2022-07-16 thomas }
196 8a35f56c 2022-07-16 thomas
197 8a35f56c 2022-07-16 thomas error = gotweb_render_header(c);
198 8a35f56c 2022-07-16 thomas if (error) {
199 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
200 8a35f56c 2022-07-16 thomas goto err;
201 8a35f56c 2022-07-16 thomas }
202 8a35f56c 2022-07-16 thomas
203 8a35f56c 2022-07-16 thomas if (error2) {
204 8a35f56c 2022-07-16 thomas error = error2;
205 8a35f56c 2022-07-16 thomas goto err;
206 8a35f56c 2022-07-16 thomas }
207 8a35f56c 2022-07-16 thomas
208 8a35f56c 2022-07-16 thomas switch(qs->action) {
209 8a35f56c 2022-07-16 thomas case BLAME:
210 8a35f56c 2022-07-16 thomas error = gotweb_render_blame(c);
211 8a35f56c 2022-07-16 thomas if (error) {
212 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
213 8a35f56c 2022-07-16 thomas goto err;
214 8a35f56c 2022-07-16 thomas }
215 8a35f56c 2022-07-16 thomas break;
216 8a35f56c 2022-07-16 thomas case BRIEFS:
217 8a35f56c 2022-07-16 thomas error = gotweb_render_briefs(c);
218 8a35f56c 2022-07-16 thomas if (error) {
219 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
220 8a35f56c 2022-07-16 thomas goto err;
221 8a35f56c 2022-07-16 thomas }
222 8a35f56c 2022-07-16 thomas break;
223 8a35f56c 2022-07-16 thomas case COMMITS:
224 8a35f56c 2022-07-16 thomas error = gotweb_render_commits(c);
225 8a35f56c 2022-07-16 thomas if (error) {
226 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
227 8a35f56c 2022-07-16 thomas goto err;
228 8a35f56c 2022-07-16 thomas }
229 8a35f56c 2022-07-16 thomas break;
230 8a35f56c 2022-07-16 thomas case DIFF:
231 8a35f56c 2022-07-16 thomas error = gotweb_render_diff(c);
232 8a35f56c 2022-07-16 thomas if (error) {
233 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
234 8a35f56c 2022-07-16 thomas goto err;
235 8a35f56c 2022-07-16 thomas }
236 8a35f56c 2022-07-16 thomas break;
237 8a35f56c 2022-07-16 thomas case INDEX:
238 8a35f56c 2022-07-16 thomas error = gotweb_render_index(c);
239 8a35f56c 2022-07-16 thomas if (error) {
240 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
241 8a35f56c 2022-07-16 thomas goto err;
242 8a35f56c 2022-07-16 thomas }
243 8a35f56c 2022-07-16 thomas break;
244 8a35f56c 2022-07-16 thomas case SUMMARY:
245 8a35f56c 2022-07-16 thomas error = gotweb_render_summary(c);
246 8a35f56c 2022-07-16 thomas if (error) {
247 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
248 8a35f56c 2022-07-16 thomas goto err;
249 8a35f56c 2022-07-16 thomas }
250 8a35f56c 2022-07-16 thomas break;
251 8a35f56c 2022-07-16 thomas case TAG:
252 8a35f56c 2022-07-16 thomas error = gotweb_render_tag(c);
253 8a35f56c 2022-07-16 thomas if (error) {
254 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
255 8a35f56c 2022-07-16 thomas goto err;
256 8a35f56c 2022-07-16 thomas }
257 8a35f56c 2022-07-16 thomas break;
258 8a35f56c 2022-07-16 thomas case TAGS:
259 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
260 8a35f56c 2022-07-16 thomas if (error) {
261 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
262 8a35f56c 2022-07-16 thomas goto err;
263 8a35f56c 2022-07-16 thomas }
264 8a35f56c 2022-07-16 thomas break;
265 8a35f56c 2022-07-16 thomas case TREE:
266 8a35f56c 2022-07-16 thomas error = gotweb_render_tree(c);
267 8a35f56c 2022-07-16 thomas if (error) {
268 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
269 8a35f56c 2022-07-16 thomas goto err;
270 8a35f56c 2022-07-16 thomas }
271 8a35f56c 2022-07-16 thomas break;
272 8a35f56c 2022-07-16 thomas case ERR:
273 8a35f56c 2022-07-16 thomas default:
274 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
275 79393471 2022-08-27 thomas "Erorr: Bad Querystring");
276 79393471 2022-08-27 thomas if (r == -1)
277 8a35f56c 2022-07-16 thomas goto err;
278 8a35f56c 2022-07-16 thomas break;
279 8a35f56c 2022-07-16 thomas }
280 8a35f56c 2022-07-16 thomas
281 8a35f56c 2022-07-16 thomas goto done;
282 8a35f56c 2022-07-16 thomas err:
283 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
284 8a35f56c 2022-07-16 thomas return;
285 a5f25a12 2022-10-31 thomas if (fcgi_printf(c, "\n%s", err) == -1)
286 8a35f56c 2022-07-16 thomas return;
287 8a35f56c 2022-07-16 thomas if (error) {
288 79393471 2022-08-27 thomas if (fcgi_printf(c, "%s", error->msg) == -1)
289 8a35f56c 2022-07-16 thomas return;
290 8a35f56c 2022-07-16 thomas } else {
291 79393471 2022-08-27 thomas if (fcgi_printf(c, "see daemon logs for details") == -1)
292 8a35f56c 2022-07-16 thomas return;
293 8a35f56c 2022-07-16 thomas }
294 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "</div>\n") == -1)
295 8a35f56c 2022-07-16 thomas return;
296 8a35f56c 2022-07-16 thomas done:
297 8a35f56c 2022-07-16 thomas if (html && srv != NULL)
298 8a35f56c 2022-07-16 thomas gotweb_render_footer(c);
299 8a35f56c 2022-07-16 thomas }
300 8a35f56c 2022-07-16 thomas
301 8a35f56c 2022-07-16 thomas struct server *
302 3f0158b4 2022-08-30 thomas gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
303 8a35f56c 2022-07-16 thomas {
304 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
305 8a35f56c 2022-07-16 thomas
306 3f0158b4 2022-08-30 thomas /* check against the server name first */
307 8a35f56c 2022-07-16 thomas if (strlen(server_name) > 0)
308 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
309 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
310 8a35f56c 2022-07-16 thomas goto done;
311 8a35f56c 2022-07-16 thomas
312 3f0158b4 2022-08-30 thomas /* check against subdomain second */
313 8a35f56c 2022-07-16 thomas if (strlen(subdomain) > 0)
314 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, subdomain) == 0)
316 8a35f56c 2022-07-16 thomas goto done;
317 8a35f56c 2022-07-16 thomas
318 8a35f56c 2022-07-16 thomas /* if those fail, send first server */
319 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
320 8a35f56c 2022-07-16 thomas if (srv != NULL)
321 8a35f56c 2022-07-16 thomas break;
322 8a35f56c 2022-07-16 thomas done:
323 8a35f56c 2022-07-16 thomas return srv;
324 8a35f56c 2022-07-16 thomas };
325 8a35f56c 2022-07-16 thomas
326 8a35f56c 2022-07-16 thomas const struct got_error *
327 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
328 8a35f56c 2022-07-16 thomas {
329 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
330 8a35f56c 2022-07-16 thomas
331 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
332 8a35f56c 2022-07-16 thomas if (*t == NULL)
333 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
334 8a35f56c 2022-07-16 thomas
335 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
336 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
337 8a35f56c 2022-07-16 thomas
338 8a35f56c 2022-07-16 thomas (*t)->repo = NULL;
339 8a35f56c 2022-07-16 thomas (*t)->repo_dir = NULL;
340 8a35f56c 2022-07-16 thomas (*t)->qs = NULL;
341 8a35f56c 2022-07-16 thomas (*t)->next_id = NULL;
342 8a35f56c 2022-07-16 thomas (*t)->prev_id = NULL;
343 8a35f56c 2022-07-16 thomas (*t)->next_disp = 0;
344 8a35f56c 2022-07-16 thomas (*t)->prev_disp = 0;
345 8a35f56c 2022-07-16 thomas
346 8a35f56c 2022-07-16 thomas return error;
347 8a35f56c 2022-07-16 thomas }
348 8a35f56c 2022-07-16 thomas
349 8a35f56c 2022-07-16 thomas static const struct got_error *
350 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
351 8a35f56c 2022-07-16 thomas {
352 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
353 8a35f56c 2022-07-16 thomas
354 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
355 8a35f56c 2022-07-16 thomas if (*qs == NULL)
356 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
357 8a35f56c 2022-07-16 thomas
358 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
359 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
360 84630254 2022-09-02 thomas free(*qs);
361 84630254 2022-09-02 thomas *qs = NULL;
362 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
363 8a35f56c 2022-07-16 thomas }
364 84630254 2022-09-02 thomas
365 84630254 2022-09-02 thomas (*qs)->action = INDEX;
366 84630254 2022-09-02 thomas (*qs)->commit = NULL;
367 84630254 2022-09-02 thomas (*qs)->file = NULL;
368 84630254 2022-09-02 thomas (*qs)->folder = NULL;
369 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
370 8a35f56c 2022-07-16 thomas (*qs)->index_page_str = NULL;
371 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
372 8a35f56c 2022-07-16 thomas
373 8a35f56c 2022-07-16 thomas return error;
374 8a35f56c 2022-07-16 thomas }
375 8a35f56c 2022-07-16 thomas
376 8a35f56c 2022-07-16 thomas static const struct got_error *
377 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
378 8a35f56c 2022-07-16 thomas {
379 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
380 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
381 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
382 8a35f56c 2022-07-16 thomas
383 8a35f56c 2022-07-16 thomas if (qst == NULL)
384 8a35f56c 2022-07-16 thomas return error;
385 8a35f56c 2022-07-16 thomas
386 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
387 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
388 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
389 8a35f56c 2022-07-16 thomas
390 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
391 8a35f56c 2022-07-16 thomas tok1_end = tok1;
392 8a35f56c 2022-07-16 thomas
393 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
394 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
395 8a35f56c 2022-07-16 thomas
396 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
397 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
398 8a35f56c 2022-07-16 thomas free(tok1);
399 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
400 8a35f56c 2022-07-16 thomas }
401 8a35f56c 2022-07-16 thomas
402 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
403 8a35f56c 2022-07-16 thomas tok2_end = tok2;
404 8a35f56c 2022-07-16 thomas
405 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
406 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
407 8a35f56c 2022-07-16 thomas if (tok2_end) {
408 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
409 8a35f56c 2022-07-16 thomas tok2_end);
410 8a35f56c 2022-07-16 thomas if (error)
411 8a35f56c 2022-07-16 thomas goto err;
412 8a35f56c 2022-07-16 thomas }
413 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
414 8a35f56c 2022-07-16 thomas }
415 8a35f56c 2022-07-16 thomas free(tok2);
416 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
417 8a35f56c 2022-07-16 thomas }
418 8a35f56c 2022-07-16 thomas free(tok1);
419 8a35f56c 2022-07-16 thomas return error;
420 8a35f56c 2022-07-16 thomas err:
421 8a35f56c 2022-07-16 thomas free(tok2);
422 8a35f56c 2022-07-16 thomas free(tok1);
423 8a35f56c 2022-07-16 thomas return error;
424 8a35f56c 2022-07-16 thomas }
425 8a35f56c 2022-07-16 thomas
426 2ac684a4 2022-09-03 thomas /*
427 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
428 2ac684a4 2022-09-03 thomas */
429 8a35f56c 2022-07-16 thomas static const struct got_error *
430 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
431 2ac684a4 2022-09-03 thomas {
432 2ac684a4 2022-09-03 thomas char *p, *q;
433 2ac684a4 2022-09-03 thomas char hex[3];
434 2ac684a4 2022-09-03 thomas unsigned long x;
435 2ac684a4 2022-09-03 thomas
436 2ac684a4 2022-09-03 thomas hex[2] = '\0';
437 2ac684a4 2022-09-03 thomas p = q = url;
438 2ac684a4 2022-09-03 thomas
439 2ac684a4 2022-09-03 thomas while (*p != '\0') {
440 2ac684a4 2022-09-03 thomas switch (*p) {
441 2ac684a4 2022-09-03 thomas case '%':
442 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
443 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
444 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
445 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
446 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
447 2ac684a4 2022-09-03 thomas
448 2ac684a4 2022-09-03 thomas hex[0] = p[1];
449 2ac684a4 2022-09-03 thomas hex[1] = p[2];
450 2ac684a4 2022-09-03 thomas
451 2ac684a4 2022-09-03 thomas /*
452 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
453 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
454 2ac684a4 2022-09-03 thomas */
455 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
456 2ac684a4 2022-09-03 thomas *q = (char)x;
457 2ac684a4 2022-09-03 thomas p += 2;
458 2ac684a4 2022-09-03 thomas break;
459 2ac684a4 2022-09-03 thomas default:
460 2ac684a4 2022-09-03 thomas *q = *p;
461 2ac684a4 2022-09-03 thomas break;
462 2ac684a4 2022-09-03 thomas }
463 2ac684a4 2022-09-03 thomas p++;
464 2ac684a4 2022-09-03 thomas q++;
465 2ac684a4 2022-09-03 thomas }
466 2ac684a4 2022-09-03 thomas *q = '\0';
467 2ac684a4 2022-09-03 thomas
468 2ac684a4 2022-09-03 thomas return NULL;
469 2ac684a4 2022-09-03 thomas }
470 2ac684a4 2022-09-03 thomas
471 2ac684a4 2022-09-03 thomas static const struct got_error *
472 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
473 8a35f56c 2022-07-16 thomas {
474 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
475 8a35f56c 2022-07-16 thomas const char *errstr;
476 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
477 8a35f56c 2022-07-16 thomas
478 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
479 2ac684a4 2022-09-03 thomas if (error)
480 2ac684a4 2022-09-03 thomas return error;
481 2ac684a4 2022-09-03 thomas
482 8a35f56c 2022-07-16 thomas for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
483 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
484 8a35f56c 2022-07-16 thomas continue;
485 8a35f56c 2022-07-16 thomas
486 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
487 8a35f56c 2022-07-16 thomas case ACTION:
488 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
489 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
490 8a35f56c 2022-07-16 thomas continue;
491 8a35f56c 2022-07-16 thomas else if (strcmp(value,
492 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
493 8a35f56c 2022-07-16 thomas (*qs)->action =
494 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
495 8a35f56c 2022-07-16 thomas goto qa_found;
496 8a35f56c 2022-07-16 thomas }
497 8a35f56c 2022-07-16 thomas }
498 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
499 8a35f56c 2022-07-16 thomas qa_found:
500 8a35f56c 2022-07-16 thomas break;
501 8a35f56c 2022-07-16 thomas case COMMIT:
502 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
503 8a35f56c 2022-07-16 thomas if ((*qs)->commit == 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 RFILE:
510 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
511 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
512 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
513 8a35f56c 2022-07-16 thomas __func__);
514 8a35f56c 2022-07-16 thomas goto done;
515 8a35f56c 2022-07-16 thomas }
516 8a35f56c 2022-07-16 thomas break;
517 8a35f56c 2022-07-16 thomas case FOLDER:
518 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
519 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
520 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
521 8a35f56c 2022-07-16 thomas __func__);
522 8a35f56c 2022-07-16 thomas goto done;
523 8a35f56c 2022-07-16 thomas }
524 8a35f56c 2022-07-16 thomas break;
525 8a35f56c 2022-07-16 thomas case HEADREF:
526 b0764984 2022-09-02 thomas free((*qs)->headref);
527 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
528 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
529 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
530 8a35f56c 2022-07-16 thomas __func__);
531 8a35f56c 2022-07-16 thomas goto done;
532 8a35f56c 2022-07-16 thomas }
533 8a35f56c 2022-07-16 thomas break;
534 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
535 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
536 8a35f56c 2022-07-16 thomas break;
537 8a35f56c 2022-07-16 thomas (*qs)->index_page_str = strdup(value);
538 8a35f56c 2022-07-16 thomas if ((*qs)->index_page_str == NULL) {
539 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
540 8a35f56c 2022-07-16 thomas __func__);
541 8a35f56c 2022-07-16 thomas goto done;
542 8a35f56c 2022-07-16 thomas }
543 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
544 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
545 8a35f56c 2022-07-16 thomas if (errstr) {
546 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
547 8a35f56c 2022-07-16 thomas __func__, errstr);
548 8a35f56c 2022-07-16 thomas goto done;
549 8a35f56c 2022-07-16 thomas }
550 8a35f56c 2022-07-16 thomas if ((*qs)->index_page < 0) {
551 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
552 8a35f56c 2022-07-16 thomas sprintf((*qs)->index_page_str, "%d", 0);
553 8a35f56c 2022-07-16 thomas }
554 8a35f56c 2022-07-16 thomas break;
555 8a35f56c 2022-07-16 thomas case PATH:
556 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
557 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
558 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
559 8a35f56c 2022-07-16 thomas __func__);
560 8a35f56c 2022-07-16 thomas goto done;
561 8a35f56c 2022-07-16 thomas }
562 8a35f56c 2022-07-16 thomas break;
563 8a35f56c 2022-07-16 thomas case PAGE:
564 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
565 8a35f56c 2022-07-16 thomas break;
566 8a35f56c 2022-07-16 thomas (*qs)->page_str = strdup(value);
567 8a35f56c 2022-07-16 thomas if ((*qs)->page_str == NULL) {
568 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
569 8a35f56c 2022-07-16 thomas __func__);
570 8a35f56c 2022-07-16 thomas goto done;
571 8a35f56c 2022-07-16 thomas }
572 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
573 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
574 8a35f56c 2022-07-16 thomas if (errstr) {
575 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
576 8a35f56c 2022-07-16 thomas __func__, errstr);
577 8a35f56c 2022-07-16 thomas goto done;
578 8a35f56c 2022-07-16 thomas }
579 8a35f56c 2022-07-16 thomas if ((*qs)->page < 0) {
580 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
581 8a35f56c 2022-07-16 thomas sprintf((*qs)->page_str, "%d", 0);
582 8a35f56c 2022-07-16 thomas }
583 8a35f56c 2022-07-16 thomas break;
584 8a35f56c 2022-07-16 thomas default:
585 8a35f56c 2022-07-16 thomas break;
586 8a35f56c 2022-07-16 thomas }
587 8a35f56c 2022-07-16 thomas }
588 8a35f56c 2022-07-16 thomas done:
589 8a35f56c 2022-07-16 thomas return error;
590 8a35f56c 2022-07-16 thomas }
591 8a35f56c 2022-07-16 thomas
592 8a35f56c 2022-07-16 thomas void
593 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
594 8a35f56c 2022-07-16 thomas {
595 8a35f56c 2022-07-16 thomas if (rt != NULL) {
596 8a35f56c 2022-07-16 thomas free(rt->commit_id);
597 217813df 2022-09-02 thomas free(rt->tag_name);
598 217813df 2022-09-02 thomas free(rt->tag_commit);
599 217813df 2022-09-02 thomas free(rt->commit_msg);
600 8a35f56c 2022-07-16 thomas free(rt->tagger);
601 8a35f56c 2022-07-16 thomas }
602 8a35f56c 2022-07-16 thomas free(rt);
603 8a35f56c 2022-07-16 thomas }
604 8a35f56c 2022-07-16 thomas
605 8a35f56c 2022-07-16 thomas void
606 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
607 8a35f56c 2022-07-16 thomas {
608 8a35f56c 2022-07-16 thomas if (rc != NULL) {
609 8a35f56c 2022-07-16 thomas free(rc->path);
610 8a35f56c 2022-07-16 thomas free(rc->refs_str);
611 8a35f56c 2022-07-16 thomas free(rc->commit_id);
612 8a35f56c 2022-07-16 thomas free(rc->parent_id);
613 8a35f56c 2022-07-16 thomas free(rc->tree_id);
614 8a35f56c 2022-07-16 thomas free(rc->author);
615 8a35f56c 2022-07-16 thomas free(rc->committer);
616 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
617 8a35f56c 2022-07-16 thomas }
618 8a35f56c 2022-07-16 thomas free(rc);
619 8a35f56c 2022-07-16 thomas }
620 8a35f56c 2022-07-16 thomas
621 8a35f56c 2022-07-16 thomas static void
622 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
623 8a35f56c 2022-07-16 thomas {
624 8a35f56c 2022-07-16 thomas if (qs != NULL) {
625 8a35f56c 2022-07-16 thomas free(qs->commit);
626 8a35f56c 2022-07-16 thomas free(qs->file);
627 8a35f56c 2022-07-16 thomas free(qs->folder);
628 8a35f56c 2022-07-16 thomas free(qs->headref);
629 8a35f56c 2022-07-16 thomas free(qs->index_page_str);
630 8a35f56c 2022-07-16 thomas free(qs->path);
631 8a35f56c 2022-07-16 thomas free(qs->page_str);
632 8a35f56c 2022-07-16 thomas }
633 8a35f56c 2022-07-16 thomas free(qs);
634 8a35f56c 2022-07-16 thomas }
635 8a35f56c 2022-07-16 thomas
636 8a35f56c 2022-07-16 thomas static void
637 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
638 8a35f56c 2022-07-16 thomas {
639 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
640 8a35f56c 2022-07-16 thomas free(repo_dir->name);
641 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
642 8a35f56c 2022-07-16 thomas free(repo_dir->description);
643 8a35f56c 2022-07-16 thomas free(repo_dir->url);
644 8a35f56c 2022-07-16 thomas free(repo_dir->age);
645 8a35f56c 2022-07-16 thomas free(repo_dir->path);
646 8a35f56c 2022-07-16 thomas }
647 8a35f56c 2022-07-16 thomas free(repo_dir);
648 8a35f56c 2022-07-16 thomas }
649 8a35f56c 2022-07-16 thomas
650 8a35f56c 2022-07-16 thomas void
651 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
652 8a35f56c 2022-07-16 thomas {
653 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
654 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
655 8a35f56c 2022-07-16 thomas
656 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
657 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
658 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
659 8a35f56c 2022-07-16 thomas }
660 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
661 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
662 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
663 8a35f56c 2022-07-16 thomas }
664 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
665 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
666 6b42af1e 2022-09-02 thomas free(t->next_id);
667 6b42af1e 2022-09-02 thomas free(t->prev_id);
668 8a35f56c 2022-07-16 thomas free(t);
669 8a35f56c 2022-07-16 thomas }
670 8a35f56c 2022-07-16 thomas
671 8a35f56c 2022-07-16 thomas const struct got_error *
672 8a35f56c 2022-07-16 thomas gotweb_render_content_type(struct request *c, const uint8_t *type)
673 8a35f56c 2022-07-16 thomas {
674 0b75e088 2022-08-27 thomas const char *csp = "default-src 'self'; script-src 'none'; "
675 0b75e088 2022-08-27 thomas "object-src 'none';";
676 0b75e088 2022-08-27 thomas
677 0b75e088 2022-08-27 thomas fcgi_printf(c,
678 0b75e088 2022-08-27 thomas "Content-Security-Policy: %s\r\n"
679 0b75e088 2022-08-27 thomas "Content-Type: %s\r\n\r\n",
680 0b75e088 2022-08-27 thomas csp, type);
681 79393471 2022-08-27 thomas return NULL;
682 8a35f56c 2022-07-16 thomas }
683 8a35f56c 2022-07-16 thomas
684 8a35f56c 2022-07-16 thomas const struct got_error *
685 8a35f56c 2022-07-16 thomas gotweb_render_content_type_file(struct request *c, const uint8_t *type,
686 8a35f56c 2022-07-16 thomas char *file)
687 8a35f56c 2022-07-16 thomas {
688 79393471 2022-08-27 thomas fcgi_printf(c, "Content-type: %s\r\n"
689 8a35f56c 2022-07-16 thomas "Content-disposition: attachment; filename=%s\r\n\r\n",
690 79393471 2022-08-27 thomas type, file);
691 79393471 2022-08-27 thomas return NULL;
692 8a35f56c 2022-07-16 thomas }
693 8a35f56c 2022-07-16 thomas
694 8a35f56c 2022-07-16 thomas static const struct got_error *
695 8a35f56c 2022-07-16 thomas gotweb_render_header(struct request *c)
696 8a35f56c 2022-07-16 thomas {
697 55144267 2022-09-07 thomas const struct got_error *err = NULL;
698 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
699 8a35f56c 2022-07-16 thomas struct querystring *qs = c->t->qs;
700 79393471 2022-08-27 thomas int r;
701 8a35f56c 2022-07-16 thomas
702 79393471 2022-08-27 thomas r = fcgi_printf(c, "<!doctype html>\n"
703 79393471 2022-08-27 thomas "<html>\n"
704 79393471 2022-08-27 thomas "<head>\n"
705 79393471 2022-08-27 thomas "<title>%s</title>\n"
706 79393471 2022-08-27 thomas "<meta charset='utf-8' />\n"
707 79393471 2022-08-27 thomas "<meta name='viewport' content='initial-scale=.75' />\n"
708 79393471 2022-08-27 thomas "<meta name='msapplication-TileColor' content='#da532c' />\n"
709 79393471 2022-08-27 thomas "<meta name='theme-color' content='#ffffff'/>\n"
710 79393471 2022-08-27 thomas "<link rel='apple-touch-icon' sizes='180x180'"
711 fb3a8240 2022-09-02 thomas " href='%sapple-touch-icon.png' />\n"
712 79393471 2022-08-27 thomas "<link rel='icon' type='image/png' sizes='32x32'"
713 fb3a8240 2022-09-02 thomas " href='%sfavicon-32x32.png' />\n"
714 79393471 2022-08-27 thomas "<link rel='icon' type='image/png' sizes='16x16'"
715 fb3a8240 2022-09-02 thomas " href='%sfavicon-16x16.png' />\n"
716 fb3a8240 2022-09-02 thomas "<link rel='manifest' href='%ssite.webmanifest'/>\n"
717 fb3a8240 2022-09-02 thomas "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
718 79393471 2022-08-27 thomas "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
719 79393471 2022-08-27 thomas "</head>\n"
720 79393471 2022-08-27 thomas "<body>\n"
721 79393471 2022-08-27 thomas "<div id='gw_body'>\n"
722 79393471 2022-08-27 thomas "<div id='header'>\n"
723 79393471 2022-08-27 thomas "<div id='got_link'>"
724 0b3823fd 2022-08-27 thomas "<a href='%s' target='_blank'>"
725 79393471 2022-08-27 thomas "<img src='%s%s' alt='logo' id='logo' />"
726 79393471 2022-08-27 thomas "</a>\n"
727 79393471 2022-08-27 thomas "</div>\n" /* #got_link */
728 79393471 2022-08-27 thomas "</div>\n" /* #header */
729 79393471 2022-08-27 thomas "<div id='site_path'>\n"
730 79393471 2022-08-27 thomas "<div id='site_link'>\n"
731 3f0158b4 2022-08-30 thomas "<a href='?index_page=%d'>%s</a>",
732 686a7117 2022-09-06 thomas srv->site_name,
733 fb3a8240 2022-09-02 thomas c->script_name,
734 fb3a8240 2022-09-02 thomas c->script_name,
735 fb3a8240 2022-09-02 thomas c->script_name,
736 fb3a8240 2022-09-02 thomas c->script_name,
737 fb3a8240 2022-09-02 thomas c->script_name,
738 3f0158b4 2022-08-30 thomas c->script_name, srv->custom_css,
739 79393471 2022-08-27 thomas srv->logo_url,
740 3f0158b4 2022-08-30 thomas c->script_name, srv->logo,
741 3f0158b4 2022-08-30 thomas qs->index_page, srv->site_link);
742 79393471 2022-08-27 thomas if (r == -1)
743 8a35f56c 2022-07-16 thomas goto done;
744 8a35f56c 2022-07-16 thomas
745 790ca73c 2022-09-22 thomas if (qs->path != NULL) {
746 790ca73c 2022-09-22 thomas char *epath;
747 55144267 2022-09-07 thomas
748 790ca73c 2022-09-22 thomas if (fcgi_printf(c, " / ") == -1)
749 790ca73c 2022-09-22 thomas goto done;
750 55144267 2022-09-07 thomas
751 790ca73c 2022-09-22 thomas err = gotweb_escape_html(&epath, qs->path);
752 790ca73c 2022-09-22 thomas if (err)
753 790ca73c 2022-09-22 thomas return err;
754 790ca73c 2022-09-22 thomas r = gotweb_link(c, &(struct gotweb_url){
755 790ca73c 2022-09-22 thomas .action = SUMMARY,
756 790ca73c 2022-09-22 thomas .index_page = -1,
757 790ca73c 2022-09-22 thomas .page = -1,
758 790ca73c 2022-09-22 thomas .path = qs->path,
759 790ca73c 2022-09-22 thomas }, "%s", epath);
760 790ca73c 2022-09-22 thomas free(epath);
761 790ca73c 2022-09-22 thomas if (r == -1)
762 790ca73c 2022-09-22 thomas goto done;
763 790ca73c 2022-09-22 thomas }
764 790ca73c 2022-09-22 thomas if (qs->action != INDEX) {
765 790ca73c 2022-09-22 thomas const char *action = "";
766 79393471 2022-08-27 thomas
767 790ca73c 2022-09-22 thomas switch (qs->action) {
768 790ca73c 2022-09-22 thomas case BLAME:
769 790ca73c 2022-09-22 thomas action = "blame";
770 790ca73c 2022-09-22 thomas break;
771 790ca73c 2022-09-22 thomas case BRIEFS:
772 790ca73c 2022-09-22 thomas action = "briefs";
773 790ca73c 2022-09-22 thomas break;
774 790ca73c 2022-09-22 thomas case COMMITS:
775 790ca73c 2022-09-22 thomas action = "commits";
776 790ca73c 2022-09-22 thomas break;
777 790ca73c 2022-09-22 thomas case DIFF:
778 790ca73c 2022-09-22 thomas action = "diff";
779 790ca73c 2022-09-22 thomas break;
780 790ca73c 2022-09-22 thomas case SUMMARY:
781 790ca73c 2022-09-22 thomas action = "summary";
782 790ca73c 2022-09-22 thomas break;
783 790ca73c 2022-09-22 thomas case TAG:
784 790ca73c 2022-09-22 thomas action = "tag";
785 790ca73c 2022-09-22 thomas break;
786 790ca73c 2022-09-22 thomas case TAGS:
787 790ca73c 2022-09-22 thomas action = "tags";
788 790ca73c 2022-09-22 thomas break;
789 790ca73c 2022-09-22 thomas case TREE:
790 790ca73c 2022-09-22 thomas action = "tree";
791 790ca73c 2022-09-22 thomas break;
792 79393471 2022-08-27 thomas }
793 790ca73c 2022-09-22 thomas
794 790ca73c 2022-09-22 thomas if (fcgi_printf(c, " / %s", action) == -1)
795 790ca73c 2022-09-22 thomas goto done;
796 8a35f56c 2022-07-16 thomas }
797 8a35f56c 2022-07-16 thomas
798 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n" /* #site_path */
799 79393471 2022-08-27 thomas "</div>\n" /* #site_link */
800 79393471 2022-08-27 thomas "<div id='content'>\n");
801 79393471 2022-08-27 thomas
802 79393471 2022-08-27 thomas done:
803 79393471 2022-08-27 thomas return NULL;
804 8a35f56c 2022-07-16 thomas }
805 8a35f56c 2022-07-16 thomas
806 8a35f56c 2022-07-16 thomas static const struct got_error *
807 8a35f56c 2022-07-16 thomas gotweb_render_footer(struct request *c)
808 8a35f56c 2022-07-16 thomas {
809 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
810 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
811 79393471 2022-08-27 thomas const char *siteowner = "&nbsp;";
812 79393471 2022-08-27 thomas char *escaped_owner = NULL;
813 8a35f56c 2022-07-16 thomas
814 8a35f56c 2022-07-16 thomas if (srv->show_site_owner) {
815 79393471 2022-08-27 thomas error = gotweb_escape_html(&escaped_owner, srv->site_owner);
816 8a35f56c 2022-07-16 thomas if (error)
817 79393471 2022-08-27 thomas return error;
818 79393471 2022-08-27 thomas siteowner = escaped_owner;
819 79393471 2022-08-27 thomas }
820 8a35f56c 2022-07-16 thomas
821 79393471 2022-08-27 thomas fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
822 79393471 2022-08-27 thomas "<div id='site_owner'>%s</div>\n"
823 79393471 2022-08-27 thomas "</div>\n" /* #site_owner_wrapper */
824 79393471 2022-08-27 thomas "</div>\n" /* #content */
825 79393471 2022-08-27 thomas "</div>\n" /* #gw_body */
826 79393471 2022-08-27 thomas "</body>\n</html>\n", siteowner);
827 79393471 2022-08-27 thomas
828 79393471 2022-08-27 thomas free(escaped_owner);
829 79393471 2022-08-27 thomas return NULL;
830 8a35f56c 2022-07-16 thomas }
831 8a35f56c 2022-07-16 thomas
832 8a35f56c 2022-07-16 thomas static const struct got_error *
833 8a35f56c 2022-07-16 thomas gotweb_render_navs(struct request *c)
834 8a35f56c 2022-07-16 thomas {
835 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
836 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
837 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
838 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
839 55144267 2022-09-07 thomas int r;
840 8a35f56c 2022-07-16 thomas
841 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
842 79393471 2022-08-27 thomas if (r == -1)
843 8a35f56c 2022-07-16 thomas goto done;
844 8a35f56c 2022-07-16 thomas
845 8a35f56c 2022-07-16 thomas switch(qs->action) {
846 8a35f56c 2022-07-16 thomas case INDEX:
847 8a35f56c 2022-07-16 thomas if (qs->index_page > 0) {
848 55144267 2022-09-07 thomas struct gotweb_url url = {
849 55144267 2022-09-07 thomas .action = -1,
850 55144267 2022-09-07 thomas .index_page = qs->index_page - 1,
851 55144267 2022-09-07 thomas .page = -1,
852 55144267 2022-09-07 thomas };
853 55144267 2022-09-07 thomas
854 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Previous");
855 8a35f56c 2022-07-16 thomas }
856 8a35f56c 2022-07-16 thomas break;
857 8a35f56c 2022-07-16 thomas case BRIEFS:
858 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
859 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
860 55144267 2022-09-07 thomas struct gotweb_url url = {
861 55144267 2022-09-07 thomas .action = BRIEFS,
862 55144267 2022-09-07 thomas .index_page = -1,
863 55144267 2022-09-07 thomas .page = qs->page - 1,
864 55144267 2022-09-07 thomas .path = qs->path,
865 55144267 2022-09-07 thomas .commit = t->prev_id,
866 55144267 2022-09-07 thomas .headref = qs->headref,
867 55144267 2022-09-07 thomas };
868 55144267 2022-09-07 thomas
869 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Previous");
870 8a35f56c 2022-07-16 thomas }
871 8a35f56c 2022-07-16 thomas break;
872 8a35f56c 2022-07-16 thomas case COMMITS:
873 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
874 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
875 55144267 2022-09-07 thomas struct gotweb_url url = {
876 55144267 2022-09-07 thomas .action = COMMIT,
877 55144267 2022-09-07 thomas .index_page = -1,
878 55144267 2022-09-07 thomas .page = qs->page - 1,
879 55144267 2022-09-07 thomas .path = qs->path,
880 55144267 2022-09-07 thomas .commit = t->prev_id,
881 55144267 2022-09-07 thomas .headref = qs->headref,
882 55144267 2022-09-07 thomas .folder = qs->folder,
883 55144267 2022-09-07 thomas .file = qs->file,
884 55144267 2022-09-07 thomas };
885 55144267 2022-09-07 thomas
886 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Previous");
887 8a35f56c 2022-07-16 thomas }
888 8a35f56c 2022-07-16 thomas break;
889 8a35f56c 2022-07-16 thomas case TAGS:
890 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
891 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
892 55144267 2022-09-07 thomas struct gotweb_url url = {
893 55144267 2022-09-07 thomas .action = TAGS,
894 55144267 2022-09-07 thomas .index_page = -1,
895 55144267 2022-09-07 thomas .page = qs->page - 1,
896 55144267 2022-09-07 thomas .path = qs->path,
897 55144267 2022-09-07 thomas .commit = t->prev_id,
898 55144267 2022-09-07 thomas .headref = qs->headref,
899 55144267 2022-09-07 thomas };
900 55144267 2022-09-07 thomas
901 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Previous");
902 8a35f56c 2022-07-16 thomas }
903 8a35f56c 2022-07-16 thomas break;
904 8a35f56c 2022-07-16 thomas }
905 8a35f56c 2022-07-16 thomas
906 55144267 2022-09-07 thomas if (r == -1)
907 55144267 2022-09-07 thomas goto done;
908 79393471 2022-08-27 thomas
909 79393471 2022-08-27 thomas r = fcgi_printf(c, "</div>\n" /* #nav_prev */
910 79393471 2022-08-27 thomas "<div id='nav_next'>");
911 79393471 2022-08-27 thomas if (r == -1)
912 8a35f56c 2022-07-16 thomas goto done;
913 8a35f56c 2022-07-16 thomas
914 8a35f56c 2022-07-16 thomas switch(qs->action) {
915 8a35f56c 2022-07-16 thomas case INDEX:
916 8a35f56c 2022-07-16 thomas if (t->next_disp == srv->max_repos_display &&
917 8a35f56c 2022-07-16 thomas t->repos_total != (qs->index_page + 1) *
918 8a35f56c 2022-07-16 thomas srv->max_repos_display) {
919 55144267 2022-09-07 thomas struct gotweb_url url = {
920 55144267 2022-09-07 thomas .action = -1,
921 55144267 2022-09-07 thomas .index_page = qs->index_page + 1,
922 55144267 2022-09-07 thomas .page = -1,
923 55144267 2022-09-07 thomas };
924 55144267 2022-09-07 thomas
925 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Next");
926 8a35f56c 2022-07-16 thomas }
927 8a35f56c 2022-07-16 thomas break;
928 8a35f56c 2022-07-16 thomas case BRIEFS:
929 8a35f56c 2022-07-16 thomas if (t->next_id) {
930 55144267 2022-09-07 thomas struct gotweb_url url = {
931 55144267 2022-09-07 thomas .action = BRIEFS,
932 55144267 2022-09-07 thomas .index_page = -1,
933 55144267 2022-09-07 thomas .page = qs->page + 1,
934 55144267 2022-09-07 thomas .path = qs->path,
935 55144267 2022-09-07 thomas .commit = t->next_id,
936 55144267 2022-09-07 thomas .headref = qs->headref,
937 55144267 2022-09-07 thomas };
938 55144267 2022-09-07 thomas
939 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Next");
940 8a35f56c 2022-07-16 thomas }
941 8a35f56c 2022-07-16 thomas break;
942 8a35f56c 2022-07-16 thomas case COMMITS:
943 8a35f56c 2022-07-16 thomas if (t->next_id) {
944 55144267 2022-09-07 thomas struct gotweb_url url = {
945 55144267 2022-09-07 thomas .action = COMMIT,
946 55144267 2022-09-07 thomas .index_page = -1,
947 55144267 2022-09-07 thomas .page = qs->page + 1,
948 55144267 2022-09-07 thomas .path = qs->path,
949 55144267 2022-09-07 thomas .commit = t->next_id,
950 55144267 2022-09-07 thomas .headref = qs->headref,
951 55144267 2022-09-07 thomas .folder = qs->folder,
952 55144267 2022-09-07 thomas .file = qs->file,
953 55144267 2022-09-07 thomas };
954 55144267 2022-09-07 thomas
955 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Next");
956 8a35f56c 2022-07-16 thomas }
957 8a35f56c 2022-07-16 thomas break;
958 8a35f56c 2022-07-16 thomas case TAGS:
959 8a35f56c 2022-07-16 thomas if (t->next_id) {
960 55144267 2022-09-07 thomas struct gotweb_url url = {
961 55144267 2022-09-07 thomas .action = TAGS,
962 55144267 2022-09-07 thomas .index_page = -1,
963 55144267 2022-09-07 thomas .page = qs->page + 1,
964 55144267 2022-09-07 thomas .path = qs->path,
965 55144267 2022-09-07 thomas .commit = t->next_id,
966 55144267 2022-09-07 thomas .headref = qs->headref,
967 55144267 2022-09-07 thomas };
968 55144267 2022-09-07 thomas
969 55144267 2022-09-07 thomas r = gotweb_link(c, &url, "Next");
970 8a35f56c 2022-07-16 thomas }
971 8a35f56c 2022-07-16 thomas break;
972 8a35f56c 2022-07-16 thomas }
973 55144267 2022-09-07 thomas if (r == -1)
974 55144267 2022-09-07 thomas goto done;
975 55144267 2022-09-07 thomas
976 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #nav_next */
977 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #np_wrapper */
978 8a35f56c 2022-07-16 thomas done:
979 8a35f56c 2022-07-16 thomas free(t->next_id);
980 8a35f56c 2022-07-16 thomas t->next_id = NULL;
981 8a35f56c 2022-07-16 thomas free(t->prev_id);
982 8a35f56c 2022-07-16 thomas t->prev_id = NULL;
983 8a35f56c 2022-07-16 thomas return error;
984 8a35f56c 2022-07-16 thomas }
985 8a35f56c 2022-07-16 thomas
986 8a35f56c 2022-07-16 thomas static const struct got_error *
987 8a35f56c 2022-07-16 thomas gotweb_render_index(struct request *c)
988 8a35f56c 2022-07-16 thomas {
989 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
990 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
991 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
992 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
993 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
994 8a35f56c 2022-07-16 thomas DIR *d;
995 f530ab0e 2022-09-02 thomas struct dirent **sd_dent = NULL;
996 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
997 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
998 24240f6a 2022-11-23 thomas int r, type;
999 8a35f56c 2022-07-16 thomas
1000 8a35f56c 2022-07-16 thomas d = opendir(srv->repos_path);
1001 8a35f56c 2022-07-16 thomas if (d == NULL) {
1002 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("opendir", srv->repos_path);
1003 8a35f56c 2022-07-16 thomas return error;
1004 8a35f56c 2022-07-16 thomas }
1005 8a35f56c 2022-07-16 thomas
1006 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1007 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
1008 f530ab0e 2022-09-02 thomas sd_dent = NULL;
1009 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
1010 8a35f56c 2022-07-16 thomas goto done;
1011 8a35f56c 2022-07-16 thomas }
1012 8a35f56c 2022-07-16 thomas
1013 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='index_header'>\n"
1014 79393471 2022-08-27 thomas "<div id='index_header_project'>Project</div>\n");
1015 79393471 2022-08-27 thomas if (r == -1)
1016 8a35f56c 2022-07-16 thomas goto done;
1017 79393471 2022-08-27 thomas
1018 8a35f56c 2022-07-16 thomas if (srv->show_repo_description)
1019 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='index_header_description'>"
1020 8a35f56c 2022-07-16 thomas "Description</div>\n") == -1)
1021 8a35f56c 2022-07-16 thomas goto done;
1022 8a35f56c 2022-07-16 thomas if (srv->show_repo_owner)
1023 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='index_header_owner'>"
1024 8a35f56c 2022-07-16 thomas "Owner</div>\n") == -1)
1025 8a35f56c 2022-07-16 thomas goto done;
1026 8a35f56c 2022-07-16 thomas if (srv->show_repo_age)
1027 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='index_header_age'>"
1028 8a35f56c 2022-07-16 thomas "Last Change</div>\n") == -1)
1029 8a35f56c 2022-07-16 thomas goto done;
1030 79393471 2022-08-27 thomas if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1031 8a35f56c 2022-07-16 thomas goto done;
1032 8a35f56c 2022-07-16 thomas
1033 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
1034 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
1035 57e88d7c 2022-11-23 thomas break;
1036 8a35f56c 2022-07-16 thomas
1037 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1038 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
1039 24240f6a 2022-11-23 thomas d_skipped++;
1040 24240f6a 2022-11-23 thomas continue;
1041 24240f6a 2022-11-23 thomas }
1042 24240f6a 2022-11-23 thomas
1043 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
1044 24240f6a 2022-11-23 thomas sd_dent[d_i]);
1045 24240f6a 2022-11-23 thomas if (error)
1046 24240f6a 2022-11-23 thomas goto done;
1047 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
1048 24240f6a 2022-11-23 thomas d_skipped++;
1049 8a35f56c 2022-07-16 thomas continue;
1050 24240f6a 2022-11-23 thomas }
1051 8a35f56c 2022-07-16 thomas
1052 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
1053 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
1054 8a35f56c 2022-07-16 thomas t->prev_disp++;
1055 8a35f56c 2022-07-16 thomas continue;
1056 8a35f56c 2022-07-16 thomas }
1057 8a35f56c 2022-07-16 thomas
1058 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1059 8a35f56c 2022-07-16 thomas if (error)
1060 8a35f56c 2022-07-16 thomas goto done;
1061 8a35f56c 2022-07-16 thomas
1062 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
1063 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1064 8a35f56c 2022-07-16 thomas error = NULL;
1065 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
1066 8a35f56c 2022-07-16 thomas repo_dir = NULL;
1067 24240f6a 2022-11-23 thomas d_skipped++;
1068 8a35f56c 2022-07-16 thomas continue;
1069 8a35f56c 2022-07-16 thomas }
1070 24240f6a 2022-11-23 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1071 24240f6a 2022-11-23 thomas goto done;
1072 24240f6a 2022-11-23 thomas
1073 8a35f56c 2022-07-16 thomas d_disp++;
1074 8a35f56c 2022-07-16 thomas t->prev_disp++;
1075 8a35f56c 2022-07-16 thomas
1076 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1077 55144267 2022-09-07 thomas "<div class='index_project'>") == -1)
1078 55144267 2022-09-07 thomas goto done;
1079 55144267 2022-09-07 thomas
1080 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1081 55144267 2022-09-07 thomas .action = SUMMARY,
1082 55144267 2022-09-07 thomas .index_page = -1,
1083 55144267 2022-09-07 thomas .page = -1,
1084 55144267 2022-09-07 thomas .path = repo_dir->name,
1085 55144267 2022-09-07 thomas }, "%s", repo_dir->name);
1086 79393471 2022-08-27 thomas if (r == -1)
1087 8a35f56c 2022-07-16 thomas goto done;
1088 8a35f56c 2022-07-16 thomas
1089 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1090 55144267 2022-09-07 thomas goto done;
1091 55144267 2022-09-07 thomas
1092 8a35f56c 2022-07-16 thomas if (srv->show_repo_description) {
1093 79393471 2022-08-27 thomas r = fcgi_printf(c,
1094 79393471 2022-08-27 thomas "<div class='index_project_description'>\n"
1095 79393471 2022-08-27 thomas "%s</div>\n", repo_dir->description);
1096 79393471 2022-08-27 thomas if (r == -1)
1097 8a35f56c 2022-07-16 thomas goto done;
1098 8a35f56c 2022-07-16 thomas }
1099 8a35f56c 2022-07-16 thomas
1100 8a35f56c 2022-07-16 thomas if (srv->show_repo_owner) {
1101 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='index_project_owner'>"
1102 79393471 2022-08-27 thomas "%s</div>\n", repo_dir->owner);
1103 79393471 2022-08-27 thomas if (r == -1)
1104 8a35f56c 2022-07-16 thomas goto done;
1105 8a35f56c 2022-07-16 thomas }
1106 8a35f56c 2022-07-16 thomas
1107 8a35f56c 2022-07-16 thomas if (srv->show_repo_age) {
1108 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='index_project_age'>"
1109 79393471 2022-08-27 thomas "%s</div>\n", repo_dir->age);
1110 79393471 2022-08-27 thomas if (r == -1)
1111 8a35f56c 2022-07-16 thomas goto done;
1112 8a35f56c 2022-07-16 thomas }
1113 8a35f56c 2022-07-16 thomas
1114 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='navs_wrapper'>"
1115 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1116 55144267 2022-09-07 thomas goto done;
1117 55144267 2022-09-07 thomas
1118 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1119 55144267 2022-09-07 thomas .action = SUMMARY,
1120 55144267 2022-09-07 thomas .index_page = -1,
1121 55144267 2022-09-07 thomas .page = -1,
1122 55144267 2022-09-07 thomas .path = repo_dir->name
1123 55144267 2022-09-07 thomas }, "summary");
1124 55144267 2022-09-07 thomas if (r == -1)
1125 55144267 2022-09-07 thomas goto done;
1126 55144267 2022-09-07 thomas
1127 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1128 55144267 2022-09-07 thomas goto done;
1129 55144267 2022-09-07 thomas
1130 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1131 55144267 2022-09-07 thomas .action = BRIEFS,
1132 55144267 2022-09-07 thomas .index_page = -1,
1133 55144267 2022-09-07 thomas .page = -1,
1134 55144267 2022-09-07 thomas .path = repo_dir->name
1135 55144267 2022-09-07 thomas }, "commit briefs");
1136 55144267 2022-09-07 thomas if (r == -1)
1137 55144267 2022-09-07 thomas goto done;
1138 55144267 2022-09-07 thomas
1139 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1140 55144267 2022-09-07 thomas goto done;
1141 55144267 2022-09-07 thomas
1142 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1143 55144267 2022-09-07 thomas .action = COMMITS,
1144 55144267 2022-09-07 thomas .index_page = -1,
1145 55144267 2022-09-07 thomas .page = -1,
1146 55144267 2022-09-07 thomas .path = repo_dir->name
1147 55144267 2022-09-07 thomas }, "commits");
1148 55144267 2022-09-07 thomas if (r == -1)
1149 55144267 2022-09-07 thomas goto done;
1150 55144267 2022-09-07 thomas
1151 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1152 55144267 2022-09-07 thomas goto done;
1153 55144267 2022-09-07 thomas
1154 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1155 55144267 2022-09-07 thomas .action = TAGS,
1156 55144267 2022-09-07 thomas .index_page = -1,
1157 55144267 2022-09-07 thomas .page = -1,
1158 55144267 2022-09-07 thomas .path = repo_dir->name
1159 55144267 2022-09-07 thomas }, "tags");
1160 55144267 2022-09-07 thomas if (r == -1)
1161 55144267 2022-09-07 thomas goto done;
1162 55144267 2022-09-07 thomas
1163 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1164 55144267 2022-09-07 thomas goto done;
1165 55144267 2022-09-07 thomas
1166 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1167 55144267 2022-09-07 thomas .action = TREE,
1168 55144267 2022-09-07 thomas .index_page = -1,
1169 55144267 2022-09-07 thomas .page = -1,
1170 55144267 2022-09-07 thomas .path = repo_dir->name
1171 55144267 2022-09-07 thomas }, "tree");
1172 55144267 2022-09-07 thomas if (r == -1)
1173 55144267 2022-09-07 thomas goto done;
1174 55144267 2022-09-07 thomas
1175 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>" /* .navs */
1176 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1177 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1178 55144267 2022-09-07 thomas "</div>\n"); /* .index_wrapper */
1179 79393471 2022-08-27 thomas if (r == -1)
1180 8a35f56c 2022-07-16 thomas goto done;
1181 8a35f56c 2022-07-16 thomas
1182 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
1183 8a35f56c 2022-07-16 thomas repo_dir = NULL;
1184 8a35f56c 2022-07-16 thomas t->next_disp++;
1185 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
1186 8a35f56c 2022-07-16 thomas break;
1187 8a35f56c 2022-07-16 thomas }
1188 24240f6a 2022-11-23 thomas t->repos_total = d_cnt - d_skipped;
1189 24240f6a 2022-11-23 thomas
1190 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
1191 79393471 2022-08-27 thomas goto done;
1192 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1193 79393471 2022-08-27 thomas goto done;
1194 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
1195 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
1196 79393471 2022-08-27 thomas goto done;
1197 8a35f56c 2022-07-16 thomas
1198 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
1199 8a35f56c 2022-07-16 thomas if (error)
1200 8a35f56c 2022-07-16 thomas goto done;
1201 8a35f56c 2022-07-16 thomas done:
1202 f530ab0e 2022-09-02 thomas if (sd_dent) {
1203 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
1204 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
1205 f530ab0e 2022-09-02 thomas free(sd_dent);
1206 f530ab0e 2022-09-02 thomas }
1207 8a35f56c 2022-07-16 thomas if (d != NULL && closedir(d) == EOF && error == NULL)
1208 8a35f56c 2022-07-16 thomas error = got_error_from_errno("closedir");
1209 8a35f56c 2022-07-16 thomas return error;
1210 8a35f56c 2022-07-16 thomas }
1211 8a35f56c 2022-07-16 thomas
1212 8a35f56c 2022-07-16 thomas static const struct got_error *
1213 8a35f56c 2022-07-16 thomas gotweb_render_blame(struct request *c)
1214 8a35f56c 2022-07-16 thomas {
1215 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1216 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1217 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1218 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
1219 79393471 2022-08-27 thomas int r;
1220 8a35f56c 2022-07-16 thomas
1221 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1222 8a35f56c 2022-07-16 thomas if (error)
1223 8a35f56c 2022-07-16 thomas return error;
1224 8a35f56c 2022-07-16 thomas
1225 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1226 8a35f56c 2022-07-16 thomas
1227 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1228 255f4022 2022-08-27 thomas if (error)
1229 255f4022 2022-08-27 thomas goto done;
1230 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1231 8a35f56c 2022-07-16 thomas if (error)
1232 8a35f56c 2022-07-16 thomas goto done;
1233 8a35f56c 2022-07-16 thomas
1234 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1235 79393471 2022-08-27 thomas "<div id='blame_title'>Blame</div>\n"
1236 79393471 2022-08-27 thomas "</div>\n" /* #blame_title_wrapper */
1237 79393471 2022-08-27 thomas "<div id='blame_content'>\n"
1238 79393471 2022-08-27 thomas "<div id='blame_header_wrapper'>\n"
1239 79393471 2022-08-27 thomas "<div id='blame_header'>\n"
1240 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1241 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1242 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1243 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1244 79393471 2022-08-27 thomas "</div>\n" /* #blame_header */
1245 79393471 2022-08-27 thomas "</div>\n" /* #blame_header_wrapper */
1246 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1247 79393471 2022-08-27 thomas "<div id='blame'>\n",
1248 2ad628aa 2022-08-31 thomas age,
1249 255f4022 2022-08-27 thomas msg);
1250 79393471 2022-08-27 thomas if (r == -1)
1251 8a35f56c 2022-07-16 thomas goto done;
1252 8a35f56c 2022-07-16 thomas
1253 8a35f56c 2022-07-16 thomas error = got_output_file_blame(c);
1254 8a35f56c 2022-07-16 thomas if (error)
1255 8a35f56c 2022-07-16 thomas goto done;
1256 8a35f56c 2022-07-16 thomas
1257 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n" /* #blame */
1258 79393471 2022-08-27 thomas "</div>\n"); /* #blame_content */
1259 8a35f56c 2022-07-16 thomas done:
1260 2ad628aa 2022-08-31 thomas free(age);
1261 255f4022 2022-08-27 thomas free(msg);
1262 8a35f56c 2022-07-16 thomas return error;
1263 8a35f56c 2022-07-16 thomas }
1264 8a35f56c 2022-07-16 thomas
1265 8a35f56c 2022-07-16 thomas static const struct got_error *
1266 8a35f56c 2022-07-16 thomas gotweb_render_briefs(struct request *c)
1267 8a35f56c 2022-07-16 thomas {
1268 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1269 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1270 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1271 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1272 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1273 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1274 8a35f56c 2022-07-16 thomas char *smallerthan, *newline;
1275 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1276 79393471 2022-08-27 thomas int r;
1277 8a35f56c 2022-07-16 thomas
1278 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1279 79393471 2022-08-27 thomas "<div id='briefs_title'>Commit Briefs</div>\n"
1280 79393471 2022-08-27 thomas "</div>\n" /* #briefs_title_wrapper */
1281 79393471 2022-08-27 thomas "<div id='briefs_content'>\n");
1282 79393471 2022-08-27 thomas if (r == -1)
1283 8a35f56c 2022-07-16 thomas goto done;
1284 8a35f56c 2022-07-16 thomas
1285 8a35f56c 2022-07-16 thomas if (qs->action == SUMMARY) {
1286 8a35f56c 2022-07-16 thomas qs->action = BRIEFS;
1287 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1288 8a35f56c 2022-07-16 thomas } else
1289 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, srv->max_commits_display);
1290 8a35f56c 2022-07-16 thomas if (error)
1291 8a35f56c 2022-07-16 thomas goto done;
1292 8a35f56c 2022-07-16 thomas
1293 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1294 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1295 8a35f56c 2022-07-16 thomas if (error)
1296 8a35f56c 2022-07-16 thomas goto done;
1297 8a35f56c 2022-07-16 thomas
1298 8a35f56c 2022-07-16 thomas smallerthan = strchr(rc->author, '<');
1299 8a35f56c 2022-07-16 thomas if (smallerthan)
1300 8a35f56c 2022-07-16 thomas *smallerthan = '\0';
1301 8a35f56c 2022-07-16 thomas
1302 8a35f56c 2022-07-16 thomas newline = strchr(rc->commit_msg, '\n');
1303 8a35f56c 2022-07-16 thomas if (newline)
1304 8a35f56c 2022-07-16 thomas *newline = '\0';
1305 8a35f56c 2022-07-16 thomas
1306 255f4022 2022-08-27 thomas error = gotweb_escape_html(&author, rc->author);
1307 255f4022 2022-08-27 thomas if (error)
1308 255f4022 2022-08-27 thomas goto done;
1309 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1310 255f4022 2022-08-27 thomas if (error)
1311 255f4022 2022-08-27 thomas goto done;
1312 255f4022 2022-08-27 thomas
1313 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1314 79393471 2022-08-27 thomas "<div class='briefs_author'>%s</div>\n"
1315 55144267 2022-09-07 thomas "<div class='briefs_log'>",
1316 55144267 2022-09-07 thomas age, author);
1317 79393471 2022-08-27 thomas if (r == -1)
1318 8a35f56c 2022-07-16 thomas goto done;
1319 79393471 2022-08-27 thomas
1320 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1321 55144267 2022-09-07 thomas .action = DIFF,
1322 55144267 2022-09-07 thomas .index_page = -1,
1323 55144267 2022-09-07 thomas .page = -1,
1324 55144267 2022-09-07 thomas .path = repo_dir->name,
1325 55144267 2022-09-07 thomas .commit = rc->commit_id,
1326 55144267 2022-09-07 thomas .headref = qs->headref,
1327 55144267 2022-09-07 thomas }, "%s", msg);
1328 55144267 2022-09-07 thomas if (r == -1)
1329 55144267 2022-09-07 thomas goto done;
1330 55144267 2022-09-07 thomas
1331 8a35f56c 2022-07-16 thomas if (rc->refs_str) {
1332 255f4022 2022-08-27 thomas char *refs;
1333 255f4022 2022-08-27 thomas
1334 255f4022 2022-08-27 thomas error = gotweb_escape_html(&refs, rc->refs_str);
1335 255f4022 2022-08-27 thomas if (error)
1336 255f4022 2022-08-27 thomas goto done;
1337 79393471 2022-08-27 thomas r = fcgi_printf(c,
1338 255f4022 2022-08-27 thomas " <span class='refs_str'>(%s)</span>", refs);
1339 255f4022 2022-08-27 thomas free(refs);
1340 79393471 2022-08-27 thomas if (r == -1)
1341 8a35f56c 2022-07-16 thomas goto done;
1342 8a35f56c 2022-07-16 thomas }
1343 79393471 2022-08-27 thomas if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1344 8a35f56c 2022-07-16 thomas goto done;
1345 8a35f56c 2022-07-16 thomas
1346 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1347 55144267 2022-09-07 thomas "<div class='navs'>");
1348 79393471 2022-08-27 thomas if (r == -1)
1349 8a35f56c 2022-07-16 thomas goto done;
1350 8a35f56c 2022-07-16 thomas
1351 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1352 55144267 2022-09-07 thomas .action = DIFF,
1353 55144267 2022-09-07 thomas .index_page = -1,
1354 55144267 2022-09-07 thomas .page = -1,
1355 55144267 2022-09-07 thomas .path = repo_dir->name,
1356 55144267 2022-09-07 thomas .commit = rc->commit_id,
1357 55144267 2022-09-07 thomas .headref = qs->headref,
1358 55144267 2022-09-07 thomas }, "diff");
1359 55144267 2022-09-07 thomas if (r == -1)
1360 55144267 2022-09-07 thomas goto done;
1361 55144267 2022-09-07 thomas
1362 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1363 55144267 2022-09-07 thomas goto done;
1364 55144267 2022-09-07 thomas
1365 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1366 55144267 2022-09-07 thomas .action = TREE,
1367 55144267 2022-09-07 thomas .index_page = -1,
1368 55144267 2022-09-07 thomas .page = -1,
1369 55144267 2022-09-07 thomas .path = repo_dir->name,
1370 55144267 2022-09-07 thomas .commit = rc->commit_id,
1371 55144267 2022-09-07 thomas .headref = qs->headref,
1372 55144267 2022-09-07 thomas }, "tree");
1373 55144267 2022-09-07 thomas if (r == -1)
1374 55144267 2022-09-07 thomas goto done;
1375 55144267 2022-09-07 thomas
1376 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .navs */
1377 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1378 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n") == -1)
1379 55144267 2022-09-07 thomas goto done;
1380 55144267 2022-09-07 thomas
1381 8a35f56c 2022-07-16 thomas free(age);
1382 8a35f56c 2022-07-16 thomas age = NULL;
1383 255f4022 2022-08-27 thomas free(author);
1384 255f4022 2022-08-27 thomas author = NULL;
1385 255f4022 2022-08-27 thomas free(msg);
1386 255f4022 2022-08-27 thomas msg = NULL;
1387 8a35f56c 2022-07-16 thomas }
1388 8a35f56c 2022-07-16 thomas
1389 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1390 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
1391 8a35f56c 2022-07-16 thomas if (error)
1392 8a35f56c 2022-07-16 thomas goto done;
1393 8a35f56c 2022-07-16 thomas }
1394 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #briefs_content */
1395 8a35f56c 2022-07-16 thomas done:
1396 8a35f56c 2022-07-16 thomas free(age);
1397 255f4022 2022-08-27 thomas free(author);
1398 255f4022 2022-08-27 thomas free(msg);
1399 8a35f56c 2022-07-16 thomas return error;
1400 8a35f56c 2022-07-16 thomas }
1401 8a35f56c 2022-07-16 thomas
1402 8a35f56c 2022-07-16 thomas static const struct got_error *
1403 8a35f56c 2022-07-16 thomas gotweb_render_commits(struct request *c)
1404 8a35f56c 2022-07-16 thomas {
1405 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1406 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1407 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1408 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1409 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1410 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1411 79393471 2022-08-27 thomas int r;
1412 8a35f56c 2022-07-16 thomas
1413 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1414 79393471 2022-08-27 thomas "<div class='commits_title'>Commits</div>\n"
1415 79393471 2022-08-27 thomas "</div>\n" /* .commits_title_wrapper */
1416 79393471 2022-08-27 thomas "<div class='commits_content'>\n");
1417 79393471 2022-08-27 thomas if (r == -1)
1418 8a35f56c 2022-07-16 thomas goto done;
1419 8a35f56c 2022-07-16 thomas
1420 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, srv->max_commits_display);
1421 8a35f56c 2022-07-16 thomas if (error)
1422 8a35f56c 2022-07-16 thomas goto done;
1423 8a35f56c 2022-07-16 thomas
1424 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1425 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1426 8a35f56c 2022-07-16 thomas if (error)
1427 8a35f56c 2022-07-16 thomas goto done;
1428 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rc->author);
1429 8a35f56c 2022-07-16 thomas if (error)
1430 8a35f56c 2022-07-16 thomas goto done;
1431 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1432 255f4022 2022-08-27 thomas if (error)
1433 255f4022 2022-08-27 thomas goto done;
1434 8a35f56c 2022-07-16 thomas
1435 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1436 79393471 2022-08-27 thomas "<div class='commits_header'>\n"
1437 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1438 79393471 2022-08-27 thomas "<div class='header_commit'>%s</div>\n"
1439 79393471 2022-08-27 thomas "<div class='header_author_title'>Author:</div>\n"
1440 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1441 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1442 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1443 79393471 2022-08-27 thomas "</div>\n" /* .commits_header */
1444 79393471 2022-08-27 thomas "</div>\n" /* .commits_header_wrapper */
1445 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1446 79393471 2022-08-27 thomas "<div class='commit'>\n%s</div>\n",
1447 79393471 2022-08-27 thomas rc->commit_id,
1448 255f4022 2022-08-27 thomas author,
1449 2ad628aa 2022-08-31 thomas age,
1450 255f4022 2022-08-27 thomas msg);
1451 79393471 2022-08-27 thomas if (r == -1)
1452 8a35f56c 2022-07-16 thomas goto done;
1453 8a35f56c 2022-07-16 thomas
1454 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1455 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1456 55144267 2022-09-07 thomas goto done;
1457 55144267 2022-09-07 thomas
1458 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1459 55144267 2022-09-07 thomas .action = DIFF,
1460 55144267 2022-09-07 thomas .index_page = -1,
1461 55144267 2022-09-07 thomas .page = -1,
1462 55144267 2022-09-07 thomas .path = repo_dir->name,
1463 55144267 2022-09-07 thomas .commit = rc->commit_id,
1464 55144267 2022-09-07 thomas }, "diff");
1465 1510f72d 2022-09-02 thomas if (r == -1)
1466 1510f72d 2022-09-02 thomas goto done;
1467 8a35f56c 2022-07-16 thomas
1468 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1469 55144267 2022-09-07 thomas goto done;
1470 55144267 2022-09-07 thomas
1471 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1472 55144267 2022-09-07 thomas .action = TREE,
1473 55144267 2022-09-07 thomas .index_page = -1,
1474 55144267 2022-09-07 thomas .page = -1,
1475 55144267 2022-09-07 thomas .path = repo_dir->name,
1476 55144267 2022-09-07 thomas .commit = rc->commit_id,
1477 55144267 2022-09-07 thomas }, "tree");
1478 55144267 2022-09-07 thomas if (r == -1)
1479 55144267 2022-09-07 thomas goto done;
1480 55144267 2022-09-07 thomas
1481 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .navs */
1482 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1483 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n") == -1)
1484 55144267 2022-09-07 thomas goto done;
1485 55144267 2022-09-07 thomas
1486 8a35f56c 2022-07-16 thomas free(age);
1487 8a35f56c 2022-07-16 thomas age = NULL;
1488 8a35f56c 2022-07-16 thomas free(author);
1489 8a35f56c 2022-07-16 thomas author = NULL;
1490 255f4022 2022-08-27 thomas free(msg);
1491 255f4022 2022-08-27 thomas msg = NULL;
1492 8a35f56c 2022-07-16 thomas }
1493 8a35f56c 2022-07-16 thomas
1494 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1495 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
1496 8a35f56c 2022-07-16 thomas if (error)
1497 8a35f56c 2022-07-16 thomas goto done;
1498 8a35f56c 2022-07-16 thomas }
1499 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* .commits_content */
1500 8a35f56c 2022-07-16 thomas done:
1501 8a35f56c 2022-07-16 thomas free(age);
1502 255f4022 2022-08-27 thomas free(author);
1503 255f4022 2022-08-27 thomas free(msg);
1504 8a35f56c 2022-07-16 thomas return error;
1505 8a35f56c 2022-07-16 thomas }
1506 8a35f56c 2022-07-16 thomas
1507 8a35f56c 2022-07-16 thomas static const struct got_error *
1508 8a35f56c 2022-07-16 thomas gotweb_render_branches(struct request *c)
1509 8a35f56c 2022-07-16 thomas {
1510 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1511 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1512 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
1513 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1514 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1515 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1516 55144267 2022-09-07 thomas char *escaped_refname = NULL;
1517 8a35f56c 2022-07-16 thomas char *age = NULL;
1518 79393471 2022-08-27 thomas int r;
1519 8a35f56c 2022-07-16 thomas
1520 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1521 8a35f56c 2022-07-16 thomas
1522 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
1523 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
1524 8a35f56c 2022-07-16 thomas if (error)
1525 8a35f56c 2022-07-16 thomas goto done;
1526 8a35f56c 2022-07-16 thomas
1527 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1528 79393471 2022-08-27 thomas "<div id='branches_title'>Branches</div>\n"
1529 79393471 2022-08-27 thomas "</div>\n" /* #branches_title_wrapper */
1530 79393471 2022-08-27 thomas "<div id='branches_content'>\n");
1531 79393471 2022-08-27 thomas if (r == -1)
1532 8a35f56c 2022-07-16 thomas goto done;
1533 8a35f56c 2022-07-16 thomas
1534 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
1535 255f4022 2022-08-27 thomas const char *refname = NULL;
1536 8a35f56c 2022-07-16 thomas
1537 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
1538 8a35f56c 2022-07-16 thomas continue;
1539 8a35f56c 2022-07-16 thomas
1540 255f4022 2022-08-27 thomas refname = got_ref_get_name(re->ref);
1541 8a35f56c 2022-07-16 thomas if (refname == NULL) {
1542 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
1543 8a35f56c 2022-07-16 thomas goto done;
1544 8a35f56c 2022-07-16 thomas }
1545 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) != 0)
1546 8a35f56c 2022-07-16 thomas continue;
1547 8a35f56c 2022-07-16 thomas
1548 8a35f56c 2022-07-16 thomas error = got_get_repo_age(&age, c, qs->path, refname,
1549 8a35f56c 2022-07-16 thomas TM_DIFF);
1550 8a35f56c 2022-07-16 thomas if (error)
1551 8a35f56c 2022-07-16 thomas goto done;
1552 8a35f56c 2022-07-16 thomas
1553 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
1554 8a35f56c 2022-07-16 thomas refname += 11;
1555 255f4022 2022-08-27 thomas error = gotweb_escape_html(&escaped_refname, refname);
1556 255f4022 2022-08-27 thomas if (error)
1557 255f4022 2022-08-27 thomas goto done;
1558 8a35f56c 2022-07-16 thomas
1559 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1560 79393471 2022-08-27 thomas "<div class='branches_age'>%s</div>\n"
1561 79393471 2022-08-27 thomas "<div class='branches_space'>&nbsp;</div>\n"
1562 55144267 2022-09-07 thomas "<div class='branch'>", age);
1563 55144267 2022-09-07 thomas if (r == -1)
1564 55144267 2022-09-07 thomas goto done;
1565 55144267 2022-09-07 thomas
1566 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1567 55144267 2022-09-07 thomas .action = SUMMARY,
1568 55144267 2022-09-07 thomas .index_page = -1,
1569 55144267 2022-09-07 thomas .page = -1,
1570 55144267 2022-09-07 thomas .path = qs->path,
1571 55144267 2022-09-07 thomas .headref = refname,
1572 55144267 2022-09-07 thomas }, "%s", escaped_refname);
1573 55144267 2022-09-07 thomas if (r == -1)
1574 55144267 2022-09-07 thomas goto done;
1575 55144267 2022-09-07 thomas
1576 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .branch */
1577 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1578 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1579 55144267 2022-09-07 thomas goto done;
1580 55144267 2022-09-07 thomas
1581 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1582 55144267 2022-09-07 thomas .action = SUMMARY,
1583 55144267 2022-09-07 thomas .index_page = -1,
1584 55144267 2022-09-07 thomas .page = -1,
1585 55144267 2022-09-07 thomas .path = qs->path,
1586 55144267 2022-09-07 thomas .headref = refname,
1587 55144267 2022-09-07 thomas }, "summary");
1588 55144267 2022-09-07 thomas if (r == -1)
1589 55144267 2022-09-07 thomas goto done;
1590 55144267 2022-09-07 thomas
1591 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1592 55144267 2022-09-07 thomas goto done;
1593 55144267 2022-09-07 thomas
1594 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1595 55144267 2022-09-07 thomas .action = BRIEFS,
1596 55144267 2022-09-07 thomas .index_page = -1,
1597 55144267 2022-09-07 thomas .page = -1,
1598 55144267 2022-09-07 thomas .path = qs->path,
1599 55144267 2022-09-07 thomas .headref = refname,
1600 55144267 2022-09-07 thomas }, "commit briefs");
1601 55144267 2022-09-07 thomas if (r == -1)
1602 55144267 2022-09-07 thomas goto done;
1603 55144267 2022-09-07 thomas
1604 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1605 55144267 2022-09-07 thomas goto done;
1606 55144267 2022-09-07 thomas
1607 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1608 55144267 2022-09-07 thomas .action = COMMITS,
1609 55144267 2022-09-07 thomas .index_page = -1,
1610 55144267 2022-09-07 thomas .page = -1,
1611 55144267 2022-09-07 thomas .path = qs->path,
1612 55144267 2022-09-07 thomas .headref = refname,
1613 55144267 2022-09-07 thomas }, "commits");
1614 55144267 2022-09-07 thomas if (r == -1)
1615 55144267 2022-09-07 thomas goto done;
1616 55144267 2022-09-07 thomas
1617 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>\n" /* .navs */
1618 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1619 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1620 55144267 2022-09-07 thomas "</div>\n"); /* .branches_wrapper */
1621 79393471 2022-08-27 thomas if (r == -1)
1622 8a35f56c 2022-07-16 thomas goto done;
1623 8a35f56c 2022-07-16 thomas
1624 8a35f56c 2022-07-16 thomas free(age);
1625 8a35f56c 2022-07-16 thomas age = NULL;
1626 55144267 2022-09-07 thomas free(escaped_refname);
1627 55144267 2022-09-07 thomas escaped_refname = NULL;
1628 8a35f56c 2022-07-16 thomas }
1629 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #branches_content */
1630 8a35f56c 2022-07-16 thomas done:
1631 f95e65a1 2022-09-02 thomas free(age);
1632 55144267 2022-09-07 thomas free(escaped_refname);
1633 f95e65a1 2022-09-02 thomas got_ref_list_free(&refs);
1634 8a35f56c 2022-07-16 thomas return error;
1635 8a35f56c 2022-07-16 thomas }
1636 8a35f56c 2022-07-16 thomas
1637 8a35f56c 2022-07-16 thomas static const struct got_error *
1638 8a35f56c 2022-07-16 thomas gotweb_render_tree(struct request *c)
1639 8a35f56c 2022-07-16 thomas {
1640 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1641 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1642 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1643 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
1644 79393471 2022-08-27 thomas int r;
1645 8a35f56c 2022-07-16 thomas
1646 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1647 8a35f56c 2022-07-16 thomas if (error)
1648 8a35f56c 2022-07-16 thomas return error;
1649 8a35f56c 2022-07-16 thomas
1650 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1651 8a35f56c 2022-07-16 thomas
1652 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1653 8a35f56c 2022-07-16 thomas if (error)
1654 8a35f56c 2022-07-16 thomas goto done;
1655 8a35f56c 2022-07-16 thomas
1656 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1657 255f4022 2022-08-27 thomas if (error)
1658 255f4022 2022-08-27 thomas goto done;
1659 255f4022 2022-08-27 thomas
1660 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1661 79393471 2022-08-27 thomas "<div id='tree_title'>Tree</div>\n"
1662 79393471 2022-08-27 thomas "</div>\n" /* #tree_title_wrapper */
1663 79393471 2022-08-27 thomas "<div id='tree_content'>\n"
1664 79393471 2022-08-27 thomas "<div id='tree_header_wrapper'>\n"
1665 79393471 2022-08-27 thomas "<div id='tree_header'>\n"
1666 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1667 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1668 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1669 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1670 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1671 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1672 79393471 2022-08-27 thomas "</div>\n" /* #tree_header */
1673 79393471 2022-08-27 thomas "</div>\n" /* #tree_header_wrapper */
1674 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1675 79393471 2022-08-27 thomas "<div id='tree'>\n",
1676 79393471 2022-08-27 thomas rc->tree_id,
1677 2ad628aa 2022-08-31 thomas age,
1678 255f4022 2022-08-27 thomas msg);
1679 79393471 2022-08-27 thomas if (r == -1)
1680 8a35f56c 2022-07-16 thomas goto done;
1681 8a35f56c 2022-07-16 thomas
1682 8a35f56c 2022-07-16 thomas error = got_output_repo_tree(c);
1683 8a35f56c 2022-07-16 thomas if (error)
1684 8a35f56c 2022-07-16 thomas goto done;
1685 8a35f56c 2022-07-16 thomas
1686 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree */
1687 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree_content */
1688 8a35f56c 2022-07-16 thomas done:
1689 2ad628aa 2022-08-31 thomas free(age);
1690 255f4022 2022-08-27 thomas free(msg);
1691 8a35f56c 2022-07-16 thomas return error;
1692 8a35f56c 2022-07-16 thomas }
1693 8a35f56c 2022-07-16 thomas
1694 8a35f56c 2022-07-16 thomas static const struct got_error *
1695 8a35f56c 2022-07-16 thomas gotweb_render_diff(struct request *c)
1696 8a35f56c 2022-07-16 thomas {
1697 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1698 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1699 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1700 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1701 79393471 2022-08-27 thomas int r;
1702 8a35f56c 2022-07-16 thomas
1703 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1704 8a35f56c 2022-07-16 thomas if (error)
1705 8a35f56c 2022-07-16 thomas return error;
1706 8a35f56c 2022-07-16 thomas
1707 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1708 8a35f56c 2022-07-16 thomas
1709 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1710 8a35f56c 2022-07-16 thomas if (error)
1711 8a35f56c 2022-07-16 thomas goto done;
1712 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rc->author);
1713 8a35f56c 2022-07-16 thomas if (error)
1714 8a35f56c 2022-07-16 thomas goto done;
1715 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1716 255f4022 2022-08-27 thomas if (error)
1717 255f4022 2022-08-27 thomas goto done;
1718 8a35f56c 2022-07-16 thomas
1719 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1720 79393471 2022-08-27 thomas "<div id='diff_title'>Commit Diff</div>\n"
1721 79393471 2022-08-27 thomas "</div>\n" /* #diff_title_wrapper */
1722 79393471 2022-08-27 thomas "<div id='diff_content'>\n"
1723 79393471 2022-08-27 thomas "<div id='diff_header_wrapper'>\n"
1724 79393471 2022-08-27 thomas "<div id='diff_header'>\n"
1725 79393471 2022-08-27 thomas "<div id='header_diff_title'>Diff:</div>\n"
1726 79393471 2022-08-27 thomas "<div id='header_diff'>%s<br />%s</div>\n"
1727 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1728 79393471 2022-08-27 thomas "<div class='header_commit'>%s</div>\n"
1729 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1730 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1731 79393471 2022-08-27 thomas "<div class='header_author_title'>Author:</div>\n"
1732 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1733 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1734 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1735 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1736 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1737 79393471 2022-08-27 thomas "</div>\n" /* #diff_header */
1738 79393471 2022-08-27 thomas "</div>\n" /* #diff_header_wrapper */
1739 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1740 79393471 2022-08-27 thomas "<div id='diff'>\n",
1741 79393471 2022-08-27 thomas rc->parent_id, rc->commit_id,
1742 79393471 2022-08-27 thomas rc->commit_id,
1743 79393471 2022-08-27 thomas rc->tree_id,
1744 255f4022 2022-08-27 thomas author,
1745 2ad628aa 2022-08-31 thomas age,
1746 255f4022 2022-08-27 thomas msg);
1747 79393471 2022-08-27 thomas if (r == -1)
1748 8a35f56c 2022-07-16 thomas goto done;
1749 8a35f56c 2022-07-16 thomas
1750 8a35f56c 2022-07-16 thomas error = got_output_repo_diff(c);
1751 8a35f56c 2022-07-16 thomas if (error)
1752 8a35f56c 2022-07-16 thomas goto done;
1753 8a35f56c 2022-07-16 thomas
1754 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff */
1755 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff_content */
1756 8a35f56c 2022-07-16 thomas done:
1757 8a35f56c 2022-07-16 thomas free(age);
1758 8a35f56c 2022-07-16 thomas free(author);
1759 255f4022 2022-08-27 thomas free(msg);
1760 8a35f56c 2022-07-16 thomas return error;
1761 8a35f56c 2022-07-16 thomas }
1762 8a35f56c 2022-07-16 thomas
1763 8a35f56c 2022-07-16 thomas static const struct got_error *
1764 8a35f56c 2022-07-16 thomas gotweb_render_summary(struct request *c)
1765 8a35f56c 2022-07-16 thomas {
1766 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1767 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1768 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1769 79393471 2022-08-27 thomas int r;
1770 8a35f56c 2022-07-16 thomas
1771 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1772 8a35f56c 2022-07-16 thomas goto done;
1773 8a35f56c 2022-07-16 thomas
1774 79393471 2022-08-27 thomas if (srv->show_repo_description) {
1775 79393471 2022-08-27 thomas r = fcgi_printf(c,
1776 79393471 2022-08-27 thomas "<div id='description_title'>Description:</div>\n"
1777 79393471 2022-08-27 thomas "<div id='description'>%s</div>\n",
1778 ddf2e5c2 2022-08-27 thomas t->repo_dir->description ? t->repo_dir->description : "");
1779 79393471 2022-08-27 thomas if (r == -1)
1780 79393471 2022-08-27 thomas goto done;
1781 79393471 2022-08-27 thomas }
1782 8a35f56c 2022-07-16 thomas
1783 79393471 2022-08-27 thomas if (srv->show_repo_owner) {
1784 79393471 2022-08-27 thomas r = fcgi_printf(c,
1785 79393471 2022-08-27 thomas "<div id='repo_owner_title'>Owner:</div>\n"
1786 79393471 2022-08-27 thomas "<div id='repo_owner'>%s</div>\n",
1787 ddf2e5c2 2022-08-27 thomas t->repo_dir->owner ? t->repo_dir->owner : "");
1788 79393471 2022-08-27 thomas if (r == -1)
1789 79393471 2022-08-27 thomas goto done;
1790 79393471 2022-08-27 thomas }
1791 8a35f56c 2022-07-16 thomas
1792 79393471 2022-08-27 thomas if (srv->show_repo_age) {
1793 79393471 2022-08-27 thomas r = fcgi_printf(c,
1794 79393471 2022-08-27 thomas "<div id='last_change_title'>Last Change:</div>\n"
1795 79393471 2022-08-27 thomas "<div id='last_change'>%s</div>\n",
1796 79393471 2022-08-27 thomas t->repo_dir->age);
1797 79393471 2022-08-27 thomas if (r == -1)
1798 79393471 2022-08-27 thomas goto done;
1799 79393471 2022-08-27 thomas }
1800 8a35f56c 2022-07-16 thomas
1801 79393471 2022-08-27 thomas if (srv->show_repo_cloneurl) {
1802 79393471 2022-08-27 thomas r = fcgi_printf(c,
1803 79393471 2022-08-27 thomas "<div id='cloneurl_title'>Clone URL:</div>\n"
1804 79393471 2022-08-27 thomas "<div id='cloneurl'>%s</div>\n",
1805 79393471 2022-08-27 thomas t->repo_dir->url ? t->repo_dir->url : "");
1806 79393471 2022-08-27 thomas if (r == -1)
1807 79393471 2022-08-27 thomas goto done;
1808 79393471 2022-08-27 thomas }
1809 8a35f56c 2022-07-16 thomas
1810 79393471 2022-08-27 thomas r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1811 79393471 2022-08-27 thomas if (r == -1)
1812 8a35f56c 2022-07-16 thomas goto done;
1813 8a35f56c 2022-07-16 thomas
1814 8a35f56c 2022-07-16 thomas error = gotweb_render_briefs(c);
1815 8a35f56c 2022-07-16 thomas if (error) {
1816 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1817 8a35f56c 2022-07-16 thomas goto done;
1818 8a35f56c 2022-07-16 thomas }
1819 8a35f56c 2022-07-16 thomas
1820 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
1821 8a35f56c 2022-07-16 thomas if (error) {
1822 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1823 8a35f56c 2022-07-16 thomas goto done;
1824 8a35f56c 2022-07-16 thomas }
1825 8a35f56c 2022-07-16 thomas
1826 8a35f56c 2022-07-16 thomas error = gotweb_render_branches(c);
1827 8a35f56c 2022-07-16 thomas if (error)
1828 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1829 8a35f56c 2022-07-16 thomas done:
1830 8a35f56c 2022-07-16 thomas return error;
1831 8a35f56c 2022-07-16 thomas }
1832 8a35f56c 2022-07-16 thomas
1833 8a35f56c 2022-07-16 thomas static const struct got_error *
1834 8a35f56c 2022-07-16 thomas gotweb_render_tag(struct request *c)
1835 8a35f56c 2022-07-16 thomas {
1836 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1837 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1838 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1839 255f4022 2022-08-27 thomas char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1840 8a35f56c 2022-07-16 thomas
1841 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, 1);
1842 8a35f56c 2022-07-16 thomas if (error)
1843 8a35f56c 2022-07-16 thomas goto done;
1844 8a35f56c 2022-07-16 thomas
1845 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1846 8a35f56c 2022-07-16 thomas error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1847 8a35f56c 2022-07-16 thomas "bad commit id");
1848 8a35f56c 2022-07-16 thomas goto done;
1849 8a35f56c 2022-07-16 thomas }
1850 8a35f56c 2022-07-16 thomas
1851 8a35f56c 2022-07-16 thomas rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1852 8a35f56c 2022-07-16 thomas
1853 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1854 8a35f56c 2022-07-16 thomas if (error)
1855 8a35f56c 2022-07-16 thomas goto done;
1856 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rt->tagger);
1857 8a35f56c 2022-07-16 thomas if (error)
1858 8a35f56c 2022-07-16 thomas goto done;
1859 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->commit_msg);
1860 255f4022 2022-08-27 thomas if (error)
1861 255f4022 2022-08-27 thomas goto done;
1862 8a35f56c 2022-07-16 thomas
1863 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1864 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/", 5) == 0)
1865 c8d0196f 2022-09-02 thomas tagname += 5;
1866 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1867 255f4022 2022-08-27 thomas if (error)
1868 255f4022 2022-08-27 thomas goto done;
1869 8a35f56c 2022-07-16 thomas
1870 79393471 2022-08-27 thomas fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1871 79393471 2022-08-27 thomas "<div id='tags_title'>Tag</div>\n"
1872 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1873 79393471 2022-08-27 thomas "<div id='tags_content'>\n"
1874 79393471 2022-08-27 thomas "<div id='tag_header_wrapper'>\n"
1875 79393471 2022-08-27 thomas "<div id='tag_header'>\n"
1876 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1877 79393471 2022-08-27 thomas "<div class='header_commit'>%s"
1878 79393471 2022-08-27 thomas " <span class='refs_str'>(%s)</span></div>\n"
1879 79393471 2022-08-27 thomas "<div class='header_author_title'>Tagger:</div>\n"
1880 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1881 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1882 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1883 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1884 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1885 79393471 2022-08-27 thomas "</div>\n" /* #tag_header */
1886 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1887 79393471 2022-08-27 thomas "<div id='tag_commit'>\n%s</div>"
1888 80ca8b0f 2022-09-06 thomas "</div>" /* #tag_header_wrapper */
1889 80ca8b0f 2022-09-06 thomas "</div>", /* #tags_content */
1890 79393471 2022-08-27 thomas rt->commit_id,
1891 255f4022 2022-08-27 thomas tagname,
1892 255f4022 2022-08-27 thomas author,
1893 2ad628aa 2022-08-31 thomas age,
1894 255f4022 2022-08-27 thomas msg,
1895 79393471 2022-08-27 thomas rt->tag_commit);
1896 8a35f56c 2022-07-16 thomas
1897 8a35f56c 2022-07-16 thomas done:
1898 8a35f56c 2022-07-16 thomas free(age);
1899 8a35f56c 2022-07-16 thomas free(author);
1900 255f4022 2022-08-27 thomas free(msg);
1901 8a35f56c 2022-07-16 thomas return error;
1902 8a35f56c 2022-07-16 thomas }
1903 8a35f56c 2022-07-16 thomas
1904 8a35f56c 2022-07-16 thomas static const struct got_error *
1905 8a35f56c 2022-07-16 thomas gotweb_render_tags(struct request *c)
1906 8a35f56c 2022-07-16 thomas {
1907 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1908 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1909 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1910 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1911 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1912 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1913 255f4022 2022-08-27 thomas char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1914 79393471 2022-08-27 thomas int r, commit_found = 0;
1915 8a35f56c 2022-07-16 thomas
1916 8a35f56c 2022-07-16 thomas if (qs->action == BRIEFS) {
1917 8a35f56c 2022-07-16 thomas qs->action = TAGS;
1918 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1919 8a35f56c 2022-07-16 thomas } else
1920 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, srv->max_commits_display);
1921 8a35f56c 2022-07-16 thomas if (error)
1922 8a35f56c 2022-07-16 thomas goto done;
1923 8a35f56c 2022-07-16 thomas
1924 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1925 79393471 2022-08-27 thomas "<div id='tags_title'>Tags</div>\n"
1926 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1927 79393471 2022-08-27 thomas "<div id='tags_content'>\n");
1928 79393471 2022-08-27 thomas if (r == -1)
1929 8a35f56c 2022-07-16 thomas goto done;
1930 8a35f56c 2022-07-16 thomas
1931 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1932 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1933 79393471 2022-08-27 thomas "This repository contains no tags");
1934 79393471 2022-08-27 thomas if (r == -1)
1935 8a35f56c 2022-07-16 thomas goto done;
1936 8a35f56c 2022-07-16 thomas }
1937 8a35f56c 2022-07-16 thomas
1938 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1939 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL) {
1940 8a35f56c 2022-07-16 thomas if (strcmp(qs->commit, rt->commit_id) != 0)
1941 8a35f56c 2022-07-16 thomas continue;
1942 8a35f56c 2022-07-16 thomas else
1943 8a35f56c 2022-07-16 thomas commit_found = 1;
1944 8a35f56c 2022-07-16 thomas }
1945 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1946 8a35f56c 2022-07-16 thomas if (error)
1947 8a35f56c 2022-07-16 thomas goto done;
1948 8a35f56c 2022-07-16 thomas
1949 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1950 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/tags/", 10) == 0)
1951 c8d0196f 2022-09-02 thomas tagname += 10;
1952 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1953 255f4022 2022-08-27 thomas if (error)
1954 255f4022 2022-08-27 thomas goto done;
1955 8a35f56c 2022-07-16 thomas
1956 8a35f56c 2022-07-16 thomas if (rt->tag_commit != NULL) {
1957 8a35f56c 2022-07-16 thomas newline = strchr(rt->tag_commit, '\n');
1958 8a35f56c 2022-07-16 thomas if (newline)
1959 8a35f56c 2022-07-16 thomas *newline = '\0';
1960 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->tag_commit);
1961 255f4022 2022-08-27 thomas if (error)
1962 255f4022 2022-08-27 thomas goto done;
1963 8a35f56c 2022-07-16 thomas }
1964 8a35f56c 2022-07-16 thomas
1965 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1966 79393471 2022-08-27 thomas "<div class='tag'>%s</div>\n"
1967 55144267 2022-09-07 thomas "<div class='tag_log'>", age, tagname) == -1)
1968 55144267 2022-09-07 thomas goto done;
1969 55144267 2022-09-07 thomas
1970 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1971 55144267 2022-09-07 thomas .action = TAG,
1972 55144267 2022-09-07 thomas .index_page = -1,
1973 55144267 2022-09-07 thomas .page = -1,
1974 55144267 2022-09-07 thomas .path = repo_dir->name,
1975 55144267 2022-09-07 thomas .commit = rt->commit_id,
1976 55144267 2022-09-07 thomas }, "%s", msg ? msg : "");
1977 55144267 2022-09-07 thomas if (r == -1)
1978 55144267 2022-09-07 thomas goto done;
1979 55144267 2022-09-07 thomas
1980 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .tag_log */
1981 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1982 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1983 55144267 2022-09-07 thomas goto done;
1984 55144267 2022-09-07 thomas
1985 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1986 55144267 2022-09-07 thomas .action = TAG,
1987 55144267 2022-09-07 thomas .index_page = -1,
1988 55144267 2022-09-07 thomas .page = -1,
1989 55144267 2022-09-07 thomas .path = repo_dir->name,
1990 55144267 2022-09-07 thomas .commit = rt->commit_id,
1991 55144267 2022-09-07 thomas }, "tag");
1992 55144267 2022-09-07 thomas if (r == -1)
1993 55144267 2022-09-07 thomas goto done;
1994 55144267 2022-09-07 thomas
1995 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1996 55144267 2022-09-07 thomas goto done;
1997 55144267 2022-09-07 thomas
1998 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1999 55144267 2022-09-07 thomas .action = BRIEFS,
2000 55144267 2022-09-07 thomas .index_page = -1,
2001 55144267 2022-09-07 thomas .page = -1,
2002 55144267 2022-09-07 thomas .path = repo_dir->name,
2003 55144267 2022-09-07 thomas .commit = rt->commit_id,
2004 55144267 2022-09-07 thomas }, "commit briefs");
2005 55144267 2022-09-07 thomas if (r == -1)
2006 55144267 2022-09-07 thomas goto done;
2007 55144267 2022-09-07 thomas
2008 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
2009 55144267 2022-09-07 thomas goto done;
2010 55144267 2022-09-07 thomas
2011 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
2012 55144267 2022-09-07 thomas .action = COMMITS,
2013 55144267 2022-09-07 thomas .index_page = -1,
2014 55144267 2022-09-07 thomas .page = -1,
2015 55144267 2022-09-07 thomas .path = repo_dir->name,
2016 55144267 2022-09-07 thomas .commit = rt->commit_id,
2017 55144267 2022-09-07 thomas }, "commits");
2018 55144267 2022-09-07 thomas if (r == -1)
2019 55144267 2022-09-07 thomas goto done;
2020 55144267 2022-09-07 thomas
2021 55144267 2022-09-07 thomas r = fcgi_printf(c,
2022 79393471 2022-08-27 thomas "</div>\n" /* .navs */
2023 79393471 2022-08-27 thomas "</div>\n" /* .navs_wrapper */
2024 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n");
2025 79393471 2022-08-27 thomas if (r == -1)
2026 8a35f56c 2022-07-16 thomas goto done;
2027 8a35f56c 2022-07-16 thomas
2028 8a35f56c 2022-07-16 thomas free(age);
2029 8a35f56c 2022-07-16 thomas age = NULL;
2030 255f4022 2022-08-27 thomas free(tagname);
2031 255f4022 2022-08-27 thomas tagname = NULL;
2032 255f4022 2022-08-27 thomas free(msg);
2033 255f4022 2022-08-27 thomas msg = NULL;
2034 8a35f56c 2022-07-16 thomas }
2035 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
2036 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
2037 8a35f56c 2022-07-16 thomas if (error)
2038 8a35f56c 2022-07-16 thomas goto done;
2039 8a35f56c 2022-07-16 thomas }
2040 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tags_content */
2041 8a35f56c 2022-07-16 thomas done:
2042 8a35f56c 2022-07-16 thomas free(age);
2043 255f4022 2022-08-27 thomas free(tagname);
2044 255f4022 2022-08-27 thomas free(msg);
2045 8a35f56c 2022-07-16 thomas return error;
2046 8a35f56c 2022-07-16 thomas }
2047 8a35f56c 2022-07-16 thomas
2048 8a35f56c 2022-07-16 thomas const struct got_error *
2049 8a35f56c 2022-07-16 thomas gotweb_escape_html(char **escaped_html, const char *orig_html)
2050 8a35f56c 2022-07-16 thomas {
2051 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2052 8a35f56c 2022-07-16 thomas struct escape_pair {
2053 8a35f56c 2022-07-16 thomas char c;
2054 8a35f56c 2022-07-16 thomas const char *s;
2055 8a35f56c 2022-07-16 thomas } esc[] = {
2056 8a35f56c 2022-07-16 thomas { '>', "&gt;" },
2057 8a35f56c 2022-07-16 thomas { '<', "&lt;" },
2058 8a35f56c 2022-07-16 thomas { '&', "&amp;" },
2059 8a35f56c 2022-07-16 thomas { '"', "&quot;" },
2060 8a35f56c 2022-07-16 thomas { '\'', "&apos;" },
2061 8a35f56c 2022-07-16 thomas { '\n', "<br />" },
2062 8a35f56c 2022-07-16 thomas };
2063 8a35f56c 2022-07-16 thomas size_t orig_len, len;
2064 8a35f56c 2022-07-16 thomas int i, j, x;
2065 8a35f56c 2022-07-16 thomas
2066 8a35f56c 2022-07-16 thomas orig_len = strlen(orig_html);
2067 8a35f56c 2022-07-16 thomas len = orig_len;
2068 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
2069 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
2070 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
2071 8a35f56c 2022-07-16 thomas continue;
2072 8a35f56c 2022-07-16 thomas len += strlen(esc[j].s) - 1 /* escaped char */;
2073 8a35f56c 2022-07-16 thomas }
2074 8a35f56c 2022-07-16 thomas }
2075 8a35f56c 2022-07-16 thomas
2076 8a35f56c 2022-07-16 thomas *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2077 8a35f56c 2022-07-16 thomas if (*escaped_html == NULL)
2078 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
2079 8a35f56c 2022-07-16 thomas
2080 8a35f56c 2022-07-16 thomas x = 0;
2081 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
2082 8a35f56c 2022-07-16 thomas int escaped = 0;
2083 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
2084 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
2085 8a35f56c 2022-07-16 thomas continue;
2086 8a35f56c 2022-07-16 thomas
2087 8a35f56c 2022-07-16 thomas if (strlcat(*escaped_html, esc[j].s, len + 1)
2088 8a35f56c 2022-07-16 thomas >= len + 1) {
2089 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_SPACE);
2090 8a35f56c 2022-07-16 thomas goto done;
2091 8a35f56c 2022-07-16 thomas }
2092 8a35f56c 2022-07-16 thomas x += strlen(esc[j].s);
2093 8a35f56c 2022-07-16 thomas escaped = 1;
2094 8a35f56c 2022-07-16 thomas break;
2095 8a35f56c 2022-07-16 thomas }
2096 8a35f56c 2022-07-16 thomas if (!escaped) {
2097 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = orig_html[i];
2098 8a35f56c 2022-07-16 thomas x++;
2099 8a35f56c 2022-07-16 thomas }
2100 8a35f56c 2022-07-16 thomas }
2101 8a35f56c 2022-07-16 thomas done:
2102 8a35f56c 2022-07-16 thomas if (error) {
2103 8a35f56c 2022-07-16 thomas free(*escaped_html);
2104 8a35f56c 2022-07-16 thomas *escaped_html = NULL;
2105 8a35f56c 2022-07-16 thomas } else {
2106 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = '\0';
2107 8a35f56c 2022-07-16 thomas }
2108 8a35f56c 2022-07-16 thomas
2109 8a35f56c 2022-07-16 thomas return error;
2110 8a35f56c 2022-07-16 thomas }
2111 8a35f56c 2022-07-16 thomas
2112 55144267 2022-09-07 thomas static inline int
2113 55144267 2022-09-07 thomas should_urlencode(int c)
2114 55144267 2022-09-07 thomas {
2115 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
2116 55144267 2022-09-07 thomas return 1;
2117 55144267 2022-09-07 thomas
2118 55144267 2022-09-07 thomas switch (c) {
2119 55144267 2022-09-07 thomas /* gen-delim */
2120 55144267 2022-09-07 thomas case ':':
2121 55144267 2022-09-07 thomas case '/':
2122 55144267 2022-09-07 thomas case '?':
2123 55144267 2022-09-07 thomas case '#':
2124 55144267 2022-09-07 thomas case '[':
2125 55144267 2022-09-07 thomas case ']':
2126 55144267 2022-09-07 thomas case '@':
2127 55144267 2022-09-07 thomas /* sub-delims */
2128 55144267 2022-09-07 thomas case '!':
2129 55144267 2022-09-07 thomas case '$':
2130 55144267 2022-09-07 thomas case '&':
2131 55144267 2022-09-07 thomas case '\'':
2132 55144267 2022-09-07 thomas case '(':
2133 55144267 2022-09-07 thomas case ')':
2134 55144267 2022-09-07 thomas case '*':
2135 55144267 2022-09-07 thomas case '+':
2136 55144267 2022-09-07 thomas case ',':
2137 55144267 2022-09-07 thomas case ';':
2138 55144267 2022-09-07 thomas case '=':
2139 55144267 2022-09-07 thomas return 1;
2140 55144267 2022-09-07 thomas default:
2141 55144267 2022-09-07 thomas return 0;
2142 55144267 2022-09-07 thomas }
2143 55144267 2022-09-07 thomas }
2144 55144267 2022-09-07 thomas
2145 55144267 2022-09-07 thomas static char *
2146 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
2147 55144267 2022-09-07 thomas {
2148 55144267 2022-09-07 thomas const char *s;
2149 55144267 2022-09-07 thomas char *escaped;
2150 55144267 2022-09-07 thomas size_t i, len;
2151 55144267 2022-09-07 thomas int a, b;
2152 55144267 2022-09-07 thomas
2153 55144267 2022-09-07 thomas len = 0;
2154 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
2155 55144267 2022-09-07 thomas len++;
2156 55144267 2022-09-07 thomas if (should_urlencode(*s))
2157 55144267 2022-09-07 thomas len += 2;
2158 55144267 2022-09-07 thomas }
2159 55144267 2022-09-07 thomas
2160 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
2161 55144267 2022-09-07 thomas if (escaped == NULL)
2162 55144267 2022-09-07 thomas return NULL;
2163 55144267 2022-09-07 thomas
2164 55144267 2022-09-07 thomas i = 0;
2165 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
2166 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
2167 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
2168 55144267 2022-09-07 thomas b = (*s & 0x0F);
2169 55144267 2022-09-07 thomas
2170 55144267 2022-09-07 thomas escaped[i++] = '%';
2171 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2172 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2173 55144267 2022-09-07 thomas } else
2174 55144267 2022-09-07 thomas escaped[i++] = *s;
2175 55144267 2022-09-07 thomas }
2176 55144267 2022-09-07 thomas
2177 55144267 2022-09-07 thomas return escaped;
2178 55144267 2022-09-07 thomas }
2179 55144267 2022-09-07 thomas
2180 55144267 2022-09-07 thomas static inline const char *
2181 55144267 2022-09-07 thomas action_name(int action)
2182 55144267 2022-09-07 thomas {
2183 55144267 2022-09-07 thomas switch (action) {
2184 55144267 2022-09-07 thomas case BLAME:
2185 55144267 2022-09-07 thomas return "blame";
2186 55144267 2022-09-07 thomas case BLOB:
2187 55144267 2022-09-07 thomas return "blob";
2188 55144267 2022-09-07 thomas case BRIEFS:
2189 55144267 2022-09-07 thomas return "briefs";
2190 55144267 2022-09-07 thomas case COMMITS:
2191 55144267 2022-09-07 thomas return "commits";
2192 55144267 2022-09-07 thomas case DIFF:
2193 55144267 2022-09-07 thomas return "diff";
2194 55144267 2022-09-07 thomas case ERR:
2195 55144267 2022-09-07 thomas return "err";
2196 55144267 2022-09-07 thomas case INDEX:
2197 55144267 2022-09-07 thomas return "index";
2198 55144267 2022-09-07 thomas case SUMMARY:
2199 55144267 2022-09-07 thomas return "summary";
2200 55144267 2022-09-07 thomas case TAG:
2201 55144267 2022-09-07 thomas return "tag";
2202 55144267 2022-09-07 thomas case TAGS:
2203 55144267 2022-09-07 thomas return "tags";
2204 55144267 2022-09-07 thomas case TREE:
2205 55144267 2022-09-07 thomas return "tree";
2206 55144267 2022-09-07 thomas default:
2207 55144267 2022-09-07 thomas return NULL;
2208 55144267 2022-09-07 thomas }
2209 55144267 2022-09-07 thomas }
2210 55144267 2022-09-07 thomas
2211 55144267 2022-09-07 thomas static int
2212 55144267 2022-09-07 thomas gotweb_print_url(struct request *c, struct gotweb_url *url)
2213 55144267 2022-09-07 thomas {
2214 55144267 2022-09-07 thomas const char *sep = "?", *action;
2215 55144267 2022-09-07 thomas char *tmp;
2216 55144267 2022-09-07 thomas int r;
2217 55144267 2022-09-07 thomas
2218 55144267 2022-09-07 thomas action = action_name(url->action);
2219 55144267 2022-09-07 thomas if (action != NULL) {
2220 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
2221 55144267 2022-09-07 thomas return -1;
2222 55144267 2022-09-07 thomas sep = "&";
2223 55144267 2022-09-07 thomas }
2224 55144267 2022-09-07 thomas
2225 55144267 2022-09-07 thomas if (url->commit) {
2226 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2227 55144267 2022-09-07 thomas return -1;
2228 55144267 2022-09-07 thomas sep = "&";
2229 55144267 2022-09-07 thomas }
2230 55144267 2022-09-07 thomas
2231 55144267 2022-09-07 thomas if (url->previd) {
2232 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2233 55144267 2022-09-07 thomas return -1;
2234 55144267 2022-09-07 thomas sep = "&";
2235 55144267 2022-09-07 thomas }
2236 55144267 2022-09-07 thomas
2237 55144267 2022-09-07 thomas if (url->prevset) {
2238 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2239 55144267 2022-09-07 thomas return -1;
2240 55144267 2022-09-07 thomas sep = "&";
2241 55144267 2022-09-07 thomas }
2242 55144267 2022-09-07 thomas
2243 55144267 2022-09-07 thomas if (url->file) {
2244 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
2245 55144267 2022-09-07 thomas if (tmp == NULL)
2246 55144267 2022-09-07 thomas return -1;
2247 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2248 55144267 2022-09-07 thomas free(tmp);
2249 55144267 2022-09-07 thomas if (r == -1)
2250 55144267 2022-09-07 thomas return -1;
2251 55144267 2022-09-07 thomas sep = "&";
2252 55144267 2022-09-07 thomas }
2253 55144267 2022-09-07 thomas
2254 55144267 2022-09-07 thomas if (url->folder) {
2255 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
2256 55144267 2022-09-07 thomas if (tmp == NULL)
2257 55144267 2022-09-07 thomas return -1;
2258 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2259 55144267 2022-09-07 thomas free(tmp);
2260 55144267 2022-09-07 thomas if (r == -1)
2261 55144267 2022-09-07 thomas return -1;
2262 55144267 2022-09-07 thomas sep = "&";
2263 55144267 2022-09-07 thomas }
2264 55144267 2022-09-07 thomas
2265 55144267 2022-09-07 thomas if (url->headref) {
2266 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
2267 55144267 2022-09-07 thomas if (tmp == NULL)
2268 55144267 2022-09-07 thomas return -1;
2269 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2270 55144267 2022-09-07 thomas free(tmp);
2271 55144267 2022-09-07 thomas if (r == -1)
2272 55144267 2022-09-07 thomas return -1;
2273 55144267 2022-09-07 thomas sep = "&";
2274 55144267 2022-09-07 thomas }
2275 55144267 2022-09-07 thomas
2276 55144267 2022-09-07 thomas if (url->index_page != -1) {
2277 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
2278 55144267 2022-09-07 thomas url->index_page) == -1)
2279 55144267 2022-09-07 thomas return -1;
2280 55144267 2022-09-07 thomas sep = "&";
2281 55144267 2022-09-07 thomas }
2282 55144267 2022-09-07 thomas
2283 55144267 2022-09-07 thomas if (url->path) {
2284 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
2285 55144267 2022-09-07 thomas if (tmp == NULL)
2286 55144267 2022-09-07 thomas return -1;
2287 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
2288 55144267 2022-09-07 thomas free(tmp);
2289 55144267 2022-09-07 thomas if (r == -1)
2290 55144267 2022-09-07 thomas return -1;
2291 55144267 2022-09-07 thomas sep = "&";
2292 55144267 2022-09-07 thomas }
2293 55144267 2022-09-07 thomas
2294 55144267 2022-09-07 thomas if (url->page != -1) {
2295 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2296 55144267 2022-09-07 thomas return -1;
2297 55144267 2022-09-07 thomas sep = "&";
2298 55144267 2022-09-07 thomas }
2299 55144267 2022-09-07 thomas
2300 55144267 2022-09-07 thomas return 0;
2301 55144267 2022-09-07 thomas }
2302 55144267 2022-09-07 thomas
2303 55144267 2022-09-07 thomas int
2304 55144267 2022-09-07 thomas gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2305 55144267 2022-09-07 thomas {
2306 55144267 2022-09-07 thomas va_list ap;
2307 55144267 2022-09-07 thomas int r;
2308 55144267 2022-09-07 thomas
2309 55144267 2022-09-07 thomas if (fcgi_printf(c, "<a href='") == -1)
2310 55144267 2022-09-07 thomas return -1;
2311 55144267 2022-09-07 thomas
2312 55144267 2022-09-07 thomas if (gotweb_print_url(c, url) == -1)
2313 55144267 2022-09-07 thomas return -1;
2314 55144267 2022-09-07 thomas
2315 55144267 2022-09-07 thomas if (fcgi_printf(c, "'>") == -1)
2316 55144267 2022-09-07 thomas return -1;
2317 55144267 2022-09-07 thomas
2318 55144267 2022-09-07 thomas va_start(ap, fmt);
2319 55144267 2022-09-07 thomas r = fcgi_vprintf(c, fmt, ap);
2320 55144267 2022-09-07 thomas va_end(ap);
2321 55144267 2022-09-07 thomas if (r == -1)
2322 55144267 2022-09-07 thomas return -1;
2323 55144267 2022-09-07 thomas
2324 55144267 2022-09-07 thomas if (fcgi_printf(c, "</a>"))
2325 55144267 2022-09-07 thomas return -1;
2326 55144267 2022-09-07 thomas return 0;
2327 55144267 2022-09-07 thomas }
2328 55144267 2022-09-07 thomas
2329 55e6cffd 2022-09-01 thomas static struct got_repository *
2330 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
2331 55e6cffd 2022-09-01 thomas {
2332 55e6cffd 2022-09-01 thomas int i;
2333 55e6cffd 2022-09-01 thomas
2334 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
2335 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
2336 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
2337 55e6cffd 2022-09-01 thomas }
2338 55e6cffd 2022-09-01 thomas
2339 55e6cffd 2022-09-01 thomas return NULL;
2340 55e6cffd 2022-09-01 thomas }
2341 55e6cffd 2022-09-01 thomas
2342 8a35f56c 2022-07-16 thomas static const struct got_error *
2343 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
2344 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
2345 55e6cffd 2022-09-01 thomas {
2346 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
2347 55e6cffd 2022-09-01 thomas struct got_repository *repo;
2348 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
2349 55e6cffd 2022-09-01 thomas int evicted = 0;
2350 55e6cffd 2022-09-01 thomas
2351 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2352 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
2353 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
2354 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
2355 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
2356 55e6cffd 2022-09-01 thomas if (error)
2357 55e6cffd 2022-09-01 thomas return error;
2358 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2359 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
2360 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
2361 55e6cffd 2022-09-01 thomas evicted = 1;
2362 55e6cffd 2022-09-01 thomas } else {
2363 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
2364 55e6cffd 2022-09-01 thomas }
2365 55e6cffd 2022-09-01 thomas
2366 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2367 55e6cffd 2022-09-01 thomas if (error) {
2368 55e6cffd 2022-09-01 thomas if (evicted) {
2369 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2370 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
2371 55e6cffd 2022-09-01 thomas }
2372 55e6cffd 2022-09-01 thomas return error;
2373 55e6cffd 2022-09-01 thomas }
2374 55e6cffd 2022-09-01 thomas
2375 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2376 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
2377 55e6cffd 2022-09-01 thomas if (evicted) {
2378 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2379 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
2380 55e6cffd 2022-09-01 thomas }
2381 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
2382 55e6cffd 2022-09-01 thomas }
2383 55e6cffd 2022-09-01 thomas
2384 55e6cffd 2022-09-01 thomas cr->repo = repo;
2385 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
2386 55e6cffd 2022-09-01 thomas *new = repo;
2387 55e6cffd 2022-09-01 thomas return NULL;
2388 55e6cffd 2022-09-01 thomas }
2389 55e6cffd 2022-09-01 thomas
2390 55e6cffd 2022-09-01 thomas static const struct got_error *
2391 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2392 8a35f56c 2022-07-16 thomas {
2393 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2394 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
2395 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
2396 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
2397 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
2398 8a35f56c 2022-07-16 thomas DIR *dt;
2399 8a35f56c 2022-07-16 thomas char *dir_test;
2400 8a35f56c 2022-07-16 thomas
2401 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2402 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
2403 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2404 8a35f56c 2022-07-16 thomas
2405 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
2406 8a35f56c 2022-07-16 thomas if (dt == NULL) {
2407 8a35f56c 2022-07-16 thomas free(dir_test);
2408 8a35f56c 2022-07-16 thomas } else {
2409 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
2410 8a35f56c 2022-07-16 thomas dir_test = NULL;
2411 c2d3d9a0 2022-09-01 thomas goto done;
2412 8a35f56c 2022-07-16 thomas }
2413 8a35f56c 2022-07-16 thomas
2414 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2415 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
2416 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
2417 8a35f56c 2022-07-16 thomas
2418 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
2419 8a35f56c 2022-07-16 thomas if (dt == NULL) {
2420 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2421 8a35f56c 2022-07-16 thomas goto err;
2422 c2d3d9a0 2022-09-01 thomas } else {
2423 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
2424 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
2425 c2d3d9a0 2022-09-01 thomas }
2426 c2d3d9a0 2022-09-01 thomas
2427 8a35f56c 2022-07-16 thomas done:
2428 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
2429 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2430 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2431 3991b2a5 2022-10-31 thomas goto err;
2432 3991b2a5 2022-10-31 thomas }
2433 3991b2a5 2022-10-31 thomas
2434 3991b2a5 2022-10-31 thomas
2435 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
2436 55e6cffd 2022-09-01 thomas if (repo == NULL) {
2437 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
2438 55e6cffd 2022-09-01 thomas if (error)
2439 55e6cffd 2022-09-01 thomas goto err;
2440 55e6cffd 2022-09-01 thomas }
2441 55e6cffd 2022-09-01 thomas t->repo = repo;
2442 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
2443 8a35f56c 2022-07-16 thomas repo_dir->path);
2444 8a35f56c 2022-07-16 thomas if (error)
2445 8a35f56c 2022-07-16 thomas goto err;
2446 8a35f56c 2022-07-16 thomas error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2447 8a35f56c 2022-07-16 thomas if (error)
2448 8a35f56c 2022-07-16 thomas goto err;
2449 8a35f56c 2022-07-16 thomas error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2450 8a35f56c 2022-07-16 thomas NULL, TM_DIFF);
2451 8a35f56c 2022-07-16 thomas if (error)
2452 8a35f56c 2022-07-16 thomas goto err;
2453 8a35f56c 2022-07-16 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2454 8a35f56c 2022-07-16 thomas err:
2455 8a35f56c 2022-07-16 thomas free(dir_test);
2456 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
2457 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
2458 8a35f56c 2022-07-16 thomas return error;
2459 8a35f56c 2022-07-16 thomas }
2460 8a35f56c 2022-07-16 thomas
2461 8a35f56c 2022-07-16 thomas static const struct got_error *
2462 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2463 8a35f56c 2022-07-16 thomas {
2464 8a35f56c 2022-07-16 thomas const struct got_error *error;
2465 8a35f56c 2022-07-16 thomas
2466 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
2467 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
2468 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
2469 8a35f56c 2022-07-16 thomas
2470 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2471 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
2472 8a35f56c 2022-07-16 thomas free(*repo_dir);
2473 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
2474 8a35f56c 2022-07-16 thomas return error;
2475 8a35f56c 2022-07-16 thomas }
2476 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
2477 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
2478 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
2479 8a35f56c 2022-07-16 thomas (*repo_dir)->age = NULL;
2480 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
2481 8a35f56c 2022-07-16 thomas
2482 8a35f56c 2022-07-16 thomas return NULL;
2483 8a35f56c 2022-07-16 thomas }
2484 8a35f56c 2022-07-16 thomas
2485 8a35f56c 2022-07-16 thomas static const struct got_error *
2486 8a35f56c 2022-07-16 thomas gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2487 8a35f56c 2022-07-16 thomas {
2488 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2489 8a35f56c 2022-07-16 thomas FILE *f = NULL;
2490 8a35f56c 2022-07-16 thomas char *d_file = NULL;
2491 8a35f56c 2022-07-16 thomas unsigned int len;
2492 8a35f56c 2022-07-16 thomas size_t n;
2493 8a35f56c 2022-07-16 thomas
2494 8a35f56c 2022-07-16 thomas *description = NULL;
2495 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
2496 8a35f56c 2022-07-16 thomas return NULL;
2497 8a35f56c 2022-07-16 thomas
2498 8a35f56c 2022-07-16 thomas if (asprintf(&d_file, "%s/description", dir) == -1)
2499 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2500 8a35f56c 2022-07-16 thomas
2501 8a35f56c 2022-07-16 thomas f = fopen(d_file, "r");
2502 8a35f56c 2022-07-16 thomas if (f == NULL) {
2503 31797419 2022-09-02 thomas if (errno != ENOENT && errno != EACCES)
2504 31797419 2022-09-02 thomas error = got_error_from_errno2("fopen", d_file);
2505 8a35f56c 2022-07-16 thomas goto done;
2506 8a35f56c 2022-07-16 thomas }
2507 8a35f56c 2022-07-16 thomas
2508 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_END) == -1) {
2509 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2510 8a35f56c 2022-07-16 thomas goto done;
2511 8a35f56c 2022-07-16 thomas }
2512 8a35f56c 2022-07-16 thomas len = ftell(f);
2513 8a35f56c 2022-07-16 thomas if (len == -1) {
2514 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2515 8a35f56c 2022-07-16 thomas goto done;
2516 8a35f56c 2022-07-16 thomas }
2517 8a35f56c 2022-07-16 thomas
2518 2f26d334 2022-08-30 thomas if (len == 0) {
2519 2f26d334 2022-08-30 thomas *description = strdup("");
2520 2f26d334 2022-08-30 thomas if (*description == NULL)
2521 31797419 2022-09-02 thomas error = got_error_from_errno("strdup");
2522 8a35f56c 2022-07-16 thomas goto done;
2523 2f26d334 2022-08-30 thomas }
2524 8a35f56c 2022-07-16 thomas
2525 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_SET) == -1) {
2526 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2527 8a35f56c 2022-07-16 thomas goto done;
2528 8a35f56c 2022-07-16 thomas }
2529 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
2530 8a35f56c 2022-07-16 thomas if (*description == NULL) {
2531 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
2532 8a35f56c 2022-07-16 thomas goto done;
2533 8a35f56c 2022-07-16 thomas }
2534 8a35f56c 2022-07-16 thomas
2535 8a35f56c 2022-07-16 thomas n = fread(*description, 1, len, f);
2536 8a35f56c 2022-07-16 thomas if (n == 0 && ferror(f))
2537 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2538 8a35f56c 2022-07-16 thomas done:
2539 8a35f56c 2022-07-16 thomas if (f != NULL && fclose(f) == EOF && error == NULL)
2540 8a35f56c 2022-07-16 thomas error = got_error_from_errno("fclose");
2541 8a35f56c 2022-07-16 thomas free(d_file);
2542 8a35f56c 2022-07-16 thomas return error;
2543 8a35f56c 2022-07-16 thomas }
2544 8a35f56c 2022-07-16 thomas
2545 8a35f56c 2022-07-16 thomas static const struct got_error *
2546 8a35f56c 2022-07-16 thomas gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2547 8a35f56c 2022-07-16 thomas {
2548 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2549 8a35f56c 2022-07-16 thomas FILE *f;
2550 8a35f56c 2022-07-16 thomas char *d_file = NULL;
2551 8a35f56c 2022-07-16 thomas unsigned int len;
2552 8a35f56c 2022-07-16 thomas size_t n;
2553 8a35f56c 2022-07-16 thomas
2554 8a35f56c 2022-07-16 thomas *url = NULL;
2555 8a35f56c 2022-07-16 thomas
2556 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
2557 8a35f56c 2022-07-16 thomas return NULL;
2558 8a35f56c 2022-07-16 thomas
2559 8a35f56c 2022-07-16 thomas if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2560 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2561 8a35f56c 2022-07-16 thomas
2562 8a35f56c 2022-07-16 thomas f = fopen(d_file, "r");
2563 8a35f56c 2022-07-16 thomas if (f == NULL) {
2564 8a35f56c 2022-07-16 thomas if (errno != ENOENT && errno != EACCES)
2565 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("fopen", d_file);
2566 8a35f56c 2022-07-16 thomas goto done;
2567 8a35f56c 2022-07-16 thomas }
2568 8a35f56c 2022-07-16 thomas
2569 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_END) == -1) {
2570 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2571 8a35f56c 2022-07-16 thomas goto done;
2572 8a35f56c 2022-07-16 thomas }
2573 8a35f56c 2022-07-16 thomas len = ftell(f);
2574 8a35f56c 2022-07-16 thomas if (len == -1) {
2575 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2576 8a35f56c 2022-07-16 thomas goto done;
2577 8a35f56c 2022-07-16 thomas }
2578 8a35f56c 2022-07-16 thomas if (len == 0)
2579 8a35f56c 2022-07-16 thomas goto done;
2580 8a35f56c 2022-07-16 thomas
2581 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_SET) == -1) {
2582 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2583 8a35f56c 2022-07-16 thomas goto done;
2584 8a35f56c 2022-07-16 thomas }
2585 8a35f56c 2022-07-16 thomas
2586 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
2587 8a35f56c 2022-07-16 thomas if (*url == NULL) {
2588 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
2589 8a35f56c 2022-07-16 thomas goto done;
2590 8a35f56c 2022-07-16 thomas }
2591 8a35f56c 2022-07-16 thomas
2592 8a35f56c 2022-07-16 thomas n = fread(*url, 1, len, f);
2593 8a35f56c 2022-07-16 thomas if (n == 0 && ferror(f))
2594 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2595 8a35f56c 2022-07-16 thomas done:
2596 8a35f56c 2022-07-16 thomas if (f != NULL && fclose(f) == EOF && error == NULL)
2597 8a35f56c 2022-07-16 thomas error = got_error_from_errno("fclose");
2598 8a35f56c 2022-07-16 thomas free(d_file);
2599 8a35f56c 2022-07-16 thomas return error;
2600 8a35f56c 2022-07-16 thomas }
2601 8a35f56c 2022-07-16 thomas
2602 8a35f56c 2022-07-16 thomas const struct got_error *
2603 8a35f56c 2022-07-16 thomas gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2604 8a35f56c 2022-07-16 thomas {
2605 8a35f56c 2022-07-16 thomas struct tm tm;
2606 399ea8e4 2022-07-20 thomas long long diff_time;
2607 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
2608 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
2609 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
2610 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
2611 8a35f56c 2022-07-16 thomas char *s;
2612 8a35f56c 2022-07-16 thomas char datebuf[29];
2613 8a35f56c 2022-07-16 thomas
2614 8a35f56c 2022-07-16 thomas *repo_age = NULL;
2615 8a35f56c 2022-07-16 thomas
2616 8a35f56c 2022-07-16 thomas switch (ref_tm) {
2617 8a35f56c 2022-07-16 thomas case TM_DIFF:
2618 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
2619 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
2620 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2621 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
2622 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2623 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2624 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2625 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
2626 8a35f56c 2022-07-16 thomas months) == -1)
2627 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2628 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2629 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2630 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2631 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2632 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
2633 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2634 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
2635 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2636 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
2637 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2638 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
2639 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2640 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
2641 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2642 8a35f56c 2022-07-16 thomas minutes) == -1)
2643 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2644 8a35f56c 2022-07-16 thomas } else if (diff_time > 2) {
2645 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", diff_time,
2646 8a35f56c 2022-07-16 thomas seconds) == -1)
2647 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2648 8a35f56c 2022-07-16 thomas } else {
2649 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s", now) == -1)
2650 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2651 8a35f56c 2022-07-16 thomas }
2652 8a35f56c 2022-07-16 thomas break;
2653 8a35f56c 2022-07-16 thomas case TM_LONG:
2654 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2655 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
2656 8a35f56c 2022-07-16 thomas
2657 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
2658 8a35f56c 2022-07-16 thomas if (s == NULL)
2659 8a35f56c 2022-07-16 thomas return got_error_from_errno("asctime_r");
2660 8a35f56c 2022-07-16 thomas
2661 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2662 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2663 8a35f56c 2022-07-16 thomas break;
2664 8a35f56c 2022-07-16 thomas }
2665 8a35f56c 2022-07-16 thomas return NULL;
2666 3e12c168 2022-07-16 thomas }