author | Lars Hjemli <hjemli@gmail.com> | 2009-08-11 08:12:53 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2009-08-11 08:12:53 (UTC) |
commit | 2c742d1a925486a47f74710325b3076a7742a9f9 (patch) (unidiff) | |
tree | e16e8de4c65cb659a5b487ac3f8a7d7254fef395 /ui-shared.c | |
parent | fcc6056230e367e276dcb5a292d45e272cc6e7e6 (diff) | |
parent | 80550bbe028b551550395653d32a0ba50db540ef (diff) | |
download | cgit-2c742d1a925486a47f74710325b3076a7742a9f9.zip cgit-2c742d1a925486a47f74710325b3076a7742a9f9.tar.gz cgit-2c742d1a925486a47f74710325b3076a7742a9f9.tar.bz2 |
Merge branch 'lh/embedded'
-rw-r--r-- | ui-shared.c | 51 |
1 files changed, 22 insertions, 29 deletions
diff --git a/ui-shared.c b/ui-shared.c index 015c52b..cf06511 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -7,91 +7,79 @@ | |||
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 | ||
13 | const char cgit_doctype[] = | 13 | const 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 | ||
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; |
90 | 78 | ||
91 | if (ctx.cfg.virtual_root) { | 79 | if (ctx.cfg.virtual_root) { |
92 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, | 80 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, |
93 | pagename, (filename ? filename:"")); | 81 | pagename, (filename ? filename:"")); |
94 | delim = "?"; | 82 | delim = "?"; |
95 | } else { | 83 | } else { |
96 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, | 84 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, |
97 | (filename ? filename : "")); | 85 | (filename ? filename : "")); |
@@ -438,143 +426,148 @@ void cgit_print_age(time_t t, time_t max_relative, char *format) | |||
438 | 426 | ||
439 | if (secs < TM_HOUR * 2) { | 427 | if (secs < TM_HOUR * 2) { |
440 | htmlf("<span class='age-mins'>%.0f min.</span>", | 428 | htmlf("<span class='age-mins'>%.0f min.</span>", |
441 | secs * 1.0 / TM_MIN); | 429 | secs * 1.0 / TM_MIN); |
442 | return; | 430 | return; |
443 | } | 431 | } |
444 | if (secs < TM_DAY * 2) { | 432 | if (secs < TM_DAY * 2) { |
445 | htmlf("<span class='age-hours'>%.0f hours</span>", | 433 | htmlf("<span class='age-hours'>%.0f hours</span>", |
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"); | 458 | if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1")) |
471 | |||
472 | 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) { |
485 | if (ctx->cfg.header) | ||
486 | html_include(ctx->cfg.header); | ||
499 | return; | 487 | return; |
488 | } | ||
500 | 489 | ||
501 | char *host = cgit_hosturl(); | 490 | char *host = cgit_hosturl(); |
502 | html(cgit_doctype); | 491 | html(cgit_doctype); |
503 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); | 492 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); |
504 | html("<head>\n"); | 493 | html("<head>\n"); |
505 | html("<title>"); | 494 | html("<title>"); |
506 | html_txt(ctx->page.title); | 495 | html_txt(ctx->page.title); |
507 | html("</title>\n"); | 496 | html("</title>\n"); |
508 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); | 497 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
509 | if (ctx->cfg.robots && *ctx->cfg.robots) | 498 | if (ctx->cfg.robots && *ctx->cfg.robots) |
510 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); | 499 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); |
511 | html("<link rel='stylesheet' type='text/css' href='"); | 500 | html("<link rel='stylesheet' type='text/css' href='"); |
512 | html_attr(ctx->cfg.css); | 501 | html_attr(ctx->cfg.css); |
513 | html("'/>\n"); | 502 | html("'/>\n"); |
514 | if (ctx->cfg.favicon) { | 503 | if (ctx->cfg.favicon) { |
515 | html("<link rel='shortcut icon' href='"); | 504 | html("<link rel='shortcut icon' href='"); |
516 | html_attr(ctx->cfg.favicon); | 505 | html_attr(ctx->cfg.favicon); |
517 | html("'/>\n"); | 506 | html("'/>\n"); |
518 | } | 507 | } |
519 | if (host && ctx->repo) { | 508 | if (host && ctx->repo) { |
520 | html("<link rel='alternate' title='Atom feed' href='"); | 509 | html("<link rel='alternate' title='Atom feed' href='"); |
521 | html(cgit_httpscheme()); | 510 | html(cgit_httpscheme()); |
522 | html_attr(cgit_hosturl()); | 511 | html_attr(cgit_hosturl()); |
523 | html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path, | 512 | html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path, |
524 | fmt("h=%s", ctx->qry.head))); | 513 | fmt("h=%s", ctx->qry.head))); |
525 | html("' type='application/atom+xml'/>\n"); | 514 | html("' type='application/atom+xml'/>\n"); |
526 | } | 515 | } |
527 | if (ctx->cfg.head_include) | 516 | if (ctx->cfg.head_include) |
528 | html_include(ctx->cfg.head_include); | 517 | html_include(ctx->cfg.head_include); |
529 | html("</head>\n"); | 518 | html("</head>\n"); |
530 | html("<body>\n"); | 519 | html("<body>\n"); |
531 | if (ctx->cfg.header) | 520 | if (ctx->cfg.header) |
532 | html_include(ctx->cfg.header); | 521 | html_include(ctx->cfg.header); |
533 | } | 522 | } |
534 | 523 | ||
535 | void cgit_print_docend() | 524 | void cgit_print_docend() |
536 | { | 525 | { |
537 | html("</div>"); | 526 | html("</div> <!-- class=content -->\n"); |
527 | if (ctx.cfg.embedded) { | ||
528 | html("</div> <!-- id=cgit -->\n"); | ||
529 | if (ctx.cfg.footer) | ||
530 | html_include(ctx.cfg.footer); | ||
531 | return; | ||
532 | } | ||
538 | if (ctx.cfg.footer) | 533 | if (ctx.cfg.footer) |
539 | html_include(ctx.cfg.footer); | 534 | html_include(ctx.cfg.footer); |
540 | else { | 535 | else { |
541 | htmlf("<div class='footer'>generated by cgit %s at ", | 536 | htmlf("<div class='footer'>generated by cgit %s at ", |
542 | cgit_version); | 537 | cgit_version); |
543 | cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time); | 538 | cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time); |
544 | html("</div>\n"); | 539 | html("</div>\n"); |
545 | } | 540 | } |
546 | html("</div>"); | 541 | html("</div> <!-- id=cgit -->\n"); |
547 | if (ctx.cfg.embedded) | ||
548 | return; | ||
549 | html("</body>\n</html>\n"); | 542 | html("</body>\n</html>\n"); |
550 | } | 543 | } |
551 | 544 | ||
552 | int print_branch_option(const char *refname, const unsigned char *sha1, | 545 | int print_branch_option(const char *refname, const unsigned char *sha1, |
553 | int flags, void *cb_data) | 546 | int flags, void *cb_data) |
554 | { | 547 | { |
555 | char *name = (char *)refname; | 548 | char *name = (char *)refname; |
556 | html_option(name, name, ctx.qry.head); | 549 | html_option(name, name, ctx.qry.head); |
557 | return 0; | 550 | return 0; |
558 | } | 551 | } |
559 | 552 | ||
560 | int print_archive_ref(const char *refname, const unsigned char *sha1, | 553 | int print_archive_ref(const char *refname, const unsigned char *sha1, |
561 | int flags, void *cb_data) | 554 | int flags, void *cb_data) |
562 | { | 555 | { |
563 | struct tag *tag; | 556 | struct tag *tag; |
564 | struct taginfo *info; | 557 | struct taginfo *info; |
565 | struct object *obj; | 558 | struct object *obj; |
566 | char buf[256], *url; | 559 | char buf[256], *url; |
567 | unsigned char fileid[20]; | 560 | unsigned char fileid[20]; |
568 | int *header = (int *)cb_data; | 561 | int *header = (int *)cb_data; |
569 | 562 | ||
570 | if (prefixcmp(refname, "refs/archives")) | 563 | if (prefixcmp(refname, "refs/archives")) |
571 | return 0; | 564 | return 0; |
572 | strncpy(buf, refname+14, sizeof(buf)); | 565 | strncpy(buf, refname+14, sizeof(buf)); |
573 | obj = parse_object(sha1); | 566 | obj = parse_object(sha1); |
574 | if (!obj) | 567 | if (!obj) |
575 | return 1; | 568 | return 1; |
576 | if (obj->type == OBJ_TAG) { | 569 | if (obj->type == OBJ_TAG) { |
577 | tag = lookup_tag(sha1); | 570 | tag = lookup_tag(sha1); |
578 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) | 571 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) |
579 | return 0; | 572 | return 0; |
580 | hashcpy(fileid, tag->tagged->sha1); | 573 | hashcpy(fileid, tag->tagged->sha1); |