Blame


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