Blame


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