Blame


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