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 5822e79e 2023-02-23 op #include <sha2.h>
35 a596b957 2022-07-14 tracey #include <stdio.h>
36 a596b957 2022-07-14 tracey #include <stdlib.h>
37 a596b957 2022-07-14 tracey #include <string.h>
38 a596b957 2022-07-14 tracey #include <unistd.h>
39 a596b957 2022-07-14 tracey
40 a596b957 2022-07-14 tracey #include "got_error.h"
41 a596b957 2022-07-14 tracey #include "got_object.h"
42 a596b957 2022-07-14 tracey #include "got_reference.h"
43 a596b957 2022-07-14 tracey #include "got_repository.h"
44 a596b957 2022-07-14 tracey #include "got_path.h"
45 a596b957 2022-07-14 tracey #include "got_cancel.h"
46 a596b957 2022-07-14 tracey #include "got_worktree.h"
47 a596b957 2022-07-14 tracey #include "got_diff.h"
48 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
49 a596b957 2022-07-14 tracey #include "got_blame.h"
50 a596b957 2022-07-14 tracey #include "got_privsep.h"
51 a596b957 2022-07-14 tracey
52 a596b957 2022-07-14 tracey #include "proc.h"
53 a596b957 2022-07-14 tracey #include "gotwebd.h"
54 1abb18e1 2022-12-20 op #include "tmpl.h"
55 a596b957 2022-07-14 tracey
56 a596b957 2022-07-14 tracey static const struct querystring_keys querystring_keys[] = {
57 a596b957 2022-07-14 tracey { "action", ACTION },
58 a596b957 2022-07-14 tracey { "commit", COMMIT },
59 a596b957 2022-07-14 tracey { "file", RFILE },
60 a596b957 2022-07-14 tracey { "folder", FOLDER },
61 a596b957 2022-07-14 tracey { "headref", HEADREF },
62 a596b957 2022-07-14 tracey { "index_page", INDEX_PAGE },
63 a596b957 2022-07-14 tracey { "path", PATH },
64 a596b957 2022-07-14 tracey { "page", PAGE },
65 a596b957 2022-07-14 tracey };
66 a596b957 2022-07-14 tracey
67 a596b957 2022-07-14 tracey static const struct action_keys action_keys[] = {
68 a596b957 2022-07-14 tracey { "blame", BLAME },
69 a596b957 2022-07-14 tracey { "blob", BLOB },
70 298f95fb 2023-01-05 op { "blobraw", BLOBRAW },
71 a596b957 2022-07-14 tracey { "briefs", BRIEFS },
72 a596b957 2022-07-14 tracey { "commits", COMMITS },
73 a596b957 2022-07-14 tracey { "diff", DIFF },
74 a596b957 2022-07-14 tracey { "error", ERR },
75 a596b957 2022-07-14 tracey { "index", INDEX },
76 a596b957 2022-07-14 tracey { "summary", SUMMARY },
77 a596b957 2022-07-14 tracey { "tag", TAG },
78 a596b957 2022-07-14 tracey { "tags", TAGS },
79 a596b957 2022-07-14 tracey { "tree", TREE },
80 1abb18e1 2022-12-20 op { "rss", RSS },
81 a596b957 2022-07-14 tracey };
82 a596b957 2022-07-14 tracey
83 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_querystring(struct querystring **);
84 a596b957 2022-07-14 tracey static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 a596b957 2022-07-14 tracey char *);
86 a596b957 2022-07-14 tracey static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 a596b957 2022-07-14 tracey char *, char *);
88 df2d3cd2 2023-03-11 op static int gotweb_render_index(struct template *);
89 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 a596b957 2022-07-14 tracey const char *);
91 a596b957 2022-07-14 tracey static const struct got_error *gotweb_load_got_path(struct request *c,
92 a596b957 2022-07-14 tracey struct repo_dir *);
93 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_repo_description(char **,
94 3b81530f 2022-11-22 op struct server *, const char *, int);
95 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 3b81530f 2022-11-22 op const char *, int);
97 ed619ca0 2022-12-14 op
98 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
99 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
100 a596b957 2022-07-14 tracey
101 95a4a5a1 2022-08-30 op struct server *gotweb_get_server(uint8_t *, uint8_t *);
102 a596b957 2022-07-14 tracey
103 6cdf29f9 2023-01-21 op static int
104 6cdf29f9 2023-01-21 op gotweb_reply(struct request *c, int status, const char *ctype,
105 6cdf29f9 2023-01-21 op struct gotweb_url *location)
106 6cdf29f9 2023-01-21 op {
107 6cdf29f9 2023-01-21 op const char *csp;
108 6cdf29f9 2023-01-21 op
109 6cdf29f9 2023-01-21 op if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
110 6cdf29f9 2023-01-21 op return -1;
111 6cdf29f9 2023-01-21 op
112 6cdf29f9 2023-01-21 op if (location) {
113 6cdf29f9 2023-01-21 op if (fcgi_puts(c->tp, "Location: ") == -1 ||
114 6cdf29f9 2023-01-21 op gotweb_render_url(c, location) == -1 ||
115 6cdf29f9 2023-01-21 op fcgi_puts(c->tp, "\r\n") == -1)
116 6cdf29f9 2023-01-21 op return -1;
117 6cdf29f9 2023-01-21 op }
118 6cdf29f9 2023-01-21 op
119 6cdf29f9 2023-01-21 op csp = "Content-Security-Policy: default-src 'self'; "
120 6cdf29f9 2023-01-21 op "script-src 'none'; object-src 'none';\r\n";
121 6cdf29f9 2023-01-21 op if (fcgi_puts(c->tp, csp) == -1)
122 6cdf29f9 2023-01-21 op return -1;
123 6cdf29f9 2023-01-21 op
124 6cdf29f9 2023-01-21 op if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
125 6cdf29f9 2023-01-21 op return -1;
126 6cdf29f9 2023-01-21 op
127 6cdf29f9 2023-01-21 op return fcgi_puts(c->tp, "\r\n");
128 6cdf29f9 2023-01-21 op }
129 6cdf29f9 2023-01-21 op
130 6cdf29f9 2023-01-21 op static int
131 6cdf29f9 2023-01-21 op gotweb_reply_file(struct request *c, const char *ctype, const char *file,
132 6cdf29f9 2023-01-21 op const char *suffix)
133 6cdf29f9 2023-01-21 op {
134 6cdf29f9 2023-01-21 op int r;
135 6cdf29f9 2023-01-21 op
136 6cdf29f9 2023-01-21 op r = fcgi_printf(c, "Content-Disposition: attachment; "
137 6cdf29f9 2023-01-21 op "filename=%s%s\r\n", file, suffix ? suffix : "");
138 6cdf29f9 2023-01-21 op if (r == -1)
139 6cdf29f9 2023-01-21 op return -1;
140 6cdf29f9 2023-01-21 op return gotweb_reply(c, 200, ctype, NULL);
141 6cdf29f9 2023-01-21 op }
142 6cdf29f9 2023-01-21 op
143 a596b957 2022-07-14 tracey void
144 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
145 a596b957 2022-07-14 tracey {
146 a596b957 2022-07-14 tracey const struct got_error *error = NULL, *error2 = NULL;
147 a596b957 2022-07-14 tracey struct server *srv = NULL;
148 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
149 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
150 a596b957 2022-07-14 tracey uint8_t err[] = "gotwebd experienced an error: ";
151 df2d3cd2 2023-03-11 op int r, html = 0;
152 69525b4e 2023-01-13 op
153 a596b957 2022-07-14 tracey /* init the transport */
154 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
155 a596b957 2022-07-14 tracey if (error) {
156 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
157 f0680473 2022-08-25 op return;
158 a596b957 2022-07-14 tracey }
159 a596b957 2022-07-14 tracey /* don't process any further if client disconnected */
160 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
161 a596b957 2022-07-14 tracey return;
162 a596b957 2022-07-14 tracey /* get the gotwebd server */
163 95a4a5a1 2022-08-30 op srv = gotweb_get_server(c->server_name, c->http_host);
164 a596b957 2022-07-14 tracey if (srv == NULL) {
165 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
166 a596b957 2022-07-14 tracey goto err;
167 a596b957 2022-07-14 tracey }
168 a596b957 2022-07-14 tracey c->srv = srv;
169 a596b957 2022-07-14 tracey /* parse our querystring */
170 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
171 a596b957 2022-07-14 tracey if (error) {
172 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
173 a596b957 2022-07-14 tracey goto err;
174 a596b957 2022-07-14 tracey }
175 a596b957 2022-07-14 tracey c->t->qs = qs;
176 a596b957 2022-07-14 tracey error = gotweb_parse_querystring(&qs, c->querystring);
177 a596b957 2022-07-14 tracey if (error) {
178 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
179 a596b957 2022-07-14 tracey goto err;
180 a596b957 2022-07-14 tracey }
181 a596b957 2022-07-14 tracey
182 a596b957 2022-07-14 tracey /*
183 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
184 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
185 a596b957 2022-07-14 tracey * querystring.
186 a596b957 2022-07-14 tracey */
187 a596b957 2022-07-14 tracey
188 298f95fb 2023-01-05 op if (qs->action == BLAME || qs->action == BLOB ||
189 298f95fb 2023-01-05 op qs->action == BLOBRAW || qs->action == DIFF) {
190 298f95fb 2023-01-05 op if (qs->commit == NULL) {
191 298f95fb 2023-01-05 op error2 = got_error(GOT_ERR_QUERYSTRING);
192 298f95fb 2023-01-05 op goto render;
193 298f95fb 2023-01-05 op }
194 a596b957 2022-07-14 tracey }
195 a596b957 2022-07-14 tracey
196 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
197 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
198 a596b957 2022-07-14 tracey if (error)
199 a596b957 2022-07-14 tracey goto done;
200 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
201 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
202 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
203 a596b957 2022-07-14 tracey goto err;
204 a596b957 2022-07-14 tracey }
205 a596b957 2022-07-14 tracey
206 298f95fb 2023-01-05 op if (qs->action == BLOBRAW) {
207 76007998 2023-01-15 op const uint8_t *buf;
208 76007998 2023-01-15 op size_t len;
209 6cdf29f9 2023-01-21 op int binary, r;
210 76007998 2023-01-15 op
211 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
212 a596b957 2022-07-14 tracey if (error)
213 a596b957 2022-07-14 tracey goto done;
214 76007998 2023-01-15 op
215 df2d3cd2 2023-03-11 op error2 = got_open_blob_for_output(&c->t->blob, &c->t->fd,
216 df2d3cd2 2023-03-11 op &binary, c);
217 76007998 2023-01-15 op if (error2)
218 76007998 2023-01-15 op goto render;
219 76007998 2023-01-15 op
220 76007998 2023-01-15 op if (binary)
221 6cdf29f9 2023-01-21 op r = gotweb_reply_file(c, "application/octet-stream",
222 6cdf29f9 2023-01-21 op qs->file, NULL);
223 76007998 2023-01-15 op else
224 6cdf29f9 2023-01-21 op r = gotweb_reply(c, 200, "text/plain", NULL);
225 6cdf29f9 2023-01-21 op if (r == -1)
226 76007998 2023-01-15 op goto done;
227 76007998 2023-01-15 op
228 76007998 2023-01-15 op for (;;) {
229 df2d3cd2 2023-03-11 op error = got_object_blob_read_block(&len, c->t->blob);
230 76007998 2023-01-15 op if (error)
231 76007998 2023-01-15 op goto done;
232 76007998 2023-01-15 op if (len == 0)
233 76007998 2023-01-15 op break;
234 df2d3cd2 2023-03-11 op buf = got_object_blob_get_read_buf(c->t->blob);
235 76007998 2023-01-15 op if (fcgi_gen_binary_response(c, buf, len) == -1)
236 76007998 2023-01-15 op goto done;
237 1abb18e1 2022-12-20 op }
238 76007998 2023-01-15 op
239 1abb18e1 2022-12-20 op goto done;
240 298f95fb 2023-01-05 op }
241 298f95fb 2023-01-05 op
242 298f95fb 2023-01-05 op if (qs->action == BLOB) {
243 298f95fb 2023-01-05 op int binary;
244 298f95fb 2023-01-05 op struct gotweb_url url = {
245 298f95fb 2023-01-05 op .index_page = -1,
246 298f95fb 2023-01-05 op .page = -1,
247 298f95fb 2023-01-05 op .action = BLOBRAW,
248 298f95fb 2023-01-05 op .path = qs->path,
249 298f95fb 2023-01-05 op .commit = qs->commit,
250 298f95fb 2023-01-05 op .folder = qs->folder,
251 298f95fb 2023-01-05 op .file = qs->file,
252 298f95fb 2023-01-05 op };
253 298f95fb 2023-01-05 op
254 298f95fb 2023-01-05 op error = got_get_repo_commits(c, 1);
255 298f95fb 2023-01-05 op if (error)
256 298f95fb 2023-01-05 op goto done;
257 298f95fb 2023-01-05 op
258 df2d3cd2 2023-03-11 op error2 = got_open_blob_for_output(&c->t->blob, &c->t->fd,
259 df2d3cd2 2023-03-11 op &binary, c);
260 298f95fb 2023-01-05 op if (error2)
261 298f95fb 2023-01-05 op goto render;
262 298f95fb 2023-01-05 op if (binary) {
263 6cdf29f9 2023-01-21 op gotweb_reply(c, 302, NULL, &url);
264 298f95fb 2023-01-05 op goto done;
265 298f95fb 2023-01-05 op }
266 1abb18e1 2022-12-20 op }
267 1abb18e1 2022-12-20 op
268 1abb18e1 2022-12-20 op if (qs->action == RSS) {
269 6cdf29f9 2023-01-21 op const char *ctype = "application/rss+xml;charset=utf-8";
270 1abb18e1 2022-12-20 op
271 6cdf29f9 2023-01-21 op if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
272 6cdf29f9 2023-01-21 op goto done;
273 6cdf29f9 2023-01-21 op
274 1abb18e1 2022-12-20 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
275 a596b957 2022-07-14 tracey if (error) {
276 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
277 a596b957 2022-07-14 tracey goto err;
278 a596b957 2022-07-14 tracey }
279 1abb18e1 2022-12-20 op if (gotweb_render_rss(c->tp) == -1)
280 1abb18e1 2022-12-20 op goto err;
281 a596b957 2022-07-14 tracey goto done;
282 6970304f 2022-12-04 op }
283 6970304f 2022-12-04 op
284 6970304f 2022-12-04 op render:
285 6cdf29f9 2023-01-21 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
286 6cdf29f9 2023-01-21 op goto done;
287 6970304f 2022-12-04 op html = 1;
288 a596b957 2022-07-14 tracey
289 a596b957 2022-07-14 tracey if (error2) {
290 a596b957 2022-07-14 tracey error = error2;
291 a596b957 2022-07-14 tracey goto err;
292 a596b957 2022-07-14 tracey }
293 a596b957 2022-07-14 tracey
294 a596b957 2022-07-14 tracey switch(qs->action) {
295 a596b957 2022-07-14 tracey case BLAME:
296 8319855f 2023-01-15 op error = got_get_repo_commits(c, 1);
297 a596b957 2022-07-14 tracey if (error) {
298 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
299 a596b957 2022-07-14 tracey goto err;
300 a596b957 2022-07-14 tracey }
301 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_blame) == -1)
302 8319855f 2023-01-15 op goto done;
303 298f95fb 2023-01-05 op break;
304 298f95fb 2023-01-05 op case BLOB:
305 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_blob) == -1)
306 298f95fb 2023-01-05 op goto err;
307 a596b957 2022-07-14 tracey break;
308 a596b957 2022-07-14 tracey case BRIEFS:
309 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_briefs) == -1)
310 a596b957 2022-07-14 tracey goto err;
311 a596b957 2022-07-14 tracey break;
312 a596b957 2022-07-14 tracey case COMMITS:
313 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
314 a596b957 2022-07-14 tracey if (error) {
315 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
316 a596b957 2022-07-14 tracey goto err;
317 a596b957 2022-07-14 tracey }
318 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_commits) == -1)
319 156a1144 2022-12-17 op goto err;
320 a596b957 2022-07-14 tracey break;
321 a596b957 2022-07-14 tracey case DIFF:
322 587550a5 2023-01-10 op error = got_get_repo_commits(c, 1);
323 169b1631 2023-01-06 op if (error) {
324 169b1631 2023-01-06 op log_warnx("%s: %s", __func__, error->msg);
325 169b1631 2023-01-06 op goto err;
326 169b1631 2023-01-06 op }
327 df2d3cd2 2023-03-11 op error = got_open_diff_for_output(&c->t->fp, &c->t->fd, c);
328 587550a5 2023-01-10 op if (error) {
329 587550a5 2023-01-10 op log_warnx("%s: %s", __func__, error->msg);
330 587550a5 2023-01-10 op goto err;
331 587550a5 2023-01-10 op }
332 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_diff) == -1)
333 587550a5 2023-01-10 op goto err;
334 a596b957 2022-07-14 tracey break;
335 a596b957 2022-07-14 tracey case INDEX:
336 df2d3cd2 2023-03-11 op c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
337 df2d3cd2 2023-03-11 op alphasort);
338 df2d3cd2 2023-03-11 op if (c->t->nrepos == -1) {
339 df2d3cd2 2023-03-11 op c->t->repos = NULL;
340 df2d3cd2 2023-03-11 op error = got_error_from_errno2("scandir",
341 df2d3cd2 2023-03-11 op srv->repos_path);
342 a596b957 2022-07-14 tracey goto err;
343 a596b957 2022-07-14 tracey }
344 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_index) == -1)
345 df2d3cd2 2023-03-11 op goto err;
346 a596b957 2022-07-14 tracey break;
347 a596b957 2022-07-14 tracey case SUMMARY:
348 df2d3cd2 2023-03-11 op error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
349 69525b4e 2023-01-13 op got_ref_cmp_by_name, NULL);
350 a596b957 2022-07-14 tracey if (error) {
351 69525b4e 2023-01-13 op log_warnx("%s: got_ref_list: %s", __func__,
352 69525b4e 2023-01-13 op error->msg);
353 a596b957 2022-07-14 tracey goto err;
354 a596b957 2022-07-14 tracey }
355 69525b4e 2023-01-13 op qs->action = TAGS;
356 69525b4e 2023-01-13 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
357 69525b4e 2023-01-13 op if (error) {
358 69525b4e 2023-01-13 op log_warnx("%s: got_get_repo_tags: %s", __func__,
359 69525b4e 2023-01-13 op error->msg);
360 69525b4e 2023-01-13 op goto err;
361 69525b4e 2023-01-13 op }
362 69525b4e 2023-01-13 op qs->action = SUMMARY;
363 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_summary) == -1)
364 69525b4e 2023-01-13 op goto done;
365 a596b957 2022-07-14 tracey break;
366 a596b957 2022-07-14 tracey case TAG:
367 dc07f76c 2023-01-09 op error = got_get_repo_tags(c, 1);
368 a596b957 2022-07-14 tracey if (error) {
369 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
370 dc07f76c 2023-01-09 op goto err;
371 dc07f76c 2023-01-09 op }
372 dc07f76c 2023-01-09 op if (c->t->tag_count == 0) {
373 dc07f76c 2023-01-09 op error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
374 dc07f76c 2023-01-09 op "bad commit id");
375 a596b957 2022-07-14 tracey goto err;
376 a596b957 2022-07-14 tracey }
377 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_tag) == -1)
378 dc07f76c 2023-01-09 op goto done;
379 a596b957 2022-07-14 tracey break;
380 a596b957 2022-07-14 tracey case TAGS:
381 d60961d2 2023-01-13 op error = got_get_repo_tags(c, srv->max_commits_display);
382 a596b957 2022-07-14 tracey if (error) {
383 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
384 a596b957 2022-07-14 tracey goto err;
385 a596b957 2022-07-14 tracey }
386 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_tags) == -1)
387 d60961d2 2023-01-13 op goto done;
388 a596b957 2022-07-14 tracey break;
389 a596b957 2022-07-14 tracey case TREE:
390 43d421de 2023-01-05 op error = got_get_repo_commits(c, 1);
391 a596b957 2022-07-14 tracey if (error) {
392 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
393 a596b957 2022-07-14 tracey goto err;
394 a596b957 2022-07-14 tracey }
395 df2d3cd2 2023-03-11 op if (gotweb_render_page(c->tp, gotweb_render_tree) == -1)
396 43d421de 2023-01-05 op goto err;
397 a596b957 2022-07-14 tracey break;
398 a596b957 2022-07-14 tracey case ERR:
399 a596b957 2022-07-14 tracey default:
400 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
401 01498c42 2022-08-19 op "Erorr: Bad Querystring");
402 01498c42 2022-08-19 op if (r == -1)
403 a596b957 2022-07-14 tracey goto err;
404 a596b957 2022-07-14 tracey break;
405 a596b957 2022-07-14 tracey }
406 a596b957 2022-07-14 tracey
407 a596b957 2022-07-14 tracey goto done;
408 a596b957 2022-07-14 tracey err:
409 01498c42 2022-08-19 op if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
410 a596b957 2022-07-14 tracey return;
411 b2e7d31e 2022-10-31 landry if (fcgi_printf(c, "\n%s", err) == -1)
412 a596b957 2022-07-14 tracey return;
413 a596b957 2022-07-14 tracey if (error) {
414 01498c42 2022-08-19 op if (fcgi_printf(c, "%s", error->msg) == -1)
415 a596b957 2022-07-14 tracey return;
416 a596b957 2022-07-14 tracey } else {
417 01498c42 2022-08-19 op if (fcgi_printf(c, "see daemon logs for details") == -1)
418 a596b957 2022-07-14 tracey return;
419 a596b957 2022-07-14 tracey }
420 01498c42 2022-08-19 op if (html && fcgi_printf(c, "</div>\n") == -1)
421 a596b957 2022-07-14 tracey return;
422 a596b957 2022-07-14 tracey done:
423 df2d3cd2 2023-03-11 op return;
424 a596b957 2022-07-14 tracey }
425 a596b957 2022-07-14 tracey
426 a596b957 2022-07-14 tracey struct server *
427 95a4a5a1 2022-08-30 op gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
428 a596b957 2022-07-14 tracey {
429 a596b957 2022-07-14 tracey struct server *srv = NULL;
430 a596b957 2022-07-14 tracey
431 95a4a5a1 2022-08-30 op /* check against the server name first */
432 a596b957 2022-07-14 tracey if (strlen(server_name) > 0)
433 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
434 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
435 a596b957 2022-07-14 tracey goto done;
436 a596b957 2022-07-14 tracey
437 95a4a5a1 2022-08-30 op /* check against subdomain second */
438 a596b957 2022-07-14 tracey if (strlen(subdomain) > 0)
439 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
440 a596b957 2022-07-14 tracey if (strcmp(srv->name, subdomain) == 0)
441 a596b957 2022-07-14 tracey goto done;
442 a596b957 2022-07-14 tracey
443 a596b957 2022-07-14 tracey /* if those fail, send first server */
444 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
445 a596b957 2022-07-14 tracey if (srv != NULL)
446 a596b957 2022-07-14 tracey break;
447 a596b957 2022-07-14 tracey done:
448 a596b957 2022-07-14 tracey return srv;
449 a596b957 2022-07-14 tracey };
450 a596b957 2022-07-14 tracey
451 a596b957 2022-07-14 tracey const struct got_error *
452 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
453 a596b957 2022-07-14 tracey {
454 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
455 a596b957 2022-07-14 tracey
456 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
457 a596b957 2022-07-14 tracey if (*t == NULL)
458 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
459 a596b957 2022-07-14 tracey
460 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
461 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
462 df2d3cd2 2023-03-11 op TAILQ_INIT(&(*t)->refs);
463 a596b957 2022-07-14 tracey
464 a596b957 2022-07-14 tracey (*t)->repo = NULL;
465 a596b957 2022-07-14 tracey (*t)->repo_dir = NULL;
466 a596b957 2022-07-14 tracey (*t)->qs = NULL;
467 a596b957 2022-07-14 tracey (*t)->next_id = NULL;
468 a596b957 2022-07-14 tracey (*t)->prev_id = NULL;
469 a596b957 2022-07-14 tracey (*t)->next_disp = 0;
470 a596b957 2022-07-14 tracey (*t)->prev_disp = 0;
471 df2d3cd2 2023-03-11 op
472 df2d3cd2 2023-03-11 op (*t)->fd = -1;
473 a596b957 2022-07-14 tracey
474 a596b957 2022-07-14 tracey return error;
475 a596b957 2022-07-14 tracey }
476 a596b957 2022-07-14 tracey
477 a596b957 2022-07-14 tracey static const struct got_error *
478 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
479 a596b957 2022-07-14 tracey {
480 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
481 a596b957 2022-07-14 tracey
482 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
483 a596b957 2022-07-14 tracey if (*qs == NULL)
484 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
485 a596b957 2022-07-14 tracey
486 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
487 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
488 6c37ad7b 2022-09-01 op free(*qs);
489 6c37ad7b 2022-09-01 op *qs = NULL;
490 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
491 a596b957 2022-07-14 tracey }
492 6c37ad7b 2022-09-01 op
493 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
494 6c37ad7b 2022-09-01 op (*qs)->commit = NULL;
495 6c37ad7b 2022-09-01 op (*qs)->file = NULL;
496 6c37ad7b 2022-09-01 op (*qs)->folder = NULL;
497 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
498 a596b957 2022-07-14 tracey (*qs)->path = NULL;
499 a596b957 2022-07-14 tracey
500 a596b957 2022-07-14 tracey return error;
501 a596b957 2022-07-14 tracey }
502 a596b957 2022-07-14 tracey
503 a596b957 2022-07-14 tracey static const struct got_error *
504 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
505 a596b957 2022-07-14 tracey {
506 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
507 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
508 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
509 a596b957 2022-07-14 tracey
510 a596b957 2022-07-14 tracey if (qst == NULL)
511 a596b957 2022-07-14 tracey return error;
512 a596b957 2022-07-14 tracey
513 a596b957 2022-07-14 tracey tok1 = strdup(qst);
514 a596b957 2022-07-14 tracey if (tok1 == NULL)
515 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
516 a596b957 2022-07-14 tracey
517 a596b957 2022-07-14 tracey tok1_pair = tok1;
518 a596b957 2022-07-14 tracey tok1_end = tok1;
519 a596b957 2022-07-14 tracey
520 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
521 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
522 a596b957 2022-07-14 tracey
523 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
524 a596b957 2022-07-14 tracey if (tok2 == NULL) {
525 a596b957 2022-07-14 tracey free(tok1);
526 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
527 a596b957 2022-07-14 tracey }
528 a596b957 2022-07-14 tracey
529 a596b957 2022-07-14 tracey tok2_pair = tok2;
530 a596b957 2022-07-14 tracey tok2_end = tok2;
531 a596b957 2022-07-14 tracey
532 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
533 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
534 a596b957 2022-07-14 tracey if (tok2_end) {
535 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
536 a596b957 2022-07-14 tracey tok2_end);
537 a596b957 2022-07-14 tracey if (error)
538 a596b957 2022-07-14 tracey goto err;
539 a596b957 2022-07-14 tracey }
540 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
541 a596b957 2022-07-14 tracey }
542 a596b957 2022-07-14 tracey free(tok2);
543 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
544 a596b957 2022-07-14 tracey }
545 a596b957 2022-07-14 tracey free(tok1);
546 a596b957 2022-07-14 tracey return error;
547 a596b957 2022-07-14 tracey err:
548 a596b957 2022-07-14 tracey free(tok2);
549 a596b957 2022-07-14 tracey free(tok1);
550 a596b957 2022-07-14 tracey return error;
551 a596b957 2022-07-14 tracey }
552 a596b957 2022-07-14 tracey
553 58381f70 2022-09-03 op /*
554 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
555 58381f70 2022-09-03 op */
556 a596b957 2022-07-14 tracey static const struct got_error *
557 58381f70 2022-09-03 op gotweb_urldecode(char *url)
558 58381f70 2022-09-03 op {
559 58381f70 2022-09-03 op char *p, *q;
560 58381f70 2022-09-03 op char hex[3];
561 58381f70 2022-09-03 op unsigned long x;
562 58381f70 2022-09-03 op
563 58381f70 2022-09-03 op hex[2] = '\0';
564 58381f70 2022-09-03 op p = q = url;
565 58381f70 2022-09-03 op
566 58381f70 2022-09-03 op while (*p != '\0') {
567 58381f70 2022-09-03 op switch (*p) {
568 58381f70 2022-09-03 op case '%':
569 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
570 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
571 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
572 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
573 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
574 58381f70 2022-09-03 op
575 58381f70 2022-09-03 op hex[0] = p[1];
576 58381f70 2022-09-03 op hex[1] = p[2];
577 58381f70 2022-09-03 op
578 58381f70 2022-09-03 op /*
579 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
580 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
581 58381f70 2022-09-03 op */
582 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
583 58381f70 2022-09-03 op *q = (char)x;
584 58381f70 2022-09-03 op p += 2;
585 58381f70 2022-09-03 op break;
586 58381f70 2022-09-03 op default:
587 58381f70 2022-09-03 op *q = *p;
588 58381f70 2022-09-03 op break;
589 58381f70 2022-09-03 op }
590 58381f70 2022-09-03 op p++;
591 58381f70 2022-09-03 op q++;
592 58381f70 2022-09-03 op }
593 58381f70 2022-09-03 op *q = '\0';
594 58381f70 2022-09-03 op
595 58381f70 2022-09-03 op return NULL;
596 58381f70 2022-09-03 op }
597 58381f70 2022-09-03 op
598 58381f70 2022-09-03 op static const struct got_error *
599 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
600 a596b957 2022-07-14 tracey {
601 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
602 a596b957 2022-07-14 tracey const char *errstr;
603 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
604 a596b957 2022-07-14 tracey
605 58381f70 2022-09-03 op error = gotweb_urldecode(value);
606 58381f70 2022-09-03 op if (error)
607 58381f70 2022-09-03 op return error;
608 58381f70 2022-09-03 op
609 a596b957 2022-07-14 tracey for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
610 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
611 a596b957 2022-07-14 tracey continue;
612 a596b957 2022-07-14 tracey
613 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
614 a596b957 2022-07-14 tracey case ACTION:
615 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
616 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
617 a596b957 2022-07-14 tracey continue;
618 a596b957 2022-07-14 tracey else if (strcmp(value,
619 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
620 a596b957 2022-07-14 tracey (*qs)->action =
621 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
622 a596b957 2022-07-14 tracey goto qa_found;
623 a596b957 2022-07-14 tracey }
624 a596b957 2022-07-14 tracey }
625 a596b957 2022-07-14 tracey (*qs)->action = ERR;
626 a596b957 2022-07-14 tracey qa_found:
627 a596b957 2022-07-14 tracey break;
628 a596b957 2022-07-14 tracey case COMMIT:
629 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
630 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
631 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
632 a596b957 2022-07-14 tracey __func__);
633 a596b957 2022-07-14 tracey goto done;
634 a596b957 2022-07-14 tracey }
635 a596b957 2022-07-14 tracey break;
636 a596b957 2022-07-14 tracey case RFILE:
637 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
638 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
639 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
640 a596b957 2022-07-14 tracey __func__);
641 a596b957 2022-07-14 tracey goto done;
642 a596b957 2022-07-14 tracey }
643 a596b957 2022-07-14 tracey break;
644 a596b957 2022-07-14 tracey case FOLDER:
645 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
646 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
647 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
648 a596b957 2022-07-14 tracey __func__);
649 a596b957 2022-07-14 tracey goto done;
650 a596b957 2022-07-14 tracey }
651 a596b957 2022-07-14 tracey break;
652 a596b957 2022-07-14 tracey case HEADREF:
653 f8faf9f1 2022-09-01 op free((*qs)->headref);
654 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
655 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
656 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
657 a596b957 2022-07-14 tracey __func__);
658 a596b957 2022-07-14 tracey goto done;
659 a596b957 2022-07-14 tracey }
660 a596b957 2022-07-14 tracey break;
661 a596b957 2022-07-14 tracey case INDEX_PAGE:
662 a596b957 2022-07-14 tracey if (strlen(value) == 0)
663 a596b957 2022-07-14 tracey break;
664 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
665 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
666 a596b957 2022-07-14 tracey if (errstr) {
667 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
668 a596b957 2022-07-14 tracey __func__, errstr);
669 a596b957 2022-07-14 tracey goto done;
670 a596b957 2022-07-14 tracey }
671 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
672 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
673 a596b957 2022-07-14 tracey break;
674 a596b957 2022-07-14 tracey case PATH:
675 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
676 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
677 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
678 a596b957 2022-07-14 tracey __func__);
679 a596b957 2022-07-14 tracey goto done;
680 a596b957 2022-07-14 tracey }
681 a596b957 2022-07-14 tracey break;
682 a596b957 2022-07-14 tracey case PAGE:
683 a596b957 2022-07-14 tracey if (strlen(value) == 0)
684 a596b957 2022-07-14 tracey break;
685 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
686 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
687 a596b957 2022-07-14 tracey if (errstr) {
688 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
689 a596b957 2022-07-14 tracey __func__, errstr);
690 a596b957 2022-07-14 tracey goto done;
691 a596b957 2022-07-14 tracey }
692 03f6a843 2022-12-17 op if ((*qs)->page < 0)
693 a596b957 2022-07-14 tracey (*qs)->page = 0;
694 a596b957 2022-07-14 tracey break;
695 a596b957 2022-07-14 tracey default:
696 a596b957 2022-07-14 tracey break;
697 a596b957 2022-07-14 tracey }
698 a596b957 2022-07-14 tracey }
699 a596b957 2022-07-14 tracey done:
700 a596b957 2022-07-14 tracey return error;
701 a596b957 2022-07-14 tracey }
702 a596b957 2022-07-14 tracey
703 a596b957 2022-07-14 tracey void
704 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
705 a596b957 2022-07-14 tracey {
706 a596b957 2022-07-14 tracey if (rt != NULL) {
707 a596b957 2022-07-14 tracey free(rt->commit_id);
708 625e5896 2022-09-01 op free(rt->tag_name);
709 625e5896 2022-09-01 op free(rt->tag_commit);
710 625e5896 2022-09-01 op free(rt->commit_msg);
711 a596b957 2022-07-14 tracey free(rt->tagger);
712 a596b957 2022-07-14 tracey }
713 a596b957 2022-07-14 tracey free(rt);
714 a596b957 2022-07-14 tracey }
715 a596b957 2022-07-14 tracey
716 a596b957 2022-07-14 tracey void
717 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
718 a596b957 2022-07-14 tracey {
719 a596b957 2022-07-14 tracey if (rc != NULL) {
720 a596b957 2022-07-14 tracey free(rc->path);
721 a596b957 2022-07-14 tracey free(rc->refs_str);
722 a596b957 2022-07-14 tracey free(rc->commit_id);
723 a596b957 2022-07-14 tracey free(rc->parent_id);
724 a596b957 2022-07-14 tracey free(rc->tree_id);
725 a596b957 2022-07-14 tracey free(rc->author);
726 a596b957 2022-07-14 tracey free(rc->committer);
727 a596b957 2022-07-14 tracey free(rc->commit_msg);
728 a596b957 2022-07-14 tracey }
729 a596b957 2022-07-14 tracey free(rc);
730 a596b957 2022-07-14 tracey }
731 a596b957 2022-07-14 tracey
732 a596b957 2022-07-14 tracey static void
733 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
734 a596b957 2022-07-14 tracey {
735 a596b957 2022-07-14 tracey if (qs != NULL) {
736 a596b957 2022-07-14 tracey free(qs->commit);
737 a596b957 2022-07-14 tracey free(qs->file);
738 a596b957 2022-07-14 tracey free(qs->folder);
739 a596b957 2022-07-14 tracey free(qs->headref);
740 a596b957 2022-07-14 tracey free(qs->path);
741 a596b957 2022-07-14 tracey }
742 a596b957 2022-07-14 tracey free(qs);
743 a596b957 2022-07-14 tracey }
744 a596b957 2022-07-14 tracey
745 a596b957 2022-07-14 tracey static void
746 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
747 a596b957 2022-07-14 tracey {
748 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
749 a596b957 2022-07-14 tracey free(repo_dir->name);
750 a596b957 2022-07-14 tracey free(repo_dir->owner);
751 a596b957 2022-07-14 tracey free(repo_dir->description);
752 a596b957 2022-07-14 tracey free(repo_dir->url);
753 a596b957 2022-07-14 tracey free(repo_dir->path);
754 a596b957 2022-07-14 tracey }
755 a596b957 2022-07-14 tracey free(repo_dir);
756 a596b957 2022-07-14 tracey }
757 a596b957 2022-07-14 tracey
758 a596b957 2022-07-14 tracey void
759 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
760 a596b957 2022-07-14 tracey {
761 df2d3cd2 2023-03-11 op const struct got_error *err;
762 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
763 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
764 df2d3cd2 2023-03-11 op int i;
765 a596b957 2022-07-14 tracey
766 df2d3cd2 2023-03-11 op got_ref_list_free(&t->refs);
767 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
768 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
769 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
770 a596b957 2022-07-14 tracey }
771 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
772 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
773 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
774 a596b957 2022-07-14 tracey }
775 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
776 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
777 e3662697 2023-02-03 op free(t->more_id);
778 341fa7ca 2022-09-01 op free(t->next_id);
779 341fa7ca 2022-09-01 op free(t->prev_id);
780 df2d3cd2 2023-03-11 op if (t->blob)
781 df2d3cd2 2023-03-11 op got_object_blob_close(t->blob);
782 df2d3cd2 2023-03-11 op if (t->fp) {
783 df2d3cd2 2023-03-11 op err = got_gotweb_flushfile(t->fp, t->fd);
784 df2d3cd2 2023-03-11 op if (err)
785 df2d3cd2 2023-03-11 op log_warnx("%s: got_gotweb_flushfile failure: %s",
786 df2d3cd2 2023-03-11 op __func__, err->msg);
787 df2d3cd2 2023-03-11 op t->fd = -1;
788 df2d3cd2 2023-03-11 op }
789 df2d3cd2 2023-03-11 op if (t->fd != -1)
790 df2d3cd2 2023-03-11 op close(t->fd);
791 df2d3cd2 2023-03-11 op if (t->repos) {
792 df2d3cd2 2023-03-11 op for (i = 0; i < t->nrepos; ++i)
793 df2d3cd2 2023-03-11 op free(t->repos[i]);
794 df2d3cd2 2023-03-11 op free(t->repos);
795 df2d3cd2 2023-03-11 op }
796 a596b957 2022-07-14 tracey free(t);
797 a596b957 2022-07-14 tracey }
798 a596b957 2022-07-14 tracey
799 b4c0bd72 2022-12-17 op void
800 b4c0bd72 2022-12-17 op gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
801 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
802 a596b957 2022-07-14 tracey {
803 a596b957 2022-07-14 tracey struct transport *t = c->t;
804 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
805 a596b957 2022-07-14 tracey struct server *srv = c->srv;
806 a596b957 2022-07-14 tracey
807 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
808 a596b957 2022-07-14 tracey
809 a596b957 2022-07-14 tracey switch(qs->action) {
810 a596b957 2022-07-14 tracey case INDEX:
811 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
812 b4c0bd72 2022-12-17 op *have_prev = 1;
813 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
814 8d02314f 2022-09-07 op .action = -1,
815 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
816 8d02314f 2022-09-07 op .page = -1,
817 8d02314f 2022-09-07 op };
818 a596b957 2022-07-14 tracey }
819 b4c0bd72 2022-12-17 op if (t->next_disp == srv->max_repos_display &&
820 b4c0bd72 2022-12-17 op t->repos_total != (qs->index_page + 1) *
821 b4c0bd72 2022-12-17 op srv->max_repos_display) {
822 b4c0bd72 2022-12-17 op *have_next = 1;
823 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
824 b4c0bd72 2022-12-17 op .action = -1,
825 b4c0bd72 2022-12-17 op .index_page = qs->index_page + 1,
826 b4c0bd72 2022-12-17 op .page = -1,
827 b4c0bd72 2022-12-17 op };
828 b4c0bd72 2022-12-17 op }
829 a596b957 2022-07-14 tracey break;
830 a596b957 2022-07-14 tracey case TAGS:
831 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
832 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
833 b4c0bd72 2022-12-17 op *have_prev = 1;
834 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
835 b4c0bd72 2022-12-17 op .action = TAGS,
836 b4c0bd72 2022-12-17 op .index_page = -1,
837 b4c0bd72 2022-12-17 op .page = qs->page - 1,
838 b4c0bd72 2022-12-17 op .path = qs->path,
839 b4c0bd72 2022-12-17 op .commit = t->prev_id,
840 b4c0bd72 2022-12-17 op .headref = qs->headref,
841 b4c0bd72 2022-12-17 op };
842 b4c0bd72 2022-12-17 op }
843 a596b957 2022-07-14 tracey if (t->next_id) {
844 b4c0bd72 2022-12-17 op *have_next = 1;
845 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
846 8d02314f 2022-09-07 op .action = TAGS,
847 8d02314f 2022-09-07 op .index_page = -1,
848 8d02314f 2022-09-07 op .page = qs->page + 1,
849 8d02314f 2022-09-07 op .path = qs->path,
850 8d02314f 2022-09-07 op .commit = t->next_id,
851 8d02314f 2022-09-07 op .headref = qs->headref,
852 8d02314f 2022-09-07 op };
853 a596b957 2022-07-14 tracey }
854 a596b957 2022-07-14 tracey break;
855 a596b957 2022-07-14 tracey }
856 a596b957 2022-07-14 tracey }
857 a596b957 2022-07-14 tracey
858 df2d3cd2 2023-03-11 op static int
859 df2d3cd2 2023-03-11 op gotweb_render_index(struct template *tp)
860 a596b957 2022-07-14 tracey {
861 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
862 df2d3cd2 2023-03-11 op struct request *c = tp->tp_arg;
863 a596b957 2022-07-14 tracey struct server *srv = c->srv;
864 a596b957 2022-07-14 tracey struct transport *t = c->t;
865 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
866 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
867 df2d3cd2 2023-03-11 op struct dirent **sd_dent = t->repos;
868 df2d3cd2 2023-03-11 op unsigned int d_i, d_disp = 0;
869 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
870 df2d3cd2 2023-03-11 op int type, r;
871 a596b957 2022-07-14 tracey
872 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
873 df2d3cd2 2023-03-11 op return -1;
874 01498c42 2022-08-19 op
875 df2d3cd2 2023-03-11 op for (d_i = 0; d_i < t->nrepos; d_i++) {
876 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
877 659fa237 2022-11-22 op break;
878 a596b957 2022-07-14 tracey
879 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
880 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
881 525dfdf4 2022-11-22 op d_skipped++;
882 525dfdf4 2022-11-22 op continue;
883 525dfdf4 2022-11-22 op }
884 525dfdf4 2022-11-22 op
885 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
886 525dfdf4 2022-11-22 op sd_dent[d_i]);
887 525dfdf4 2022-11-22 op if (error)
888 df2d3cd2 2023-03-11 op continue;
889 525dfdf4 2022-11-22 op if (type != DT_DIR) {
890 525dfdf4 2022-11-22 op d_skipped++;
891 a596b957 2022-07-14 tracey continue;
892 525dfdf4 2022-11-22 op }
893 a596b957 2022-07-14 tracey
894 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
895 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
896 a596b957 2022-07-14 tracey t->prev_disp++;
897 a596b957 2022-07-14 tracey continue;
898 a596b957 2022-07-14 tracey }
899 a596b957 2022-07-14 tracey
900 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
901 a596b957 2022-07-14 tracey if (error)
902 df2d3cd2 2023-03-11 op continue;
903 a596b957 2022-07-14 tracey
904 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
905 df2d3cd2 2023-03-11 op if (error && error->code == GOT_ERR_LONELY_PACKIDX) {
906 df2d3cd2 2023-03-11 op if (error->code != GOT_ERR_NOT_GIT_REPO)
907 df2d3cd2 2023-03-11 op log_warnx("%s: %s: %s", __func__,
908 df2d3cd2 2023-03-11 op sd_dent[d_i]->d_name, error->msg);
909 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
910 a596b957 2022-07-14 tracey repo_dir = NULL;
911 525dfdf4 2022-11-22 op d_skipped++;
912 a596b957 2022-07-14 tracey continue;
913 a596b957 2022-07-14 tracey }
914 525dfdf4 2022-11-22 op
915 a596b957 2022-07-14 tracey d_disp++;
916 a596b957 2022-07-14 tracey t->prev_disp++;
917 a596b957 2022-07-14 tracey
918 df2d3cd2 2023-03-11 op r = gotweb_render_repo_fragment(c->tp, repo_dir);
919 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
920 df2d3cd2 2023-03-11 op if (r == -1)
921 df2d3cd2 2023-03-11 op return -1;
922 df2d3cd2 2023-03-11 op
923 a596b957 2022-07-14 tracey t->next_disp++;
924 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
925 a596b957 2022-07-14 tracey break;
926 a596b957 2022-07-14 tracey }
927 df2d3cd2 2023-03-11 op t->repos_total = t->nrepos - d_skipped;
928 525dfdf4 2022-11-22 op
929 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
930 df2d3cd2 2023-03-11 op return 0;
931 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
932 df2d3cd2 2023-03-11 op return 0;
933 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
934 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
935 df2d3cd2 2023-03-11 op return 0;
936 a596b957 2022-07-14 tracey
937 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
938 df2d3cd2 2023-03-11 op return -1;
939 df2d3cd2 2023-03-11 op
940 df2d3cd2 2023-03-11 op return 0;
941 a596b957 2022-07-14 tracey }
942 a596b957 2022-07-14 tracey
943 8d02314f 2022-09-07 op static inline int
944 8d02314f 2022-09-07 op should_urlencode(int c)
945 8d02314f 2022-09-07 op {
946 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
947 8d02314f 2022-09-07 op return 1;
948 8d02314f 2022-09-07 op
949 8d02314f 2022-09-07 op switch (c) {
950 8d02314f 2022-09-07 op /* gen-delim */
951 8d02314f 2022-09-07 op case ':':
952 8d02314f 2022-09-07 op case '/':
953 8d02314f 2022-09-07 op case '?':
954 8d02314f 2022-09-07 op case '#':
955 8d02314f 2022-09-07 op case '[':
956 8d02314f 2022-09-07 op case ']':
957 8d02314f 2022-09-07 op case '@':
958 8d02314f 2022-09-07 op /* sub-delims */
959 8d02314f 2022-09-07 op case '!':
960 8d02314f 2022-09-07 op case '$':
961 8d02314f 2022-09-07 op case '&':
962 8d02314f 2022-09-07 op case '\'':
963 8d02314f 2022-09-07 op case '(':
964 8d02314f 2022-09-07 op case ')':
965 8d02314f 2022-09-07 op case '*':
966 8d02314f 2022-09-07 op case '+':
967 8d02314f 2022-09-07 op case ',':
968 8d02314f 2022-09-07 op case ';':
969 8d02314f 2022-09-07 op case '=':
970 4a7f5bae 2023-01-05 op /* needed because the URLs are embedded into the HTML */
971 4a7f5bae 2023-01-05 op case '\"':
972 8d02314f 2022-09-07 op return 1;
973 8d02314f 2022-09-07 op default:
974 8d02314f 2022-09-07 op return 0;
975 8d02314f 2022-09-07 op }
976 8d02314f 2022-09-07 op }
977 8d02314f 2022-09-07 op
978 8d02314f 2022-09-07 op static char *
979 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
980 8d02314f 2022-09-07 op {
981 8d02314f 2022-09-07 op const char *s;
982 8d02314f 2022-09-07 op char *escaped;
983 8d02314f 2022-09-07 op size_t i, len;
984 8d02314f 2022-09-07 op int a, b;
985 8d02314f 2022-09-07 op
986 8d02314f 2022-09-07 op len = 0;
987 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
988 8d02314f 2022-09-07 op len++;
989 8d02314f 2022-09-07 op if (should_urlencode(*s))
990 8d02314f 2022-09-07 op len += 2;
991 8d02314f 2022-09-07 op }
992 8d02314f 2022-09-07 op
993 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
994 8d02314f 2022-09-07 op if (escaped == NULL)
995 8d02314f 2022-09-07 op return NULL;
996 8d02314f 2022-09-07 op
997 8d02314f 2022-09-07 op i = 0;
998 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
999 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
1000 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
1001 8d02314f 2022-09-07 op b = (*s & 0x0F);
1002 8d02314f 2022-09-07 op
1003 8d02314f 2022-09-07 op escaped[i++] = '%';
1004 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1005 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1006 8d02314f 2022-09-07 op } else
1007 8d02314f 2022-09-07 op escaped[i++] = *s;
1008 8d02314f 2022-09-07 op }
1009 8d02314f 2022-09-07 op
1010 8d02314f 2022-09-07 op return escaped;
1011 8d02314f 2022-09-07 op }
1012 8d02314f 2022-09-07 op
1013 ed619ca0 2022-12-14 op const char *
1014 ed619ca0 2022-12-14 op gotweb_action_name(int action)
1015 8d02314f 2022-09-07 op {
1016 8d02314f 2022-09-07 op switch (action) {
1017 8d02314f 2022-09-07 op case BLAME:
1018 8d02314f 2022-09-07 op return "blame";
1019 8d02314f 2022-09-07 op case BLOB:
1020 8d02314f 2022-09-07 op return "blob";
1021 298f95fb 2023-01-05 op case BLOBRAW:
1022 298f95fb 2023-01-05 op return "blobraw";
1023 8d02314f 2022-09-07 op case BRIEFS:
1024 8d02314f 2022-09-07 op return "briefs";
1025 8d02314f 2022-09-07 op case COMMITS:
1026 8d02314f 2022-09-07 op return "commits";
1027 8d02314f 2022-09-07 op case DIFF:
1028 8d02314f 2022-09-07 op return "diff";
1029 8d02314f 2022-09-07 op case ERR:
1030 8d02314f 2022-09-07 op return "err";
1031 8d02314f 2022-09-07 op case INDEX:
1032 8d02314f 2022-09-07 op return "index";
1033 8d02314f 2022-09-07 op case SUMMARY:
1034 8d02314f 2022-09-07 op return "summary";
1035 8d02314f 2022-09-07 op case TAG:
1036 8d02314f 2022-09-07 op return "tag";
1037 8d02314f 2022-09-07 op case TAGS:
1038 8d02314f 2022-09-07 op return "tags";
1039 8d02314f 2022-09-07 op case TREE:
1040 8d02314f 2022-09-07 op return "tree";
1041 1abb18e1 2022-12-20 op case RSS:
1042 1abb18e1 2022-12-20 op return "rss";
1043 8d02314f 2022-09-07 op default:
1044 8d02314f 2022-09-07 op return NULL;
1045 8d02314f 2022-09-07 op }
1046 8d02314f 2022-09-07 op }
1047 8d02314f 2022-09-07 op
1048 ed619ca0 2022-12-14 op int
1049 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1050 8d02314f 2022-09-07 op {
1051 8d02314f 2022-09-07 op const char *sep = "?", *action;
1052 8d02314f 2022-09-07 op char *tmp;
1053 8d02314f 2022-09-07 op int r;
1054 8d02314f 2022-09-07 op
1055 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1056 8d02314f 2022-09-07 op if (action != NULL) {
1057 8d02314f 2022-09-07 op if (fcgi_printf(c, "?action=%s", action) == -1)
1058 8d02314f 2022-09-07 op return -1;
1059 8d02314f 2022-09-07 op sep = "&";
1060 8d02314f 2022-09-07 op }
1061 8d02314f 2022-09-07 op
1062 8d02314f 2022-09-07 op if (url->commit) {
1063 8d02314f 2022-09-07 op if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1064 8d02314f 2022-09-07 op return -1;
1065 8d02314f 2022-09-07 op sep = "&";
1066 8d02314f 2022-09-07 op }
1067 8d02314f 2022-09-07 op
1068 8d02314f 2022-09-07 op if (url->previd) {
1069 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1070 8d02314f 2022-09-07 op return -1;
1071 8d02314f 2022-09-07 op sep = "&";
1072 8d02314f 2022-09-07 op }
1073 8d02314f 2022-09-07 op
1074 8d02314f 2022-09-07 op if (url->prevset) {
1075 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1076 8d02314f 2022-09-07 op return -1;
1077 8d02314f 2022-09-07 op sep = "&";
1078 8d02314f 2022-09-07 op }
1079 8d02314f 2022-09-07 op
1080 8d02314f 2022-09-07 op if (url->file) {
1081 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1082 8d02314f 2022-09-07 op if (tmp == NULL)
1083 8d02314f 2022-09-07 op return -1;
1084 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1085 8d02314f 2022-09-07 op free(tmp);
1086 8d02314f 2022-09-07 op if (r == -1)
1087 8d02314f 2022-09-07 op return -1;
1088 8d02314f 2022-09-07 op sep = "&";
1089 8d02314f 2022-09-07 op }
1090 8d02314f 2022-09-07 op
1091 8d02314f 2022-09-07 op if (url->folder) {
1092 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1093 8d02314f 2022-09-07 op if (tmp == NULL)
1094 8d02314f 2022-09-07 op return -1;
1095 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1096 8d02314f 2022-09-07 op free(tmp);
1097 8d02314f 2022-09-07 op if (r == -1)
1098 8d02314f 2022-09-07 op return -1;
1099 8d02314f 2022-09-07 op sep = "&";
1100 8d02314f 2022-09-07 op }
1101 8d02314f 2022-09-07 op
1102 8d02314f 2022-09-07 op if (url->headref) {
1103 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1104 8d02314f 2022-09-07 op if (tmp == NULL)
1105 8d02314f 2022-09-07 op return -1;
1106 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1107 8d02314f 2022-09-07 op free(tmp);
1108 8d02314f 2022-09-07 op if (r == -1)
1109 8d02314f 2022-09-07 op return -1;
1110 8d02314f 2022-09-07 op sep = "&";
1111 8d02314f 2022-09-07 op }
1112 8d02314f 2022-09-07 op
1113 8d02314f 2022-09-07 op if (url->index_page != -1) {
1114 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sindex_page=%d", sep,
1115 8d02314f 2022-09-07 op url->index_page) == -1)
1116 8d02314f 2022-09-07 op return -1;
1117 8d02314f 2022-09-07 op sep = "&";
1118 8d02314f 2022-09-07 op }
1119 8d02314f 2022-09-07 op
1120 8d02314f 2022-09-07 op if (url->path) {
1121 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1122 8d02314f 2022-09-07 op if (tmp == NULL)
1123 8d02314f 2022-09-07 op return -1;
1124 8d02314f 2022-09-07 op r = fcgi_printf(c, "%spath=%s", sep, tmp);
1125 8d02314f 2022-09-07 op free(tmp);
1126 8d02314f 2022-09-07 op if (r == -1)
1127 8d02314f 2022-09-07 op return -1;
1128 8d02314f 2022-09-07 op sep = "&";
1129 8d02314f 2022-09-07 op }
1130 8d02314f 2022-09-07 op
1131 8d02314f 2022-09-07 op if (url->page != -1) {
1132 8d02314f 2022-09-07 op if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1133 8d02314f 2022-09-07 op return -1;
1134 8d02314f 2022-09-07 op sep = "&";
1135 8d02314f 2022-09-07 op }
1136 8d02314f 2022-09-07 op
1137 8d02314f 2022-09-07 op return 0;
1138 8d02314f 2022-09-07 op }
1139 8d02314f 2022-09-07 op
1140 8d02314f 2022-09-07 op int
1141 1abb18e1 2022-12-20 op gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1142 1abb18e1 2022-12-20 op {
1143 1abb18e1 2022-12-20 op struct template *tp = c->tp;
1144 1abb18e1 2022-12-20 op const char *proto = c->https ? "https" : "http";
1145 1abb18e1 2022-12-20 op
1146 1abb18e1 2022-12-20 op if (fcgi_puts(tp, proto) == -1 ||
1147 1abb18e1 2022-12-20 op fcgi_puts(tp, "://") == -1 ||
1148 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->server_name) == -1 ||
1149 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->document_uri) == -1)
1150 1abb18e1 2022-12-20 op return -1;
1151 1abb18e1 2022-12-20 op
1152 1abb18e1 2022-12-20 op return gotweb_render_url(c, url);
1153 8d02314f 2022-09-07 op }
1154 8d02314f 2022-09-07 op
1155 b5c757f5 2022-09-01 stsp static struct got_repository *
1156 b5c757f5 2022-09-01 stsp find_cached_repo(struct server *srv, const char *path)
1157 b5c757f5 2022-09-01 stsp {
1158 b5c757f5 2022-09-01 stsp int i;
1159 b5c757f5 2022-09-01 stsp
1160 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++) {
1161 b5c757f5 2022-09-01 stsp if (strcmp(srv->cached_repos[i].path, path) == 0)
1162 b5c757f5 2022-09-01 stsp return srv->cached_repos[i].repo;
1163 b5c757f5 2022-09-01 stsp }
1164 b5c757f5 2022-09-01 stsp
1165 b5c757f5 2022-09-01 stsp return NULL;
1166 b5c757f5 2022-09-01 stsp }
1167 b5c757f5 2022-09-01 stsp
1168 a596b957 2022-07-14 tracey static const struct got_error *
1169 b5c757f5 2022-09-01 stsp cache_repo(struct got_repository **new, struct server *srv,
1170 b5c757f5 2022-09-01 stsp struct repo_dir *repo_dir, struct socket *sock)
1171 b5c757f5 2022-09-01 stsp {
1172 b5c757f5 2022-09-01 stsp const struct got_error *error = NULL;
1173 b5c757f5 2022-09-01 stsp struct got_repository *repo;
1174 b5c757f5 2022-09-01 stsp struct cached_repo *cr;
1175 b5c757f5 2022-09-01 stsp int evicted = 0;
1176 b5c757f5 2022-09-01 stsp
1177 7e0ec052 2022-09-06 op if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1178 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos - 1];
1179 b5c757f5 2022-09-01 stsp error = got_repo_close(cr->repo);
1180 b5c757f5 2022-09-01 stsp memset(cr, 0, sizeof(*cr));
1181 b5c757f5 2022-09-01 stsp srv->ncached_repos--;
1182 b5c757f5 2022-09-01 stsp if (error)
1183 b5c757f5 2022-09-01 stsp return error;
1184 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1185 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1186 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[0];
1187 b5c757f5 2022-09-01 stsp evicted = 1;
1188 b5c757f5 2022-09-01 stsp } else {
1189 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos];
1190 b5c757f5 2022-09-01 stsp }
1191 b5c757f5 2022-09-01 stsp
1192 b5c757f5 2022-09-01 stsp error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1193 b5c757f5 2022-09-01 stsp if (error) {
1194 b5c757f5 2022-09-01 stsp if (evicted) {
1195 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1196 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1197 b5c757f5 2022-09-01 stsp }
1198 b5c757f5 2022-09-01 stsp return error;
1199 b5c757f5 2022-09-01 stsp }
1200 b5c757f5 2022-09-01 stsp
1201 b5c757f5 2022-09-01 stsp if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1202 b5c757f5 2022-09-01 stsp >= sizeof(cr->path)) {
1203 b5c757f5 2022-09-01 stsp if (evicted) {
1204 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1205 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1206 b5c757f5 2022-09-01 stsp }
1207 b5c757f5 2022-09-01 stsp return got_error(GOT_ERR_NO_SPACE);
1208 b5c757f5 2022-09-01 stsp }
1209 b5c757f5 2022-09-01 stsp
1210 b5c757f5 2022-09-01 stsp cr->repo = repo;
1211 b5c757f5 2022-09-01 stsp srv->ncached_repos++;
1212 b5c757f5 2022-09-01 stsp *new = repo;
1213 b5c757f5 2022-09-01 stsp return NULL;
1214 b5c757f5 2022-09-01 stsp }
1215 b5c757f5 2022-09-01 stsp
1216 b5c757f5 2022-09-01 stsp static const struct got_error *
1217 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1218 a596b957 2022-07-14 tracey {
1219 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1220 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1221 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1222 a596b957 2022-07-14 tracey struct transport *t = c->t;
1223 b5c757f5 2022-09-01 stsp struct got_repository *repo = NULL;
1224 a596b957 2022-07-14 tracey DIR *dt;
1225 a596b957 2022-07-14 tracey char *dir_test;
1226 a596b957 2022-07-14 tracey
1227 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1228 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1229 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1230 a596b957 2022-07-14 tracey
1231 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1232 a596b957 2022-07-14 tracey if (dt == NULL) {
1233 a596b957 2022-07-14 tracey free(dir_test);
1234 a596b957 2022-07-14 tracey } else {
1235 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1236 a596b957 2022-07-14 tracey dir_test = NULL;
1237 0fad85dd 2022-09-01 op goto done;
1238 a596b957 2022-07-14 tracey }
1239 a596b957 2022-07-14 tracey
1240 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1241 0fad85dd 2022-09-01 op repo_dir->name) == -1)
1242 0fad85dd 2022-09-01 op return got_error_from_errno("asprintf");
1243 a596b957 2022-07-14 tracey
1244 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1245 a596b957 2022-07-14 tracey if (dt == NULL) {
1246 a596b957 2022-07-14 tracey error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1247 a596b957 2022-07-14 tracey goto err;
1248 0fad85dd 2022-09-01 op } else {
1249 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1250 0fad85dd 2022-09-01 op dir_test = NULL;
1251 0fad85dd 2022-09-01 op }
1252 0fad85dd 2022-09-01 op
1253 a596b957 2022-07-14 tracey done:
1254 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1255 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1256 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1257 d5996b9e 2022-10-31 landry goto err;
1258 d5996b9e 2022-10-31 landry }
1259 d5996b9e 2022-10-31 landry
1260 b5c757f5 2022-09-01 stsp repo = find_cached_repo(srv, repo_dir->path);
1261 b5c757f5 2022-09-01 stsp if (repo == NULL) {
1262 b5c757f5 2022-09-01 stsp error = cache_repo(&repo, srv, repo_dir, sock);
1263 b5c757f5 2022-09-01 stsp if (error)
1264 b5c757f5 2022-09-01 stsp goto err;
1265 b5c757f5 2022-09-01 stsp }
1266 b5c757f5 2022-09-01 stsp t->repo = repo;
1267 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1268 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1269 a596b957 2022-07-14 tracey if (error)
1270 a596b957 2022-07-14 tracey goto err;
1271 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1272 a596b957 2022-07-14 tracey if (error)
1273 a596b957 2022-07-14 tracey goto err;
1274 cb93ab40 2023-01-22 op error = got_get_repo_age(&repo_dir->age, c, NULL);
1275 a596b957 2022-07-14 tracey if (error)
1276 a596b957 2022-07-14 tracey goto err;
1277 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1278 3b81530f 2022-11-22 op dirfd(dt));
1279 a596b957 2022-07-14 tracey err:
1280 a596b957 2022-07-14 tracey free(dir_test);
1281 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1282 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1283 a596b957 2022-07-14 tracey return error;
1284 a596b957 2022-07-14 tracey }
1285 a596b957 2022-07-14 tracey
1286 a596b957 2022-07-14 tracey static const struct got_error *
1287 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1288 a596b957 2022-07-14 tracey {
1289 a596b957 2022-07-14 tracey const struct got_error *error;
1290 a596b957 2022-07-14 tracey
1291 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
1292 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
1293 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1294 a596b957 2022-07-14 tracey
1295 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1296 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1297 a596b957 2022-07-14 tracey free(*repo_dir);
1298 a596b957 2022-07-14 tracey *repo_dir = NULL;
1299 a596b957 2022-07-14 tracey return error;
1300 a596b957 2022-07-14 tracey }
1301 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
1302 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
1303 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
1304 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
1305 a596b957 2022-07-14 tracey
1306 a596b957 2022-07-14 tracey return NULL;
1307 a596b957 2022-07-14 tracey }
1308 a596b957 2022-07-14 tracey
1309 a596b957 2022-07-14 tracey static const struct got_error *
1310 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1311 3b81530f 2022-11-22 op const char *dirpath, int dir)
1312 a596b957 2022-07-14 tracey {
1313 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1314 3b81530f 2022-11-22 op struct stat sb;
1315 3b81530f 2022-11-22 op int fd = -1;
1316 3b81530f 2022-11-22 op off_t len;
1317 a596b957 2022-07-14 tracey
1318 a596b957 2022-07-14 tracey *description = NULL;
1319 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1320 a596b957 2022-07-14 tracey return NULL;
1321 a596b957 2022-07-14 tracey
1322 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1323 3b81530f 2022-11-22 op if (fd == -1) {
1324 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1325 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1326 3b81530f 2022-11-22 op dirpath, "description");
1327 3b81530f 2022-11-22 op }
1328 a596b957 2022-07-14 tracey goto done;
1329 a596b957 2022-07-14 tracey }
1330 a596b957 2022-07-14 tracey
1331 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1332 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1333 3b81530f 2022-11-22 op dirpath, "description");
1334 a596b957 2022-07-14 tracey goto done;
1335 a596b957 2022-07-14 tracey }
1336 a596b957 2022-07-14 tracey
1337 3b81530f 2022-11-22 op len = sb.st_size;
1338 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1339 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1340 a596b957 2022-07-14 tracey
1341 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1342 a596b957 2022-07-14 tracey if (*description == NULL) {
1343 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1344 a596b957 2022-07-14 tracey goto done;
1345 a596b957 2022-07-14 tracey }
1346 a596b957 2022-07-14 tracey
1347 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1348 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1349 a596b957 2022-07-14 tracey done:
1350 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1351 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1352 a596b957 2022-07-14 tracey return error;
1353 a596b957 2022-07-14 tracey }
1354 a596b957 2022-07-14 tracey
1355 a596b957 2022-07-14 tracey static const struct got_error *
1356 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1357 3b81530f 2022-11-22 op int dir)
1358 a596b957 2022-07-14 tracey {
1359 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1360 3b81530f 2022-11-22 op struct stat sb;
1361 3b81530f 2022-11-22 op int fd = -1;
1362 3b81530f 2022-11-22 op off_t len;
1363 a596b957 2022-07-14 tracey
1364 a596b957 2022-07-14 tracey *url = NULL;
1365 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1366 a596b957 2022-07-14 tracey return NULL;
1367 a596b957 2022-07-14 tracey
1368 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1369 3b81530f 2022-11-22 op if (fd == -1) {
1370 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1371 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1372 3b81530f 2022-11-22 op dirpath, "cloneurl");
1373 3b81530f 2022-11-22 op }
1374 a596b957 2022-07-14 tracey goto done;
1375 a596b957 2022-07-14 tracey }
1376 a596b957 2022-07-14 tracey
1377 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1378 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1379 3b81530f 2022-11-22 op dirpath, "cloneurl");
1380 a596b957 2022-07-14 tracey goto done;
1381 a596b957 2022-07-14 tracey }
1382 a596b957 2022-07-14 tracey
1383 3b81530f 2022-11-22 op len = sb.st_size;
1384 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1385 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1386 a596b957 2022-07-14 tracey
1387 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1388 a596b957 2022-07-14 tracey if (*url == NULL) {
1389 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1390 a596b957 2022-07-14 tracey goto done;
1391 a596b957 2022-07-14 tracey }
1392 a596b957 2022-07-14 tracey
1393 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1394 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1395 a596b957 2022-07-14 tracey done:
1396 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1397 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1398 a596b957 2022-07-14 tracey return error;
1399 a596b957 2022-07-14 tracey }
1400 a596b957 2022-07-14 tracey
1401 cb93ab40 2023-01-22 op int
1402 cb93ab40 2023-01-22 op gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1403 a596b957 2022-07-14 tracey {
1404 cb93ab40 2023-01-22 op struct request *c = tp->tp_arg;
1405 a596b957 2022-07-14 tracey struct tm tm;
1406 fced5a66 2022-07-20 naddy long long diff_time;
1407 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
1408 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
1409 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
1410 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
1411 a596b957 2022-07-14 tracey char *s;
1412 1abb18e1 2022-12-20 op char datebuf[64];
1413 1abb18e1 2022-12-20 op size_t r;
1414 a596b957 2022-07-14 tracey
1415 a596b957 2022-07-14 tracey switch (ref_tm) {
1416 a596b957 2022-07-14 tracey case TM_DIFF:
1417 a596b957 2022-07-14 tracey diff_time = time(NULL) - committer_time;
1418 a596b957 2022-07-14 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1419 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1420 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 365), years) == -1)
1421 cb93ab40 2023-01-22 op return -1;
1422 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1423 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1424 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1425 a596b957 2022-07-14 tracey months) == -1)
1426 cb93ab40 2023-01-22 op return -1;
1427 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1428 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1429 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1430 cb93ab40 2023-01-22 op return -1;
1431 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1432 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1433 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24), days) == -1)
1434 cb93ab40 2023-01-22 op return -1;
1435 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 2) {
1436 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1437 a596b957 2022-07-14 tracey (diff_time / 60 / 60), hours) == -1)
1438 cb93ab40 2023-01-22 op return -1;
1439 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 2) {
1440 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1441 a596b957 2022-07-14 tracey minutes) == -1)
1442 cb93ab40 2023-01-22 op return -1;
1443 cb93ab40 2023-01-22 op } else if (diff_time > 2) {
1444 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s", diff_time,
1445 a596b957 2022-07-14 tracey seconds) == -1)
1446 cb93ab40 2023-01-22 op return -1;
1447 a596b957 2022-07-14 tracey } else {
1448 cb93ab40 2023-01-22 op if (fcgi_puts(tp, now) == -1)
1449 cb93ab40 2023-01-22 op return -1;
1450 a596b957 2022-07-14 tracey }
1451 a596b957 2022-07-14 tracey break;
1452 a596b957 2022-07-14 tracey case TM_LONG:
1453 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1454 cb93ab40 2023-01-22 op return -1;
1455 a596b957 2022-07-14 tracey
1456 a596b957 2022-07-14 tracey s = asctime_r(&tm, datebuf);
1457 a596b957 2022-07-14 tracey if (s == NULL)
1458 cb93ab40 2023-01-22 op return -1;
1459 a596b957 2022-07-14 tracey
1460 cb93ab40 2023-01-22 op if (fcgi_puts(tp, datebuf) == -1 ||
1461 cb93ab40 2023-01-22 op fcgi_puts(tp, " UTC") == -1)
1462 cb93ab40 2023-01-22 op return -1;
1463 a596b957 2022-07-14 tracey break;
1464 1abb18e1 2022-12-20 op case TM_RFC822:
1465 1abb18e1 2022-12-20 op if (gmtime_r(&committer_time, &tm) == NULL)
1466 cb93ab40 2023-01-22 op return -1;
1467 1abb18e1 2022-12-20 op
1468 1abb18e1 2022-12-20 op r = strftime(datebuf, sizeof(datebuf),
1469 1abb18e1 2022-12-20 op "%a, %d %b %Y %H:%M:%S GMT", &tm);
1470 1abb18e1 2022-12-20 op if (r == 0)
1471 cb93ab40 2023-01-22 op return -1;
1472 1abb18e1 2022-12-20 op
1473 cb93ab40 2023-01-22 op if (fcgi_puts(tp, datebuf) == -1)
1474 cb93ab40 2023-01-22 op return -1;
1475 1abb18e1 2022-12-20 op break;
1476 a596b957 2022-07-14 tracey }
1477 cb93ab40 2023-01-22 op return 0;
1478 b4c20a19 2022-07-15 naddy }