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