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 79393471 2022-08-27 thomas if (fcgi_printf(c, "%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 char *c_path = NULL;
997 8a35f56c 2022-07-16 thomas struct stat st;
998 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
999 79393471 2022-08-27 thomas int r;
1000 8a35f56c 2022-07-16 thomas
1001 8a35f56c 2022-07-16 thomas d = opendir(srv->repos_path);
1002 8a35f56c 2022-07-16 thomas if (d == NULL) {
1003 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("opendir", srv->repos_path);
1004 8a35f56c 2022-07-16 thomas return error;
1005 8a35f56c 2022-07-16 thomas }
1006 8a35f56c 2022-07-16 thomas
1007 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1008 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
1009 f530ab0e 2022-09-02 thomas sd_dent = NULL;
1010 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
1011 8a35f56c 2022-07-16 thomas goto done;
1012 8a35f56c 2022-07-16 thomas }
1013 8a35f56c 2022-07-16 thomas
1014 8a35f56c 2022-07-16 thomas /* get total count of repos */
1015 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
1016 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1017 8a35f56c 2022-07-16 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0)
1018 8a35f56c 2022-07-16 thomas continue;
1019 8a35f56c 2022-07-16 thomas
1020 8a35f56c 2022-07-16 thomas if (asprintf(&c_path, "%s/%s", srv->repos_path,
1021 8a35f56c 2022-07-16 thomas sd_dent[d_i]->d_name) == -1) {
1022 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1023 8a35f56c 2022-07-16 thomas return error;
1024 8a35f56c 2022-07-16 thomas }
1025 8a35f56c 2022-07-16 thomas
1026 8a35f56c 2022-07-16 thomas if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
1027 8a35f56c 2022-07-16 thomas !got_path_dir_is_empty(c_path))
1028 718f1524 2022-08-31 thomas t->repos_total++;
1029 8a35f56c 2022-07-16 thomas free(c_path);
1030 8a35f56c 2022-07-16 thomas c_path = NULL;
1031 8a35f56c 2022-07-16 thomas }
1032 8a35f56c 2022-07-16 thomas
1033 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='index_header'>\n"
1034 79393471 2022-08-27 thomas "<div id='index_header_project'>Project</div>\n");
1035 79393471 2022-08-27 thomas if (r == -1)
1036 8a35f56c 2022-07-16 thomas goto done;
1037 79393471 2022-08-27 thomas
1038 8a35f56c 2022-07-16 thomas if (srv->show_repo_description)
1039 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='index_header_description'>"
1040 8a35f56c 2022-07-16 thomas "Description</div>\n") == -1)
1041 8a35f56c 2022-07-16 thomas goto done;
1042 8a35f56c 2022-07-16 thomas if (srv->show_repo_owner)
1043 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='index_header_owner'>"
1044 8a35f56c 2022-07-16 thomas "Owner</div>\n") == -1)
1045 8a35f56c 2022-07-16 thomas goto done;
1046 8a35f56c 2022-07-16 thomas if (srv->show_repo_age)
1047 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='index_header_age'>"
1048 8a35f56c 2022-07-16 thomas "Last Change</div>\n") == -1)
1049 8a35f56c 2022-07-16 thomas goto done;
1050 79393471 2022-08-27 thomas if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1051 8a35f56c 2022-07-16 thomas goto done;
1052 8a35f56c 2022-07-16 thomas
1053 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
1054 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1055 8a35f56c 2022-07-16 thomas break; /* account for parent and self */
1056 8a35f56c 2022-07-16 thomas
1057 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1058 8a35f56c 2022-07-16 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0)
1059 8a35f56c 2022-07-16 thomas continue;
1060 8a35f56c 2022-07-16 thomas
1061 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
1062 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
1063 8a35f56c 2022-07-16 thomas t->prev_disp++;
1064 8a35f56c 2022-07-16 thomas continue;
1065 8a35f56c 2022-07-16 thomas }
1066 8a35f56c 2022-07-16 thomas
1067 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1068 8a35f56c 2022-07-16 thomas if (error)
1069 8a35f56c 2022-07-16 thomas goto done;
1070 8a35f56c 2022-07-16 thomas
1071 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
1072 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1073 8a35f56c 2022-07-16 thomas error = NULL;
1074 8a35f56c 2022-07-16 thomas continue;
1075 8a35f56c 2022-07-16 thomas }
1076 8a35f56c 2022-07-16 thomas else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1077 8a35f56c 2022-07-16 thomas goto done;
1078 8a35f56c 2022-07-16 thomas
1079 8a35f56c 2022-07-16 thomas if (lstat(repo_dir->path, &st) == 0 &&
1080 8a35f56c 2022-07-16 thomas S_ISDIR(st.st_mode) &&
1081 8a35f56c 2022-07-16 thomas !got_path_dir_is_empty(repo_dir->path))
1082 8a35f56c 2022-07-16 thomas goto render;
1083 8a35f56c 2022-07-16 thomas else {
1084 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
1085 8a35f56c 2022-07-16 thomas repo_dir = NULL;
1086 8a35f56c 2022-07-16 thomas continue;
1087 8a35f56c 2022-07-16 thomas }
1088 8a35f56c 2022-07-16 thomas render:
1089 8a35f56c 2022-07-16 thomas d_disp++;
1090 8a35f56c 2022-07-16 thomas t->prev_disp++;
1091 8a35f56c 2022-07-16 thomas
1092 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1093 55144267 2022-09-07 thomas "<div class='index_project'>") == -1)
1094 55144267 2022-09-07 thomas goto done;
1095 55144267 2022-09-07 thomas
1096 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1097 55144267 2022-09-07 thomas .action = SUMMARY,
1098 55144267 2022-09-07 thomas .index_page = -1,
1099 55144267 2022-09-07 thomas .page = -1,
1100 55144267 2022-09-07 thomas .path = repo_dir->name,
1101 55144267 2022-09-07 thomas }, "%s", repo_dir->name);
1102 79393471 2022-08-27 thomas if (r == -1)
1103 8a35f56c 2022-07-16 thomas goto done;
1104 8a35f56c 2022-07-16 thomas
1105 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1106 55144267 2022-09-07 thomas goto done;
1107 55144267 2022-09-07 thomas
1108 8a35f56c 2022-07-16 thomas if (srv->show_repo_description) {
1109 79393471 2022-08-27 thomas r = fcgi_printf(c,
1110 79393471 2022-08-27 thomas "<div class='index_project_description'>\n"
1111 79393471 2022-08-27 thomas "%s</div>\n", repo_dir->description);
1112 79393471 2022-08-27 thomas if (r == -1)
1113 8a35f56c 2022-07-16 thomas goto done;
1114 8a35f56c 2022-07-16 thomas }
1115 8a35f56c 2022-07-16 thomas
1116 8a35f56c 2022-07-16 thomas if (srv->show_repo_owner) {
1117 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='index_project_owner'>"
1118 79393471 2022-08-27 thomas "%s</div>\n", repo_dir->owner);
1119 79393471 2022-08-27 thomas if (r == -1)
1120 8a35f56c 2022-07-16 thomas goto done;
1121 8a35f56c 2022-07-16 thomas }
1122 8a35f56c 2022-07-16 thomas
1123 8a35f56c 2022-07-16 thomas if (srv->show_repo_age) {
1124 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='index_project_age'>"
1125 79393471 2022-08-27 thomas "%s</div>\n", repo_dir->age);
1126 79393471 2022-08-27 thomas if (r == -1)
1127 8a35f56c 2022-07-16 thomas goto done;
1128 8a35f56c 2022-07-16 thomas }
1129 8a35f56c 2022-07-16 thomas
1130 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='navs_wrapper'>"
1131 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1132 55144267 2022-09-07 thomas goto done;
1133 55144267 2022-09-07 thomas
1134 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1135 55144267 2022-09-07 thomas .action = SUMMARY,
1136 55144267 2022-09-07 thomas .index_page = -1,
1137 55144267 2022-09-07 thomas .page = -1,
1138 55144267 2022-09-07 thomas .path = repo_dir->name
1139 55144267 2022-09-07 thomas }, "summary");
1140 55144267 2022-09-07 thomas if (r == -1)
1141 55144267 2022-09-07 thomas goto done;
1142 55144267 2022-09-07 thomas
1143 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1144 55144267 2022-09-07 thomas goto done;
1145 55144267 2022-09-07 thomas
1146 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1147 55144267 2022-09-07 thomas .action = BRIEFS,
1148 55144267 2022-09-07 thomas .index_page = -1,
1149 55144267 2022-09-07 thomas .page = -1,
1150 55144267 2022-09-07 thomas .path = repo_dir->name
1151 55144267 2022-09-07 thomas }, "commit briefs");
1152 55144267 2022-09-07 thomas if (r == -1)
1153 55144267 2022-09-07 thomas goto done;
1154 55144267 2022-09-07 thomas
1155 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1156 55144267 2022-09-07 thomas goto done;
1157 55144267 2022-09-07 thomas
1158 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1159 55144267 2022-09-07 thomas .action = COMMITS,
1160 55144267 2022-09-07 thomas .index_page = -1,
1161 55144267 2022-09-07 thomas .page = -1,
1162 55144267 2022-09-07 thomas .path = repo_dir->name
1163 55144267 2022-09-07 thomas }, "commits");
1164 55144267 2022-09-07 thomas if (r == -1)
1165 55144267 2022-09-07 thomas goto done;
1166 55144267 2022-09-07 thomas
1167 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1168 55144267 2022-09-07 thomas goto done;
1169 55144267 2022-09-07 thomas
1170 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1171 55144267 2022-09-07 thomas .action = TAGS,
1172 55144267 2022-09-07 thomas .index_page = -1,
1173 55144267 2022-09-07 thomas .page = -1,
1174 55144267 2022-09-07 thomas .path = repo_dir->name
1175 55144267 2022-09-07 thomas }, "tags");
1176 55144267 2022-09-07 thomas if (r == -1)
1177 55144267 2022-09-07 thomas goto done;
1178 55144267 2022-09-07 thomas
1179 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1180 55144267 2022-09-07 thomas goto done;
1181 55144267 2022-09-07 thomas
1182 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1183 55144267 2022-09-07 thomas .action = TREE,
1184 55144267 2022-09-07 thomas .index_page = -1,
1185 55144267 2022-09-07 thomas .page = -1,
1186 55144267 2022-09-07 thomas .path = repo_dir->name
1187 55144267 2022-09-07 thomas }, "tree");
1188 55144267 2022-09-07 thomas if (r == -1)
1189 55144267 2022-09-07 thomas goto done;
1190 55144267 2022-09-07 thomas
1191 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>" /* .navs */
1192 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1193 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1194 55144267 2022-09-07 thomas "</div>\n"); /* .index_wrapper */
1195 79393471 2022-08-27 thomas if (r == -1)
1196 8a35f56c 2022-07-16 thomas goto done;
1197 8a35f56c 2022-07-16 thomas
1198 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
1199 8a35f56c 2022-07-16 thomas repo_dir = NULL;
1200 8a35f56c 2022-07-16 thomas t->next_disp++;
1201 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
1202 8a35f56c 2022-07-16 thomas break;
1203 8a35f56c 2022-07-16 thomas }
1204 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
1205 79393471 2022-08-27 thomas goto done;
1206 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1207 79393471 2022-08-27 thomas goto done;
1208 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
1209 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
1210 79393471 2022-08-27 thomas goto done;
1211 8a35f56c 2022-07-16 thomas
1212 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
1213 8a35f56c 2022-07-16 thomas if (error)
1214 8a35f56c 2022-07-16 thomas goto done;
1215 8a35f56c 2022-07-16 thomas done:
1216 f530ab0e 2022-09-02 thomas if (sd_dent) {
1217 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
1218 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
1219 f530ab0e 2022-09-02 thomas free(sd_dent);
1220 f530ab0e 2022-09-02 thomas }
1221 8a35f56c 2022-07-16 thomas if (d != NULL && closedir(d) == EOF && error == NULL)
1222 8a35f56c 2022-07-16 thomas error = got_error_from_errno("closedir");
1223 8a35f56c 2022-07-16 thomas return error;
1224 8a35f56c 2022-07-16 thomas }
1225 8a35f56c 2022-07-16 thomas
1226 8a35f56c 2022-07-16 thomas static const struct got_error *
1227 8a35f56c 2022-07-16 thomas gotweb_render_blame(struct request *c)
1228 8a35f56c 2022-07-16 thomas {
1229 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1230 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1231 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1232 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
1233 79393471 2022-08-27 thomas int r;
1234 8a35f56c 2022-07-16 thomas
1235 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1236 8a35f56c 2022-07-16 thomas if (error)
1237 8a35f56c 2022-07-16 thomas return error;
1238 8a35f56c 2022-07-16 thomas
1239 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1240 8a35f56c 2022-07-16 thomas
1241 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1242 255f4022 2022-08-27 thomas if (error)
1243 255f4022 2022-08-27 thomas goto done;
1244 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1245 8a35f56c 2022-07-16 thomas if (error)
1246 8a35f56c 2022-07-16 thomas goto done;
1247 8a35f56c 2022-07-16 thomas
1248 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1249 79393471 2022-08-27 thomas "<div id='blame_title'>Blame</div>\n"
1250 79393471 2022-08-27 thomas "</div>\n" /* #blame_title_wrapper */
1251 79393471 2022-08-27 thomas "<div id='blame_content'>\n"
1252 79393471 2022-08-27 thomas "<div id='blame_header_wrapper'>\n"
1253 79393471 2022-08-27 thomas "<div id='blame_header'>\n"
1254 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1255 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1256 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1257 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1258 79393471 2022-08-27 thomas "</div>\n" /* #blame_header */
1259 79393471 2022-08-27 thomas "</div>\n" /* #blame_header_wrapper */
1260 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1261 79393471 2022-08-27 thomas "<div id='blame'>\n",
1262 2ad628aa 2022-08-31 thomas age,
1263 255f4022 2022-08-27 thomas msg);
1264 79393471 2022-08-27 thomas if (r == -1)
1265 8a35f56c 2022-07-16 thomas goto done;
1266 8a35f56c 2022-07-16 thomas
1267 8a35f56c 2022-07-16 thomas error = got_output_file_blame(c);
1268 8a35f56c 2022-07-16 thomas if (error)
1269 8a35f56c 2022-07-16 thomas goto done;
1270 8a35f56c 2022-07-16 thomas
1271 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n" /* #blame */
1272 79393471 2022-08-27 thomas "</div>\n"); /* #blame_content */
1273 8a35f56c 2022-07-16 thomas done:
1274 2ad628aa 2022-08-31 thomas free(age);
1275 255f4022 2022-08-27 thomas free(msg);
1276 8a35f56c 2022-07-16 thomas return error;
1277 8a35f56c 2022-07-16 thomas }
1278 8a35f56c 2022-07-16 thomas
1279 8a35f56c 2022-07-16 thomas static const struct got_error *
1280 8a35f56c 2022-07-16 thomas gotweb_render_briefs(struct request *c)
1281 8a35f56c 2022-07-16 thomas {
1282 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1283 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1284 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1285 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1286 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1287 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1288 8a35f56c 2022-07-16 thomas char *smallerthan, *newline;
1289 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1290 79393471 2022-08-27 thomas int r;
1291 8a35f56c 2022-07-16 thomas
1292 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1293 79393471 2022-08-27 thomas "<div id='briefs_title'>Commit Briefs</div>\n"
1294 79393471 2022-08-27 thomas "</div>\n" /* #briefs_title_wrapper */
1295 79393471 2022-08-27 thomas "<div id='briefs_content'>\n");
1296 79393471 2022-08-27 thomas if (r == -1)
1297 8a35f56c 2022-07-16 thomas goto done;
1298 8a35f56c 2022-07-16 thomas
1299 8a35f56c 2022-07-16 thomas if (qs->action == SUMMARY) {
1300 8a35f56c 2022-07-16 thomas qs->action = BRIEFS;
1301 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1302 8a35f56c 2022-07-16 thomas } else
1303 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, srv->max_commits_display);
1304 8a35f56c 2022-07-16 thomas if (error)
1305 8a35f56c 2022-07-16 thomas goto done;
1306 8a35f56c 2022-07-16 thomas
1307 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1308 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1309 8a35f56c 2022-07-16 thomas if (error)
1310 8a35f56c 2022-07-16 thomas goto done;
1311 8a35f56c 2022-07-16 thomas
1312 8a35f56c 2022-07-16 thomas smallerthan = strchr(rc->author, '<');
1313 8a35f56c 2022-07-16 thomas if (smallerthan)
1314 8a35f56c 2022-07-16 thomas *smallerthan = '\0';
1315 8a35f56c 2022-07-16 thomas
1316 8a35f56c 2022-07-16 thomas newline = strchr(rc->commit_msg, '\n');
1317 8a35f56c 2022-07-16 thomas if (newline)
1318 8a35f56c 2022-07-16 thomas *newline = '\0';
1319 8a35f56c 2022-07-16 thomas
1320 255f4022 2022-08-27 thomas error = gotweb_escape_html(&author, rc->author);
1321 255f4022 2022-08-27 thomas if (error)
1322 255f4022 2022-08-27 thomas goto done;
1323 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1324 255f4022 2022-08-27 thomas if (error)
1325 255f4022 2022-08-27 thomas goto done;
1326 255f4022 2022-08-27 thomas
1327 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1328 79393471 2022-08-27 thomas "<div class='briefs_author'>%s</div>\n"
1329 55144267 2022-09-07 thomas "<div class='briefs_log'>",
1330 55144267 2022-09-07 thomas age, author);
1331 79393471 2022-08-27 thomas if (r == -1)
1332 8a35f56c 2022-07-16 thomas goto done;
1333 79393471 2022-08-27 thomas
1334 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1335 55144267 2022-09-07 thomas .action = DIFF,
1336 55144267 2022-09-07 thomas .index_page = -1,
1337 55144267 2022-09-07 thomas .page = -1,
1338 55144267 2022-09-07 thomas .path = repo_dir->name,
1339 55144267 2022-09-07 thomas .commit = rc->commit_id,
1340 55144267 2022-09-07 thomas .headref = qs->headref,
1341 55144267 2022-09-07 thomas }, "%s", msg);
1342 55144267 2022-09-07 thomas if (r == -1)
1343 55144267 2022-09-07 thomas goto done;
1344 55144267 2022-09-07 thomas
1345 8a35f56c 2022-07-16 thomas if (rc->refs_str) {
1346 255f4022 2022-08-27 thomas char *refs;
1347 255f4022 2022-08-27 thomas
1348 255f4022 2022-08-27 thomas error = gotweb_escape_html(&refs, rc->refs_str);
1349 255f4022 2022-08-27 thomas if (error)
1350 255f4022 2022-08-27 thomas goto done;
1351 79393471 2022-08-27 thomas r = fcgi_printf(c,
1352 255f4022 2022-08-27 thomas " <span class='refs_str'>(%s)</span>", refs);
1353 255f4022 2022-08-27 thomas free(refs);
1354 79393471 2022-08-27 thomas if (r == -1)
1355 8a35f56c 2022-07-16 thomas goto done;
1356 8a35f56c 2022-07-16 thomas }
1357 79393471 2022-08-27 thomas if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1358 8a35f56c 2022-07-16 thomas goto done;
1359 8a35f56c 2022-07-16 thomas
1360 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1361 55144267 2022-09-07 thomas "<div class='navs'>");
1362 79393471 2022-08-27 thomas if (r == -1)
1363 8a35f56c 2022-07-16 thomas goto done;
1364 8a35f56c 2022-07-16 thomas
1365 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1366 55144267 2022-09-07 thomas .action = DIFF,
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 }, "diff");
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, " | ") == -1)
1377 55144267 2022-09-07 thomas goto done;
1378 55144267 2022-09-07 thomas
1379 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1380 55144267 2022-09-07 thomas .action = TREE,
1381 55144267 2022-09-07 thomas .index_page = -1,
1382 55144267 2022-09-07 thomas .page = -1,
1383 55144267 2022-09-07 thomas .path = repo_dir->name,
1384 55144267 2022-09-07 thomas .commit = rc->commit_id,
1385 55144267 2022-09-07 thomas .headref = qs->headref,
1386 55144267 2022-09-07 thomas }, "tree");
1387 55144267 2022-09-07 thomas if (r == -1)
1388 55144267 2022-09-07 thomas goto done;
1389 55144267 2022-09-07 thomas
1390 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .navs */
1391 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1392 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n") == -1)
1393 55144267 2022-09-07 thomas goto done;
1394 55144267 2022-09-07 thomas
1395 8a35f56c 2022-07-16 thomas free(age);
1396 8a35f56c 2022-07-16 thomas age = NULL;
1397 255f4022 2022-08-27 thomas free(author);
1398 255f4022 2022-08-27 thomas author = NULL;
1399 255f4022 2022-08-27 thomas free(msg);
1400 255f4022 2022-08-27 thomas msg = NULL;
1401 8a35f56c 2022-07-16 thomas }
1402 8a35f56c 2022-07-16 thomas
1403 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1404 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
1405 8a35f56c 2022-07-16 thomas if (error)
1406 8a35f56c 2022-07-16 thomas goto done;
1407 8a35f56c 2022-07-16 thomas }
1408 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #briefs_content */
1409 8a35f56c 2022-07-16 thomas done:
1410 8a35f56c 2022-07-16 thomas free(age);
1411 255f4022 2022-08-27 thomas free(author);
1412 255f4022 2022-08-27 thomas free(msg);
1413 8a35f56c 2022-07-16 thomas return error;
1414 8a35f56c 2022-07-16 thomas }
1415 8a35f56c 2022-07-16 thomas
1416 8a35f56c 2022-07-16 thomas static const struct got_error *
1417 8a35f56c 2022-07-16 thomas gotweb_render_commits(struct request *c)
1418 8a35f56c 2022-07-16 thomas {
1419 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1420 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1421 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1422 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1423 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1424 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1425 79393471 2022-08-27 thomas int r;
1426 8a35f56c 2022-07-16 thomas
1427 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1428 79393471 2022-08-27 thomas "<div class='commits_title'>Commits</div>\n"
1429 79393471 2022-08-27 thomas "</div>\n" /* .commits_title_wrapper */
1430 79393471 2022-08-27 thomas "<div class='commits_content'>\n");
1431 79393471 2022-08-27 thomas if (r == -1)
1432 8a35f56c 2022-07-16 thomas goto done;
1433 8a35f56c 2022-07-16 thomas
1434 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, srv->max_commits_display);
1435 8a35f56c 2022-07-16 thomas if (error)
1436 8a35f56c 2022-07-16 thomas goto done;
1437 8a35f56c 2022-07-16 thomas
1438 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1439 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1440 8a35f56c 2022-07-16 thomas if (error)
1441 8a35f56c 2022-07-16 thomas goto done;
1442 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rc->author);
1443 8a35f56c 2022-07-16 thomas if (error)
1444 8a35f56c 2022-07-16 thomas goto done;
1445 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1446 255f4022 2022-08-27 thomas if (error)
1447 255f4022 2022-08-27 thomas goto done;
1448 8a35f56c 2022-07-16 thomas
1449 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1450 79393471 2022-08-27 thomas "<div class='commits_header'>\n"
1451 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1452 79393471 2022-08-27 thomas "<div class='header_commit'>%s</div>\n"
1453 79393471 2022-08-27 thomas "<div class='header_author_title'>Author:</div>\n"
1454 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1455 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1456 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1457 79393471 2022-08-27 thomas "</div>\n" /* .commits_header */
1458 79393471 2022-08-27 thomas "</div>\n" /* .commits_header_wrapper */
1459 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1460 79393471 2022-08-27 thomas "<div class='commit'>\n%s</div>\n",
1461 79393471 2022-08-27 thomas rc->commit_id,
1462 255f4022 2022-08-27 thomas author,
1463 2ad628aa 2022-08-31 thomas age,
1464 255f4022 2022-08-27 thomas msg);
1465 79393471 2022-08-27 thomas if (r == -1)
1466 8a35f56c 2022-07-16 thomas goto done;
1467 8a35f56c 2022-07-16 thomas
1468 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1469 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1470 55144267 2022-09-07 thomas goto done;
1471 55144267 2022-09-07 thomas
1472 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1473 55144267 2022-09-07 thomas .action = DIFF,
1474 55144267 2022-09-07 thomas .index_page = -1,
1475 55144267 2022-09-07 thomas .page = -1,
1476 55144267 2022-09-07 thomas .path = repo_dir->name,
1477 55144267 2022-09-07 thomas .commit = rc->commit_id,
1478 55144267 2022-09-07 thomas }, "diff");
1479 1510f72d 2022-09-02 thomas if (r == -1)
1480 1510f72d 2022-09-02 thomas goto done;
1481 8a35f56c 2022-07-16 thomas
1482 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1483 55144267 2022-09-07 thomas goto done;
1484 55144267 2022-09-07 thomas
1485 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1486 55144267 2022-09-07 thomas .action = TREE,
1487 55144267 2022-09-07 thomas .index_page = -1,
1488 55144267 2022-09-07 thomas .page = -1,
1489 55144267 2022-09-07 thomas .path = repo_dir->name,
1490 55144267 2022-09-07 thomas .commit = rc->commit_id,
1491 55144267 2022-09-07 thomas }, "tree");
1492 55144267 2022-09-07 thomas if (r == -1)
1493 55144267 2022-09-07 thomas goto done;
1494 55144267 2022-09-07 thomas
1495 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .navs */
1496 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1497 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n") == -1)
1498 55144267 2022-09-07 thomas goto done;
1499 55144267 2022-09-07 thomas
1500 8a35f56c 2022-07-16 thomas free(age);
1501 8a35f56c 2022-07-16 thomas age = NULL;
1502 8a35f56c 2022-07-16 thomas free(author);
1503 8a35f56c 2022-07-16 thomas author = NULL;
1504 255f4022 2022-08-27 thomas free(msg);
1505 255f4022 2022-08-27 thomas msg = NULL;
1506 8a35f56c 2022-07-16 thomas }
1507 8a35f56c 2022-07-16 thomas
1508 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1509 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
1510 8a35f56c 2022-07-16 thomas if (error)
1511 8a35f56c 2022-07-16 thomas goto done;
1512 8a35f56c 2022-07-16 thomas }
1513 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* .commits_content */
1514 8a35f56c 2022-07-16 thomas done:
1515 8a35f56c 2022-07-16 thomas free(age);
1516 255f4022 2022-08-27 thomas free(author);
1517 255f4022 2022-08-27 thomas free(msg);
1518 8a35f56c 2022-07-16 thomas return error;
1519 8a35f56c 2022-07-16 thomas }
1520 8a35f56c 2022-07-16 thomas
1521 8a35f56c 2022-07-16 thomas static const struct got_error *
1522 8a35f56c 2022-07-16 thomas gotweb_render_branches(struct request *c)
1523 8a35f56c 2022-07-16 thomas {
1524 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1525 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1526 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
1527 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1528 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1529 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1530 55144267 2022-09-07 thomas char *escaped_refname = NULL;
1531 8a35f56c 2022-07-16 thomas char *age = NULL;
1532 79393471 2022-08-27 thomas int r;
1533 8a35f56c 2022-07-16 thomas
1534 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1535 8a35f56c 2022-07-16 thomas
1536 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
1537 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
1538 8a35f56c 2022-07-16 thomas if (error)
1539 8a35f56c 2022-07-16 thomas goto done;
1540 8a35f56c 2022-07-16 thomas
1541 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1542 79393471 2022-08-27 thomas "<div id='branches_title'>Branches</div>\n"
1543 79393471 2022-08-27 thomas "</div>\n" /* #branches_title_wrapper */
1544 79393471 2022-08-27 thomas "<div id='branches_content'>\n");
1545 79393471 2022-08-27 thomas if (r == -1)
1546 8a35f56c 2022-07-16 thomas goto done;
1547 8a35f56c 2022-07-16 thomas
1548 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
1549 255f4022 2022-08-27 thomas const char *refname = NULL;
1550 8a35f56c 2022-07-16 thomas
1551 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
1552 8a35f56c 2022-07-16 thomas continue;
1553 8a35f56c 2022-07-16 thomas
1554 255f4022 2022-08-27 thomas refname = got_ref_get_name(re->ref);
1555 8a35f56c 2022-07-16 thomas if (refname == NULL) {
1556 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
1557 8a35f56c 2022-07-16 thomas goto done;
1558 8a35f56c 2022-07-16 thomas }
1559 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) != 0)
1560 8a35f56c 2022-07-16 thomas continue;
1561 8a35f56c 2022-07-16 thomas
1562 8a35f56c 2022-07-16 thomas error = got_get_repo_age(&age, c, qs->path, refname,
1563 8a35f56c 2022-07-16 thomas TM_DIFF);
1564 8a35f56c 2022-07-16 thomas if (error)
1565 8a35f56c 2022-07-16 thomas goto done;
1566 8a35f56c 2022-07-16 thomas
1567 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
1568 8a35f56c 2022-07-16 thomas refname += 11;
1569 255f4022 2022-08-27 thomas error = gotweb_escape_html(&escaped_refname, refname);
1570 255f4022 2022-08-27 thomas if (error)
1571 255f4022 2022-08-27 thomas goto done;
1572 8a35f56c 2022-07-16 thomas
1573 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1574 79393471 2022-08-27 thomas "<div class='branches_age'>%s</div>\n"
1575 79393471 2022-08-27 thomas "<div class='branches_space'>&nbsp;</div>\n"
1576 55144267 2022-09-07 thomas "<div class='branch'>", age);
1577 55144267 2022-09-07 thomas if (r == -1)
1578 55144267 2022-09-07 thomas goto done;
1579 55144267 2022-09-07 thomas
1580 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1581 55144267 2022-09-07 thomas .action = SUMMARY,
1582 55144267 2022-09-07 thomas .index_page = -1,
1583 55144267 2022-09-07 thomas .page = -1,
1584 55144267 2022-09-07 thomas .path = qs->path,
1585 55144267 2022-09-07 thomas .headref = refname,
1586 55144267 2022-09-07 thomas }, "%s", escaped_refname);
1587 55144267 2022-09-07 thomas if (r == -1)
1588 55144267 2022-09-07 thomas goto done;
1589 55144267 2022-09-07 thomas
1590 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .branch */
1591 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1592 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1593 55144267 2022-09-07 thomas goto done;
1594 55144267 2022-09-07 thomas
1595 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1596 55144267 2022-09-07 thomas .action = SUMMARY,
1597 55144267 2022-09-07 thomas .index_page = -1,
1598 55144267 2022-09-07 thomas .page = -1,
1599 55144267 2022-09-07 thomas .path = qs->path,
1600 55144267 2022-09-07 thomas .headref = refname,
1601 55144267 2022-09-07 thomas }, "summary");
1602 55144267 2022-09-07 thomas if (r == -1)
1603 55144267 2022-09-07 thomas goto done;
1604 55144267 2022-09-07 thomas
1605 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1606 55144267 2022-09-07 thomas goto done;
1607 55144267 2022-09-07 thomas
1608 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1609 55144267 2022-09-07 thomas .action = BRIEFS,
1610 55144267 2022-09-07 thomas .index_page = -1,
1611 55144267 2022-09-07 thomas .page = -1,
1612 55144267 2022-09-07 thomas .path = qs->path,
1613 55144267 2022-09-07 thomas .headref = refname,
1614 55144267 2022-09-07 thomas }, "commit briefs");
1615 55144267 2022-09-07 thomas if (r == -1)
1616 55144267 2022-09-07 thomas goto done;
1617 55144267 2022-09-07 thomas
1618 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1619 55144267 2022-09-07 thomas goto done;
1620 55144267 2022-09-07 thomas
1621 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1622 55144267 2022-09-07 thomas .action = COMMITS,
1623 55144267 2022-09-07 thomas .index_page = -1,
1624 55144267 2022-09-07 thomas .page = -1,
1625 55144267 2022-09-07 thomas .path = qs->path,
1626 55144267 2022-09-07 thomas .headref = refname,
1627 55144267 2022-09-07 thomas }, "commits");
1628 55144267 2022-09-07 thomas if (r == -1)
1629 55144267 2022-09-07 thomas goto done;
1630 55144267 2022-09-07 thomas
1631 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>\n" /* .navs */
1632 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1633 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1634 55144267 2022-09-07 thomas "</div>\n"); /* .branches_wrapper */
1635 79393471 2022-08-27 thomas if (r == -1)
1636 8a35f56c 2022-07-16 thomas goto done;
1637 8a35f56c 2022-07-16 thomas
1638 8a35f56c 2022-07-16 thomas free(age);
1639 8a35f56c 2022-07-16 thomas age = NULL;
1640 55144267 2022-09-07 thomas free(escaped_refname);
1641 55144267 2022-09-07 thomas escaped_refname = NULL;
1642 8a35f56c 2022-07-16 thomas }
1643 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #branches_content */
1644 8a35f56c 2022-07-16 thomas done:
1645 f95e65a1 2022-09-02 thomas free(age);
1646 55144267 2022-09-07 thomas free(escaped_refname);
1647 f95e65a1 2022-09-02 thomas got_ref_list_free(&refs);
1648 8a35f56c 2022-07-16 thomas return error;
1649 8a35f56c 2022-07-16 thomas }
1650 8a35f56c 2022-07-16 thomas
1651 8a35f56c 2022-07-16 thomas static const struct got_error *
1652 8a35f56c 2022-07-16 thomas gotweb_render_tree(struct request *c)
1653 8a35f56c 2022-07-16 thomas {
1654 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1655 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1656 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1657 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
1658 79393471 2022-08-27 thomas int r;
1659 8a35f56c 2022-07-16 thomas
1660 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1661 8a35f56c 2022-07-16 thomas if (error)
1662 8a35f56c 2022-07-16 thomas return error;
1663 8a35f56c 2022-07-16 thomas
1664 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1665 8a35f56c 2022-07-16 thomas
1666 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1667 8a35f56c 2022-07-16 thomas if (error)
1668 8a35f56c 2022-07-16 thomas goto done;
1669 8a35f56c 2022-07-16 thomas
1670 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1671 255f4022 2022-08-27 thomas if (error)
1672 255f4022 2022-08-27 thomas goto done;
1673 255f4022 2022-08-27 thomas
1674 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1675 79393471 2022-08-27 thomas "<div id='tree_title'>Tree</div>\n"
1676 79393471 2022-08-27 thomas "</div>\n" /* #tree_title_wrapper */
1677 79393471 2022-08-27 thomas "<div id='tree_content'>\n"
1678 79393471 2022-08-27 thomas "<div id='tree_header_wrapper'>\n"
1679 79393471 2022-08-27 thomas "<div id='tree_header'>\n"
1680 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1681 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1682 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1683 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1684 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1685 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1686 79393471 2022-08-27 thomas "</div>\n" /* #tree_header */
1687 79393471 2022-08-27 thomas "</div>\n" /* #tree_header_wrapper */
1688 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1689 79393471 2022-08-27 thomas "<div id='tree'>\n",
1690 79393471 2022-08-27 thomas rc->tree_id,
1691 2ad628aa 2022-08-31 thomas age,
1692 255f4022 2022-08-27 thomas msg);
1693 79393471 2022-08-27 thomas if (r == -1)
1694 8a35f56c 2022-07-16 thomas goto done;
1695 8a35f56c 2022-07-16 thomas
1696 8a35f56c 2022-07-16 thomas error = got_output_repo_tree(c);
1697 8a35f56c 2022-07-16 thomas if (error)
1698 8a35f56c 2022-07-16 thomas goto done;
1699 8a35f56c 2022-07-16 thomas
1700 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree */
1701 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree_content */
1702 8a35f56c 2022-07-16 thomas done:
1703 2ad628aa 2022-08-31 thomas free(age);
1704 255f4022 2022-08-27 thomas free(msg);
1705 8a35f56c 2022-07-16 thomas return error;
1706 8a35f56c 2022-07-16 thomas }
1707 8a35f56c 2022-07-16 thomas
1708 8a35f56c 2022-07-16 thomas static const struct got_error *
1709 8a35f56c 2022-07-16 thomas gotweb_render_diff(struct request *c)
1710 8a35f56c 2022-07-16 thomas {
1711 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1712 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1713 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1714 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1715 79393471 2022-08-27 thomas int r;
1716 8a35f56c 2022-07-16 thomas
1717 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1718 8a35f56c 2022-07-16 thomas if (error)
1719 8a35f56c 2022-07-16 thomas return error;
1720 8a35f56c 2022-07-16 thomas
1721 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1722 8a35f56c 2022-07-16 thomas
1723 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1724 8a35f56c 2022-07-16 thomas if (error)
1725 8a35f56c 2022-07-16 thomas goto done;
1726 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rc->author);
1727 8a35f56c 2022-07-16 thomas if (error)
1728 8a35f56c 2022-07-16 thomas goto done;
1729 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1730 255f4022 2022-08-27 thomas if (error)
1731 255f4022 2022-08-27 thomas goto done;
1732 8a35f56c 2022-07-16 thomas
1733 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1734 79393471 2022-08-27 thomas "<div id='diff_title'>Commit Diff</div>\n"
1735 79393471 2022-08-27 thomas "</div>\n" /* #diff_title_wrapper */
1736 79393471 2022-08-27 thomas "<div id='diff_content'>\n"
1737 79393471 2022-08-27 thomas "<div id='diff_header_wrapper'>\n"
1738 79393471 2022-08-27 thomas "<div id='diff_header'>\n"
1739 79393471 2022-08-27 thomas "<div id='header_diff_title'>Diff:</div>\n"
1740 79393471 2022-08-27 thomas "<div id='header_diff'>%s<br />%s</div>\n"
1741 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1742 79393471 2022-08-27 thomas "<div class='header_commit'>%s</div>\n"
1743 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1744 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1745 79393471 2022-08-27 thomas "<div class='header_author_title'>Author:</div>\n"
1746 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1747 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1748 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1749 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1750 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1751 79393471 2022-08-27 thomas "</div>\n" /* #diff_header */
1752 79393471 2022-08-27 thomas "</div>\n" /* #diff_header_wrapper */
1753 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1754 79393471 2022-08-27 thomas "<div id='diff'>\n",
1755 79393471 2022-08-27 thomas rc->parent_id, rc->commit_id,
1756 79393471 2022-08-27 thomas rc->commit_id,
1757 79393471 2022-08-27 thomas rc->tree_id,
1758 255f4022 2022-08-27 thomas author,
1759 2ad628aa 2022-08-31 thomas age,
1760 255f4022 2022-08-27 thomas msg);
1761 79393471 2022-08-27 thomas if (r == -1)
1762 8a35f56c 2022-07-16 thomas goto done;
1763 8a35f56c 2022-07-16 thomas
1764 8a35f56c 2022-07-16 thomas error = got_output_repo_diff(c);
1765 8a35f56c 2022-07-16 thomas if (error)
1766 8a35f56c 2022-07-16 thomas goto done;
1767 8a35f56c 2022-07-16 thomas
1768 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff */
1769 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff_content */
1770 8a35f56c 2022-07-16 thomas done:
1771 8a35f56c 2022-07-16 thomas free(age);
1772 8a35f56c 2022-07-16 thomas free(author);
1773 255f4022 2022-08-27 thomas free(msg);
1774 8a35f56c 2022-07-16 thomas return error;
1775 8a35f56c 2022-07-16 thomas }
1776 8a35f56c 2022-07-16 thomas
1777 8a35f56c 2022-07-16 thomas static const struct got_error *
1778 8a35f56c 2022-07-16 thomas gotweb_render_summary(struct request *c)
1779 8a35f56c 2022-07-16 thomas {
1780 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1781 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1782 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1783 79393471 2022-08-27 thomas int r;
1784 8a35f56c 2022-07-16 thomas
1785 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1786 8a35f56c 2022-07-16 thomas goto done;
1787 8a35f56c 2022-07-16 thomas
1788 79393471 2022-08-27 thomas if (srv->show_repo_description) {
1789 79393471 2022-08-27 thomas r = fcgi_printf(c,
1790 79393471 2022-08-27 thomas "<div id='description_title'>Description:</div>\n"
1791 79393471 2022-08-27 thomas "<div id='description'>%s</div>\n",
1792 ddf2e5c2 2022-08-27 thomas t->repo_dir->description ? t->repo_dir->description : "");
1793 79393471 2022-08-27 thomas if (r == -1)
1794 79393471 2022-08-27 thomas goto done;
1795 79393471 2022-08-27 thomas }
1796 8a35f56c 2022-07-16 thomas
1797 79393471 2022-08-27 thomas if (srv->show_repo_owner) {
1798 79393471 2022-08-27 thomas r = fcgi_printf(c,
1799 79393471 2022-08-27 thomas "<div id='repo_owner_title'>Owner:</div>\n"
1800 79393471 2022-08-27 thomas "<div id='repo_owner'>%s</div>\n",
1801 ddf2e5c2 2022-08-27 thomas t->repo_dir->owner ? t->repo_dir->owner : "");
1802 79393471 2022-08-27 thomas if (r == -1)
1803 79393471 2022-08-27 thomas goto done;
1804 79393471 2022-08-27 thomas }
1805 8a35f56c 2022-07-16 thomas
1806 79393471 2022-08-27 thomas if (srv->show_repo_age) {
1807 79393471 2022-08-27 thomas r = fcgi_printf(c,
1808 79393471 2022-08-27 thomas "<div id='last_change_title'>Last Change:</div>\n"
1809 79393471 2022-08-27 thomas "<div id='last_change'>%s</div>\n",
1810 79393471 2022-08-27 thomas t->repo_dir->age);
1811 79393471 2022-08-27 thomas if (r == -1)
1812 79393471 2022-08-27 thomas goto done;
1813 79393471 2022-08-27 thomas }
1814 8a35f56c 2022-07-16 thomas
1815 79393471 2022-08-27 thomas if (srv->show_repo_cloneurl) {
1816 79393471 2022-08-27 thomas r = fcgi_printf(c,
1817 79393471 2022-08-27 thomas "<div id='cloneurl_title'>Clone URL:</div>\n"
1818 79393471 2022-08-27 thomas "<div id='cloneurl'>%s</div>\n",
1819 79393471 2022-08-27 thomas t->repo_dir->url ? t->repo_dir->url : "");
1820 79393471 2022-08-27 thomas if (r == -1)
1821 79393471 2022-08-27 thomas goto done;
1822 79393471 2022-08-27 thomas }
1823 8a35f56c 2022-07-16 thomas
1824 79393471 2022-08-27 thomas r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1825 79393471 2022-08-27 thomas if (r == -1)
1826 8a35f56c 2022-07-16 thomas goto done;
1827 8a35f56c 2022-07-16 thomas
1828 8a35f56c 2022-07-16 thomas error = gotweb_render_briefs(c);
1829 8a35f56c 2022-07-16 thomas if (error) {
1830 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1831 8a35f56c 2022-07-16 thomas goto done;
1832 8a35f56c 2022-07-16 thomas }
1833 8a35f56c 2022-07-16 thomas
1834 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
1835 8a35f56c 2022-07-16 thomas if (error) {
1836 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1837 8a35f56c 2022-07-16 thomas goto done;
1838 8a35f56c 2022-07-16 thomas }
1839 8a35f56c 2022-07-16 thomas
1840 8a35f56c 2022-07-16 thomas error = gotweb_render_branches(c);
1841 8a35f56c 2022-07-16 thomas if (error)
1842 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1843 8a35f56c 2022-07-16 thomas done:
1844 8a35f56c 2022-07-16 thomas return error;
1845 8a35f56c 2022-07-16 thomas }
1846 8a35f56c 2022-07-16 thomas
1847 8a35f56c 2022-07-16 thomas static const struct got_error *
1848 8a35f56c 2022-07-16 thomas gotweb_render_tag(struct request *c)
1849 8a35f56c 2022-07-16 thomas {
1850 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1851 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1852 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1853 255f4022 2022-08-27 thomas char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1854 8a35f56c 2022-07-16 thomas
1855 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, 1);
1856 8a35f56c 2022-07-16 thomas if (error)
1857 8a35f56c 2022-07-16 thomas goto done;
1858 8a35f56c 2022-07-16 thomas
1859 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1860 8a35f56c 2022-07-16 thomas error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1861 8a35f56c 2022-07-16 thomas "bad commit id");
1862 8a35f56c 2022-07-16 thomas goto done;
1863 8a35f56c 2022-07-16 thomas }
1864 8a35f56c 2022-07-16 thomas
1865 8a35f56c 2022-07-16 thomas rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1866 8a35f56c 2022-07-16 thomas
1867 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1868 8a35f56c 2022-07-16 thomas if (error)
1869 8a35f56c 2022-07-16 thomas goto done;
1870 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rt->tagger);
1871 8a35f56c 2022-07-16 thomas if (error)
1872 8a35f56c 2022-07-16 thomas goto done;
1873 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->commit_msg);
1874 255f4022 2022-08-27 thomas if (error)
1875 255f4022 2022-08-27 thomas goto done;
1876 8a35f56c 2022-07-16 thomas
1877 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1878 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/", 5) == 0)
1879 c8d0196f 2022-09-02 thomas tagname += 5;
1880 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1881 255f4022 2022-08-27 thomas if (error)
1882 255f4022 2022-08-27 thomas goto done;
1883 8a35f56c 2022-07-16 thomas
1884 79393471 2022-08-27 thomas fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1885 79393471 2022-08-27 thomas "<div id='tags_title'>Tag</div>\n"
1886 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1887 79393471 2022-08-27 thomas "<div id='tags_content'>\n"
1888 79393471 2022-08-27 thomas "<div id='tag_header_wrapper'>\n"
1889 79393471 2022-08-27 thomas "<div id='tag_header'>\n"
1890 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1891 79393471 2022-08-27 thomas "<div class='header_commit'>%s"
1892 79393471 2022-08-27 thomas " <span class='refs_str'>(%s)</span></div>\n"
1893 79393471 2022-08-27 thomas "<div class='header_author_title'>Tagger:</div>\n"
1894 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1895 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1896 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1897 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1898 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1899 79393471 2022-08-27 thomas "</div>\n" /* #tag_header */
1900 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1901 79393471 2022-08-27 thomas "<div id='tag_commit'>\n%s</div>"
1902 80ca8b0f 2022-09-06 thomas "</div>" /* #tag_header_wrapper */
1903 80ca8b0f 2022-09-06 thomas "</div>", /* #tags_content */
1904 79393471 2022-08-27 thomas rt->commit_id,
1905 255f4022 2022-08-27 thomas tagname,
1906 255f4022 2022-08-27 thomas author,
1907 2ad628aa 2022-08-31 thomas age,
1908 255f4022 2022-08-27 thomas msg,
1909 79393471 2022-08-27 thomas rt->tag_commit);
1910 8a35f56c 2022-07-16 thomas
1911 8a35f56c 2022-07-16 thomas done:
1912 8a35f56c 2022-07-16 thomas free(age);
1913 8a35f56c 2022-07-16 thomas free(author);
1914 255f4022 2022-08-27 thomas free(msg);
1915 8a35f56c 2022-07-16 thomas return error;
1916 8a35f56c 2022-07-16 thomas }
1917 8a35f56c 2022-07-16 thomas
1918 8a35f56c 2022-07-16 thomas static const struct got_error *
1919 8a35f56c 2022-07-16 thomas gotweb_render_tags(struct request *c)
1920 8a35f56c 2022-07-16 thomas {
1921 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1922 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1923 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1924 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1925 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1926 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1927 255f4022 2022-08-27 thomas char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1928 79393471 2022-08-27 thomas int r, commit_found = 0;
1929 8a35f56c 2022-07-16 thomas
1930 8a35f56c 2022-07-16 thomas if (qs->action == BRIEFS) {
1931 8a35f56c 2022-07-16 thomas qs->action = TAGS;
1932 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1933 8a35f56c 2022-07-16 thomas } else
1934 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, srv->max_commits_display);
1935 8a35f56c 2022-07-16 thomas if (error)
1936 8a35f56c 2022-07-16 thomas goto done;
1937 8a35f56c 2022-07-16 thomas
1938 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1939 79393471 2022-08-27 thomas "<div id='tags_title'>Tags</div>\n"
1940 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1941 79393471 2022-08-27 thomas "<div id='tags_content'>\n");
1942 79393471 2022-08-27 thomas if (r == -1)
1943 8a35f56c 2022-07-16 thomas goto done;
1944 8a35f56c 2022-07-16 thomas
1945 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1946 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1947 79393471 2022-08-27 thomas "This repository contains no tags");
1948 79393471 2022-08-27 thomas if (r == -1)
1949 8a35f56c 2022-07-16 thomas goto done;
1950 8a35f56c 2022-07-16 thomas }
1951 8a35f56c 2022-07-16 thomas
1952 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1953 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL) {
1954 8a35f56c 2022-07-16 thomas if (strcmp(qs->commit, rt->commit_id) != 0)
1955 8a35f56c 2022-07-16 thomas continue;
1956 8a35f56c 2022-07-16 thomas else
1957 8a35f56c 2022-07-16 thomas commit_found = 1;
1958 8a35f56c 2022-07-16 thomas }
1959 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1960 8a35f56c 2022-07-16 thomas if (error)
1961 8a35f56c 2022-07-16 thomas goto done;
1962 8a35f56c 2022-07-16 thomas
1963 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1964 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/tags/", 10) == 0)
1965 c8d0196f 2022-09-02 thomas tagname += 10;
1966 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1967 255f4022 2022-08-27 thomas if (error)
1968 255f4022 2022-08-27 thomas goto done;
1969 8a35f56c 2022-07-16 thomas
1970 8a35f56c 2022-07-16 thomas if (rt->tag_commit != NULL) {
1971 8a35f56c 2022-07-16 thomas newline = strchr(rt->tag_commit, '\n');
1972 8a35f56c 2022-07-16 thomas if (newline)
1973 8a35f56c 2022-07-16 thomas *newline = '\0';
1974 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->tag_commit);
1975 255f4022 2022-08-27 thomas if (error)
1976 255f4022 2022-08-27 thomas goto done;
1977 8a35f56c 2022-07-16 thomas }
1978 8a35f56c 2022-07-16 thomas
1979 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1980 79393471 2022-08-27 thomas "<div class='tag'>%s</div>\n"
1981 55144267 2022-09-07 thomas "<div class='tag_log'>", age, tagname) == -1)
1982 55144267 2022-09-07 thomas goto done;
1983 55144267 2022-09-07 thomas
1984 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1985 55144267 2022-09-07 thomas .action = TAG,
1986 55144267 2022-09-07 thomas .index_page = -1,
1987 55144267 2022-09-07 thomas .page = -1,
1988 55144267 2022-09-07 thomas .path = repo_dir->name,
1989 55144267 2022-09-07 thomas .commit = rt->commit_id,
1990 55144267 2022-09-07 thomas }, "%s", msg ? msg : "");
1991 55144267 2022-09-07 thomas if (r == -1)
1992 55144267 2022-09-07 thomas goto done;
1993 55144267 2022-09-07 thomas
1994 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .tag_log */
1995 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1996 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1997 55144267 2022-09-07 thomas goto done;
1998 55144267 2022-09-07 thomas
1999 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
2000 55144267 2022-09-07 thomas .action = TAG,
2001 55144267 2022-09-07 thomas .index_page = -1,
2002 55144267 2022-09-07 thomas .page = -1,
2003 55144267 2022-09-07 thomas .path = repo_dir->name,
2004 55144267 2022-09-07 thomas .commit = rt->commit_id,
2005 55144267 2022-09-07 thomas }, "tag");
2006 55144267 2022-09-07 thomas if (r == -1)
2007 55144267 2022-09-07 thomas goto done;
2008 55144267 2022-09-07 thomas
2009 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
2010 55144267 2022-09-07 thomas goto done;
2011 55144267 2022-09-07 thomas
2012 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
2013 55144267 2022-09-07 thomas .action = BRIEFS,
2014 55144267 2022-09-07 thomas .index_page = -1,
2015 55144267 2022-09-07 thomas .page = -1,
2016 55144267 2022-09-07 thomas .path = repo_dir->name,
2017 55144267 2022-09-07 thomas .commit = rt->commit_id,
2018 55144267 2022-09-07 thomas }, "commit briefs");
2019 55144267 2022-09-07 thomas if (r == -1)
2020 55144267 2022-09-07 thomas goto done;
2021 55144267 2022-09-07 thomas
2022 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
2023 55144267 2022-09-07 thomas goto done;
2024 55144267 2022-09-07 thomas
2025 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
2026 55144267 2022-09-07 thomas .action = COMMITS,
2027 55144267 2022-09-07 thomas .index_page = -1,
2028 55144267 2022-09-07 thomas .page = -1,
2029 55144267 2022-09-07 thomas .path = repo_dir->name,
2030 55144267 2022-09-07 thomas .commit = rt->commit_id,
2031 55144267 2022-09-07 thomas }, "commits");
2032 55144267 2022-09-07 thomas if (r == -1)
2033 55144267 2022-09-07 thomas goto done;
2034 55144267 2022-09-07 thomas
2035 55144267 2022-09-07 thomas r = fcgi_printf(c,
2036 79393471 2022-08-27 thomas "</div>\n" /* .navs */
2037 79393471 2022-08-27 thomas "</div>\n" /* .navs_wrapper */
2038 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n");
2039 79393471 2022-08-27 thomas if (r == -1)
2040 8a35f56c 2022-07-16 thomas goto done;
2041 8a35f56c 2022-07-16 thomas
2042 8a35f56c 2022-07-16 thomas free(age);
2043 8a35f56c 2022-07-16 thomas age = NULL;
2044 255f4022 2022-08-27 thomas free(tagname);
2045 255f4022 2022-08-27 thomas tagname = NULL;
2046 255f4022 2022-08-27 thomas free(msg);
2047 255f4022 2022-08-27 thomas msg = NULL;
2048 8a35f56c 2022-07-16 thomas }
2049 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
2050 8a35f56c 2022-07-16 thomas error = gotweb_render_navs(c);
2051 8a35f56c 2022-07-16 thomas if (error)
2052 8a35f56c 2022-07-16 thomas goto done;
2053 8a35f56c 2022-07-16 thomas }
2054 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tags_content */
2055 8a35f56c 2022-07-16 thomas done:
2056 8a35f56c 2022-07-16 thomas free(age);
2057 255f4022 2022-08-27 thomas free(tagname);
2058 255f4022 2022-08-27 thomas free(msg);
2059 8a35f56c 2022-07-16 thomas return error;
2060 8a35f56c 2022-07-16 thomas }
2061 8a35f56c 2022-07-16 thomas
2062 8a35f56c 2022-07-16 thomas const struct got_error *
2063 8a35f56c 2022-07-16 thomas gotweb_escape_html(char **escaped_html, const char *orig_html)
2064 8a35f56c 2022-07-16 thomas {
2065 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2066 8a35f56c 2022-07-16 thomas struct escape_pair {
2067 8a35f56c 2022-07-16 thomas char c;
2068 8a35f56c 2022-07-16 thomas const char *s;
2069 8a35f56c 2022-07-16 thomas } esc[] = {
2070 8a35f56c 2022-07-16 thomas { '>', "&gt;" },
2071 8a35f56c 2022-07-16 thomas { '<', "&lt;" },
2072 8a35f56c 2022-07-16 thomas { '&', "&amp;" },
2073 8a35f56c 2022-07-16 thomas { '"', "&quot;" },
2074 8a35f56c 2022-07-16 thomas { '\'', "&apos;" },
2075 8a35f56c 2022-07-16 thomas { '\n', "<br />" },
2076 8a35f56c 2022-07-16 thomas };
2077 8a35f56c 2022-07-16 thomas size_t orig_len, len;
2078 8a35f56c 2022-07-16 thomas int i, j, x;
2079 8a35f56c 2022-07-16 thomas
2080 8a35f56c 2022-07-16 thomas orig_len = strlen(orig_html);
2081 8a35f56c 2022-07-16 thomas len = orig_len;
2082 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
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 len += strlen(esc[j].s) - 1 /* escaped char */;
2087 8a35f56c 2022-07-16 thomas }
2088 8a35f56c 2022-07-16 thomas }
2089 8a35f56c 2022-07-16 thomas
2090 8a35f56c 2022-07-16 thomas *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2091 8a35f56c 2022-07-16 thomas if (*escaped_html == NULL)
2092 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
2093 8a35f56c 2022-07-16 thomas
2094 8a35f56c 2022-07-16 thomas x = 0;
2095 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
2096 8a35f56c 2022-07-16 thomas int escaped = 0;
2097 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
2098 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
2099 8a35f56c 2022-07-16 thomas continue;
2100 8a35f56c 2022-07-16 thomas
2101 8a35f56c 2022-07-16 thomas if (strlcat(*escaped_html, esc[j].s, len + 1)
2102 8a35f56c 2022-07-16 thomas >= len + 1) {
2103 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_SPACE);
2104 8a35f56c 2022-07-16 thomas goto done;
2105 8a35f56c 2022-07-16 thomas }
2106 8a35f56c 2022-07-16 thomas x += strlen(esc[j].s);
2107 8a35f56c 2022-07-16 thomas escaped = 1;
2108 8a35f56c 2022-07-16 thomas break;
2109 8a35f56c 2022-07-16 thomas }
2110 8a35f56c 2022-07-16 thomas if (!escaped) {
2111 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = orig_html[i];
2112 8a35f56c 2022-07-16 thomas x++;
2113 8a35f56c 2022-07-16 thomas }
2114 8a35f56c 2022-07-16 thomas }
2115 8a35f56c 2022-07-16 thomas done:
2116 8a35f56c 2022-07-16 thomas if (error) {
2117 8a35f56c 2022-07-16 thomas free(*escaped_html);
2118 8a35f56c 2022-07-16 thomas *escaped_html = NULL;
2119 8a35f56c 2022-07-16 thomas } else {
2120 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = '\0';
2121 8a35f56c 2022-07-16 thomas }
2122 8a35f56c 2022-07-16 thomas
2123 8a35f56c 2022-07-16 thomas return error;
2124 8a35f56c 2022-07-16 thomas }
2125 8a35f56c 2022-07-16 thomas
2126 55144267 2022-09-07 thomas static inline int
2127 55144267 2022-09-07 thomas should_urlencode(int c)
2128 55144267 2022-09-07 thomas {
2129 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
2130 55144267 2022-09-07 thomas return 1;
2131 55144267 2022-09-07 thomas
2132 55144267 2022-09-07 thomas switch (c) {
2133 55144267 2022-09-07 thomas /* gen-delim */
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 case ']':
2140 55144267 2022-09-07 thomas case '@':
2141 55144267 2022-09-07 thomas /* sub-delims */
2142 55144267 2022-09-07 thomas case '!':
2143 55144267 2022-09-07 thomas case '$':
2144 55144267 2022-09-07 thomas case '&':
2145 55144267 2022-09-07 thomas case '\'':
2146 55144267 2022-09-07 thomas case '(':
2147 55144267 2022-09-07 thomas case ')':
2148 55144267 2022-09-07 thomas case '*':
2149 55144267 2022-09-07 thomas case '+':
2150 55144267 2022-09-07 thomas case ',':
2151 55144267 2022-09-07 thomas case ';':
2152 55144267 2022-09-07 thomas case '=':
2153 55144267 2022-09-07 thomas return 1;
2154 55144267 2022-09-07 thomas default:
2155 55144267 2022-09-07 thomas return 0;
2156 55144267 2022-09-07 thomas }
2157 55144267 2022-09-07 thomas }
2158 55144267 2022-09-07 thomas
2159 55144267 2022-09-07 thomas static char *
2160 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
2161 55144267 2022-09-07 thomas {
2162 55144267 2022-09-07 thomas const char *s;
2163 55144267 2022-09-07 thomas char *escaped;
2164 55144267 2022-09-07 thomas size_t i, len;
2165 55144267 2022-09-07 thomas int a, b;
2166 55144267 2022-09-07 thomas
2167 55144267 2022-09-07 thomas len = 0;
2168 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
2169 55144267 2022-09-07 thomas len++;
2170 55144267 2022-09-07 thomas if (should_urlencode(*s))
2171 55144267 2022-09-07 thomas len += 2;
2172 55144267 2022-09-07 thomas }
2173 55144267 2022-09-07 thomas
2174 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
2175 55144267 2022-09-07 thomas if (escaped == NULL)
2176 55144267 2022-09-07 thomas return NULL;
2177 55144267 2022-09-07 thomas
2178 55144267 2022-09-07 thomas i = 0;
2179 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
2180 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
2181 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
2182 55144267 2022-09-07 thomas b = (*s & 0x0F);
2183 55144267 2022-09-07 thomas
2184 55144267 2022-09-07 thomas escaped[i++] = '%';
2185 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2186 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2187 55144267 2022-09-07 thomas } else
2188 55144267 2022-09-07 thomas escaped[i++] = *s;
2189 55144267 2022-09-07 thomas }
2190 55144267 2022-09-07 thomas
2191 55144267 2022-09-07 thomas return escaped;
2192 55144267 2022-09-07 thomas }
2193 55144267 2022-09-07 thomas
2194 55144267 2022-09-07 thomas static inline const char *
2195 55144267 2022-09-07 thomas action_name(int action)
2196 55144267 2022-09-07 thomas {
2197 55144267 2022-09-07 thomas switch (action) {
2198 55144267 2022-09-07 thomas case BLAME:
2199 55144267 2022-09-07 thomas return "blame";
2200 55144267 2022-09-07 thomas case BLOB:
2201 55144267 2022-09-07 thomas return "blob";
2202 55144267 2022-09-07 thomas case BRIEFS:
2203 55144267 2022-09-07 thomas return "briefs";
2204 55144267 2022-09-07 thomas case COMMITS:
2205 55144267 2022-09-07 thomas return "commits";
2206 55144267 2022-09-07 thomas case DIFF:
2207 55144267 2022-09-07 thomas return "diff";
2208 55144267 2022-09-07 thomas case ERR:
2209 55144267 2022-09-07 thomas return "err";
2210 55144267 2022-09-07 thomas case INDEX:
2211 55144267 2022-09-07 thomas return "index";
2212 55144267 2022-09-07 thomas case SUMMARY:
2213 55144267 2022-09-07 thomas return "summary";
2214 55144267 2022-09-07 thomas case TAG:
2215 55144267 2022-09-07 thomas return "tag";
2216 55144267 2022-09-07 thomas case TAGS:
2217 55144267 2022-09-07 thomas return "tags";
2218 55144267 2022-09-07 thomas case TREE:
2219 55144267 2022-09-07 thomas return "tree";
2220 55144267 2022-09-07 thomas default:
2221 55144267 2022-09-07 thomas return NULL;
2222 55144267 2022-09-07 thomas }
2223 55144267 2022-09-07 thomas }
2224 55144267 2022-09-07 thomas
2225 55144267 2022-09-07 thomas static int
2226 55144267 2022-09-07 thomas gotweb_print_url(struct request *c, struct gotweb_url *url)
2227 55144267 2022-09-07 thomas {
2228 55144267 2022-09-07 thomas const char *sep = "?", *action;
2229 55144267 2022-09-07 thomas char *tmp;
2230 55144267 2022-09-07 thomas int r;
2231 55144267 2022-09-07 thomas
2232 55144267 2022-09-07 thomas action = action_name(url->action);
2233 55144267 2022-09-07 thomas if (action != NULL) {
2234 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
2235 55144267 2022-09-07 thomas return -1;
2236 55144267 2022-09-07 thomas sep = "&";
2237 55144267 2022-09-07 thomas }
2238 55144267 2022-09-07 thomas
2239 55144267 2022-09-07 thomas if (url->commit) {
2240 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2241 55144267 2022-09-07 thomas return -1;
2242 55144267 2022-09-07 thomas sep = "&";
2243 55144267 2022-09-07 thomas }
2244 55144267 2022-09-07 thomas
2245 55144267 2022-09-07 thomas if (url->previd) {
2246 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2247 55144267 2022-09-07 thomas return -1;
2248 55144267 2022-09-07 thomas sep = "&";
2249 55144267 2022-09-07 thomas }
2250 55144267 2022-09-07 thomas
2251 55144267 2022-09-07 thomas if (url->prevset) {
2252 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2253 55144267 2022-09-07 thomas return -1;
2254 55144267 2022-09-07 thomas sep = "&";
2255 55144267 2022-09-07 thomas }
2256 55144267 2022-09-07 thomas
2257 55144267 2022-09-07 thomas if (url->file) {
2258 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
2259 55144267 2022-09-07 thomas if (tmp == NULL)
2260 55144267 2022-09-07 thomas return -1;
2261 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2262 55144267 2022-09-07 thomas free(tmp);
2263 55144267 2022-09-07 thomas if (r == -1)
2264 55144267 2022-09-07 thomas return -1;
2265 55144267 2022-09-07 thomas sep = "&";
2266 55144267 2022-09-07 thomas }
2267 55144267 2022-09-07 thomas
2268 55144267 2022-09-07 thomas if (url->folder) {
2269 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
2270 55144267 2022-09-07 thomas if (tmp == NULL)
2271 55144267 2022-09-07 thomas return -1;
2272 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2273 55144267 2022-09-07 thomas free(tmp);
2274 55144267 2022-09-07 thomas if (r == -1)
2275 55144267 2022-09-07 thomas return -1;
2276 55144267 2022-09-07 thomas sep = "&";
2277 55144267 2022-09-07 thomas }
2278 55144267 2022-09-07 thomas
2279 55144267 2022-09-07 thomas if (url->headref) {
2280 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
2281 55144267 2022-09-07 thomas if (tmp == NULL)
2282 55144267 2022-09-07 thomas return -1;
2283 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2284 55144267 2022-09-07 thomas free(tmp);
2285 55144267 2022-09-07 thomas if (r == -1)
2286 55144267 2022-09-07 thomas return -1;
2287 55144267 2022-09-07 thomas sep = "&";
2288 55144267 2022-09-07 thomas }
2289 55144267 2022-09-07 thomas
2290 55144267 2022-09-07 thomas if (url->index_page != -1) {
2291 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
2292 55144267 2022-09-07 thomas url->index_page) == -1)
2293 55144267 2022-09-07 thomas return -1;
2294 55144267 2022-09-07 thomas sep = "&";
2295 55144267 2022-09-07 thomas }
2296 55144267 2022-09-07 thomas
2297 55144267 2022-09-07 thomas if (url->path) {
2298 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
2299 55144267 2022-09-07 thomas if (tmp == NULL)
2300 55144267 2022-09-07 thomas return -1;
2301 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
2302 55144267 2022-09-07 thomas free(tmp);
2303 55144267 2022-09-07 thomas if (r == -1)
2304 55144267 2022-09-07 thomas return -1;
2305 55144267 2022-09-07 thomas sep = "&";
2306 55144267 2022-09-07 thomas }
2307 55144267 2022-09-07 thomas
2308 55144267 2022-09-07 thomas if (url->page != -1) {
2309 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2310 55144267 2022-09-07 thomas return -1;
2311 55144267 2022-09-07 thomas sep = "&";
2312 55144267 2022-09-07 thomas }
2313 55144267 2022-09-07 thomas
2314 55144267 2022-09-07 thomas return 0;
2315 55144267 2022-09-07 thomas }
2316 55144267 2022-09-07 thomas
2317 55144267 2022-09-07 thomas int
2318 55144267 2022-09-07 thomas gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2319 55144267 2022-09-07 thomas {
2320 55144267 2022-09-07 thomas va_list ap;
2321 55144267 2022-09-07 thomas int r;
2322 55144267 2022-09-07 thomas
2323 55144267 2022-09-07 thomas if (fcgi_printf(c, "<a href='") == -1)
2324 55144267 2022-09-07 thomas return -1;
2325 55144267 2022-09-07 thomas
2326 55144267 2022-09-07 thomas if (gotweb_print_url(c, url) == -1)
2327 55144267 2022-09-07 thomas return -1;
2328 55144267 2022-09-07 thomas
2329 55144267 2022-09-07 thomas if (fcgi_printf(c, "'>") == -1)
2330 55144267 2022-09-07 thomas return -1;
2331 55144267 2022-09-07 thomas
2332 55144267 2022-09-07 thomas va_start(ap, fmt);
2333 55144267 2022-09-07 thomas r = fcgi_vprintf(c, fmt, ap);
2334 55144267 2022-09-07 thomas va_end(ap);
2335 55144267 2022-09-07 thomas if (r == -1)
2336 55144267 2022-09-07 thomas return -1;
2337 55144267 2022-09-07 thomas
2338 55144267 2022-09-07 thomas if (fcgi_printf(c, "</a>"))
2339 55144267 2022-09-07 thomas return -1;
2340 55144267 2022-09-07 thomas return 0;
2341 55144267 2022-09-07 thomas }
2342 55144267 2022-09-07 thomas
2343 55e6cffd 2022-09-01 thomas static struct got_repository *
2344 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
2345 55e6cffd 2022-09-01 thomas {
2346 55e6cffd 2022-09-01 thomas int i;
2347 55e6cffd 2022-09-01 thomas
2348 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
2349 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
2350 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
2351 55e6cffd 2022-09-01 thomas }
2352 55e6cffd 2022-09-01 thomas
2353 55e6cffd 2022-09-01 thomas return NULL;
2354 55e6cffd 2022-09-01 thomas }
2355 55e6cffd 2022-09-01 thomas
2356 8a35f56c 2022-07-16 thomas static const struct got_error *
2357 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
2358 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
2359 55e6cffd 2022-09-01 thomas {
2360 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
2361 55e6cffd 2022-09-01 thomas struct got_repository *repo;
2362 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
2363 55e6cffd 2022-09-01 thomas int evicted = 0;
2364 55e6cffd 2022-09-01 thomas
2365 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2366 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
2367 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
2368 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
2369 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
2370 55e6cffd 2022-09-01 thomas if (error)
2371 55e6cffd 2022-09-01 thomas return error;
2372 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2373 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
2374 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
2375 55e6cffd 2022-09-01 thomas evicted = 1;
2376 55e6cffd 2022-09-01 thomas } else {
2377 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
2378 55e6cffd 2022-09-01 thomas }
2379 55e6cffd 2022-09-01 thomas
2380 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2381 55e6cffd 2022-09-01 thomas if (error) {
2382 55e6cffd 2022-09-01 thomas if (evicted) {
2383 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2384 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
2385 55e6cffd 2022-09-01 thomas }
2386 55e6cffd 2022-09-01 thomas return error;
2387 55e6cffd 2022-09-01 thomas }
2388 55e6cffd 2022-09-01 thomas
2389 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2390 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
2391 55e6cffd 2022-09-01 thomas if (evicted) {
2392 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2393 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
2394 55e6cffd 2022-09-01 thomas }
2395 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
2396 55e6cffd 2022-09-01 thomas }
2397 55e6cffd 2022-09-01 thomas
2398 55e6cffd 2022-09-01 thomas cr->repo = repo;
2399 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
2400 55e6cffd 2022-09-01 thomas *new = repo;
2401 55e6cffd 2022-09-01 thomas return NULL;
2402 55e6cffd 2022-09-01 thomas }
2403 55e6cffd 2022-09-01 thomas
2404 55e6cffd 2022-09-01 thomas static const struct got_error *
2405 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2406 8a35f56c 2022-07-16 thomas {
2407 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2408 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
2409 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
2410 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
2411 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
2412 8a35f56c 2022-07-16 thomas DIR *dt;
2413 8a35f56c 2022-07-16 thomas char *dir_test;
2414 8a35f56c 2022-07-16 thomas
2415 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2416 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
2417 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2418 8a35f56c 2022-07-16 thomas
2419 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
2420 8a35f56c 2022-07-16 thomas if (dt == NULL) {
2421 8a35f56c 2022-07-16 thomas free(dir_test);
2422 8a35f56c 2022-07-16 thomas } else {
2423 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
2424 8a35f56c 2022-07-16 thomas dir_test = NULL;
2425 c2d3d9a0 2022-09-01 thomas goto done;
2426 8a35f56c 2022-07-16 thomas }
2427 8a35f56c 2022-07-16 thomas
2428 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2429 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
2430 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
2431 8a35f56c 2022-07-16 thomas
2432 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
2433 8a35f56c 2022-07-16 thomas if (dt == NULL) {
2434 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2435 8a35f56c 2022-07-16 thomas goto err;
2436 c2d3d9a0 2022-09-01 thomas } else {
2437 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
2438 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
2439 c2d3d9a0 2022-09-01 thomas }
2440 c2d3d9a0 2022-09-01 thomas
2441 8a35f56c 2022-07-16 thomas done:
2442 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
2443 55e6cffd 2022-09-01 thomas if (repo == NULL) {
2444 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
2445 55e6cffd 2022-09-01 thomas if (error)
2446 55e6cffd 2022-09-01 thomas goto err;
2447 55e6cffd 2022-09-01 thomas }
2448 55e6cffd 2022-09-01 thomas t->repo = repo;
2449 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
2450 8a35f56c 2022-07-16 thomas repo_dir->path);
2451 8a35f56c 2022-07-16 thomas if (error)
2452 8a35f56c 2022-07-16 thomas goto err;
2453 8a35f56c 2022-07-16 thomas error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2454 8a35f56c 2022-07-16 thomas if (error)
2455 8a35f56c 2022-07-16 thomas goto err;
2456 8a35f56c 2022-07-16 thomas error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2457 8a35f56c 2022-07-16 thomas NULL, TM_DIFF);
2458 8a35f56c 2022-07-16 thomas if (error)
2459 8a35f56c 2022-07-16 thomas goto err;
2460 8a35f56c 2022-07-16 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2461 8a35f56c 2022-07-16 thomas err:
2462 8a35f56c 2022-07-16 thomas free(dir_test);
2463 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
2464 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
2465 8a35f56c 2022-07-16 thomas return error;
2466 8a35f56c 2022-07-16 thomas }
2467 8a35f56c 2022-07-16 thomas
2468 8a35f56c 2022-07-16 thomas static const struct got_error *
2469 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2470 8a35f56c 2022-07-16 thomas {
2471 8a35f56c 2022-07-16 thomas const struct got_error *error;
2472 8a35f56c 2022-07-16 thomas
2473 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
2474 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
2475 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
2476 8a35f56c 2022-07-16 thomas
2477 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2478 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
2479 8a35f56c 2022-07-16 thomas free(*repo_dir);
2480 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
2481 8a35f56c 2022-07-16 thomas return error;
2482 8a35f56c 2022-07-16 thomas }
2483 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
2484 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
2485 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
2486 8a35f56c 2022-07-16 thomas (*repo_dir)->age = NULL;
2487 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
2488 8a35f56c 2022-07-16 thomas
2489 8a35f56c 2022-07-16 thomas return NULL;
2490 8a35f56c 2022-07-16 thomas }
2491 8a35f56c 2022-07-16 thomas
2492 8a35f56c 2022-07-16 thomas static const struct got_error *
2493 8a35f56c 2022-07-16 thomas gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2494 8a35f56c 2022-07-16 thomas {
2495 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2496 8a35f56c 2022-07-16 thomas FILE *f = NULL;
2497 8a35f56c 2022-07-16 thomas char *d_file = NULL;
2498 8a35f56c 2022-07-16 thomas unsigned int len;
2499 8a35f56c 2022-07-16 thomas size_t n;
2500 8a35f56c 2022-07-16 thomas
2501 8a35f56c 2022-07-16 thomas *description = NULL;
2502 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
2503 8a35f56c 2022-07-16 thomas return NULL;
2504 8a35f56c 2022-07-16 thomas
2505 8a35f56c 2022-07-16 thomas if (asprintf(&d_file, "%s/description", dir) == -1)
2506 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2507 8a35f56c 2022-07-16 thomas
2508 8a35f56c 2022-07-16 thomas f = fopen(d_file, "r");
2509 8a35f56c 2022-07-16 thomas if (f == NULL) {
2510 31797419 2022-09-02 thomas if (errno != ENOENT && errno != EACCES)
2511 31797419 2022-09-02 thomas error = got_error_from_errno2("fopen", d_file);
2512 8a35f56c 2022-07-16 thomas goto done;
2513 8a35f56c 2022-07-16 thomas }
2514 8a35f56c 2022-07-16 thomas
2515 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_END) == -1) {
2516 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2517 8a35f56c 2022-07-16 thomas goto done;
2518 8a35f56c 2022-07-16 thomas }
2519 8a35f56c 2022-07-16 thomas len = ftell(f);
2520 8a35f56c 2022-07-16 thomas if (len == -1) {
2521 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2522 8a35f56c 2022-07-16 thomas goto done;
2523 8a35f56c 2022-07-16 thomas }
2524 8a35f56c 2022-07-16 thomas
2525 2f26d334 2022-08-30 thomas if (len == 0) {
2526 2f26d334 2022-08-30 thomas *description = strdup("");
2527 2f26d334 2022-08-30 thomas if (*description == NULL)
2528 31797419 2022-09-02 thomas error = got_error_from_errno("strdup");
2529 8a35f56c 2022-07-16 thomas goto done;
2530 2f26d334 2022-08-30 thomas }
2531 8a35f56c 2022-07-16 thomas
2532 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_SET) == -1) {
2533 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2534 8a35f56c 2022-07-16 thomas goto done;
2535 8a35f56c 2022-07-16 thomas }
2536 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
2537 8a35f56c 2022-07-16 thomas if (*description == NULL) {
2538 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
2539 8a35f56c 2022-07-16 thomas goto done;
2540 8a35f56c 2022-07-16 thomas }
2541 8a35f56c 2022-07-16 thomas
2542 8a35f56c 2022-07-16 thomas n = fread(*description, 1, len, f);
2543 8a35f56c 2022-07-16 thomas if (n == 0 && ferror(f))
2544 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2545 8a35f56c 2022-07-16 thomas done:
2546 8a35f56c 2022-07-16 thomas if (f != NULL && fclose(f) == EOF && error == NULL)
2547 8a35f56c 2022-07-16 thomas error = got_error_from_errno("fclose");
2548 8a35f56c 2022-07-16 thomas free(d_file);
2549 8a35f56c 2022-07-16 thomas return error;
2550 8a35f56c 2022-07-16 thomas }
2551 8a35f56c 2022-07-16 thomas
2552 8a35f56c 2022-07-16 thomas static const struct got_error *
2553 8a35f56c 2022-07-16 thomas gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2554 8a35f56c 2022-07-16 thomas {
2555 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
2556 8a35f56c 2022-07-16 thomas FILE *f;
2557 8a35f56c 2022-07-16 thomas char *d_file = NULL;
2558 8a35f56c 2022-07-16 thomas unsigned int len;
2559 8a35f56c 2022-07-16 thomas size_t n;
2560 8a35f56c 2022-07-16 thomas
2561 8a35f56c 2022-07-16 thomas *url = NULL;
2562 8a35f56c 2022-07-16 thomas
2563 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
2564 8a35f56c 2022-07-16 thomas return NULL;
2565 8a35f56c 2022-07-16 thomas
2566 8a35f56c 2022-07-16 thomas if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2567 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2568 8a35f56c 2022-07-16 thomas
2569 8a35f56c 2022-07-16 thomas f = fopen(d_file, "r");
2570 8a35f56c 2022-07-16 thomas if (f == NULL) {
2571 8a35f56c 2022-07-16 thomas if (errno != ENOENT && errno != EACCES)
2572 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("fopen", d_file);
2573 8a35f56c 2022-07-16 thomas goto done;
2574 8a35f56c 2022-07-16 thomas }
2575 8a35f56c 2022-07-16 thomas
2576 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_END) == -1) {
2577 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2578 8a35f56c 2022-07-16 thomas goto done;
2579 8a35f56c 2022-07-16 thomas }
2580 8a35f56c 2022-07-16 thomas len = ftell(f);
2581 8a35f56c 2022-07-16 thomas if (len == -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 if (len == 0)
2586 8a35f56c 2022-07-16 thomas goto done;
2587 8a35f56c 2022-07-16 thomas
2588 8a35f56c 2022-07-16 thomas if (fseek(f, 0, SEEK_SET) == -1) {
2589 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2590 8a35f56c 2022-07-16 thomas goto done;
2591 8a35f56c 2022-07-16 thomas }
2592 8a35f56c 2022-07-16 thomas
2593 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
2594 8a35f56c 2022-07-16 thomas if (*url == NULL) {
2595 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
2596 8a35f56c 2022-07-16 thomas goto done;
2597 8a35f56c 2022-07-16 thomas }
2598 8a35f56c 2022-07-16 thomas
2599 8a35f56c 2022-07-16 thomas n = fread(*url, 1, len, f);
2600 8a35f56c 2022-07-16 thomas if (n == 0 && ferror(f))
2601 8a35f56c 2022-07-16 thomas error = got_ferror(f, GOT_ERR_IO);
2602 8a35f56c 2022-07-16 thomas done:
2603 8a35f56c 2022-07-16 thomas if (f != NULL && fclose(f) == EOF && error == NULL)
2604 8a35f56c 2022-07-16 thomas error = got_error_from_errno("fclose");
2605 8a35f56c 2022-07-16 thomas free(d_file);
2606 8a35f56c 2022-07-16 thomas return error;
2607 8a35f56c 2022-07-16 thomas }
2608 8a35f56c 2022-07-16 thomas
2609 8a35f56c 2022-07-16 thomas const struct got_error *
2610 8a35f56c 2022-07-16 thomas gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2611 8a35f56c 2022-07-16 thomas {
2612 8a35f56c 2022-07-16 thomas struct tm tm;
2613 399ea8e4 2022-07-20 thomas long long diff_time;
2614 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
2615 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
2616 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
2617 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
2618 8a35f56c 2022-07-16 thomas char *s;
2619 8a35f56c 2022-07-16 thomas char datebuf[29];
2620 8a35f56c 2022-07-16 thomas
2621 8a35f56c 2022-07-16 thomas *repo_age = NULL;
2622 8a35f56c 2022-07-16 thomas
2623 8a35f56c 2022-07-16 thomas switch (ref_tm) {
2624 8a35f56c 2022-07-16 thomas case TM_DIFF:
2625 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
2626 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
2627 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2628 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
2629 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2630 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2631 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2632 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
2633 8a35f56c 2022-07-16 thomas months) == -1)
2634 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2635 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2636 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2637 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2638 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2639 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
2640 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2641 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
2642 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2643 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
2644 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2645 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
2646 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2647 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
2648 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2649 8a35f56c 2022-07-16 thomas minutes) == -1)
2650 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2651 8a35f56c 2022-07-16 thomas } else if (diff_time > 2) {
2652 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", diff_time,
2653 8a35f56c 2022-07-16 thomas seconds) == -1)
2654 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2655 8a35f56c 2022-07-16 thomas } else {
2656 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s", now) == -1)
2657 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2658 8a35f56c 2022-07-16 thomas }
2659 8a35f56c 2022-07-16 thomas break;
2660 8a35f56c 2022-07-16 thomas case TM_LONG:
2661 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2662 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
2663 8a35f56c 2022-07-16 thomas
2664 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
2665 8a35f56c 2022-07-16 thomas if (s == NULL)
2666 8a35f56c 2022-07-16 thomas return got_error_from_errno("asctime_r");
2667 8a35f56c 2022-07-16 thomas
2668 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2669 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2670 8a35f56c 2022-07-16 thomas break;
2671 8a35f56c 2022-07-16 thomas }
2672 8a35f56c 2022-07-16 thomas return NULL;
2673 3e12c168 2022-07-16 thomas }