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