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