Blame


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