all repos — cgit @ git.aaoth.xyz

a hyperfast web frontend for git written in c

ui-shared.c (view raw)

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