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