summaryrefslogtreecommitdiffabout
path: root/ui-shared.c
Unidiff
Diffstat (limited to 'ui-shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-shared.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/ui-shared.c b/ui-shared.c
index 2630f23..29036d0 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,84 +1,95 @@
1/* ui-shared.c: common web output functions 1/* ui-shared.c: common web output functions
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cmd.h" 10#include "cmd.h"
11#include "html.h" 11#include "html.h"
12 12
13const char cgit_doctype[] = 13const char cgit_doctype[] =
14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16 16
17static char *http_date(time_t t) 17static char *http_date(time_t t)
18{ 18{
19 static char day[][4] = 19 static char day[][4] =
20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
21 static char month[][4] = 21 static char month[][4] =
22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
24 struct tm *tm = gmtime(&t); 24 struct tm *tm = gmtime(&t);
25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
27 tm->tm_hour, tm->tm_min, tm->tm_sec); 27 tm->tm_hour, tm->tm_min, tm->tm_sec);
28} 28}
29 29
30void cgit_print_error(char *msg) 30void cgit_print_error(char *msg)
31{ 31{
32 html("<div class='error'>"); 32 html("<div class='error'>");
33 html_txt(msg); 33 html_txt(msg);
34 html("</div>\n"); 34 html("</div>\n");
35} 35}
36 36
37char *cgit_httpscheme()
38{
39 char *https;
40
41 https = getenv("HTTPS");
42 if (https != NULL && strcmp(https, "on") == 0)
43 return "https://";
44 else
45 return "http://";
46}
47
37char *cgit_hosturl() 48char *cgit_hosturl()
38{ 49{
39 char *host, *port; 50 char *host, *port;
40 51
41 host = getenv("HTTP_HOST"); 52 host = getenv("HTTP_HOST");
42 if (host) { 53 if (host) {
43 host = xstrdup(host); 54 host = xstrdup(host);
44 } else { 55 } else {
45 host = getenv("SERVER_NAME"); 56 host = getenv("SERVER_NAME");
46 if (!host) 57 if (!host)
47 return NULL; 58 return NULL;
48 port = getenv("SERVER_PORT"); 59 port = getenv("SERVER_PORT");
49 if (port && atoi(port) != 80) 60 if (port && atoi(port) != 80)
50 host = xstrdup(fmt("%s:%d", host, atoi(port))); 61 host = xstrdup(fmt("%s:%d", host, atoi(port)));
51 else 62 else
52 host = xstrdup(host); 63 host = xstrdup(host);
53 } 64 }
54 return host; 65 return host;
55} 66}
56 67
57char *cgit_rooturl() 68char *cgit_rooturl()
58{ 69{
59 if (ctx.cfg.virtual_root) 70 if (ctx.cfg.virtual_root)
60 return fmt("%s/", ctx.cfg.virtual_root); 71 return fmt("%s/", ctx.cfg.virtual_root);
61 else 72 else
62 return ctx.cfg.script_name; 73 return ctx.cfg.script_name;
63} 74}
64 75
65char *cgit_repourl(const char *reponame) 76char *cgit_repourl(const char *reponame)
66{ 77{
67 if (ctx.cfg.virtual_root) { 78 if (ctx.cfg.virtual_root) {
68 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); 79 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
69 } else { 80 } else {
70 return fmt("?r=%s", reponame); 81 return fmt("?r=%s", reponame);
71 } 82 }
72} 83}
73 84
74char *cgit_fileurl(const char *reponame, const char *pagename, 85char *cgit_fileurl(const char *reponame, const char *pagename,
75 const char *filename, const char *query) 86 const char *filename, const char *query)
76{ 87{
77 char *tmp; 88 char *tmp;
78 char *delim; 89 char *delim;
79 90
80 if (ctx.cfg.virtual_root) { 91 if (ctx.cfg.virtual_root) {
81 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, 92 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
82 pagename, (filename ? filename:"")); 93 pagename, (filename ? filename:""));
83 delim = "?"; 94 delim = "?";
84 } else { 95 } else {
@@ -449,97 +460,98 @@ void cgit_print_age(time_t t, time_t max_relative, char *format)
449 htmlf("<span class='age-months'>%.0f months</span>", 460 htmlf("<span class='age-months'>%.0f months</span>",
450 secs * 1.0 / TM_MONTH); 461 secs * 1.0 / TM_MONTH);
451 return; 462 return;
452 } 463 }
453 htmlf("<span class='age-years'>%.0f years</span>", 464 htmlf("<span class='age-years'>%.0f years</span>",
454 secs * 1.0 / TM_YEAR); 465 secs * 1.0 / TM_YEAR);
455} 466}
456 467
457void cgit_print_http_headers(struct cgit_context *ctx) 468void cgit_print_http_headers(struct cgit_context *ctx)
458{ 469{
459 if (ctx->page.status) 470 if (ctx->page.status)
460 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg); 471 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg);
461 if (ctx->page.mimetype && ctx->page.charset) 472 if (ctx->page.mimetype && ctx->page.charset)
462 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, 473 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
463 ctx->page.charset); 474 ctx->page.charset);
464 else if (ctx->page.mimetype) 475 else if (ctx->page.mimetype)
465 htmlf("Content-Type: %s\n", ctx->page.mimetype); 476 htmlf("Content-Type: %s\n", ctx->page.mimetype);
466 if (ctx->page.size) 477 if (ctx->page.size)
467 htmlf("Content-Length: %ld\n", ctx->page.size); 478 htmlf("Content-Length: %ld\n", ctx->page.size);
468 if (ctx->page.filename) 479 if (ctx->page.filename)
469 htmlf("Content-Disposition: inline; filename=\"%s\"\n", 480 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
470 ctx->page.filename); 481 ctx->page.filename);
471 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); 482 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
472 htmlf("Expires: %s\n", http_date(ctx->page.expires)); 483 htmlf("Expires: %s\n", http_date(ctx->page.expires));
473 html("\n"); 484 html("\n");
474} 485}
475 486
476void cgit_print_docstart(struct cgit_context *ctx) 487void cgit_print_docstart(struct cgit_context *ctx)
477{ 488{
478 char *host = cgit_hosturl(); 489 char *host = cgit_hosturl();
479 html(cgit_doctype); 490 html(cgit_doctype);
480 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 491 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
481 html("<head>\n"); 492 html("<head>\n");
482 html("<title>"); 493 html("<title>");
483 html_txt(ctx->page.title); 494 html_txt(ctx->page.title);
484 html("</title>\n"); 495 html("</title>\n");
485 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 496 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
486 if (ctx->cfg.robots && *ctx->cfg.robots) 497 if (ctx->cfg.robots && *ctx->cfg.robots)
487 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 498 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
488 html("<link rel='stylesheet' type='text/css' href='"); 499 html("<link rel='stylesheet' type='text/css' href='");
489 html_attr(ctx->cfg.css); 500 html_attr(ctx->cfg.css);
490 html("'/>\n"); 501 html("'/>\n");
491 if (ctx->cfg.favicon) { 502 if (ctx->cfg.favicon) {
492 html("<link rel='shortcut icon' href='"); 503 html("<link rel='shortcut icon' href='");
493 html_attr(ctx->cfg.favicon); 504 html_attr(ctx->cfg.favicon);
494 html("'/>\n"); 505 html("'/>\n");
495 } 506 }
496 if (host && ctx->repo) { 507 if (host && ctx->repo) {
497 html("<link rel='alternate' title='Atom feed' href='http://"); 508 html("<link rel='alternate' title='Atom feed' href='");
509 html(cgit_httpscheme());
498 html_attr(cgit_hosturl()); 510 html_attr(cgit_hosturl());
499 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path, 511 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path,
500 fmt("h=%s", ctx->qry.head))); 512 fmt("h=%s", ctx->qry.head)));
501 html("' type='application/atom+xml'/>"); 513 html("' type='application/atom+xml'/>");
502 } 514 }
503 html("</head>\n"); 515 html("</head>\n");
504 html("<body>\n"); 516 html("<body>\n");
505 if (ctx->cfg.header) 517 if (ctx->cfg.header)
506 html_include(ctx->cfg.header); 518 html_include(ctx->cfg.header);
507} 519}
508 520
509void cgit_print_docend() 521void cgit_print_docend()
510{ 522{
511 html("</div>"); 523 html("</div>");
512 if (ctx.cfg.footer) 524 if (ctx.cfg.footer)
513 html_include(ctx.cfg.footer); 525 html_include(ctx.cfg.footer);
514 else { 526 else {
515 htmlf("<div class='footer'>generated by cgit %s at ", 527 htmlf("<div class='footer'>generated by cgit %s at ",
516 cgit_version); 528 cgit_version);
517 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time); 529 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
518 html("</div>\n"); 530 html("</div>\n");
519 } 531 }
520 html("</body>\n</html>\n"); 532 html("</body>\n</html>\n");
521} 533}
522 534
523int print_branch_option(const char *refname, const unsigned char *sha1, 535int print_branch_option(const char *refname, const unsigned char *sha1,
524 int flags, void *cb_data) 536 int flags, void *cb_data)
525{ 537{
526 char *name = (char *)refname; 538 char *name = (char *)refname;
527 html_option(name, name, ctx.qry.head); 539 html_option(name, name, ctx.qry.head);
528 return 0; 540 return 0;
529} 541}
530 542
531int print_archive_ref(const char *refname, const unsigned char *sha1, 543int print_archive_ref(const char *refname, const unsigned char *sha1,
532 int flags, void *cb_data) 544 int flags, void *cb_data)
533{ 545{
534 struct tag *tag; 546 struct tag *tag;
535 struct taginfo *info; 547 struct taginfo *info;
536 struct object *obj; 548 struct object *obj;
537 char buf[256], *url; 549 char buf[256], *url;
538 unsigned char fileid[20]; 550 unsigned char fileid[20];
539 int *header = (int *)cb_data; 551 int *header = (int *)cb_data;
540 552
541 if (prefixcmp(refname, "refs/archives")) 553 if (prefixcmp(refname, "refs/archives"))
542 return 0; 554 return 0;
543 strncpy(buf, refname+14, sizeof(buf)); 555 strncpy(buf, refname+14, sizeof(buf));
544 obj = parse_object(sha1); 556 obj = parse_object(sha1);
545 if (!obj) 557 if (!obj)