Blame


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