]> git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
ui-snapshot: add support for zstd compression
[cgit.git] / ui-shared.c
1 /* ui-shared.c: common web output functions
2 *
3 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "ui-shared.h"
11 #include "cmd.h"
12 #include "html.h"
13 #include "version.h"
14
15 static const char cgit_doctype[] =
16 "<!DOCTYPE html>\n";
17
18 static char *http_date(time_t t)
19 {
20 static char day[][4] =
21 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
22 static char month[][4] =
23 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
24 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
25 struct tm *tm = gmtime(&t);
26 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
27 tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
28 tm->tm_hour, tm->tm_min, tm->tm_sec);
29 }
30
31 void cgit_print_error(const char *fmt, ...)
32 {
33 va_list ap;
34 va_start(ap, fmt);
35 cgit_vprint_error(fmt, ap);
36 va_end(ap);
37 }
38
39 void cgit_vprint_error(const char *fmt, va_list ap)
40 {
41 va_list cp;
42 html("<div class='error'>");
43 va_copy(cp, ap);
44 html_vtxtf(fmt, cp);
45 va_end(cp);
46 html("</div>\n");
47 }
48
49 const char *cgit_httpscheme(void)
50 {
51 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
52 return "https://";
53 else
54 return "http://";
55 }
56
57 char *cgit_hosturl(void)
58 {
59 if (ctx.env.http_host)
60 return xstrdup(ctx.env.http_host);
61 if (!ctx.env.server_name)
62 return NULL;
63 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
64 return xstrdup(ctx.env.server_name);
65 return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
66 }
67
68 char *cgit_currenturl(void)
69 {
70 const char *root = cgit_rooturl();
71
72 if (!ctx.qry.url)
73 return xstrdup(root);
74 if (root[0] && root[strlen(root) - 1] == '/')
75 return fmtalloc("%s%s", root, ctx.qry.url);
76 return fmtalloc("%s/%s", root, ctx.qry.url);
77 }
78
79 char *cgit_currentfullurl(void)
80 {
81 const char *root = cgit_rooturl();
82 const char *orig_query = ctx.env.query_string ? ctx.env.query_string : "";
83 size_t len = strlen(orig_query);
84 char *query = xmalloc(len + 2), *start_url, *ret;
85
86 /* Remove all url=... parts from query string */
87 memcpy(query + 1, orig_query, len + 1);
88 query[0] = '?';
89 start_url = query;
90 while ((start_url = strstr(start_url, "url=")) != NULL) {
91 if (start_url[-1] == '?' || start_url[-1] == '&') {
92 const char *end_url = strchr(start_url, '&');
93 if (end_url)
94 memmove(start_url, end_url + 1, strlen(end_url));
95 else
96 start_url[0] = '\0';
97 } else
98 ++start_url;
99 }
100 if (!query[1])
101 query[0] = '\0';
102
103 if (!ctx.qry.url)
104 ret = fmtalloc("%s%s", root, query);
105 else if (root[0] && root[strlen(root) - 1] == '/')
106 ret = fmtalloc("%s%s%s", root, ctx.qry.url, query);
107 else
108 ret = fmtalloc("%s/%s%s", root, ctx.qry.url, query);
109 free(query);
110 return ret;
111 }
112
113 const char *cgit_rooturl(void)
114 {
115 if (ctx.cfg.virtual_root)
116 return ctx.cfg.virtual_root;
117 else
118 return ctx.cfg.script_name;
119 }
120
121 const char *cgit_loginurl(void)
122 {
123 static const char *login_url;
124 if (!login_url)
125 login_url = fmtalloc("%s?p=login", cgit_rooturl());
126 return login_url;
127 }
128
129 char *cgit_repourl(const char *reponame)
130 {
131 if (ctx.cfg.virtual_root)
132 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
133 else
134 return fmtalloc("?r=%s", reponame);
135 }
136
137 char *cgit_fileurl(const char *reponame, const char *pagename,
138 const char *filename, const char *query)
139 {
140 struct strbuf sb = STRBUF_INIT;
141 char *delim;
142
143 if (ctx.cfg.virtual_root) {
144 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
145 pagename, (filename ? filename:""));
146 delim = "?";
147 } else {
148 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
149 (filename ? filename : ""));
150 delim = "&amp;";
151 }
152 if (query)
153 strbuf_addf(&sb, "%s%s", delim, query);
154 return strbuf_detach(&sb, NULL);
155 }
156
157 char *cgit_pageurl(const char *reponame, const char *pagename,
158 const char *query)
159 {
160 return cgit_fileurl(reponame, pagename, NULL, query);
161 }
162
163 const char *cgit_repobasename(const char *reponame)
164 {
165 /* I assume we don't need to store more than one repo basename */
166 static char rvbuf[1024];
167 int p;
168 const char *rv;
169 size_t len;
170
171 len = strlcpy(rvbuf, reponame, sizeof(rvbuf));
172 if (len >= sizeof(rvbuf))
173 die("cgit_repobasename: truncated repository name '%s'", reponame);
174 p = len - 1;
175 /* strip trailing slashes */
176 while (p && rvbuf[p] == '/')
177 rvbuf[p--] = '\0';
178 /* strip trailing .git */
179 if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
180 p -= 3;
181 rvbuf[p--] = '\0';
182 }
183 /* strip more trailing slashes if any */
184 while (p && rvbuf[p] == '/')
185 rvbuf[p--] = '\0';
186 /* find last slash in the remaining string */
187 rv = strrchr(rvbuf, '/');
188 if (rv)
189 return ++rv;
190 return rvbuf;
191 }
192
193 const char *cgit_snapshot_prefix(const struct cgit_repo *repo)
194 {
195 if (repo->snapshot_prefix)
196 return repo->snapshot_prefix;
197
198 return cgit_repobasename(repo->url);
199 }
200
201 static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
202 {
203 char *delim = "?";
204
205 if (always_root || page)
206 html_attr(cgit_rooturl());
207 else {
208 char *currenturl = cgit_currenturl();
209 html_attr(currenturl);
210 free(currenturl);
211 }
212
213 if (page) {
214 htmlf("?p=%s", page);
215 delim = "&amp;";
216 }
217 if (search) {
218 html(delim);
219 html("q=");
220 html_attr(search);
221 delim = "&amp;";
222 }
223 if (sort) {
224 html(delim);
225 html("s=");
226 html_attr(sort);
227 delim = "&amp;";
228 }
229 if (ofs) {
230 html(delim);
231 htmlf("ofs=%d", ofs);
232 }
233 }
234
235 static void site_link(const char *page, const char *name, const char *title,
236 const char *class, const char *search, const char *sort, int ofs, int always_root)
237 {
238 html("<a");
239 if (title) {
240 html(" title='");
241 html_attr(title);
242 html("'");
243 }
244 if (class) {
245 html(" class='");
246 html_attr(class);
247 html("'");
248 }
249 html(" href='");
250 site_url(page, search, sort, ofs, always_root);
251 html("'>");
252 html_txt(name);
253 html("</a>");
254 }
255
256 void cgit_index_link(const char *name, const char *title, const char *class,
257 const char *pattern, const char *sort, int ofs, int always_root)
258 {
259 site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
260 }
261
262 static char *repolink(const char *title, const char *class, const char *page,
263 const char *head, const char *path)
264 {
265 char *delim = "?";
266
267 html("<a");
268 if (title) {
269 html(" title='");
270 html_attr(title);
271 html("'");
272 }
273 if (class) {
274 html(" class='");
275 html_attr(class);
276 html("'");
277 }
278 html(" href='");
279 if (ctx.cfg.virtual_root) {
280 html_url_path(ctx.cfg.virtual_root);
281 html_url_path(ctx.repo->url);
282 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
283 html("/");
284 if (page) {
285 html_url_path(page);
286 html("/");
287 if (path)
288 html_url_path(path);
289 }
290 } else {
291 html_url_path(ctx.cfg.script_name);
292 html("?url=");
293 html_url_arg(ctx.repo->url);
294 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
295 html("/");
296 if (page) {
297 html_url_arg(page);
298 html("/");
299 if (path)
300 html_url_arg(path);
301 }
302 delim = "&amp;";
303 }
304 if (head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch)) {
305 html(delim);
306 html("h=");
307 html_url_arg(head);
308 delim = "&amp;";
309 }
310 return fmt("%s", delim);
311 }
312
313 static void reporevlink(const char *page, const char *name, const char *title,
314 const char *class, const char *head, const char *rev,
315 const char *path)
316 {
317 char *delim;
318
319 delim = repolink(title, class, page, head, path);
320 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
321 html(delim);
322 html("id=");
323 html_url_arg(rev);
324 }
325 html("'>");
326 html_txt(name);
327 html("</a>");
328 }
329
330 void cgit_summary_link(const char *name, const char *title, const char *class,
331 const char *head)
332 {
333 reporevlink(NULL, name, title, class, head, NULL, NULL);
334 }
335
336 void cgit_tag_link(const char *name, const char *title, const char *class,
337 const char *tag)
338 {
339 reporevlink("tag", name, title, class, tag, NULL, NULL);
340 }
341
342 void cgit_tree_link(const char *name, const char *title, const char *class,
343 const char *head, const char *rev, const char *path)
344 {
345 reporevlink("tree", name, title, class, head, rev, path);
346 }
347
348 void cgit_plain_link(const char *name, const char *title, const char *class,
349 const char *head, const char *rev, const char *path)
350 {
351 reporevlink("plain", name, title, class, head, rev, path);
352 }
353
354 void cgit_blame_link(const char *name, const char *title, const char *class,
355 const char *head, const char *rev, const char *path)
356 {
357 reporevlink("blame", name, title, class, head, rev, path);
358 }
359
360 void cgit_log_link(const char *name, const char *title, const char *class,
361 const char *head, const char *rev, const char *path,
362 int ofs, const char *grep, const char *pattern, int showmsg,
363 int follow)
364 {
365 char *delim;
366
367 delim = repolink(title, class, "log", head, path);
368 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
369 html(delim);
370 html("id=");
371 html_url_arg(rev);
372 delim = "&amp;";
373 }
374 if (grep && pattern) {
375 html(delim);
376 html("qt=");
377 html_url_arg(grep);
378 delim = "&amp;";
379 html(delim);
380 html("q=");
381 html_url_arg(pattern);
382 }
383 if (ofs > 0) {
384 html(delim);
385 html("ofs=");
386 htmlf("%d", ofs);
387 delim = "&amp;";
388 }
389 if (showmsg) {
390 html(delim);
391 html("showmsg=1");
392 delim = "&amp;";
393 }
394 if (follow) {
395 html(delim);
396 html("follow=1");
397 }
398 html("'>");
399 html_txt(name);
400 html("</a>");
401 }
402
403 void cgit_commit_link(const char *name, const char *title, const char *class,
404 const char *head, const char *rev, const char *path)
405 {
406 char *delim;
407
408 delim = repolink(title, class, "commit", head, path);
409 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
410 html(delim);
411 html("id=");
412 html_url_arg(rev);
413 delim = "&amp;";
414 }
415 if (ctx.qry.difftype) {
416 html(delim);
417 htmlf("dt=%d", ctx.qry.difftype);
418 delim = "&amp;";
419 }
420 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
421 html(delim);
422 html("context=");
423 htmlf("%d", ctx.qry.context);
424 delim = "&amp;";
425 }
426 if (ctx.qry.ignorews) {
427 html(delim);
428 html("ignorews=1");
429 delim = "&amp;";
430 }
431 if (ctx.qry.follow) {
432 html(delim);
433 html("follow=1");
434 }
435 html("'>");
436 if (name[0] != '\0') {
437 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
438 html_ntxt(name, ctx.cfg.max_msg_len - 3);
439 html("...");
440 } else
441 html_txt(name);
442 } else
443 html_txt("(no commit message)");
444 html("</a>");
445 }
446
447 void cgit_refs_link(const char *name, const char *title, const char *class,
448 const char *head, const char *rev, const char *path)
449 {
450 reporevlink("refs", name, title, class, head, rev, path);
451 }
452
453 void cgit_snapshot_link(const char *name, const char *title, const char *class,
454 const char *head, const char *rev,
455 const char *archivename)
456 {
457 reporevlink("snapshot", name, title, class, head, rev, archivename);
458 }
459
460 void cgit_diff_link(const char *name, const char *title, const char *class,
461 const char *head, const char *new_rev, const char *old_rev,
462 const char *path)
463 {
464 char *delim;
465
466 delim = repolink(title, class, "diff", head, path);
467 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
468 html(delim);
469 html("id=");
470 html_url_arg(new_rev);
471 delim = "&amp;";
472 }
473 if (old_rev) {
474 html(delim);
475 html("id2=");
476 html_url_arg(old_rev);
477 delim = "&amp;";
478 }
479 if (ctx.qry.difftype) {
480 html(delim);
481 htmlf("dt=%d", ctx.qry.difftype);
482 delim = "&amp;";
483 }
484 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
485 html(delim);
486 html("context=");
487 htmlf("%d", ctx.qry.context);
488 delim = "&amp;";
489 }
490 if (ctx.qry.ignorews) {
491 html(delim);
492 html("ignorews=1");
493 delim = "&amp;";
494 }
495 if (ctx.qry.follow) {
496 html(delim);
497 html("follow=1");
498 }
499 html("'>");
500 html_txt(name);
501 html("</a>");
502 }
503
504 void cgit_patch_link(const char *name, const char *title, const char *class,
505 const char *head, const char *rev, const char *path)
506 {
507 reporevlink("patch", name, title, class, head, rev, path);
508 }
509
510 void cgit_stats_link(const char *name, const char *title, const char *class,
511 const char *head, const char *path)
512 {
513 reporevlink("stats", name, title, class, head, NULL, path);
514 }
515
516 static void cgit_self_link(char *name, const char *title, const char *class)
517 {
518 if (!strcmp(ctx.qry.page, "repolist"))
519 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
520 ctx.qry.ofs, 1);
521 else if (!strcmp(ctx.qry.page, "summary"))
522 cgit_summary_link(name, title, class, ctx.qry.head);
523 else if (!strcmp(ctx.qry.page, "tag"))
524 cgit_tag_link(name, title, class, ctx.qry.has_sha1 ?
525 ctx.qry.sha1 : ctx.qry.head);
526 else if (!strcmp(ctx.qry.page, "tree"))
527 cgit_tree_link(name, title, class, ctx.qry.head,
528 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
529 ctx.qry.path);
530 else if (!strcmp(ctx.qry.page, "plain"))
531 cgit_plain_link(name, title, class, ctx.qry.head,
532 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
533 ctx.qry.path);
534 else if (!strcmp(ctx.qry.page, "blame"))
535 cgit_blame_link(name, title, class, ctx.qry.head,
536 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
537 ctx.qry.path);
538 else if (!strcmp(ctx.qry.page, "log"))
539 cgit_log_link(name, title, class, ctx.qry.head,
540 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
541 ctx.qry.path, ctx.qry.ofs,
542 ctx.qry.grep, ctx.qry.search,
543 ctx.qry.showmsg, ctx.qry.follow);
544 else if (!strcmp(ctx.qry.page, "commit"))
545 cgit_commit_link(name, title, class, ctx.qry.head,
546 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
547 ctx.qry.path);
548 else if (!strcmp(ctx.qry.page, "patch"))
549 cgit_patch_link(name, title, class, ctx.qry.head,
550 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
551 ctx.qry.path);
552 else if (!strcmp(ctx.qry.page, "refs"))
553 cgit_refs_link(name, title, class, ctx.qry.head,
554 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
555 ctx.qry.path);
556 else if (!strcmp(ctx.qry.page, "snapshot"))
557 cgit_snapshot_link(name, title, class, ctx.qry.head,
558 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
559 ctx.qry.path);
560 else if (!strcmp(ctx.qry.page, "diff"))
561 cgit_diff_link(name, title, class, ctx.qry.head,
562 ctx.qry.sha1, ctx.qry.sha2,
563 ctx.qry.path);
564 else if (!strcmp(ctx.qry.page, "stats"))
565 cgit_stats_link(name, title, class, ctx.qry.head,
566 ctx.qry.path);
567 else {
568 /* Don't known how to make link for this page */
569 repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
570 html("><!-- cgit_self_link() doesn't know how to make link for page '");
571 html_txt(ctx.qry.page);
572 html("' -->");
573 html_txt(name);
574 html("</a>");
575 }
576 }
577
578 void cgit_object_link(struct object *obj)
579 {
580 char *page, *shortrev, *fullrev, *name;
581
582 fullrev = oid_to_hex(&obj->oid);
583 shortrev = xstrdup(fullrev);
584 shortrev[10] = '\0';
585 if (obj->type == OBJ_COMMIT) {
586 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
587 ctx.qry.head, fullrev, NULL);
588 return;
589 } else if (obj->type == OBJ_TREE)
590 page = "tree";
591 else if (obj->type == OBJ_TAG)
592 page = "tag";
593 else
594 page = "blob";
595 name = fmt("%s %s...", type_name(obj->type), shortrev);
596 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
597 }
598
599 static struct string_list_item *lookup_path(struct string_list *list,
600 const char *path)
601 {
602 struct string_list_item *item;
603
604 while (path && path[0]) {
605 if ((item = string_list_lookup(list, path)))
606 return item;
607 if (!(path = strchr(path, '/')))
608 break;
609 path++;
610 }
611 return NULL;
612 }
613
614 void cgit_submodule_link(const char *class, char *path, const char *rev)
615 {
616 struct string_list *list;
617 struct string_list_item *item;
618 char tail, *dir;
619 size_t len;
620
621 len = 0;
622 tail = 0;
623 list = &ctx.repo->submodules;
624 item = lookup_path(list, path);
625 if (!item) {
626 len = strlen(path);
627 tail = path[len - 1];
628 if (tail == '/') {
629 path[len - 1] = 0;
630 item = lookup_path(list, path);
631 }
632 }
633 if (item || ctx.repo->module_link) {
634 html("<a ");
635 if (class)
636 htmlf("class='%s' ", class);
637 html("href='");
638 if (item) {
639 html_attrf(item->util, rev);
640 } else {
641 dir = strrchr(path, '/');
642 if (dir)
643 dir++;
644 else
645 dir = path;
646 html_attrf(ctx.repo->module_link, dir, rev);
647 }
648 html("'>");
649 html_txt(path);
650 html("</a>");
651 } else {
652 html("<span");
653 if (class)
654 htmlf(" class='%s'", class);
655 html(">");
656 html_txt(path);
657 html("</span>");
658 }
659 html_txtf(" @ %.7s", rev);
660 if (item && tail)
661 path[len - 1] = tail;
662 }
663
664 const struct date_mode *cgit_date_mode(enum date_mode_type type)
665 {
666 static struct date_mode mode;
667 mode.type = type;
668 mode.local = ctx.cfg.local_time;
669 return &mode;
670 }
671
672 static void print_rel_date(time_t t, int tz, double value,
673 const char *class, const char *suffix)
674 {
675 htmlf("<span class='%s' title='", class);
676 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
677 htmlf("'>%.0f %s</span>", value, suffix);
678 }
679
680 void cgit_print_age(time_t t, int tz, time_t max_relative)
681 {
682 time_t now, secs;
683
684 if (!t)
685 return;
686 time(&now);
687 secs = now - t;
688 if (secs < 0)
689 secs = 0;
690
691 if (secs > max_relative && max_relative >= 0) {
692 html("<span title='");
693 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
694 html("'>");
695 html_txt(show_date(t, tz, cgit_date_mode(DATE_SHORT)));
696 html("</span>");
697 return;
698 }
699
700 if (secs < TM_HOUR * 2) {
701 print_rel_date(t, tz, secs * 1.0 / TM_MIN, "age-mins", "min.");
702 return;
703 }
704 if (secs < TM_DAY * 2) {
705 print_rel_date(t, tz, secs * 1.0 / TM_HOUR, "age-hours", "hours");
706 return;
707 }
708 if (secs < TM_WEEK * 2) {
709 print_rel_date(t, tz, secs * 1.0 / TM_DAY, "age-days", "days");
710 return;
711 }
712 if (secs < TM_MONTH * 2) {
713 print_rel_date(t, tz, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
714 return;
715 }
716 if (secs < TM_YEAR * 2) {
717 print_rel_date(t, tz, secs * 1.0 / TM_MONTH, "age-months", "months");
718 return;
719 }
720 print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years");
721 }
722
723 void cgit_print_http_headers(void)
724 {
725 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
726 return;
727
728 if (ctx.page.status)
729 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
730 if (ctx.page.mimetype && ctx.page.charset)
731 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
732 ctx.page.charset);
733 else if (ctx.page.mimetype)
734 htmlf("Content-Type: %s\n", ctx.page.mimetype);
735 if (ctx.page.size)
736 htmlf("Content-Length: %zd\n", ctx.page.size);
737 if (ctx.page.filename) {
738 html("Content-Disposition: inline; filename=\"");
739 html_header_arg_in_quotes(ctx.page.filename);
740 html("\"\n");
741 }
742 if (!ctx.env.authenticated)
743 html("Cache-Control: no-cache, no-store\n");
744 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
745 htmlf("Expires: %s\n", http_date(ctx.page.expires));
746 if (ctx.page.etag)
747 htmlf("ETag: \"%s\"\n", ctx.page.etag);
748 html("\n");
749 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
750 exit(0);
751 }
752
753 void cgit_redirect(const char *url, bool permanent)
754 {
755 htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
756 html("Location: ");
757 html_url_path(url);
758 html("\n\n");
759 }
760
761 static void print_rel_vcs_link(const char *url)
762 {
763 html("<link rel='vcs-git' href='");
764 html_attr(url);
765 html("' title='");
766 html_attr(ctx.repo->name);
767 html(" Git repository'/>\n");
768 }
769
770 void cgit_print_docstart(void)
771 {
772 char *host = cgit_hosturl();
773
774 if (ctx.cfg.embedded) {
775 if (ctx.cfg.header)
776 html_include(ctx.cfg.header);
777 return;
778 }
779
780 html(cgit_doctype);
781 html("<html lang='en'>\n");
782 html("<head>\n");
783 html("<title>");
784 html_txt(ctx.page.title);
785 html("</title>\n");
786 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
787 if (ctx.cfg.robots && *ctx.cfg.robots)
788 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
789 html("<link rel='stylesheet' type='text/css' href='");
790 html_attr(ctx.cfg.css);
791 html("'/>\n");
792 if (ctx.cfg.favicon) {
793 html("<link rel='shortcut icon' href='");
794 html_attr(ctx.cfg.favicon);
795 html("'/>\n");
796 }
797 if (host && ctx.repo && ctx.qry.head) {
798 char *fileurl;
799 struct strbuf sb = STRBUF_INIT;
800 strbuf_addf(&sb, "h=%s", ctx.qry.head);
801
802 html("<link rel='alternate' title='Atom feed' href='");
803 html(cgit_httpscheme());
804 html_attr(host);
805 fileurl = cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
806 sb.buf);
807 html_attr(fileurl);
808 html("' type='application/atom+xml'/>\n");
809 strbuf_release(&sb);
810 free(fileurl);
811 }
812 if (ctx.repo)
813 cgit_add_clone_urls(print_rel_vcs_link);
814 if (ctx.cfg.head_include)
815 html_include(ctx.cfg.head_include);
816 if (ctx.repo && ctx.repo->extra_head_content)
817 html(ctx.repo->extra_head_content);
818 html("</head>\n");
819 html("<body>\n");
820 if (ctx.cfg.header)
821 html_include(ctx.cfg.header);
822 free(host);
823 }
824
825 void cgit_print_docend(void)
826 {
827 html("</div> <!-- class=content -->\n");
828 if (ctx.cfg.embedded) {
829 html("</div> <!-- id=cgit -->\n");
830 if (ctx.cfg.footer)
831 html_include(ctx.cfg.footer);
832 return;
833 }
834 if (ctx.cfg.footer)
835 html_include(ctx.cfg.footer);
836 else {
837 htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
838 "(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version, git_version_string);
839 html_txt(show_date(time(NULL), 0, cgit_date_mode(DATE_ISO8601)));
840 html("</div>\n");
841 }
842 html("</div> <!-- id=cgit -->\n");
843 html("</body>\n</html>\n");
844 }
845
846 void cgit_print_error_page(int code, const char *msg, const char *fmt, ...)
847 {
848 va_list ap;
849 ctx.page.expires = ctx.cfg.cache_dynamic_ttl;
850 ctx.page.status = code;
851 ctx.page.statusmsg = msg;
852 cgit_print_layout_start();
853 va_start(ap, fmt);
854 cgit_vprint_error(fmt, ap);
855 va_end(ap);
856 cgit_print_layout_end();
857 }
858
859 void cgit_print_layout_start(void)
860 {
861 cgit_print_http_headers();
862 cgit_print_docstart();
863 cgit_print_pageheader();
864 }
865
866 void cgit_print_layout_end(void)
867 {
868 cgit_print_docend();
869 }
870
871 static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
872 {
873 struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
874 int i;
875
876 for (i = 0; url_list[i]; i++) {
877 strbuf_rtrim(url_list[i]);
878 if (url_list[i]->len == 0)
879 continue;
880 if (suffix && *suffix)
881 strbuf_addf(url_list[i], "/%s", suffix);
882 fn(url_list[i]->buf);
883 }
884
885 strbuf_list_free(url_list);
886 }
887
888 void cgit_add_clone_urls(void (*fn)(const char *))
889 {
890 if (ctx.repo->clone_url)
891 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
892 else if (ctx.cfg.clone_prefix)
893 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
894 }
895
896 static int print_branch_option(const char *refname, const struct object_id *oid,
897 int flags, void *cb_data)
898 {
899 char *name = (char *)refname;
900 html_option(name, name, ctx.qry.head);
901 return 0;
902 }
903
904 void cgit_add_hidden_formfields(int incl_head, int incl_search,
905 const char *page)
906 {
907 if (!ctx.cfg.virtual_root) {
908 struct strbuf url = STRBUF_INIT;
909
910 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
911 if (ctx.qry.vpath)
912 strbuf_addf(&url, "/%s", ctx.qry.vpath);
913 html_hidden("url", url.buf);
914 strbuf_release(&url);
915 }
916
917 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
918 strcmp(ctx.qry.head, ctx.repo->defbranch))
919 html_hidden("h", ctx.qry.head);
920
921 if (ctx.qry.sha1)
922 html_hidden("id", ctx.qry.sha1);
923 if (ctx.qry.sha2)
924 html_hidden("id2", ctx.qry.sha2);
925 if (ctx.qry.showmsg)
926 html_hidden("showmsg", "1");
927
928 if (incl_search) {
929 if (ctx.qry.grep)
930 html_hidden("qt", ctx.qry.grep);
931 if (ctx.qry.search)
932 html_hidden("q", ctx.qry.search);
933 }
934 }
935
936 static const char *hc(const char *page)
937 {
938 if (!ctx.qry.page)
939 return NULL;
940
941 return strcmp(ctx.qry.page, page) ? NULL : "active";
942 }
943
944 static void cgit_print_path_crumbs(char *path)
945 {
946 char *old_path = ctx.qry.path;
947 char *p = path, *q, *end = path + strlen(path);
948 int levels = 0;
949
950 ctx.qry.path = NULL;
951 cgit_self_link("root", NULL, NULL);
952 ctx.qry.path = p = path;
953 while (p < end) {
954 if (!(q = strchr(p, '/')) || levels > 15)
955 q = end;
956 *q = '\0';
957 html_txt("/");
958 cgit_self_link(p, NULL, NULL);
959 if (q < end)
960 *q = '/';
961 p = q + 1;
962 ++levels;
963 }
964 ctx.qry.path = old_path;
965 }
966
967 static void print_header(void)
968 {
969 char *logo = NULL, *logo_link = NULL;
970
971 html("<table id='header'>\n");
972 html("<tr>\n");
973
974 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
975 logo = ctx.repo->logo;
976 else
977 logo = ctx.cfg.logo;
978 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
979 logo_link = ctx.repo->logo_link;
980 else
981 logo_link = ctx.cfg.logo_link;
982 if (logo && *logo) {
983 html("<td class='logo' rowspan='2'><a href='");
984 if (logo_link && *logo_link)
985 html_attr(logo_link);
986 else
987 html_attr(cgit_rooturl());
988 html("'><img src='");
989 html_attr(logo);
990 html("' alt='cgit logo'/></a></td>\n");
991 }
992
993 html("<td class='main'>");
994 if (ctx.repo) {
995 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
996 html(" : ");
997 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
998 if (ctx.env.authenticated) {
999 html("</td><td class='form'>");
1000 html("<form method='get'>\n");
1001 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
1002 html("<select name='h' onchange='this.form.submit();'>\n");
1003 for_each_branch_ref(print_branch_option, ctx.qry.head);
1004 if (ctx.repo->enable_remote_branches)
1005 for_each_remote_ref(print_branch_option, ctx.qry.head);
1006 html("</select> ");
1007 html("<input type='submit' value='switch'/>");
1008 html("</form>");
1009 }
1010 } else
1011 html_txt(ctx.cfg.root_title);
1012 html("</td></tr>\n");
1013
1014 html("<tr><td class='sub'>");
1015 if (ctx.repo) {
1016 html_txt(ctx.repo->desc);
1017 html("</td><td class='sub right'>");
1018 html_txt(ctx.repo->owner);
1019 } else {
1020 if (ctx.cfg.root_desc)
1021 html_txt(ctx.cfg.root_desc);
1022 }
1023 html("</td></tr></table>\n");
1024 }
1025
1026 void cgit_print_pageheader(void)
1027 {
1028 html("<div id='cgit'>");
1029 if (!ctx.env.authenticated || !ctx.cfg.noheader)
1030 print_header();
1031
1032 html("<table class='tabs'><tr><td>\n");
1033 if (ctx.env.authenticated && ctx.repo) {
1034 if (ctx.repo->readme.nr)
1035 reporevlink("about", "about", NULL,
1036 hc("about"), ctx.qry.head, NULL,
1037 NULL);
1038 cgit_summary_link("summary", NULL, hc("summary"),
1039 ctx.qry.head);
1040 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
1041 ctx.qry.sha1, NULL);
1042 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
1043 NULL, ctx.qry.vpath, 0, NULL, NULL,
1044 ctx.qry.showmsg, ctx.qry.follow);
1045 if (ctx.qry.page && !strcmp(ctx.qry.page, "blame"))
1046 cgit_blame_link("blame", NULL, hc("blame"), ctx.qry.head,
1047 ctx.qry.sha1, ctx.qry.vpath);
1048 else
1049 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
1050 ctx.qry.sha1, ctx.qry.vpath);
1051 cgit_commit_link("commit", NULL, hc("commit"),
1052 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath);
1053 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
1054 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath);
1055 if (ctx.repo->max_stats)
1056 cgit_stats_link("stats", NULL, hc("stats"),
1057 ctx.qry.head, ctx.qry.vpath);
1058 if (ctx.repo->homepage) {
1059 html("<a href='");
1060 html_attr(ctx.repo->homepage);
1061 html("'>homepage</a>");
1062 }
1063 html("</td><td class='form'>");
1064 html("<form class='right' method='get' action='");
1065 if (ctx.cfg.virtual_root) {
1066 char *fileurl = cgit_fileurl(ctx.qry.repo, "log",
1067 ctx.qry.vpath, NULL);
1068 html_url_path(fileurl);
1069 free(fileurl);
1070 }
1071 html("'>\n");
1072 cgit_add_hidden_formfields(1, 0, "log");
1073 html("<select name='qt'>\n");
1074 html_option("grep", "log msg", ctx.qry.grep);
1075 html_option("author", "author", ctx.qry.grep);
1076 html_option("committer", "committer", ctx.qry.grep);
1077 html_option("range", "range", ctx.qry.grep);
1078 html("</select>\n");
1079 html("<input class='txt' type='search' size='10' name='q' value='");
1080 html_attr(ctx.qry.search);
1081 html("'/>\n");
1082 html("<input type='submit' value='search'/>\n");
1083 html("</form>\n");
1084 } else if (ctx.env.authenticated) {
1085 char *currenturl = cgit_currenturl();
1086 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
1087 if (ctx.cfg.root_readme)
1088 site_link("about", "about", NULL, hc("about"),
1089 NULL, NULL, 0, 1);
1090 html("</td><td class='form'>");
1091 html("<form method='get' action='");
1092 html_attr(currenturl);
1093 html("'>\n");
1094 html("<input type='search' name='q' size='10' value='");
1095 html_attr(ctx.qry.search);
1096 html("'/>\n");
1097 html("<input type='submit' value='search'/>\n");
1098 html("</form>");
1099 free(currenturl);
1100 }
1101 html("</td></tr></table>\n");
1102 if (ctx.env.authenticated && ctx.repo && ctx.qry.vpath) {
1103 html("<div class='path'>");
1104 html("path: ");
1105 cgit_print_path_crumbs(ctx.qry.vpath);
1106 if (ctx.cfg.enable_follow_links && !strcmp(ctx.qry.page, "log")) {
1107 html(" (");
1108 ctx.qry.follow = !ctx.qry.follow;
1109 cgit_self_link(ctx.qry.follow ? "follow" : "unfollow",
1110 NULL, NULL);
1111 ctx.qry.follow = !ctx.qry.follow;
1112 html(")");
1113 }
1114 html("</div>");
1115 }
1116 html("<div class='content'>");
1117 }
1118
1119 void cgit_print_filemode(unsigned short mode)
1120 {
1121 if (S_ISDIR(mode))
1122 html("d");
1123 else if (S_ISLNK(mode))
1124 html("l");
1125 else if (S_ISGITLINK(mode))
1126 html("m");
1127 else
1128 html("-");
1129 html_fileperm(mode >> 6);
1130 html_fileperm(mode >> 3);
1131 html_fileperm(mode);
1132 }
1133
1134 void cgit_compose_snapshot_prefix(struct strbuf *filename, const char *base,
1135 const char *ref)
1136 {
1137 struct object_id oid;
1138
1139 /*
1140 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1141 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1142 * i.e. each stripped tag can be inverted without ambiguities.
1143 */
1144 if (get_oid(fmt("refs/tags/%s", ref), &oid) == 0 &&
1145 (ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]) &&
1146 ((get_oid(fmt("refs/tags/%s", ref + 1), &oid) == 0) +
1147 (get_oid(fmt("refs/tags/v%s", ref + 1), &oid) == 0) +
1148 (get_oid(fmt("refs/tags/V%s", ref + 1), &oid) == 0) == 1))
1149 ref++;
1150
1151 strbuf_addf(filename, "%s-%s", base, ref);
1152 }
1153
1154 void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
1155 const char *separator)
1156 {
1157 const struct cgit_snapshot_format *f;
1158 struct strbuf filename = STRBUF_INIT;
1159 const char *basename;
1160 size_t prefixlen;
1161
1162 basename = cgit_snapshot_prefix(repo);
1163 if (starts_with(ref, basename))
1164 strbuf_addstr(&filename, ref);
1165 else
1166 cgit_compose_snapshot_prefix(&filename, basename, ref);
1167
1168 prefixlen = filename.len;
1169 for (f = cgit_snapshot_formats; f->suffix; f++) {
1170 if (!(repo->snapshots & cgit_snapshot_format_bit(f)))
1171 continue;
1172 strbuf_setlen(&filename, prefixlen);
1173 strbuf_addstr(&filename, f->suffix);
1174 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1175 filename.buf);
1176 if (cgit_snapshot_get_sig(ref, f)) {
1177 strbuf_addstr(&filename, ".asc");
1178 html(" (");
1179 cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1180 filename.buf);
1181 html(")");
1182 } else if (starts_with(f->suffix, ".tar") && cgit_snapshot_get_sig(ref, &cgit_snapshot_formats[0])) {
1183 strbuf_setlen(&filename, strlen(filename.buf) - strlen(f->suffix));
1184 strbuf_addstr(&filename, ".tar.asc");
1185 html(" (");
1186 cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1187 filename.buf);
1188 html(")");
1189 }
1190 html(separator);
1191 }
1192 strbuf_release(&filename);
1193 }
1194
1195 void cgit_set_title_from_path(const char *path)
1196 {
1197 struct strbuf sb = STRBUF_INIT;
1198 const char *slash, *last_slash;
1199
1200 if (!path)
1201 return;
1202
1203 for (last_slash = path + strlen(path); (slash = memrchr(path, '/', last_slash - path)) != NULL; last_slash = slash) {
1204 strbuf_add(&sb, slash + 1, last_slash - slash - 1);
1205 strbuf_addstr(&sb, " \xc2\xab ");
1206 }
1207 strbuf_add(&sb, path, last_slash - path);
1208 strbuf_addf(&sb, " - %s", ctx.page.title);
1209 ctx.page.title = strbuf_detach(&sb, NULL);
1210 }