summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c166
1 files changed, 37 insertions, 129 deletions
diff --git a/cgit.c b/cgit.c
index a402758..ccd61f4 100644
--- a/cgit.c
+++ b/cgit.c
@@ -40,24 +40,26 @@ void config_cb(const char *name, const char *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);
@@ -138,86 +140,49 @@ static void querystring_cb(const char *name, const char *value)
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
146static void prepare_context(struct cgit_context *ctx) 148static 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;
173static 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
214struct refmatch { 179struct 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
220int find_current_ref(const char *refname, const unsigned char *sha1, 185int 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;
@@ -284,26 +249,27 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
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
296static void process_request(struct cgit_context *ctx) 261static 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;
@@ -324,100 +290,24 @@ static void process_request(struct cgit_context *ctx)
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
336static long ttl_seconds(long ttl)
337{
338 if (ttl<0)
339 return 60 * 60 * 24 * 365;
340 else
341 return ttl * 60;
342}
343
344static 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
367static 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
397static 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
412static void cgit_parse_args(int argc, const char **argv) 302static 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)) {
@@ -434,42 +324,60 @@ static void cgit_parse_args(int argc, const char **argv)
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
336static 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
446int main(int argc, const char **argv) 353int 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}