all repos — cgit @ 21dcf10386551a2eee3e552c3213bb14e535cbba

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