Blame


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