all repos — cgit @ 82aadcfc51ab9560862b99bfe5833c17f102f0ac

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