X-Git-Url: https://git.cameronkatri.com/cgit.git/blobdiff_plain/fc5880fab8b9afc589aaadd1299c34a6ef2a3c2d..e976df27952ca1e450c1c3d420532ac9f5e3036b:/ui-shared.c diff --git a/ui-shared.c b/ui-shared.c index 1418010..015c52b 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -7,6 +7,8 @@ */ #include "cgit.h" +#include "cmd.h" +#include "html.h" const char cgit_doctype[] = "tm_hour, tm->tm_min, tm->tm_sec); } -static long ttl_seconds(long ttl) -{ - if (ttl<0) - return 60 * 60 * 24 * 365; - else - return ttl * 60; -} - void cgit_print_error(char *msg) { html("
"); @@ -40,18 +34,49 @@ void cgit_print_error(char *msg) html("
\n"); } +char *cgit_httpscheme() +{ + char *https; + + https = getenv("HTTPS"); + if (https != NULL && strcmp(https, "on") == 0) + return "https://"; + else + return "http://"; +} + +char *cgit_hosturl() +{ + char *host, *port; + + host = getenv("HTTP_HOST"); + if (host) { + host = xstrdup(host); + } else { + host = getenv("SERVER_NAME"); + if (!host) + return NULL; + port = getenv("SERVER_PORT"); + if (port && atoi(port) != 80) + host = xstrdup(fmt("%s:%d", host, atoi(port))); + else + host = xstrdup(host); + } + return host; +} + char *cgit_rooturl() { - if (cgit_virtual_root) - return fmt("%s/", cgit_virtual_root); + if (ctx.cfg.virtual_root) + return fmt("%s/", ctx.cfg.virtual_root); else - return cgit_script_name; + return ctx.cfg.script_name; } char *cgit_repourl(const char *reponame) { - if (cgit_virtual_root) { - return fmt("%s/%s/", cgit_virtual_root, reponame); + if (ctx.cfg.virtual_root) { + return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); } else { return fmt("?r=%s", reponame); } @@ -60,19 +85,21 @@ char *cgit_repourl(const char *reponame) char *cgit_fileurl(const char *reponame, const char *pagename, const char *filename, const char *query) { - if (cgit_virtual_root) { - if (query) - return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame, - pagename, filename?filename:"", query); - else - return fmt("%s/%s/%s/", cgit_virtual_root, reponame, - pagename); + char *tmp; + char *delim; + + if (ctx.cfg.virtual_root) { + tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, + pagename, (filename ? filename:"")); + delim = "?"; } else { - if (query) - return fmt("?r=%s&p=%s&%s", reponame, pagename, query); - else - return fmt("?r=%s&p=%s", reponame, pagename); + tmp = fmt("?url=%s/%s/%s", reponame, pagename, + (filename ? filename : "")); + delim = "&"; } + if (query) + tmp = fmt("%s%s%s", tmp, delim, query); + return tmp; } char *cgit_pageurl(const char *reponame, const char *pagename, @@ -108,14 +135,68 @@ const char *cgit_repobasename(const char *reponame) char *cgit_currurl() { - if (!cgit_virtual_root) - return cgit_script_name; - else if (cgit_query_page) - return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); - else if (cgit_query_repo) - return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); + if (!ctx.cfg.virtual_root) + return ctx.cfg.script_name; + else if (ctx.qry.page) + return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); + else if (ctx.qry.repo) + return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); else - return fmt("%s/", cgit_virtual_root); + return fmt("%s/", ctx.cfg.virtual_root); +} + +static void site_url(char *page, char *search, int ofs) +{ + char *delim = "?"; + + if (ctx.cfg.virtual_root) { + html_attr(ctx.cfg.virtual_root); + if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') + html("/"); + } else + html(ctx.cfg.script_name); + + if (page) { + htmlf("?p=%s", page); + delim = "&"; + } + if (search) { + html(delim); + html("q="); + html_attr(search); + delim = "&"; + } + if (ofs) { + html(delim); + htmlf("ofs=%d", ofs); + } +} + +static void site_link(char *page, char *name, char *title, char *class, + char *search, int ofs) +{ + html(""); + html_txt(name); + html(""); +} + +void cgit_index_link(char *name, char *title, char *class, char *pattern, + int ofs) +{ + site_link(NULL, name, title, class, pattern, ofs); } static char *repolink(char *title, char *class, char *page, char *head, @@ -135,37 +216,37 @@ static char *repolink(char *title, char *class, char *page, char *head, html("'"); } html(" href='"); - if (cgit_virtual_root) { - html_attr(cgit_virtual_root); - if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') + if (ctx.cfg.virtual_root) { + html_url_path(ctx.cfg.virtual_root); + if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') html("/"); - html_attr(cgit_repo->url); - if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') + html_url_path(ctx.repo->url); + if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') html("/"); if (page) { - html(page); + html_url_path(page); html("/"); if (path) - html_attr(path); + html_url_path(path); } } else { - html(cgit_script_name); + html(ctx.cfg.script_name); html("?url="); - html_attr(cgit_repo->url); - if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') + html_url_arg(ctx.repo->url); + if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') html("/"); if (page) { - html(page); + html_url_arg(page); html("/"); if (path) - html_attr(path); + html_url_arg(path); } delim = "&"; } - if (head && strcmp(head, cgit_repo->defbranch)) { + if (head && strcmp(head, ctx.repo->defbranch)) { html(delim); html("h="); - html_attr(head); + html_url_arg(head); delim = "&"; } return fmt("%s", delim); @@ -177,38 +258,70 @@ static void reporevlink(char *page, char *name, char *title, char *class, char *delim; delim = repolink(title, class, page, head, path); - if (rev && strcmp(rev, cgit_query_head)) { + if (rev && strcmp(rev, ctx.qry.head)) { html(delim); html("id="); - html_attr(rev); + html_url_arg(rev); } html("'>"); html_txt(name); html(""); } +void cgit_summary_link(char *name, char *title, char *class, char *head) +{ + reporevlink(NULL, name, title, class, head, NULL, NULL); +} + +void cgit_tag_link(char *name, char *title, char *class, char *head, + char *rev) +{ + reporevlink("tag", name, title, class, head, rev, NULL); +} + void cgit_tree_link(char *name, char *title, char *class, char *head, char *rev, char *path) { reporevlink("tree", name, title, class, head, rev, path); } +void cgit_plain_link(char *name, char *title, char *class, char *head, + char *rev, char *path) +{ + reporevlink("plain", name, title, class, head, rev, path); +} + void cgit_log_link(char *name, char *title, char *class, char *head, - char *rev, char *path, int ofs) + char *rev, char *path, int ofs, char *grep, char *pattern, + int showmsg) { char *delim; delim = repolink(title, class, "log", head, path); - if (rev && strcmp(rev, cgit_query_head)) { + if (rev && strcmp(rev, ctx.qry.head)) { html(delim); html("id="); - html_attr(rev); + html_url_arg(rev); delim = "&"; } + if (grep && pattern) { + html(delim); + html("qt="); + html_url_arg(grep); + delim = "&"; + html(delim); + html("q="); + html_url_arg(pattern); + } if (ofs > 0) { html(delim); html("ofs="); htmlf("%d", ofs); + delim = "&"; + } + if (showmsg) { + html(delim); + html("showmsg=1"); } html("'>"); html_txt(name); @@ -218,11 +331,11 @@ void cgit_log_link(char *name, char *title, char *class, char *head, void cgit_commit_link(char *name, char *title, char *class, char *head, char *rev) { - if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { - name[cgit_max_msg_len] = '\0'; - name[cgit_max_msg_len - 1] = '.'; - name[cgit_max_msg_len - 2] = '.'; - name[cgit_max_msg_len - 3] = '.'; + if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { + name[ctx.cfg.max_msg_len] = '\0'; + name[ctx.cfg.max_msg_len - 1] = '.'; + name[ctx.cfg.max_msg_len - 2] = '.'; + name[ctx.cfg.max_msg_len - 3] = '.'; } reporevlink("commit", name, title, class, head, rev, NULL); } @@ -245,55 +358,66 @@ void cgit_diff_link(char *name, char *title, char *class, char *head, char *delim; delim = repolink(title, class, "diff", head, path); - if (new_rev && strcmp(new_rev, cgit_query_head)) { + if (new_rev && strcmp(new_rev, ctx.qry.head)) { html(delim); html("id="); - html_attr(new_rev); + html_url_arg(new_rev); delim = "&"; } if (old_rev) { html(delim); html("id2="); - html_attr(old_rev); + html_url_arg(old_rev); } html("'>"); html_txt(name); html(""); } +void cgit_patch_link(char *name, char *title, char *class, char *head, + char *rev) +{ + reporevlink("patch", name, title, class, head, rev, NULL); +} + +void cgit_stats_link(char *name, char *title, char *class, char *head, + char *path) +{ + reporevlink("stats", name, title, class, head, NULL, path); +} + void cgit_object_link(struct object *obj) { - char *page, *arg, *url; + char *page, *shortrev, *fullrev, *name; + fullrev = sha1_to_hex(obj->sha1); + shortrev = xstrdup(fullrev); + shortrev[10] = '\0'; if (obj->type == OBJ_COMMIT) { - cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, - cgit_query_head, sha1_to_hex(obj->sha1)); + cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL, + ctx.qry.head, fullrev); return; - } else if (obj->type == OBJ_TREE) { + } else if (obj->type == OBJ_TREE) page = "tree"; - arg = "id"; - } else if (obj->type == OBJ_TAG) { + else if (obj->type == OBJ_TAG) page = "tag"; - arg = "id"; - } else { + else page = "blob"; - arg = "id"; - } - - url = cgit_pageurl(cgit_query_repo, page, - fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); - html_link_open(url, NULL, NULL); - htmlf("%s %s", typename(obj->type), - sha1_to_hex(obj->sha1)); - html_link_close(); + name = fmt("%s %s...", typename(obj->type), shortrev); + reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL); } -void cgit_print_date(time_t secs, char *format) +void cgit_print_date(time_t secs, char *format, int local_time) { char buf[64]; struct tm *time; - time = gmtime(&secs); + if (!secs) + return; + if(local_time) + time = localtime(&secs); + else + time = gmtime(&secs); strftime(buf, sizeof(buf)-1, format, time); html_txt(buf); } @@ -302,11 +426,13 @@ void cgit_print_age(time_t t, time_t max_relative, char *format) { time_t now, secs; + if (!t) + return; time(&now); secs = now - t; if (secs > max_relative && max_relative >= 0) { - cgit_print_date(t, format); + cgit_print_date(t, format, ctx.cfg.local_time); return; } @@ -339,109 +465,313 @@ void cgit_print_age(time_t t, time_t max_relative, char *format) secs * 1.0 / TM_YEAR); } -void cgit_print_docstart(char *title, struct cacheitem *item) +void cgit_print_http_headers(struct cgit_context *ctx) { - html("Content-Type: text/html; charset=utf-8\n"); - htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); - htmlf("Expires: %s\n", http_date(item->st.st_mtime + - ttl_seconds(item->ttl))); + const char *method = getenv("REQUEST_METHOD"); + + if (ctx->cfg.embedded) + return; + + if (ctx->page.status) + htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg); + if (ctx->page.mimetype && ctx->page.charset) + htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, + ctx->page.charset); + else if (ctx->page.mimetype) + htmlf("Content-Type: %s\n", ctx->page.mimetype); + if (ctx->page.size) + htmlf("Content-Length: %ld\n", ctx->page.size); + if (ctx->page.filename) + htmlf("Content-Disposition: inline; filename=\"%s\"\n", + ctx->page.filename); + htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); + htmlf("Expires: %s\n", http_date(ctx->page.expires)); + if (ctx->page.etag) + htmlf("ETag: \"%s\"\n", ctx->page.etag); html("\n"); + if (method && !strcmp(method, "HEAD")) + exit(0); +} + +void cgit_print_docstart(struct cgit_context *ctx) +{ + if (ctx->cfg.embedded) + return; + + char *host = cgit_hosturl(); html(cgit_doctype); - html("\n"); + html("\n"); html("\n"); html(""); - html_txt(title); + html_txt(ctx->page.title); html("\n"); htmlf("\n", cgit_version); + if (ctx->cfg.robots && *ctx->cfg.robots) + htmlf("\n", ctx->cfg.robots); html("\n"); + if (ctx->cfg.favicon) { + html("\n"); + } + if (host && ctx->repo) { + html("\n"); + } + if (ctx->cfg.head_include) + html_include(ctx->cfg.head_include); html("\n"); html("\n"); + if (ctx->cfg.header) + html_include(ctx->cfg.header); } void cgit_print_docend() { - html(""); + html(""); + if (ctx.cfg.footer) + html_include(ctx.cfg.footer); + else { + htmlf("\n"); + } + html(""); + if (ctx.cfg.embedded) + return; html("\n\n"); } -void cgit_print_pageheader(char *title, int show_search) +int print_branch_option(const char *refname, const unsigned char *sha1, + int flags, void *cb_data) { - html(""); - html(""); - html(""); - html("
"); - if (cgit_query_repo) { - html_txt(cgit_repo->name); - html(" ("); - html_txt(cgit_query_head); - html(") :  "); - reporevlink(NULL, "summary", NULL, NULL, cgit_query_head, - NULL, NULL); - html(" "); - cgit_log_link("log", NULL, NULL, cgit_query_head, - cgit_query_sha1, cgit_query_path, 0); - html(" "); - cgit_tree_link("tree", NULL, NULL, cgit_query_head, - cgit_query_sha1, NULL); - html(" "); - cgit_commit_link("commit", NULL, NULL, cgit_query_head, - cgit_query_sha1); - html(" "); - cgit_diff_link("diff", NULL, NULL, cgit_query_head, - cgit_query_sha1, cgit_query_sha2, - cgit_query_path); + char *name = (char *)refname; + html_option(name, name, ctx.qry.head); + return 0; +} + +int print_archive_ref(const char *refname, const unsigned char *sha1, + int flags, void *cb_data) +{ + struct tag *tag; + struct taginfo *info; + struct object *obj; + char buf[256], *url; + unsigned char fileid[20]; + int *header = (int *)cb_data; + + if (prefixcmp(refname, "refs/archives")) + return 0; + strncpy(buf, refname+14, sizeof(buf)); + obj = parse_object(sha1); + if (!obj) + return 1; + if (obj->type == OBJ_TAG) { + tag = lookup_tag(sha1); + if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) + return 0; + hashcpy(fileid, tag->tagged->sha1); + } else if (obj->type != OBJ_BLOB) { + return 0; } else { - html_txt("Index of repositories"); + hashcpy(fileid, sha1); } - html("