-rw-r--r-- | cgit.c | 50 | ||||
-rw-r--r-- | cgit.h | 18 | ||||
-rw-r--r-- | shared.c | 3 | ||||
-rw-r--r-- | ui-blob.c | 4 | ||||
-rw-r--r-- | ui-patch.c | 4 | ||||
-rw-r--r-- | ui-repolist.c | 6 | ||||
-rw-r--r-- | ui-shared.c | 112 | ||||
-rw-r--r-- | ui-snapshot.c | 4 |
8 files changed, 113 insertions, 88 deletions
@@ -9,2 +9,3 @@ | |||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cmd.h" | ||
10 | 11 | ||
@@ -13,5 +14,7 @@ static int cgit_prepare_cache(struct cacheitem *item) | |||
13 | if (!ctx.repo && ctx.qry.repo) { | 14 | if (!ctx.repo && ctx.qry.repo) { |
14 | char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); | 15 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
15 | cgit_print_docstart(title, item); | 16 | "Bad request"); |
16 | cgit_print_pageheader(title, 0); | 17 | cgit_print_http_headers(&ctx); |
18 | cgit_print_docstart(&ctx); | ||
19 | cgit_print_pageheader(&ctx); | ||
17 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 20 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
@@ -82,3 +85,3 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
82 | { | 85 | { |
83 | char *title, *tmp; | 86 | char *tmp; |
84 | int show_search; | 87 | int show_search; |
@@ -90,7 +93,9 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
90 | if (nongit) { | 93 | if (nongit) { |
91 | title = fmt("%s - %s", ctx.cfg.root_title, "config error"); | 94 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
95 | "config error"); | ||
92 | tmp = fmt("Not a git repository: '%s'", ctx.repo->path); | 96 | tmp = fmt("Not a git repository: '%s'", ctx.repo->path); |
93 | ctx.repo = NULL; | 97 | ctx.repo = NULL; |
94 | cgit_print_docstart(title, item); | 98 | cgit_print_http_headers(&ctx); |
95 | cgit_print_pageheader(title, 0); | 99 | cgit_print_docstart(&ctx); |
100 | cgit_print_pageheader(&ctx); | ||
96 | cgit_print_error(tmp); | 101 | cgit_print_error(tmp); |
@@ -100,3 +105,3 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
100 | 105 | ||
101 | title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); | 106 | ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); |
102 | show_search = 0; | 107 | show_search = 0; |
@@ -109,4 +114,5 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
109 | if (!ctx.qry.head) { | 114 | if (!ctx.qry.head) { |
110 | cgit_print_docstart(title, item); | 115 | cgit_print_http_headers(&ctx); |
111 | cgit_print_pageheader(title, 0); | 116 | cgit_print_docstart(&ctx); |
117 | cgit_print_pageheader(&ctx); | ||
112 | cgit_print_error("Repository seems to be empty"); | 118 | cgit_print_error("Repository seems to be empty"); |
@@ -119,4 +125,5 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
119 | ctx.qry.head = ctx.repo->defbranch; | 125 | ctx.qry.head = ctx.repo->defbranch; |
120 | cgit_print_docstart(title, item); | 126 | cgit_print_http_headers(&ctx); |
121 | cgit_print_pageheader(title, 0); | 127 | cgit_print_docstart(&ctx); |
128 | cgit_print_pageheader(&ctx); | ||
122 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 129 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
@@ -145,5 +152,6 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
145 | show_search = (cgit_cmd == CMD_LOG); | 152 | show_search = (cgit_cmd == CMD_LOG); |
146 | cgit_print_docstart(title, item); | 153 | cgit_print_http_headers(&ctx); |
154 | cgit_print_docstart(&ctx); | ||
147 | if (!cgit_cmd) { | 155 | if (!cgit_cmd) { |
148 | cgit_print_pageheader("summary", show_search); | 156 | cgit_print_pageheader(&ctx); |
149 | cgit_print_summary(); | 157 | cgit_print_summary(); |
@@ -153,3 +161,3 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
153 | 161 | ||
154 | cgit_print_pageheader(ctx.qry.page, show_search); | 162 | cgit_print_pageheader(&ctx); |
155 | 163 | ||
@@ -182,2 +190,10 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
182 | 190 | ||
191 | static long ttl_seconds(long ttl) | ||
192 | { | ||
193 | if (ttl<0) | ||
194 | return 60 * 60 * 24 * 365; | ||
195 | else | ||
196 | return ttl * 60; | ||
197 | } | ||
198 | |||
183 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 199 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
@@ -186,4 +202,2 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache) | |||
186 | 202 | ||
187 | item->st.st_mtime = time(NULL); | ||
188 | |||
189 | if (use_cache) { | 203 | if (use_cache) { |
@@ -195,2 +209,4 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache) | |||
195 | 209 | ||
210 | ctx.page.modified = time(NULL); | ||
211 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | ||
196 | if (ctx.repo) | 212 | if (ctx.repo) |
@@ -176,2 +176,11 @@ struct cgit_config { | |||
176 | 176 | ||
177 | struct cgit_page { | ||
178 | time_t modified; | ||
179 | time_t expires; | ||
180 | char *mimetype; | ||
181 | char *charset; | ||
182 | char *filename; | ||
183 | char *title; | ||
184 | }; | ||
185 | |||
177 | struct cgit_context { | 186 | struct cgit_context { |
@@ -180,2 +189,3 @@ struct cgit_context { | |||
180 | struct cgit_repo *repo; | 189 | struct cgit_repo *repo; |
190 | struct cgit_page page; | ||
181 | }; | 191 | }; |
@@ -262,8 +272,6 @@ extern void cgit_print_date(time_t secs, char *format); | |||
262 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); | 272 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); |
263 | extern void cgit_print_docstart(char *title, struct cacheitem *item); | 273 | extern void cgit_print_http_headers(struct cgit_context *ctx); |
274 | extern void cgit_print_docstart(struct cgit_context *ctx); | ||
264 | extern void cgit_print_docend(); | 275 | extern void cgit_print_docend(); |
265 | extern void cgit_print_pageheader(char *title, int show_search); | 276 | extern void cgit_print_pageheader(struct cgit_context *ctx); |
266 | extern void cgit_print_snapshot_start(const char *mimetype, | ||
267 | const char *filename, | ||
268 | struct cacheitem *item); | ||
269 | extern void cgit_print_filemode(unsigned short mode); | 277 | extern void cgit_print_filemode(unsigned short mode); |
@@ -37,2 +37,5 @@ void cgit_prepare_context(struct cgit_context *ctx) | |||
37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
38 | ctx->page.mimetype = "text/html"; | ||
39 | ctx->page.charset = PAGE_ENCODING; | ||
40 | ctx->page.filename = NULL; | ||
38 | } | 41 | } |
@@ -37,3 +37,5 @@ void cgit_print_blob(struct cacheitem *item, const char *hex, char *path) | |||
37 | buf[size] = '\0'; | 37 | buf[size] = '\0'; |
38 | cgit_print_snapshot_start("text/plain", path, item); | 38 | ctx.page.mimetype = "text/plain"; |
39 | ctx.page.filename = path; | ||
40 | cgit_print_http_headers(&ctx); | ||
39 | write(htmlfd, buf, size); | 41 | write(htmlfd, buf, size); |
@@ -94,3 +94,5 @@ void cgit_print_patch(char *hex, struct cacheitem *item) | |||
94 | patchname = fmt("%s.patch", sha1_to_hex(sha1)); | 94 | patchname = fmt("%s.patch", sha1_to_hex(sha1)); |
95 | cgit_print_snapshot_start("text/plain", patchname, item); | 95 | ctx.page.mimetype = "text/plain"; |
96 | ctx.page.filename = patchname; | ||
97 | cgit_print_http_headers(&ctx); | ||
96 | htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); | 98 | htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); |
diff --git a/ui-repolist.c b/ui-repolist.c index cd4e41d..e663585 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -53,4 +53,6 @@ void cgit_print_repolist(struct cacheitem *item) | |||
53 | 53 | ||
54 | cgit_print_docstart(ctx.cfg.root_title, item); | 54 | ctx.page.title = ctx.cfg.root_title; |
55 | cgit_print_pageheader(ctx.cfg.root_title, 0); | 55 | cgit_print_http_headers(&ctx); |
56 | cgit_print_docstart(&ctx); | ||
57 | cgit_print_pageheader(&ctx); | ||
56 | 58 | ||
diff --git a/ui-shared.c b/ui-shared.c index 2eff79d..2596023 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -28,10 +28,2 @@ static char *http_date(time_t t) | |||
28 | 28 | ||
29 | static long ttl_seconds(long ttl) | ||
30 | { | ||
31 | if (ttl<0) | ||
32 | return 60 * 60 * 24 * 365; | ||
33 | else | ||
34 | return ttl * 60; | ||
35 | } | ||
36 | |||
37 | void cgit_print_error(char *msg) | 29 | void cgit_print_error(char *msg) |
@@ -363,9 +355,19 @@ void cgit_print_age(time_t t, time_t max_relative, char *format) | |||
363 | 355 | ||
364 | void cgit_print_docstart(char *title, struct cacheitem *item) | 356 | void cgit_print_http_headers(struct cgit_context *ctx) |
365 | { | 357 | { |
366 | html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); | 358 | if (ctx->page.mimetype && ctx->page.charset) |
367 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 359 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, |
368 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 360 | ctx->page.charset); |
369 | ttl_seconds(item->ttl))); | 361 | else if (ctx->page.mimetype) |
362 | htmlf("Content-Type: %s\n", ctx->page.mimetype); | ||
363 | if (ctx->page.filename) | ||
364 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", | ||
365 | ctx->page.filename); | ||
366 | htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); | ||
367 | htmlf("Expires: %s\n", http_date(ctx->page.expires)); | ||
370 | html("\n"); | 368 | html("\n"); |
369 | } | ||
370 | |||
371 | void cgit_print_docstart(struct cgit_context *ctx) | ||
372 | { | ||
371 | html(cgit_doctype); | 373 | html(cgit_doctype); |
@@ -374,9 +376,9 @@ void cgit_print_docstart(char *title, struct cacheitem *item) | |||
374 | html("<title>"); | 376 | html("<title>"); |
375 | html_txt(title); | 377 | html_txt(ctx->page.title); |
376 | html("</title>\n"); | 378 | html("</title>\n"); |
377 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); | 379 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
378 | if (ctx.cfg.robots && *ctx.cfg.robots) | 380 | if (ctx->cfg.robots && *ctx->cfg.robots) |
379 | htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots); | 381 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); |
380 | html("<link rel='stylesheet' type='text/css' href='"); | 382 | html("<link rel='stylesheet' type='text/css' href='"); |
381 | html_attr(ctx.cfg.css); | 383 | html_attr(ctx->cfg.css); |
382 | html("'/>\n"); | 384 | html("'/>\n"); |
@@ -465,3 +467,3 @@ void add_hidden_formfields(int incl_head, int incl_search, char *page) | |||
465 | 467 | ||
466 | void cgit_print_pageheader(char *title, int show_search) | 468 | void cgit_print_pageheader(struct cgit_context *ctx) |
467 | { | 469 | { |
@@ -477,26 +479,26 @@ void cgit_print_pageheader(char *title, int show_search) | |||
477 | htmlf("'><img src='%s' alt='cgit'/></a>\n", | 479 | htmlf("'><img src='%s' alt='cgit'/></a>\n", |
478 | ctx.cfg.logo); | 480 | ctx->cfg.logo); |
479 | html("</td></tr>\n<tr><td class='sidebar'>\n"); | 481 | html("</td></tr>\n<tr><td class='sidebar'>\n"); |
480 | if (ctx.repo) { | 482 | if (ctx->repo) { |
481 | html("<h1 class='first'>"); | 483 | html("<h1 class='first'>"); |
482 | html_txt(strrpart(ctx.repo->name, 20)); | 484 | html_txt(strrpart(ctx->repo->name, 20)); |
483 | html("</h1>\n"); | 485 | html("</h1>\n"); |
484 | html_txt(ctx.repo->desc); | 486 | html_txt(ctx->repo->desc); |
485 | if (ctx.repo->owner) { | 487 | if (ctx->repo->owner) { |
486 | html("<h1>owner</h1>\n"); | 488 | html("<h1>owner</h1>\n"); |
487 | html_txt(ctx.repo->owner); | 489 | html_txt(ctx->repo->owner); |
488 | } | 490 | } |
489 | html("<h1>navigate</h1>\n"); | 491 | html("<h1>navigate</h1>\n"); |
490 | reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head, | 492 | reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head, |
491 | NULL, NULL); | 493 | NULL, NULL); |
492 | cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL, | 494 | cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL, |
493 | 0, NULL, NULL); | 495 | 0, NULL, NULL); |
494 | cgit_tree_link("tree", NULL, "menu", ctx.qry.head, | 496 | cgit_tree_link("tree", NULL, "menu", ctx->qry.head, |
495 | ctx.qry.sha1, NULL); | 497 | ctx->qry.sha1, NULL); |
496 | cgit_commit_link("commit", NULL, "menu", ctx.qry.head, | 498 | cgit_commit_link("commit", NULL, "menu", ctx->qry.head, |
497 | ctx.qry.sha1); | 499 | ctx->qry.sha1); |
498 | cgit_diff_link("diff", NULL, "menu", ctx.qry.head, | 500 | cgit_diff_link("diff", NULL, "menu", ctx->qry.head, |
499 | ctx.qry.sha1, ctx.qry.sha2, NULL); | 501 | ctx->qry.sha1, ctx->qry.sha2, NULL); |
500 | cgit_patch_link("patch", NULL, "menu", ctx.qry.head, | 502 | cgit_patch_link("patch", NULL, "menu", ctx->qry.head, |
501 | ctx.qry.sha1); | 503 | ctx->qry.sha1); |
502 | 504 | ||
@@ -504,9 +506,9 @@ void cgit_print_pageheader(char *title, int show_search) | |||
504 | 506 | ||
505 | if (ctx.repo->clone_url || ctx.cfg.clone_prefix) { | 507 | if (ctx->repo->clone_url || ctx->cfg.clone_prefix) { |
506 | html("<h1>clone</h1>\n"); | 508 | html("<h1>clone</h1>\n"); |
507 | if (ctx.repo->clone_url) | 509 | if (ctx->repo->clone_url) |
508 | url = ctx.repo->clone_url; | 510 | url = ctx->repo->clone_url; |
509 | else | 511 | else |
510 | url = fmt("%s%s", ctx.cfg.clone_prefix, | 512 | url = fmt("%s%s", ctx->cfg.clone_prefix, |
511 | ctx.repo->url); | 513 | ctx->repo->url); |
512 | html("<a class='menu' href='"); | 514 | html("<a class='menu' href='"); |
@@ -522,6 +524,6 @@ void cgit_print_pageheader(char *title, int show_search) | |||
522 | html("<form method='get' action=''>\n"); | 524 | html("<form method='get' action=''>\n"); |
523 | add_hidden_formfields(0, 1, ctx.qry.page); | 525 | add_hidden_formfields(0, 1, ctx->qry.page); |
524 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); | 526 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); |
525 | html("<select name='h' onchange='this.form.submit();'>\n"); | 527 | html("<select name='h' onchange='this.form.submit();'>\n"); |
526 | for_each_branch_ref(print_branch_option, ctx.qry.head); | 528 | for_each_branch_ref(print_branch_option, ctx->qry.head); |
527 | html("</select>\n"); | 529 | html("</select>\n"); |
@@ -534,5 +536,5 @@ void cgit_print_pageheader(char *title, int show_search) | |||
534 | html("<form method='get' action='"); | 536 | html("<form method='get' action='"); |
535 | if (ctx.cfg.virtual_root) | 537 | if (ctx->cfg.virtual_root) |
536 | html_attr(cgit_fileurl(ctx.qry.repo, "log", | 538 | html_attr(cgit_fileurl(ctx->qry.repo, "log", |
537 | ctx.qry.path, NULL)); | 539 | ctx->qry.path, NULL)); |
538 | html("'>\n"); | 540 | html("'>\n"); |
@@ -540,8 +542,8 @@ void cgit_print_pageheader(char *title, int show_search) | |||
540 | html("<select name='qt'>\n"); | 542 | html("<select name='qt'>\n"); |
541 | html_option("grep", "log msg", ctx.qry.grep); | 543 | html_option("grep", "log msg", ctx->qry.grep); |
542 | html_option("author", "author", ctx.qry.grep); | 544 | html_option("author", "author", ctx->qry.grep); |
543 | html_option("committer", "committer", ctx.qry.grep); | 545 | html_option("committer", "committer", ctx->qry.grep); |
544 | html("</select>\n"); | 546 | html("</select>\n"); |
545 | html("<input class='txt' type='text' name='q' value='"); | 547 | html("<input class='txt' type='text' name='q' value='"); |
546 | html_attr(ctx.qry.search); | 548 | html_attr(ctx->qry.search); |
547 | html("'/>\n"); | 549 | html("'/>\n"); |
@@ -549,3 +551,3 @@ void cgit_print_pageheader(char *title, int show_search) | |||
549 | } else { | 551 | } else { |
550 | if (!ctx.cfg.index_info || html_include(ctx.cfg.index_info)) | 552 | if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info)) |
551 | html(default_info); | 553 | html(default_info); |
@@ -558,14 +560,2 @@ void cgit_print_pageheader(char *title, int show_search) | |||
558 | 560 | ||
559 | |||
560 | void cgit_print_snapshot_start(const char *mimetype, const char *filename, | ||
561 | struct cacheitem *item) | ||
562 | { | ||
563 | htmlf("Content-Type: %s\n", mimetype); | ||
564 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); | ||
565 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | ||
566 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | ||
567 | ttl_seconds(item->ttl))); | ||
568 | html("\n"); | ||
569 | } | ||
570 | |||
571 | void cgit_print_filemode(unsigned short mode) | 561 | void cgit_print_filemode(unsigned short mode) |
diff --git a/ui-snapshot.c b/ui-snapshot.c index 67dbbdd..4449803 100644 --- a/ui-snapshot.c +++ b/ui-snapshot.c | |||
@@ -103,3 +103,5 @@ void cgit_print_snapshot(struct cacheitem *item, const char *head, | |||
103 | args.time = commit->date; | 103 | args.time = commit->date; |
104 | cgit_print_snapshot_start(sat->mimetype, filename, item); | 104 | ctx.page.mimetype = xstrdup(sat->mimetype); |
105 | ctx.page.filename = xstrdup(filename); | ||
106 | cgit_print_http_headers(&ctx); | ||
105 | (*sat->write_func)(&args); | 107 | (*sat->write_func)(&args); |