Blame


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