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