summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2008-03-23 23:51:19 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-03-23 23:51:19 (UTC)
commitf3c1a187fe2bc33f8423cd535d5045899699995b (patch) (unidiff)
treeb5c553da7b108900535fcfcd24b78bdd0ac62387
parentb1f9b9c1459cb9a30ebf80721aff6ef788d1f891 (diff)
downloadcgit-f3c1a187fe2bc33f8423cd535d5045899699995b.zip
cgit-f3c1a187fe2bc33f8423cd535d5045899699995b.tar.gz
cgit-f3c1a187fe2bc33f8423cd535d5045899699995b.tar.bz2
Add struct cgit_page to cgit_context
This struct is used when generating http headers, and as such is another small step towards the goal of the whole cleanup series; to invoke each page/view function with a function pointer. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c50
-rw-r--r--cgit.h18
-rw-r--r--shared.c3
-rw-r--r--ui-blob.c4
-rw-r--r--ui-patch.c4
-rw-r--r--ui-repolist.c6
-rw-r--r--ui-shared.c112
-rw-r--r--ui-snapshot.c4
8 files changed, 113 insertions, 88 deletions
diff --git a/cgit.c b/cgit.c
index e6b4526..d0f6905 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,28 +1,31 @@
1/* cgit.c: cgi for the git scm 1/* cgit.c: cgi for the git scm
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 11
11static int cgit_prepare_cache(struct cacheitem *item) 12static int cgit_prepare_cache(struct cacheitem *item)
12{ 13{
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));
18 cgit_print_docend(); 21 cgit_print_docend();
19 return 0; 22 return 0;
20 } 23 }
21 24
22 if (!ctx.repo) { 25 if (!ctx.repo) {
23 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); 26 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
24 item->ttl = ctx.cfg.cache_root_ttl; 27 item->ttl = ctx.cfg.cache_root_ttl;
25 return 1; 28 return 1;
26 } 29 }
27 30
28 if (!cgit_cmd) { 31 if (!cgit_cmd) {
@@ -71,96 +74,101 @@ char *find_default_branch(struct cgit_repo *repo)
71 info.req_ref = repo->defbranch; 74 info.req_ref = repo->defbranch;
72 info.first_ref = NULL; 75 info.first_ref = NULL;
73 info.match = 0; 76 info.match = 0;
74 for_each_branch_ref(find_current_ref, &info); 77 for_each_branch_ref(find_current_ref, &info);
75 if (info.match) 78 if (info.match)
76 return info.req_ref; 79 return info.req_ref;
77 else 80 else
78 return info.first_ref; 81 return info.first_ref;
79} 82}
80 83
81static void cgit_print_repo_page(struct cacheitem *item) 84static 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;
85 unsigned char sha1[20]; 88 unsigned char sha1[20];
86 int nongit = 0; 89 int nongit = 0;
87 90
88 setenv("GIT_DIR", ctx.repo->path, 1); 91 setenv("GIT_DIR", ctx.repo->path, 1);
89 setup_git_directory_gently(&nongit); 92 setup_git_directory_gently(&nongit);
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);
97 cgit_print_docend(); 102 cgit_print_docend();
98 return; 103 return;
99 } 104 }
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;
103 108
104 if (!ctx.qry.head) { 109 if (!ctx.qry.head) {
105 ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); 110 ctx.qry.head = xstrdup(find_default_branch(ctx.repo));
106 ctx.repo->defbranch = ctx.qry.head; 111 ctx.repo->defbranch = ctx.qry.head;
107 } 112 }
108 113
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");
113 cgit_print_docend(); 119 cgit_print_docend();
114 return; 120 return;
115 } 121 }
116 122
117 if (get_sha1(ctx.qry.head, sha1)) { 123 if (get_sha1(ctx.qry.head, sha1)) {
118 tmp = xstrdup(ctx.qry.head); 124 tmp = xstrdup(ctx.qry.head);
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));
123 cgit_print_docend(); 130 cgit_print_docend();
124 return; 131 return;
125 } 132 }
126 133
127 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { 134 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) {
128 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, 135 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1,
129 cgit_repobasename(ctx.repo->url), 136 cgit_repobasename(ctx.repo->url),
130 ctx.qry.path, 137 ctx.qry.path,
131 ctx.repo->snapshots ); 138 ctx.repo->snapshots );
132 return; 139 return;
133 } 140 }
134 141
135 if (cgit_cmd == CMD_PATCH) { 142 if (cgit_cmd == CMD_PATCH) {
136 cgit_print_patch(ctx.qry.sha1, item); 143 cgit_print_patch(ctx.qry.sha1, item);
137 return; 144 return;
138 } 145 }
139 146
140 if (cgit_cmd == CMD_BLOB) { 147 if (cgit_cmd == CMD_BLOB) {
141 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); 148 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path);
142 return; 149 return;
143 } 150 }
144 151
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();
150 cgit_print_docend(); 158 cgit_print_docend();
151 return; 159 return;
152 } 160 }
153 161
154 cgit_print_pageheader(ctx.qry.page, show_search); 162 cgit_print_pageheader(&ctx);
155 163
156 switch(cgit_cmd) { 164 switch(cgit_cmd) {
157 case CMD_LOG: 165 case CMD_LOG:
158 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, 166 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs,
159 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, 167 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search,
160 ctx.qry.path, 1); 168 ctx.qry.path, 1);
161 break; 169 break;
162 case CMD_TREE: 170 case CMD_TREE:
163 cgit_print_tree(ctx.qry.sha1, ctx.qry.path); 171 cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
164 break; 172 break;
165 case CMD_COMMIT: 173 case CMD_COMMIT:
166 cgit_print_commit(ctx.qry.sha1); 174 cgit_print_commit(ctx.qry.sha1);
@@ -171,37 +179,45 @@ static void cgit_print_repo_page(struct cacheitem *item)
171 case CMD_TAG: 179 case CMD_TAG:
172 cgit_print_tag(ctx.qry.sha1); 180 cgit_print_tag(ctx.qry.sha1);
173 break; 181 break;
174 case CMD_DIFF: 182 case CMD_DIFF:
175 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); 183 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path);
176 break; 184 break;
177 default: 185 default:
178 cgit_print_error("Invalid request"); 186 cgit_print_error("Invalid request");
179 } 187 }
180 cgit_print_docend(); 188 cgit_print_docend();
181} 189}
182 190
191static long ttl_seconds(long ttl)
192{
193 if (ttl<0)
194 return 60 * 60 * 24 * 365;
195 else
196 return ttl * 60;
197}
198
183static void cgit_fill_cache(struct cacheitem *item, int use_cache) 199static void cgit_fill_cache(struct cacheitem *item, int use_cache)
184{ 200{
185 int stdout2; 201 int stdout2;
186 202
187 item->st.st_mtime = time(NULL);
188
189 if (use_cache) { 203 if (use_cache) {
190 stdout2 = chk_positive(dup(STDOUT_FILENO), 204 stdout2 = chk_positive(dup(STDOUT_FILENO),
191 "Preserving STDOUT"); 205 "Preserving STDOUT");
192 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 206 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
193 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 207 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
194 } 208 }
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)
197 cgit_print_repo_page(item); 213 cgit_print_repo_page(item);
198 else 214 else
199 cgit_print_repolist(item); 215 cgit_print_repolist(item);
200 216
201 if (use_cache) { 217 if (use_cache) {
202 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 218 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
203 chk_positive(dup2(stdout2, STDOUT_FILENO), 219 chk_positive(dup2(stdout2, STDOUT_FILENO),
204 "Restoring original STDOUT"); 220 "Restoring original STDOUT");
205 chk_zero(close(stdout2), "Closing temporary STDOUT"); 221 chk_zero(close(stdout2), "Closing temporary STDOUT");
206 } 222 }
207} 223}
diff --git a/cgit.h b/cgit.h
index 5b7ee1c..8ab8e07 100644
--- a/cgit.h
+++ b/cgit.h
@@ -165,28 +165,38 @@ struct cgit_config {
165 int max_commit_count; 165 int max_commit_count;
166 int max_lock_attempts; 166 int max_lock_attempts;
167 int max_msg_len; 167 int max_msg_len;
168 int max_repodesc_len; 168 int max_repodesc_len;
169 int nocache; 169 int nocache;
170 int renamelimit; 170 int renamelimit;
171 int snapshots; 171 int snapshots;
172 int summary_branches; 172 int summary_branches;
173 int summary_log; 173 int summary_log;
174 int summary_tags; 174 int summary_tags;
175}; 175};
176 176
177struct cgit_page {
178 time_t modified;
179 time_t expires;
180 char *mimetype;
181 char *charset;
182 char *filename;
183 char *title;
184};
185
177struct cgit_context { 186struct cgit_context {
178 struct cgit_query qry; 187 struct cgit_query qry;
179 struct cgit_config cfg; 188 struct cgit_config cfg;
180 struct cgit_repo *repo; 189 struct cgit_repo *repo;
190 struct cgit_page page;
181}; 191};
182 192
183extern const char *cgit_version; 193extern const char *cgit_version;
184 194
185extern struct cgit_repolist cgit_repolist; 195extern struct cgit_repolist cgit_repolist;
186extern struct cgit_context ctx; 196extern struct cgit_context ctx;
187extern int cgit_cmd; 197extern int cgit_cmd;
188 198
189extern void cgit_prepare_context(struct cgit_context *ctx); 199extern void cgit_prepare_context(struct cgit_context *ctx);
190extern int cgit_get_cmd_index(const char *cmd); 200extern int cgit_get_cmd_index(const char *cmd);
191extern struct cgit_repo *cgit_get_repoinfo(const char *url); 201extern struct cgit_repo *cgit_get_repoinfo(const char *url);
192extern void cgit_global_config_cb(const char *name, const char *value); 202extern void cgit_global_config_cb(const char *name, const char *value);
@@ -251,30 +261,28 @@ extern void cgit_commit_link(char *name, char *title, char *class, char *head,
251extern void cgit_refs_link(char *name, char *title, char *class, char *head, 261extern void cgit_refs_link(char *name, char *title, char *class, char *head,
252 char *rev, char *path); 262 char *rev, char *path);
253extern void cgit_snapshot_link(char *name, char *title, char *class, 263extern void cgit_snapshot_link(char *name, char *title, char *class,
254 char *head, char *rev, char *archivename); 264 char *head, char *rev, char *archivename);
255extern void cgit_diff_link(char *name, char *title, char *class, char *head, 265extern void cgit_diff_link(char *name, char *title, char *class, char *head,
256 char *new_rev, char *old_rev, char *path); 266 char *new_rev, char *old_rev, char *path);
257 267
258extern void cgit_object_link(struct object *obj); 268extern void cgit_object_link(struct object *obj);
259 269
260extern void cgit_print_error(char *msg); 270extern void cgit_print_error(char *msg);
261extern void cgit_print_date(time_t secs, char *format); 271extern void cgit_print_date(time_t secs, char *format);
262extern void cgit_print_age(time_t t, time_t max_relative, char *format); 272extern void cgit_print_age(time_t t, time_t max_relative, char *format);
263extern void cgit_print_docstart(char *title, struct cacheitem *item); 273extern void cgit_print_http_headers(struct cgit_context *ctx);
274extern void cgit_print_docstart(struct cgit_context *ctx);
264extern void cgit_print_docend(); 275extern void cgit_print_docend();
265extern void cgit_print_pageheader(char *title, int show_search); 276extern void cgit_print_pageheader(struct cgit_context *ctx);
266extern void cgit_print_snapshot_start(const char *mimetype,
267 const char *filename,
268 struct cacheitem *item);
269extern void cgit_print_filemode(unsigned short mode); 277extern void cgit_print_filemode(unsigned short mode);
270extern void cgit_print_branches(int maxcount); 278extern void cgit_print_branches(int maxcount);
271extern void cgit_print_tags(int maxcount); 279extern void cgit_print_tags(int maxcount);
272 280
273extern void cgit_print_repolist(struct cacheitem *item); 281extern void cgit_print_repolist(struct cacheitem *item);
274extern void cgit_print_summary(); 282extern void cgit_print_summary();
275extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, 283extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep,
276 char *pattern, char *path, int pager); 284 char *pattern, char *path, int pager);
277extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); 285extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path);
278extern void cgit_print_tree(const char *rev, char *path); 286extern void cgit_print_tree(const char *rev, char *path);
279extern void cgit_print_commit(char *hex); 287extern void cgit_print_commit(char *hex);
280extern void cgit_print_refs(); 288extern void cgit_print_refs();
diff --git a/shared.c b/shared.c
index 76e10d0..539d533 100644
--- a/shared.c
+++ b/shared.c
@@ -26,24 +26,27 @@ void cgit_prepare_context(struct cgit_context *ctx)
26 ctx->cfg.cache_static_ttl = -1; 26 ctx->cfg.cache_static_ttl = -1;
27 ctx->cfg.css = "/cgit.css"; 27 ctx->cfg.css = "/cgit.css";
28 ctx->cfg.logo = "/git-logo.png"; 28 ctx->cfg.logo = "/git-logo.png";
29 ctx->cfg.max_commit_count = 50; 29 ctx->cfg.max_commit_count = 50;
30 ctx->cfg.max_lock_attempts = 5; 30 ctx->cfg.max_lock_attempts = 5;
31 ctx->cfg.max_msg_len = 60; 31 ctx->cfg.max_msg_len = 60;
32 ctx->cfg.max_repodesc_len = 60; 32 ctx->cfg.max_repodesc_len = 60;
33 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 33 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
34 ctx->cfg.renamelimit = -1; 34 ctx->cfg.renamelimit = -1;
35 ctx->cfg.robots = "index, nofollow"; 35 ctx->cfg.robots = "index, nofollow";
36 ctx->cfg.root_title = "Git repository browser"; 36 ctx->cfg.root_title = "Git repository browser";
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}
39 42
40int cgit_get_cmd_index(const char *cmd) 43int cgit_get_cmd_index(const char *cmd)
41{ 44{
42 static char *cmds[] = {"log", "commit", "diff", "tree", "blob", 45 static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
43 "snapshot", "tag", "refs", "patch", NULL}; 46 "snapshot", "tag", "refs", "patch", NULL};
44 int i; 47 int i;
45 48
46 for(i = 0; cmds[i]; i++) 49 for(i = 0; cmds[i]; i++)
47 if (!strcmp(cmd, cmds[i])) 50 if (!strcmp(cmd, cmds[i]))
48 return i + 1; 51 return i + 1;
49 return 0; 52 return 0;
diff --git a/ui-blob.c b/ui-blob.c
index be4fb88..bd44574 100644
--- a/ui-blob.c
+++ b/ui-blob.c
@@ -26,15 +26,17 @@ void cgit_print_blob(struct cacheitem *item, const char *hex, char *path)
26 if (type == OBJ_BAD) { 26 if (type == OBJ_BAD) {
27 cgit_print_error(fmt("Bad object name: %s", hex)); 27 cgit_print_error(fmt("Bad object name: %s", hex));
28 return; 28 return;
29 } 29 }
30 30
31 buf = read_sha1_file(sha1, &type, &size); 31 buf = read_sha1_file(sha1, &type, &size);
32 if (!buf) { 32 if (!buf) {
33 cgit_print_error(fmt("Error reading object %s", hex)); 33 cgit_print_error(fmt("Error reading object %s", hex));
34 return; 34 return;
35 } 35 }
36 36
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);
40} 42}
diff --git a/ui-patch.c b/ui-patch.c
index d93426b..a77f3f6 100644
--- a/ui-patch.c
+++ b/ui-patch.c
@@ -83,25 +83,27 @@ void cgit_print_patch(char *hex, struct cacheitem *item)
83 cgit_print_error(fmt("Bad object id: %s", hex)); 83 cgit_print_error(fmt("Bad object id: %s", hex));
84 return; 84 return;
85 } 85 }
86 commit = lookup_commit_reference(sha1); 86 commit = lookup_commit_reference(sha1);
87 if (!commit) { 87 if (!commit) {
88 cgit_print_error(fmt("Bad commit reference: %s", hex)); 88 cgit_print_error(fmt("Bad commit reference: %s", hex));
89 return; 89 return;
90 } 90 }
91 info = cgit_parse_commit(commit); 91 info = cgit_parse_commit(commit);
92 hashcpy(old_sha1, commit->parents->item->object.sha1); 92 hashcpy(old_sha1, commit->parents->item->object.sha1);
93 93
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));
97 htmlf("From: %s%s\n", info->author, info->author_email); 99 htmlf("From: %s%s\n", info->author, info->author_email);
98 html("Date: "); 100 html("Date: ");
99 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n"); 101 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n");
100 htmlf("Subject: %s\n\n", info->subject); 102 htmlf("Subject: %s\n\n", info->subject);
101 if (info->msg && *info->msg) { 103 if (info->msg && *info->msg) {
102 htmlf("%s", info->msg); 104 htmlf("%s", info->msg);
103 if (info->msg[strlen(info->msg) - 1] != '\n') 105 if (info->msg[strlen(info->msg) - 1] != '\n')
104 html("\n"); 106 html("\n");
105 } 107 }
106 html("---\n"); 108 html("---\n");
107 cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL); 109 cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL);
diff --git a/ui-repolist.c b/ui-repolist.c
index cd4e41d..e663585 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -42,26 +42,28 @@ static void print_modtime(struct cgit_repo *repo)
42 return; 42 return;
43 cgit_print_age(s.st_mtime, -1, NULL); 43 cgit_print_age(s.st_mtime, -1, NULL);
44} 44}
45 45
46void cgit_print_repolist(struct cacheitem *item) 46void cgit_print_repolist(struct cacheitem *item)
47{ 47{
48 int i, columns = 4; 48 int i, columns = 4;
49 char *last_group = NULL; 49 char *last_group = NULL;
50 50
51 if (ctx.cfg.enable_index_links) 51 if (ctx.cfg.enable_index_links)
52 columns++; 52 columns++;
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
57 html("<table summary='repository list' class='list nowrap'>"); 59 html("<table summary='repository list' class='list nowrap'>");
58 if (ctx.cfg.index_header) { 60 if (ctx.cfg.index_header) {
59 htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", 61 htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>",
60 columns); 62 columns);
61 html_include(ctx.cfg.index_header); 63 html_include(ctx.cfg.index_header);
62 html("</td></tr>"); 64 html("</td></tr>");
63 } 65 }
64 html("<tr class='nohover'>" 66 html("<tr class='nohover'>"
65 "<th class='left'>Name</th>" 67 "<th class='left'>Name</th>"
66 "<th class='left'>Description</th>" 68 "<th class='left'>Description</th>"
67 "<th class='left'>Owner</th>" 69 "<th class='left'>Owner</th>"
diff --git a/ui-shared.c b/ui-shared.c
index 2eff79d..2596023 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -17,32 +17,24 @@ static char *http_date(time_t t)
17{ 17{
18 static char day[][4] = 18 static char day[][4] =
19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
20 static char month[][4] = 20 static char month[][4] =
21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
23 struct tm *tm = gmtime(&t); 23 struct tm *tm = gmtime(&t);
24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
25 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 25 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
26 tm->tm_hour, tm->tm_min, tm->tm_sec); 26 tm->tm_hour, tm->tm_min, tm->tm_sec);
27} 27}
28 28
29static long ttl_seconds(long ttl)
30{
31 if (ttl<0)
32 return 60 * 60 * 24 * 365;
33 else
34 return ttl * 60;
35}
36
37void cgit_print_error(char *msg) 29void cgit_print_error(char *msg)
38{ 30{
39 html("<div class='error'>"); 31 html("<div class='error'>");
40 html_txt(msg); 32 html_txt(msg);
41 html("</div>\n"); 33 html("</div>\n");
42} 34}
43 35
44char *cgit_rooturl() 36char *cgit_rooturl()
45{ 37{
46 if (ctx.cfg.virtual_root) 38 if (ctx.cfg.virtual_root)
47 return fmt("%s/", ctx.cfg.virtual_root); 39 return fmt("%s/", ctx.cfg.virtual_root);
48 else 40 else
@@ -352,42 +344,52 @@ void cgit_print_age(time_t t, time_t max_relative, char *format)
352 secs * 1.0 / TM_WEEK); 344 secs * 1.0 / TM_WEEK);
353 return; 345 return;
354 } 346 }
355 if (secs < TM_YEAR * 2) { 347 if (secs < TM_YEAR * 2) {
356 htmlf("<span class='age-months'>%.0f months</span>", 348 htmlf("<span class='age-months'>%.0f months</span>",
357 secs * 1.0 / TM_MONTH); 349 secs * 1.0 / TM_MONTH);
358 return; 350 return;
359 } 351 }
360 htmlf("<span class='age-years'>%.0f years</span>", 352 htmlf("<span class='age-years'>%.0f years</span>",
361 secs * 1.0 / TM_YEAR); 353 secs * 1.0 / TM_YEAR);
362} 354}
363 355
364void cgit_print_docstart(char *title, struct cacheitem *item) 356void 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
371void cgit_print_docstart(struct cgit_context *ctx)
372{
371 html(cgit_doctype); 373 html(cgit_doctype);
372 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 374 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
373 html("<head>\n"); 375 html("<head>\n");
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");
383 html("</head>\n"); 385 html("</head>\n");
384 html("<body>\n"); 386 html("<body>\n");
385} 387}
386 388
387void cgit_print_docend() 389void cgit_print_docend()
388{ 390{
389 html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); 391 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
390} 392}
391 393
392int print_branch_option(const char *refname, const unsigned char *sha1, 394int print_branch_option(const char *refname, const unsigned char *sha1,
393 int flags, void *cb_data) 395 int flags, void *cb_data)
@@ -454,129 +456,117 @@ void add_hidden_formfields(int incl_head, int incl_search, char *page)
454 html_hidden("id", ctx.qry.sha1); 456 html_hidden("id", ctx.qry.sha1);
455 if (ctx.qry.sha2) 457 if (ctx.qry.sha2)
456 html_hidden("id2", ctx.qry.sha2); 458 html_hidden("id2", ctx.qry.sha2);
457 459
458 if (incl_search) { 460 if (incl_search) {
459 if (ctx.qry.grep) 461 if (ctx.qry.grep)
460 html_hidden("qt", ctx.qry.grep); 462 html_hidden("qt", ctx.qry.grep);
461 if (ctx.qry.search) 463 if (ctx.qry.search)
462 html_hidden("q", ctx.qry.search); 464 html_hidden("q", ctx.qry.search);
463 } 465 }
464} 466}
465 467
466void cgit_print_pageheader(char *title, int show_search) 468void cgit_print_pageheader(struct cgit_context *ctx)
467{ 469{
468 static const char *default_info = "This is cgit, a fast webinterface for git repositories"; 470 static const char *default_info = "This is cgit, a fast webinterface for git repositories";
469 int header = 0; 471 int header = 0;
470 char *url; 472 char *url;
471 473
472 html("<table id='layout' summary=''>\n"); 474 html("<table id='layout' summary=''>\n");
473 html("<tr><td id='sidebar'>\n"); 475 html("<tr><td id='sidebar'>\n");
474 html("<table class='sidebar' cellspacing='0' summary=''>\n"); 476 html("<table class='sidebar' cellspacing='0' summary=''>\n");
475 html("<tr><td class='sidebar'>\n<a href='"); 477 html("<tr><td class='sidebar'>\n<a href='");
476 html_attr(cgit_rooturl()); 478 html_attr(cgit_rooturl());
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
503 for_each_ref(print_archive_ref, &header); 505 for_each_ref(print_archive_ref, &header);
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='");
513 html_attr(url); 515 html_attr(url);
514 html("' title='"); 516 html("' title='");
515 html_attr(url); 517 html_attr(url);
516 html("'>\n"); 518 html("'>\n");
517 html_txt(strrpart(url, 20)); 519 html_txt(strrpart(url, 20));
518 html("</a>\n"); 520 html("</a>\n");
519 } 521 }
520 522
521 html("<h1>branch</h1>\n"); 523 html("<h1>branch</h1>\n");
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");
528 // html("</td><td>"); 530 // html("</td><td>");
529 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); 531 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
530 // html("</td></tr></table>"); 532 // html("</td></tr></table>");
531 html("</form>\n"); 533 html("</form>\n");
532 534
533 html("<h1>search</h1>\n"); 535 html("<h1>search</h1>\n");
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");
539 add_hidden_formfields(1, 0, "log"); 541 add_hidden_formfields(1, 0, "log");
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");
548 html("</form>\n"); 550 html("</form>\n");
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);
552 } 554 }
553 555
554 html("</td></tr></table></td>\n"); 556 html("</td></tr></table></td>\n");
555 557
556 html("<td id='content'>\n"); 558 html("<td id='content'>\n");
557} 559}
558 560
559
560void 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
571void cgit_print_filemode(unsigned short mode) 561void cgit_print_filemode(unsigned short mode)
572{ 562{
573 if (S_ISDIR(mode)) 563 if (S_ISDIR(mode))
574 html("d"); 564 html("d");
575 else if (S_ISLNK(mode)) 565 else if (S_ISLNK(mode))
576 html("l"); 566 html("l");
577 else if (S_ISGITLINK(mode)) 567 else if (S_ISGITLINK(mode))
578 html("m"); 568 html("m");
579 else 569 else
580 html("-"); 570 html("-");
581 html_fileperm(mode >> 6); 571 html_fileperm(mode >> 6);
582 html_fileperm(mode >> 3); 572 html_fileperm(mode >> 3);
diff --git a/ui-snapshot.c b/ui-snapshot.c
index 67dbbdd..4449803 100644
--- a/ui-snapshot.c
+++ b/ui-snapshot.c
@@ -92,25 +92,27 @@ void cgit_print_snapshot(struct cacheitem *item, const char *head,
92 cgit_print_error(fmt("Bad object id: %s", hex)); 92 cgit_print_error(fmt("Bad object id: %s", hex));
93 return; 93 return;
94 } 94 }
95 commit = lookup_commit_reference(sha1); 95 commit = lookup_commit_reference(sha1);
96 if(!commit) { 96 if(!commit) {
97 cgit_print_error(fmt("Not a commit reference: %s", hex)); 97 cgit_print_error(fmt("Not a commit reference: %s", hex));
98 return;; 98 return;;
99 } 99 }
100 memset(&args,0,sizeof(args)); 100 memset(&args,0,sizeof(args));
101 args.base = fmt("%s/", prefix); 101 args.base = fmt("%s/", prefix);
102 args.tree = commit->tree; 102 args.tree = commit->tree;
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);
106 return; 108 return;
107 } 109 }
108 cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); 110 cgit_print_error(fmt("Unsupported snapshot format: %s", filename));
109} 111}
110 112
111void cgit_print_snapshot_links(const char *repo, const char *head, 113void cgit_print_snapshot_links(const char *repo, const char *head,
112 const char *hex, int snapshots) 114 const char *hex, int snapshots)
113{ 115{
114 const struct snapshot_archive_t* sat; 116 const struct snapshot_archive_t* sat;
115 char *filename; 117 char *filename;
116 int f; 118 int f;