author | Lars Hjemli <hjemli@gmail.com> | 2008-05-03 08:10:07 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-05-03 08:10:07 (UTC) |
commit | e19683bedebc74593cb4c4518e47a334a5478e1e (patch) (unidiff) | |
tree | 8b4f231327d27b9451a6d9ab4b2af47558c61352 /cgit.c | |
parent | 112b2080626c62fff27cf8aaa9ac2fb07eb50b74 (diff) | |
parent | 9000bbf865cb3578ba5ed3810dc44253cb46ec7f (diff) | |
download | cgit-e19683bedebc74593cb4c4518e47a334a5478e1e.zip cgit-e19683bedebc74593cb4c4518e47a334a5478e1e.tar.gz cgit-e19683bedebc74593cb4c4518e47a334a5478e1e.tar.bz2 |
Merge branch 'lh/cache'
* lh/cache:
Add page 'ls_cache'
Redesign the caching layer
-rw-r--r-- | cgit.c | 166 |
1 files changed, 37 insertions, 129 deletions
@@ -1,475 +1,383 @@ | |||
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 "cache.h" | 10 | #include "cache.h" |
11 | #include "cmd.h" | 11 | #include "cmd.h" |
12 | #include "configfile.h" | 12 | #include "configfile.h" |
13 | #include "html.h" | 13 | #include "html.h" |
14 | #include "ui-shared.h" | 14 | #include "ui-shared.h" |
15 | 15 | ||
16 | const char *cgit_version = CGIT_VERSION; | 16 | const char *cgit_version = CGIT_VERSION; |
17 | 17 | ||
18 | void config_cb(const char *name, const char *value) | 18 | void config_cb(const char *name, const char *value) |
19 | { | 19 | { |
20 | if (!strcmp(name, "root-title")) | 20 | if (!strcmp(name, "root-title")) |
21 | ctx.cfg.root_title = xstrdup(value); | 21 | ctx.cfg.root_title = xstrdup(value); |
22 | else if (!strcmp(name, "root-desc")) | 22 | else if (!strcmp(name, "root-desc")) |
23 | ctx.cfg.root_desc = xstrdup(value); | 23 | ctx.cfg.root_desc = xstrdup(value); |
24 | else if (!strcmp(name, "root-readme")) | 24 | else if (!strcmp(name, "root-readme")) |
25 | ctx.cfg.root_readme = xstrdup(value); | 25 | ctx.cfg.root_readme = xstrdup(value); |
26 | else if (!strcmp(name, "css")) | 26 | else if (!strcmp(name, "css")) |
27 | ctx.cfg.css = xstrdup(value); | 27 | ctx.cfg.css = xstrdup(value); |
28 | else if (!strcmp(name, "logo")) | 28 | else if (!strcmp(name, "logo")) |
29 | ctx.cfg.logo = xstrdup(value); | 29 | ctx.cfg.logo = xstrdup(value); |
30 | else if (!strcmp(name, "index-header")) | 30 | else if (!strcmp(name, "index-header")) |
31 | ctx.cfg.index_header = xstrdup(value); | 31 | ctx.cfg.index_header = xstrdup(value); |
32 | else if (!strcmp(name, "index-info")) | 32 | else if (!strcmp(name, "index-info")) |
33 | ctx.cfg.index_info = xstrdup(value); | 33 | ctx.cfg.index_info = xstrdup(value); |
34 | else if (!strcmp(name, "logo-link")) | 34 | else if (!strcmp(name, "logo-link")) |
35 | ctx.cfg.logo_link = xstrdup(value); | 35 | ctx.cfg.logo_link = xstrdup(value); |
36 | else if (!strcmp(name, "module-link")) | 36 | else if (!strcmp(name, "module-link")) |
37 | ctx.cfg.module_link = xstrdup(value); | 37 | ctx.cfg.module_link = xstrdup(value); |
38 | else if (!strcmp(name, "virtual-root")) { | 38 | else if (!strcmp(name, "virtual-root")) { |
39 | ctx.cfg.virtual_root = trim_end(value, '/'); | 39 | ctx.cfg.virtual_root = trim_end(value, '/'); |
40 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | 40 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
41 | ctx.cfg.virtual_root = ""; | 41 | ctx.cfg.virtual_root = ""; |
42 | } else if (!strcmp(name, "nocache")) | 42 | } else if (!strcmp(name, "nocache")) |
43 | ctx.cfg.nocache = atoi(value); | 43 | ctx.cfg.nocache = atoi(value); |
44 | else if (!strcmp(name, "snapshots")) | 44 | else if (!strcmp(name, "snapshots")) |
45 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | 45 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
46 | else if (!strcmp(name, "enable-index-links")) | 46 | else if (!strcmp(name, "enable-index-links")) |
47 | ctx.cfg.enable_index_links = atoi(value); | 47 | ctx.cfg.enable_index_links = atoi(value); |
48 | else if (!strcmp(name, "enable-log-filecount")) | 48 | else if (!strcmp(name, "enable-log-filecount")) |
49 | ctx.cfg.enable_log_filecount = atoi(value); | 49 | ctx.cfg.enable_log_filecount = atoi(value); |
50 | else if (!strcmp(name, "enable-log-linecount")) | 50 | else if (!strcmp(name, "enable-log-linecount")) |
51 | ctx.cfg.enable_log_linecount = atoi(value); | 51 | ctx.cfg.enable_log_linecount = atoi(value); |
52 | else if (!strcmp(name, "cache-size")) | ||
53 | ctx.cfg.cache_size = atoi(value); | ||
52 | else if (!strcmp(name, "cache-root")) | 54 | else if (!strcmp(name, "cache-root")) |
53 | ctx.cfg.cache_root = xstrdup(value); | 55 | ctx.cfg.cache_root = xstrdup(value); |
54 | else if (!strcmp(name, "cache-root-ttl")) | 56 | else if (!strcmp(name, "cache-root-ttl")) |
55 | ctx.cfg.cache_root_ttl = atoi(value); | 57 | ctx.cfg.cache_root_ttl = atoi(value); |
56 | else if (!strcmp(name, "cache-repo-ttl")) | 58 | else if (!strcmp(name, "cache-repo-ttl")) |
57 | ctx.cfg.cache_repo_ttl = atoi(value); | 59 | ctx.cfg.cache_repo_ttl = atoi(value); |
58 | else if (!strcmp(name, "cache-static-ttl")) | 60 | else if (!strcmp(name, "cache-static-ttl")) |
59 | ctx.cfg.cache_static_ttl = atoi(value); | 61 | ctx.cfg.cache_static_ttl = atoi(value); |
60 | else if (!strcmp(name, "cache-dynamic-ttl")) | 62 | else if (!strcmp(name, "cache-dynamic-ttl")) |
61 | ctx.cfg.cache_dynamic_ttl = atoi(value); | 63 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
62 | else if (!strcmp(name, "max-message-length")) | 64 | else if (!strcmp(name, "max-message-length")) |
63 | ctx.cfg.max_msg_len = atoi(value); | 65 | ctx.cfg.max_msg_len = atoi(value); |
64 | else if (!strcmp(name, "max-repodesc-length")) | 66 | else if (!strcmp(name, "max-repodesc-length")) |
65 | ctx.cfg.max_repodesc_len = atoi(value); | 67 | ctx.cfg.max_repodesc_len = atoi(value); |
66 | else if (!strcmp(name, "max-commit-count")) | 68 | else if (!strcmp(name, "max-commit-count")) |
67 | ctx.cfg.max_commit_count = atoi(value); | 69 | ctx.cfg.max_commit_count = atoi(value); |
68 | else if (!strcmp(name, "summary-log")) | 70 | else if (!strcmp(name, "summary-log")) |
69 | ctx.cfg.summary_log = atoi(value); | 71 | ctx.cfg.summary_log = atoi(value); |
70 | else if (!strcmp(name, "summary-branches")) | 72 | else if (!strcmp(name, "summary-branches")) |
71 | ctx.cfg.summary_branches = atoi(value); | 73 | ctx.cfg.summary_branches = atoi(value); |
72 | else if (!strcmp(name, "summary-tags")) | 74 | else if (!strcmp(name, "summary-tags")) |
73 | ctx.cfg.summary_tags = atoi(value); | 75 | ctx.cfg.summary_tags = atoi(value); |
74 | else if (!strcmp(name, "agefile")) | 76 | else if (!strcmp(name, "agefile")) |
75 | ctx.cfg.agefile = xstrdup(value); | 77 | ctx.cfg.agefile = xstrdup(value); |
76 | else if (!strcmp(name, "renamelimit")) | 78 | else if (!strcmp(name, "renamelimit")) |
77 | ctx.cfg.renamelimit = atoi(value); | 79 | ctx.cfg.renamelimit = atoi(value); |
78 | else if (!strcmp(name, "robots")) | 80 | else if (!strcmp(name, "robots")) |
79 | ctx.cfg.robots = xstrdup(value); | 81 | ctx.cfg.robots = xstrdup(value); |
80 | else if (!strcmp(name, "clone-prefix")) | 82 | else if (!strcmp(name, "clone-prefix")) |
81 | ctx.cfg.clone_prefix = xstrdup(value); | 83 | ctx.cfg.clone_prefix = xstrdup(value); |
82 | else if (!strcmp(name, "repo.group")) | 84 | else if (!strcmp(name, "repo.group")) |
83 | ctx.cfg.repo_group = xstrdup(value); | 85 | ctx.cfg.repo_group = xstrdup(value); |
84 | else if (!strcmp(name, "repo.url")) | 86 | else if (!strcmp(name, "repo.url")) |
85 | ctx.repo = cgit_add_repo(value); | 87 | ctx.repo = cgit_add_repo(value); |
86 | else if (!strcmp(name, "repo.name")) | 88 | else if (!strcmp(name, "repo.name")) |
87 | ctx.repo->name = xstrdup(value); | 89 | ctx.repo->name = xstrdup(value); |
88 | else if (ctx.repo && !strcmp(name, "repo.path")) | 90 | else if (ctx.repo && !strcmp(name, "repo.path")) |
89 | ctx.repo->path = trim_end(value, '/'); | 91 | ctx.repo->path = trim_end(value, '/'); |
90 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | 92 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
91 | ctx.repo->clone_url = xstrdup(value); | 93 | ctx.repo->clone_url = xstrdup(value); |
92 | else if (ctx.repo && !strcmp(name, "repo.desc")) | 94 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
93 | ctx.repo->desc = xstrdup(value); | 95 | ctx.repo->desc = xstrdup(value); |
94 | else if (ctx.repo && !strcmp(name, "repo.owner")) | 96 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
95 | ctx.repo->owner = xstrdup(value); | 97 | ctx.repo->owner = xstrdup(value); |
96 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | 98 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
97 | ctx.repo->defbranch = xstrdup(value); | 99 | ctx.repo->defbranch = xstrdup(value); |
98 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | 100 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
99 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 101 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
100 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | 102 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
101 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | 103 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
102 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | 104 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
103 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | 105 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
104 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | 106 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
105 | ctx.repo->module_link= xstrdup(value); | 107 | ctx.repo->module_link= xstrdup(value); |
106 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | 108 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
107 | if (*value == '/') | 109 | if (*value == '/') |
108 | ctx.repo->readme = xstrdup(value); | 110 | ctx.repo->readme = xstrdup(value); |
109 | else | 111 | else |
110 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | 112 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
111 | } else if (!strcmp(name, "include")) | 113 | } else if (!strcmp(name, "include")) |
112 | parse_configfile(value, config_cb); | 114 | parse_configfile(value, config_cb); |
113 | } | 115 | } |
114 | 116 | ||
115 | static void querystring_cb(const char *name, const char *value) | 117 | static void querystring_cb(const char *name, const char *value) |
116 | { | 118 | { |
117 | if (!strcmp(name,"r")) { | 119 | if (!strcmp(name,"r")) { |
118 | ctx.qry.repo = xstrdup(value); | 120 | ctx.qry.repo = xstrdup(value); |
119 | ctx.repo = cgit_get_repoinfo(value); | 121 | ctx.repo = cgit_get_repoinfo(value); |
120 | } else if (!strcmp(name, "p")) { | 122 | } else if (!strcmp(name, "p")) { |
121 | ctx.qry.page = xstrdup(value); | 123 | ctx.qry.page = xstrdup(value); |
122 | } else if (!strcmp(name, "url")) { | 124 | } else if (!strcmp(name, "url")) { |
123 | cgit_parse_url(value); | 125 | cgit_parse_url(value); |
124 | } else if (!strcmp(name, "qt")) { | 126 | } else if (!strcmp(name, "qt")) { |
125 | ctx.qry.grep = xstrdup(value); | 127 | ctx.qry.grep = xstrdup(value); |
126 | } else if (!strcmp(name, "q")) { | 128 | } else if (!strcmp(name, "q")) { |
127 | ctx.qry.search = xstrdup(value); | 129 | ctx.qry.search = xstrdup(value); |
128 | } else if (!strcmp(name, "h")) { | 130 | } else if (!strcmp(name, "h")) { |
129 | ctx.qry.head = xstrdup(value); | 131 | ctx.qry.head = xstrdup(value); |
130 | ctx.qry.has_symref = 1; | 132 | ctx.qry.has_symref = 1; |
131 | } else if (!strcmp(name, "id")) { | 133 | } else if (!strcmp(name, "id")) { |
132 | ctx.qry.sha1 = xstrdup(value); | 134 | ctx.qry.sha1 = xstrdup(value); |
133 | ctx.qry.has_sha1 = 1; | 135 | ctx.qry.has_sha1 = 1; |
134 | } else if (!strcmp(name, "id2")) { | 136 | } else if (!strcmp(name, "id2")) { |
135 | ctx.qry.sha2 = xstrdup(value); | 137 | ctx.qry.sha2 = xstrdup(value); |
136 | ctx.qry.has_sha1 = 1; | 138 | ctx.qry.has_sha1 = 1; |
137 | } else if (!strcmp(name, "ofs")) { | 139 | } else if (!strcmp(name, "ofs")) { |
138 | ctx.qry.ofs = atoi(value); | 140 | ctx.qry.ofs = atoi(value); |
139 | } else if (!strcmp(name, "path")) { | 141 | } else if (!strcmp(name, "path")) { |
140 | ctx.qry.path = trim_end(value, '/'); | 142 | ctx.qry.path = trim_end(value, '/'); |
141 | } else if (!strcmp(name, "name")) { | 143 | } else if (!strcmp(name, "name")) { |
142 | ctx.qry.name = xstrdup(value); | 144 | ctx.qry.name = xstrdup(value); |
143 | } | 145 | } |
144 | } | 146 | } |
145 | 147 | ||
146 | static void prepare_context(struct cgit_context *ctx) | 148 | static void prepare_context(struct cgit_context *ctx) |
147 | { | 149 | { |
148 | memset(ctx, 0, sizeof(ctx)); | 150 | memset(ctx, 0, sizeof(ctx)); |
149 | ctx->cfg.agefile = "info/web/last-modified"; | 151 | ctx->cfg.agefile = "info/web/last-modified"; |
152 | ctx->cfg.nocache = 0; | ||
153 | ctx->cfg.cache_size = 0; | ||
150 | ctx->cfg.cache_dynamic_ttl = 5; | 154 | ctx->cfg.cache_dynamic_ttl = 5; |
151 | ctx->cfg.cache_max_create_time = 5; | 155 | ctx->cfg.cache_max_create_time = 5; |
152 | ctx->cfg.cache_repo_ttl = 5; | 156 | ctx->cfg.cache_repo_ttl = 5; |
153 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 157 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
154 | ctx->cfg.cache_root_ttl = 5; | 158 | ctx->cfg.cache_root_ttl = 5; |
155 | ctx->cfg.cache_static_ttl = -1; | 159 | ctx->cfg.cache_static_ttl = -1; |
156 | ctx->cfg.css = "/cgit.css"; | 160 | ctx->cfg.css = "/cgit.css"; |
157 | ctx->cfg.logo = "/git-logo.png"; | 161 | ctx->cfg.logo = "/git-logo.png"; |
158 | ctx->cfg.max_commit_count = 50; | 162 | ctx->cfg.max_commit_count = 50; |
159 | ctx->cfg.max_lock_attempts = 5; | 163 | ctx->cfg.max_lock_attempts = 5; |
160 | ctx->cfg.max_msg_len = 60; | 164 | ctx->cfg.max_msg_len = 60; |
161 | ctx->cfg.max_repodesc_len = 60; | 165 | ctx->cfg.max_repodesc_len = 60; |
162 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 166 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
163 | ctx->cfg.renamelimit = -1; | 167 | ctx->cfg.renamelimit = -1; |
164 | ctx->cfg.robots = "index, nofollow"; | 168 | ctx->cfg.robots = "index, nofollow"; |
165 | ctx->cfg.root_title = "Git repository browser"; | 169 | ctx->cfg.root_title = "Git repository browser"; |
166 | ctx->cfg.root_desc = "a fast webinterface for the git dscm"; | 170 | ctx->cfg.root_desc = "a fast webinterface for the git dscm"; |
167 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 171 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
168 | ctx->page.mimetype = "text/html"; | 172 | ctx->page.mimetype = "text/html"; |
169 | ctx->page.charset = PAGE_ENCODING; | 173 | ctx->page.charset = PAGE_ENCODING; |
170 | ctx->page.filename = NULL; | 174 | ctx->page.filename = NULL; |
171 | } | 175 | ctx->page.modified = time(NULL); |
172 | 176 | ctx->page.expires = ctx->page.modified; | |
173 | static int cgit_prepare_cache(struct cacheitem *item) | ||
174 | { | ||
175 | if (!ctx.repo && ctx.qry.repo) { | ||
176 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | ||
177 | "Bad request"); | ||
178 | cgit_print_http_headers(&ctx); | ||
179 | cgit_print_docstart(&ctx); | ||
180 | cgit_print_pageheader(&ctx); | ||
181 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | ||
182 | cgit_print_docend(); | ||
183 | return 0; | ||
184 | } | ||
185 | |||
186 | if (!ctx.repo) { | ||
187 | item->name = xstrdup(fmt("%s/index.%s.html", | ||
188 | ctx.cfg.cache_root, | ||
189 | cache_safe_filename(ctx.qry.raw))); | ||
190 | item->ttl = ctx.cfg.cache_root_ttl; | ||
191 | return 1; | ||
192 | } | ||
193 | |||
194 | if (!ctx.qry.page) { | ||
195 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | ||
196 | cache_safe_filename(ctx.repo->url), | ||
197 | cache_safe_filename(ctx.qry.raw))); | ||
198 | item->ttl = ctx.cfg.cache_repo_ttl; | ||
199 | } else { | ||
200 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | ||
201 | cache_safe_filename(ctx.repo->url), | ||
202 | ctx.qry.page, | ||
203 | cache_safe_filename(ctx.qry.raw))); | ||
204 | if (ctx.qry.has_symref) | ||
205 | item->ttl = ctx.cfg.cache_dynamic_ttl; | ||
206 | else if (ctx.qry.has_sha1) | ||
207 | item->ttl = ctx.cfg.cache_static_ttl; | ||
208 | else | ||
209 | item->ttl = ctx.cfg.cache_repo_ttl; | ||
210 | } | ||
211 | return 1; | ||
212 | } | 177 | } |
213 | 178 | ||
214 | struct refmatch { | 179 | struct refmatch { |
215 | char *req_ref; | 180 | char *req_ref; |
216 | char *first_ref; | 181 | char *first_ref; |
217 | int match; | 182 | int match; |
218 | }; | 183 | }; |
219 | 184 | ||
220 | int find_current_ref(const char *refname, const unsigned char *sha1, | 185 | int find_current_ref(const char *refname, const unsigned char *sha1, |
221 | int flags, void *cb_data) | 186 | int flags, void *cb_data) |
222 | { | 187 | { |
223 | struct refmatch *info; | 188 | struct refmatch *info; |
224 | 189 | ||
225 | info = (struct refmatch *)cb_data; | 190 | info = (struct refmatch *)cb_data; |
226 | if (!strcmp(refname, info->req_ref)) | 191 | if (!strcmp(refname, info->req_ref)) |
227 | info->match = 1; | 192 | info->match = 1; |
228 | if (!info->first_ref) | 193 | if (!info->first_ref) |
229 | info->first_ref = xstrdup(refname); | 194 | info->first_ref = xstrdup(refname); |
230 | return info->match; | 195 | return info->match; |
231 | } | 196 | } |
232 | 197 | ||
233 | char *find_default_branch(struct cgit_repo *repo) | 198 | char *find_default_branch(struct cgit_repo *repo) |
234 | { | 199 | { |
235 | struct refmatch info; | 200 | struct refmatch info; |
236 | 201 | ||
237 | info.req_ref = repo->defbranch; | 202 | info.req_ref = repo->defbranch; |
238 | info.first_ref = NULL; | 203 | info.first_ref = NULL; |
239 | info.match = 0; | 204 | info.match = 0; |
240 | for_each_branch_ref(find_current_ref, &info); | 205 | for_each_branch_ref(find_current_ref, &info); |
241 | if (info.match) | 206 | if (info.match) |
242 | return info.req_ref; | 207 | return info.req_ref; |
243 | else | 208 | else |
244 | return info.first_ref; | 209 | return info.first_ref; |
245 | } | 210 | } |
246 | 211 | ||
247 | static int prepare_repo_cmd(struct cgit_context *ctx) | 212 | static int prepare_repo_cmd(struct cgit_context *ctx) |
248 | { | 213 | { |
249 | char *tmp; | 214 | char *tmp; |
250 | unsigned char sha1[20]; | 215 | unsigned char sha1[20]; |
251 | int nongit = 0; | 216 | int nongit = 0; |
252 | 217 | ||
253 | setenv("GIT_DIR", ctx->repo->path, 1); | 218 | setenv("GIT_DIR", ctx->repo->path, 1); |
254 | setup_git_directory_gently(&nongit); | 219 | setup_git_directory_gently(&nongit); |
255 | if (nongit) { | 220 | if (nongit) { |
256 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 221 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
257 | "config error"); | 222 | "config error"); |
258 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | 223 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
259 | ctx->repo = NULL; | 224 | ctx->repo = NULL; |
260 | cgit_print_http_headers(ctx); | 225 | cgit_print_http_headers(ctx); |
261 | cgit_print_docstart(ctx); | 226 | cgit_print_docstart(ctx); |
262 | cgit_print_pageheader(ctx); | 227 | cgit_print_pageheader(ctx); |
263 | cgit_print_error(tmp); | 228 | cgit_print_error(tmp); |
264 | cgit_print_docend(); | 229 | cgit_print_docend(); |
265 | return 1; | 230 | return 1; |
266 | } | 231 | } |
267 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 232 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
268 | 233 | ||
269 | if (!ctx->qry.head) { | 234 | if (!ctx->qry.head) { |
270 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); | 235 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
271 | ctx->repo->defbranch = ctx->qry.head; | 236 | ctx->repo->defbranch = ctx->qry.head; |
272 | } | 237 | } |
273 | 238 | ||
274 | if (!ctx->qry.head) { | 239 | if (!ctx->qry.head) { |
275 | cgit_print_http_headers(ctx); | 240 | cgit_print_http_headers(ctx); |
276 | cgit_print_docstart(ctx); | 241 | cgit_print_docstart(ctx); |
277 | cgit_print_pageheader(ctx); | 242 | cgit_print_pageheader(ctx); |
278 | cgit_print_error("Repository seems to be empty"); | 243 | cgit_print_error("Repository seems to be empty"); |
279 | cgit_print_docend(); | 244 | cgit_print_docend(); |
280 | return 1; | 245 | return 1; |
281 | } | 246 | } |
282 | 247 | ||
283 | if (get_sha1(ctx->qry.head, sha1)) { | 248 | if (get_sha1(ctx->qry.head, sha1)) { |
284 | tmp = xstrdup(ctx->qry.head); | 249 | tmp = xstrdup(ctx->qry.head); |
285 | ctx->qry.head = ctx->repo->defbranch; | 250 | ctx->qry.head = ctx->repo->defbranch; |
286 | cgit_print_http_headers(ctx); | 251 | cgit_print_http_headers(ctx); |
287 | cgit_print_docstart(ctx); | 252 | cgit_print_docstart(ctx); |
288 | cgit_print_pageheader(ctx); | 253 | cgit_print_pageheader(ctx); |
289 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 254 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
290 | cgit_print_docend(); | 255 | cgit_print_docend(); |
291 | return 1; | 256 | return 1; |
292 | } | 257 | } |
293 | return 0; | 258 | return 0; |
294 | } | 259 | } |
295 | 260 | ||
296 | static void process_request(struct cgit_context *ctx) | 261 | static void process_request(void *cbdata) |
297 | { | 262 | { |
263 | struct cgit_context *ctx = cbdata; | ||
298 | struct cgit_cmd *cmd; | 264 | struct cgit_cmd *cmd; |
299 | 265 | ||
300 | cmd = cgit_get_cmd(ctx); | 266 | cmd = cgit_get_cmd(ctx); |
301 | if (!cmd) { | 267 | if (!cmd) { |
302 | ctx->page.title = "cgit error"; | 268 | ctx->page.title = "cgit error"; |
303 | ctx->repo = NULL; | 269 | ctx->repo = NULL; |
304 | cgit_print_http_headers(ctx); | 270 | cgit_print_http_headers(ctx); |
305 | cgit_print_docstart(ctx); | 271 | cgit_print_docstart(ctx); |
306 | cgit_print_pageheader(ctx); | 272 | cgit_print_pageheader(ctx); |
307 | cgit_print_error("Invalid request"); | 273 | cgit_print_error("Invalid request"); |
308 | cgit_print_docend(); | 274 | cgit_print_docend(); |
309 | return; | 275 | return; |
310 | } | 276 | } |
311 | 277 | ||
312 | if (cmd->want_repo && !ctx->repo) { | 278 | if (cmd->want_repo && !ctx->repo) { |
313 | cgit_print_http_headers(ctx); | 279 | cgit_print_http_headers(ctx); |
314 | cgit_print_docstart(ctx); | 280 | cgit_print_docstart(ctx); |
315 | cgit_print_pageheader(ctx); | 281 | cgit_print_pageheader(ctx); |
316 | cgit_print_error(fmt("No repository selected")); | 282 | cgit_print_error(fmt("No repository selected")); |
317 | cgit_print_docend(); | 283 | cgit_print_docend(); |
318 | return; | 284 | return; |
319 | } | 285 | } |
320 | 286 | ||
321 | if (ctx->repo && prepare_repo_cmd(ctx)) | 287 | if (ctx->repo && prepare_repo_cmd(ctx)) |
322 | return; | 288 | return; |
323 | 289 | ||
324 | if (cmd->want_layout) { | 290 | if (cmd->want_layout) { |
325 | cgit_print_http_headers(ctx); | 291 | cgit_print_http_headers(ctx); |
326 | cgit_print_docstart(ctx); | 292 | cgit_print_docstart(ctx); |
327 | cgit_print_pageheader(ctx); | 293 | cgit_print_pageheader(ctx); |
328 | } | 294 | } |
329 | 295 | ||
330 | cmd->fn(ctx); | 296 | cmd->fn(ctx); |
331 | 297 | ||
332 | if (cmd->want_layout) | 298 | if (cmd->want_layout) |
333 | cgit_print_docend(); | 299 | cgit_print_docend(); |
334 | } | 300 | } |
335 | 301 | ||
336 | static long ttl_seconds(long ttl) | ||
337 | { | ||
338 | if (ttl<0) | ||
339 | return 60 * 60 * 24 * 365; | ||
340 | else | ||
341 | return ttl * 60; | ||
342 | } | ||
343 | |||
344 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | ||
345 | { | ||
346 | int stdout2; | ||
347 | |||
348 | if (use_cache) { | ||
349 | stdout2 = chk_positive(dup(STDOUT_FILENO), | ||
350 | "Preserving STDOUT"); | ||
351 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | ||
352 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | ||
353 | } | ||
354 | |||
355 | ctx.page.modified = time(NULL); | ||
356 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | ||
357 | process_request(&ctx); | ||
358 | |||
359 | if (use_cache) { | ||
360 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | ||
361 | chk_positive(dup2(stdout2, STDOUT_FILENO), | ||
362 | "Restoring original STDOUT"); | ||
363 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | ||
364 | } | ||
365 | } | ||
366 | |||
367 | static void cgit_check_cache(struct cacheitem *item) | ||
368 | { | ||
369 | int i = 0; | ||
370 | |||
371 | top: | ||
372 | if (++i > ctx.cfg.max_lock_attempts) { | ||
373 | die("cgit_refresh_cache: unable to lock %s: %s", | ||
374 | item->name, strerror(errno)); | ||
375 | } | ||
376 | if (!cache_exist(item)) { | ||
377 | if (!cache_lock(item)) { | ||
378 | sleep(1); | ||
379 | goto top; | ||
380 | } | ||
381 | if (!cache_exist(item)) { | ||
382 | cgit_fill_cache(item, 1); | ||
383 | cache_unlock(item); | ||
384 | } else { | ||
385 | cache_cancel_lock(item); | ||
386 | } | ||
387 | } else if (cache_expired(item) && cache_lock(item)) { | ||
388 | if (cache_expired(item)) { | ||
389 | cgit_fill_cache(item, 1); | ||
390 | cache_unlock(item); | ||
391 | } else { | ||
392 | cache_cancel_lock(item); | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | |||
397 | static void cgit_print_cache(struct cacheitem *item) | ||
398 | { | ||
399 | static char buf[4096]; | ||
400 | ssize_t i; | ||
401 | |||
402 | int fd = open(item->name, O_RDONLY); | ||
403 | if (fd<0) | ||
404 | die("Unable to open cached file %s", item->name); | ||
405 | |||
406 | while((i=read(fd, buf, sizeof(buf))) > 0) | ||
407 | write(STDOUT_FILENO, buf, i); | ||
408 | |||
409 | close(fd); | ||
410 | } | ||
411 | |||
412 | static void cgit_parse_args(int argc, const char **argv) | 302 | static void cgit_parse_args(int argc, const char **argv) |
413 | { | 303 | { |
414 | int i; | 304 | int i; |
415 | 305 | ||
416 | for (i = 1; i < argc; i++) { | 306 | for (i = 1; i < argc; i++) { |
417 | if (!strncmp(argv[i], "--cache=", 8)) { | 307 | if (!strncmp(argv[i], "--cache=", 8)) { |
418 | ctx.cfg.cache_root = xstrdup(argv[i]+8); | 308 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
419 | } | 309 | } |
420 | if (!strcmp(argv[i], "--nocache")) { | 310 | if (!strcmp(argv[i], "--nocache")) { |
421 | ctx.cfg.nocache = 1; | 311 | ctx.cfg.nocache = 1; |
422 | } | 312 | } |
423 | if (!strncmp(argv[i], "--query=", 8)) { | 313 | if (!strncmp(argv[i], "--query=", 8)) { |
424 | ctx.qry.raw = xstrdup(argv[i]+8); | 314 | ctx.qry.raw = xstrdup(argv[i]+8); |
425 | } | 315 | } |
426 | if (!strncmp(argv[i], "--repo=", 7)) { | 316 | if (!strncmp(argv[i], "--repo=", 7)) { |
427 | ctx.qry.repo = xstrdup(argv[i]+7); | 317 | ctx.qry.repo = xstrdup(argv[i]+7); |
428 | } | 318 | } |
429 | if (!strncmp(argv[i], "--page=", 7)) { | 319 | if (!strncmp(argv[i], "--page=", 7)) { |
430 | ctx.qry.page = xstrdup(argv[i]+7); | 320 | ctx.qry.page = xstrdup(argv[i]+7); |
431 | } | 321 | } |
432 | if (!strncmp(argv[i], "--head=", 7)) { | 322 | if (!strncmp(argv[i], "--head=", 7)) { |
433 | ctx.qry.head = xstrdup(argv[i]+7); | 323 | ctx.qry.head = xstrdup(argv[i]+7); |
434 | ctx.qry.has_symref = 1; | 324 | ctx.qry.has_symref = 1; |
435 | } | 325 | } |
436 | if (!strncmp(argv[i], "--sha1=", 7)) { | 326 | if (!strncmp(argv[i], "--sha1=", 7)) { |
437 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 327 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
438 | ctx.qry.has_sha1 = 1; | 328 | ctx.qry.has_sha1 = 1; |
439 | } | 329 | } |
440 | if (!strncmp(argv[i], "--ofs=", 6)) { | 330 | if (!strncmp(argv[i], "--ofs=", 6)) { |
441 | ctx.qry.ofs = atoi(argv[i]+6); | 331 | ctx.qry.ofs = atoi(argv[i]+6); |
442 | } | 332 | } |
443 | } | 333 | } |
444 | } | 334 | } |
445 | 335 | ||
336 | static int calc_ttl() | ||
337 | { | ||
338 | if (!ctx.repo) | ||
339 | return ctx.cfg.cache_root_ttl; | ||
340 | |||
341 | if (!ctx.qry.page) | ||
342 | return ctx.cfg.cache_repo_ttl; | ||
343 | |||
344 | if (ctx.qry.has_symref) | ||
345 | return ctx.cfg.cache_dynamic_ttl; | ||
346 | |||
347 | if (ctx.qry.has_sha1) | ||
348 | return ctx.cfg.cache_static_ttl; | ||
349 | |||
350 | return ctx.cfg.cache_repo_ttl; | ||
351 | } | ||
352 | |||
446 | int main(int argc, const char **argv) | 353 | int main(int argc, const char **argv) |
447 | { | 354 | { |
448 | struct cacheitem item; | ||
449 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 355 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
356 | int err, ttl; | ||
450 | 357 | ||
451 | prepare_context(&ctx); | 358 | prepare_context(&ctx); |
452 | item.st.st_mtime = time(NULL); | ||
453 | cgit_repolist.length = 0; | 359 | cgit_repolist.length = 0; |
454 | cgit_repolist.count = 0; | 360 | cgit_repolist.count = 0; |
455 | cgit_repolist.repos = NULL; | 361 | cgit_repolist.repos = NULL; |
456 | 362 | ||
457 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 363 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
458 | config_cb); | 364 | config_cb); |
459 | ctx.repo = NULL; | 365 | ctx.repo = NULL; |
460 | if (getenv("SCRIPT_NAME")) | 366 | if (getenv("SCRIPT_NAME")) |
461 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); | 367 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
462 | if (getenv("QUERY_STRING")) | 368 | if (getenv("QUERY_STRING")) |
463 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 369 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
464 | cgit_parse_args(argc, argv); | 370 | cgit_parse_args(argc, argv); |
465 | http_parse_querystring(ctx.qry.raw, querystring_cb); | 371 | http_parse_querystring(ctx.qry.raw, querystring_cb); |
466 | if (!cgit_prepare_cache(&item)) | 372 | |
467 | return 0; | 373 | ttl = calc_ttl(); |
468 | if (ctx.cfg.nocache) { | 374 | ctx.page.expires += ttl*60; |
469 | cgit_fill_cache(&item, 0); | 375 | if (ctx.cfg.nocache) |
470 | } else { | 376 | ctx.cfg.cache_size = 0; |
471 | cgit_check_cache(&item); | 377 | err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, |
472 | cgit_print_cache(&item); | 378 | ctx.qry.raw, ttl, process_request, &ctx); |
473 | } | 379 | if (err) |
474 | return 0; | 380 | cache_log("[cgit] error %d - %s\n", |
381 | err, strerror(err)); | ||
382 | return err; | ||
475 | } | 383 | } |