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