/* ui-shared.c: common web output functions
*
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
*/
#include "cgit.h"
#include "ui-shared.h"
#include "cmd.h"
#include "html.h"
static const char cgit_doctype[] =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
static char *http_date(time_t t)
{
static char day[][4] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static char month[][4] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct tm *tm = gmtime(&t);
return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
void cgit_print_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
cgit_vprint_error(fmt, ap);
va_end(ap);
}
void cgit_vprint_error(const char *fmt, va_list ap)
{
va_list cp;
html("<div class='error'>");
va_copy(cp, ap);
html_vtxtf(fmt, cp);
va_end(cp);
html("</div>\n");
}
const char *cgit_httpscheme(void)
{
if (ctx.env.https && !strcmp(ctx.env.https, "on"))
return "https://";
else
return "http://";
}
char *cgit_hosturl(void)
{
if (ctx.env.http_host)
return xstrdup(ctx.env.http_host);
if (!ctx.env.server_name)
return NULL;
if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
return xstrdup(ctx.env.server_name);
return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
}
char *cgit_currenturl(void)
{
if (!ctx.qry.url)
return xstrdup(cgit_rooturl());
const char *root = cgit_rooturl();
size_t len = strlen(root);
if (len && root[len - 1] == '/')
return fmtalloc("%s%s", root, ctx.qry.url);
return fmtalloc("%s/%s", root, ctx.qry.url);
}
const char *cgit_rooturl(void)
{
if (ctx.cfg.virtual_root)
return ctx.cfg.virtual_root;
else
return ctx.cfg.script_name;
}
const char *cgit_loginurl(void)
{
static const char *login_url;
if (!login_url)
login_url = fmtalloc("%s?p=login", cgit_rooturl());
return login_url;
}
char *cgit_repourl(const char *reponame)
{
if (ctx.cfg.virtual_root)
return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
else
return fmtalloc("?r=%s", reponame);
}
char *cgit_fileurl(const char *reponame, const char *pagename,
const char *filename, const char *query)
{
struct strbuf sb = STRBUF_INIT;
char *