-rw-r--r-- | ui-shared.c | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/ui-shared.c b/ui-shared.c index 015c52b..538ddda 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -15,75 +15,63 @@ const char cgit_doctype[] = | |||
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 | ||
17 | static char *http_date(time_t t) | 17 | static 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 | ||
30 | void cgit_print_error(char *msg) | 30 | void 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 | ||
37 | char *cgit_httpscheme() | 37 | char *cgit_httpscheme() |
38 | { | 38 | { |
39 | char *https; | 39 | if (ctx.env.https && !strcmp(ctx.env.https, "on")) |
40 | |||
41 | https = getenv("HTTPS"); | ||
42 | if (https != NULL && strcmp(https, "on") == 0) | ||
43 | return "https://"; | 40 | return "https://"; |
44 | else | 41 | else |
45 | return "http://"; | 42 | return "http://"; |
46 | } | 43 | } |
47 | 44 | ||
48 | char *cgit_hosturl() | 45 | char *cgit_hosturl() |
49 | { | 46 | { |
50 | char *host, *port; | 47 | if (ctx.env.http_host) |
51 | 48 | return ctx.env.http_host; | |
52 | host = getenv("HTTP_HOST"); | 49 | if (!ctx.env.server_name) |
53 | if (host) { | 50 | return NULL; |
54 | host = xstrdup(host); | 51 | if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80) |
55 | } else { | 52 | return ctx.env.server_name; |
56 | host = getenv("SERVER_NAME"); | 53 | return xstrdup(fmt("%s:%s", ctx.env.server_name, ctx.env.server_port)); |
57 | if (!host) | ||
58 | return NULL; | ||
59 | port = getenv("SERVER_PORT"); | ||
60 | if (port && atoi(port) != 80) | ||
61 | host = xstrdup(fmt("%s:%d", host, atoi(port))); | ||
62 | else | ||
63 | host = xstrdup(host); | ||
64 | } | ||
65 | return host; | ||
66 | } | 54 | } |
67 | 55 | ||
68 | char *cgit_rooturl() | 56 | char *cgit_rooturl() |
69 | { | 57 | { |
70 | if (ctx.cfg.virtual_root) | 58 | if (ctx.cfg.virtual_root) |
71 | return fmt("%s/", ctx.cfg.virtual_root); | 59 | return fmt("%s/", ctx.cfg.virtual_root); |
72 | else | 60 | else |
73 | return ctx.cfg.script_name; | 61 | return ctx.cfg.script_name; |
74 | } | 62 | } |
75 | 63 | ||
76 | char *cgit_repourl(const char *reponame) | 64 | char *cgit_repourl(const char *reponame) |
77 | { | 65 | { |
78 | if (ctx.cfg.virtual_root) { | 66 | if (ctx.cfg.virtual_root) { |
79 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); | 67 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); |
80 | } else { | 68 | } else { |
81 | return fmt("?r=%s", reponame); | 69 | return fmt("?r=%s", reponame); |
82 | } | 70 | } |
83 | } | 71 | } |
84 | 72 | ||
85 | char *cgit_fileurl(const char *reponame, const char *pagename, | 73 | char *cgit_fileurl(const char *reponame, const char *pagename, |
86 | const char *filename, const char *query) | 74 | const char *filename, const char *query) |
87 | { | 75 | { |
88 | char *tmp; | 76 | char *tmp; |
89 | char *delim; | 77 | char *delim; |
@@ -446,71 +434,69 @@ void cgit_print_age(time_t t, time_t max_relative, char *format) | |||
446 | secs * 1.0 / TM_HOUR); | 434 | secs * 1.0 / TM_HOUR); |
447 | return; | 435 | return; |
448 | } | 436 | } |
449 | if (secs < TM_WEEK * 2) { | 437 | if (secs < TM_WEEK * 2) { |
450 | htmlf("<span class='age-days'>%.0f days</span>", | 438 | htmlf("<span class='age-days'>%.0f days</span>", |
451 | secs * 1.0 / TM_DAY); | 439 | secs * 1.0 / TM_DAY); |
452 | return; | 440 | return; |
453 | } | 441 | } |
454 | if (secs < TM_MONTH * 2) { | 442 | if (secs < TM_MONTH * 2) { |
455 | htmlf("<span class='age-weeks'>%.0f weeks</span>", | 443 | htmlf("<span class='age-weeks'>%.0f weeks</span>", |
456 | secs * 1.0 / TM_WEEK); | 444 | secs * 1.0 / TM_WEEK); |
457 | return; | 445 | return; |
458 | } | 446 | } |
459 | if (secs < TM_YEAR * 2) { | 447 | if (secs < TM_YEAR * 2) { |
460 | htmlf("<span class='age-months'>%.0f months</span>", | 448 | htmlf("<span class='age-months'>%.0f months</span>", |
461 | secs * 1.0 / TM_MONTH); | 449 | secs * 1.0 / TM_MONTH); |
462 | return; | 450 | return; |
463 | } | 451 | } |
464 | htmlf("<span class='age-years'>%.0f years</span>", | 452 | htmlf("<span class='age-years'>%.0f years</span>", |
465 | secs * 1.0 / TM_YEAR); | 453 | secs * 1.0 / TM_YEAR); |
466 | } | 454 | } |
467 | 455 | ||
468 | void cgit_print_http_headers(struct cgit_context *ctx) | 456 | void cgit_print_http_headers(struct cgit_context *ctx) |
469 | { | 457 | { |
470 | const char *method = getenv("REQUEST_METHOD"); | ||
471 | |||
472 | if (ctx->cfg.embedded) | 458 | if (ctx->cfg.embedded) |
473 | return; | 459 | return; |
474 | 460 | ||
475 | if (ctx->page.status) | 461 | if (ctx->page.status) |
476 | htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg); | 462 | htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg); |
477 | if (ctx->page.mimetype && ctx->page.charset) | 463 | if (ctx->page.mimetype && ctx->page.charset) |
478 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, | 464 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, |
479 | ctx->page.charset); | 465 | ctx->page.charset); |
480 | else if (ctx->page.mimetype) | 466 | else if (ctx->page.mimetype) |
481 | htmlf("Content-Type: %s\n", ctx->page.mimetype); | 467 | htmlf("Content-Type: %s\n", ctx->page.mimetype); |
482 | if (ctx->page.size) | 468 | if (ctx->page.size) |
483 | htmlf("Content-Length: %ld\n", ctx->page.size); | 469 | htmlf("Content-Length: %ld\n", ctx->page.size); |
484 | if (ctx->page.filename) | 470 | if (ctx->page.filename) |
485 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", | 471 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", |
486 | ctx->page.filename); | 472 | ctx->page.filename); |
487 | htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); | 473 | htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); |
488 | htmlf("Expires: %s\n", http_date(ctx->page.expires)); | 474 | htmlf("Expires: %s\n", http_date(ctx->page.expires)); |
489 | if (ctx->page.etag) | 475 | if (ctx->page.etag) |
490 | htmlf("ETag: \"%s\"\n", ctx->page.etag); | 476 | htmlf("ETag: \"%s\"\n", ctx->page.etag); |
491 | html("\n"); | 477 | html("\n"); |
492 | if (method && !strcmp(method, "HEAD")) | 478 | if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD")) |
493 | exit(0); | 479 | exit(0); |
494 | } | 480 | } |
495 | 481 | ||
496 | void cgit_print_docstart(struct cgit_context *ctx) | 482 | void cgit_print_docstart(struct cgit_context *ctx) |
497 | { | 483 | { |
498 | if (ctx->cfg.embedded) | 484 | if (ctx->cfg.embedded) |
499 | return; | 485 | return; |
500 | 486 | ||
501 | char *host = cgit_hosturl(); | 487 | char *host = cgit_hosturl(); |
502 | html(cgit_doctype); | 488 | html(cgit_doctype); |
503 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); | 489 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); |
504 | html("<head>\n"); | 490 | html("<head>\n"); |
505 | html("<title>"); | 491 | html("<title>"); |
506 | html_txt(ctx->page.title); | 492 | html_txt(ctx->page.title); |
507 | html("</title>\n"); | 493 | html("</title>\n"); |
508 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); | 494 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
509 | if (ctx->cfg.robots && *ctx->cfg.robots) | 495 | if (ctx->cfg.robots && *ctx->cfg.robots) |
510 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); | 496 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); |
511 | html("<link rel='stylesheet' type='text/css' href='"); | 497 | html("<link rel='stylesheet' type='text/css' href='"); |
512 | html_attr(ctx->cfg.css); | 498 | html_attr(ctx->cfg.css); |
513 | html("'/>\n"); | 499 | html("'/>\n"); |
514 | if (ctx->cfg.favicon) { | 500 | if (ctx->cfg.favicon) { |
515 | html("<link rel='shortcut icon' href='"); | 501 | html("<link rel='shortcut icon' href='"); |
516 | html_attr(ctx->cfg.favicon); | 502 | html_attr(ctx->cfg.favicon); |