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