|
diff --git a/cgit.c b/cgit.c index 166fbc6..d1cee58 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -250,227 +250,228 @@ static int prepare_repo_cmd(struct cgit_context *ctx) |
250 | ctx->repo = NULL; |
250 | ctx->repo = NULL; |
251 | cgit_print_http_headers(ctx); |
251 | cgit_print_http_headers(ctx); |
252 | cgit_print_docstart(ctx); |
252 | cgit_print_docstart(ctx); |
253 | cgit_print_pageheader(ctx); |
253 | cgit_print_pageheader(ctx); |
254 | cgit_print_error(tmp); |
254 | cgit_print_error(tmp); |
255 | cgit_print_docend(); |
255 | cgit_print_docend(); |
256 | return 1; |
256 | return 1; |
257 | } |
257 | } |
258 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
258 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
259 | |
259 | |
260 | if (!ctx->qry.head) { |
260 | if (!ctx->qry.head) { |
261 | ctx->qry.nohead = 1; |
261 | ctx->qry.nohead = 1; |
262 | ctx->qry.head = find_default_branch(ctx->repo); |
262 | ctx->qry.head = find_default_branch(ctx->repo); |
263 | ctx->repo->defbranch = ctx->qry.head; |
263 | ctx->repo->defbranch = ctx->qry.head; |
264 | } |
264 | } |
265 | |
265 | |
266 | if (!ctx->qry.head) { |
266 | if (!ctx->qry.head) { |
267 | cgit_print_http_headers(ctx); |
267 | cgit_print_http_headers(ctx); |
268 | cgit_print_docstart(ctx); |
268 | cgit_print_docstart(ctx); |
269 | cgit_print_pageheader(ctx); |
269 | cgit_print_pageheader(ctx); |
270 | cgit_print_error("Repository seems to be empty"); |
270 | cgit_print_error("Repository seems to be empty"); |
271 | cgit_print_docend(); |
271 | cgit_print_docend(); |
272 | return 1; |
272 | return 1; |
273 | } |
273 | } |
274 | |
274 | |
275 | if (get_sha1(ctx->qry.head, sha1)) { |
275 | if (get_sha1(ctx->qry.head, sha1)) { |
276 | tmp = xstrdup(ctx->qry.head); |
276 | tmp = xstrdup(ctx->qry.head); |
277 | ctx->qry.head = ctx->repo->defbranch; |
277 | ctx->qry.head = ctx->repo->defbranch; |
278 | cgit_print_http_headers(ctx); |
278 | cgit_print_http_headers(ctx); |
279 | cgit_print_docstart(ctx); |
279 | cgit_print_docstart(ctx); |
280 | cgit_print_pageheader(ctx); |
280 | cgit_print_pageheader(ctx); |
281 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
281 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
282 | cgit_print_docend(); |
282 | cgit_print_docend(); |
283 | return 1; |
283 | return 1; |
284 | } |
284 | } |
285 | return 0; |
285 | return 0; |
286 | } |
286 | } |
287 | |
287 | |
288 | static void process_request(void *cbdata) |
288 | static void process_request(void *cbdata) |
289 | { |
289 | { |
290 | struct cgit_context *ctx = cbdata; |
290 | struct cgit_context *ctx = cbdata; |
291 | struct cgit_cmd *cmd; |
291 | struct cgit_cmd *cmd; |
292 | |
292 | |
293 | cmd = cgit_get_cmd(ctx); |
293 | cmd = cgit_get_cmd(ctx); |
294 | if (!cmd) { |
294 | if (!cmd) { |
295 | ctx->page.title = "cgit error"; |
295 | ctx->page.title = "cgit error"; |
296 | ctx->repo = NULL; |
296 | ctx->repo = NULL; |
297 | cgit_print_http_headers(ctx); |
297 | cgit_print_http_headers(ctx); |
298 | cgit_print_docstart(ctx); |
298 | cgit_print_docstart(ctx); |
299 | cgit_print_pageheader(ctx); |
299 | cgit_print_pageheader(ctx); |
300 | cgit_print_error("Invalid request"); |
300 | cgit_print_error("Invalid request"); |
301 | cgit_print_docend(); |
301 | cgit_print_docend(); |
302 | return; |
302 | return; |
303 | } |
303 | } |
304 | |
304 | |
305 | if (cmd->want_repo && !ctx->repo) { |
305 | if (cmd->want_repo && !ctx->repo) { |
306 | cgit_print_http_headers(ctx); |
306 | cgit_print_http_headers(ctx); |
307 | cgit_print_docstart(ctx); |
307 | cgit_print_docstart(ctx); |
308 | cgit_print_pageheader(ctx); |
308 | cgit_print_pageheader(ctx); |
309 | cgit_print_error(fmt("No repository selected")); |
309 | cgit_print_error(fmt("No repository selected")); |
310 | cgit_print_docend(); |
310 | cgit_print_docend(); |
311 | return; |
311 | return; |
312 | } |
312 | } |
313 | |
313 | |
314 | if (ctx->repo && prepare_repo_cmd(ctx)) |
314 | if (ctx->repo && prepare_repo_cmd(ctx)) |
315 | return; |
315 | return; |
316 | |
316 | |
317 | if (cmd->want_layout) { |
317 | if (cmd->want_layout) { |
318 | cgit_print_http_headers(ctx); |
318 | cgit_print_http_headers(ctx); |
319 | cgit_print_docstart(ctx); |
319 | cgit_print_docstart(ctx); |
320 | cgit_print_pageheader(ctx); |
320 | cgit_print_pageheader(ctx); |
321 | } |
321 | } |
322 | |
322 | |
323 | cmd->fn(ctx); |
323 | cmd->fn(ctx); |
324 | |
324 | |
325 | if (cmd->want_layout) |
325 | if (cmd->want_layout) |
326 | cgit_print_docend(); |
326 | cgit_print_docend(); |
327 | } |
327 | } |
328 | |
328 | |
329 | int cmp_repos(const void *a, const void *b) |
329 | int cmp_repos(const void *a, const void *b) |
330 | { |
330 | { |
331 | const struct cgit_repo *ra = a, *rb = b; |
331 | const struct cgit_repo *ra = a, *rb = b; |
332 | return strcmp(ra->url, rb->url); |
332 | return strcmp(ra->url, rb->url); |
333 | } |
333 | } |
334 | |
334 | |
335 | void print_repo(struct cgit_repo *repo) |
335 | void print_repo(struct cgit_repo *repo) |
336 | { |
336 | { |
337 | printf("repo.url=%s\n", repo->url); |
337 | printf("repo.url=%s\n", repo->url); |
338 | printf("repo.name=%s\n", repo->name); |
338 | printf("repo.name=%s\n", repo->name); |
339 | printf("repo.path=%s\n", repo->path); |
339 | printf("repo.path=%s\n", repo->path); |
340 | if (repo->owner) |
340 | if (repo->owner) |
341 | printf("repo.owner=%s\n", repo->owner); |
341 | printf("repo.owner=%s\n", repo->owner); |
342 | if (repo->desc) |
342 | if (repo->desc) |
343 | printf("repo.desc=%s\n", repo->desc); |
343 | printf("repo.desc=%s\n", repo->desc); |
344 | if (repo->readme) |
344 | if (repo->readme) |
345 | printf("repo.readme=%s\n", repo->readme); |
345 | printf("repo.readme=%s\n", repo->readme); |
346 | printf("\n"); |
346 | printf("\n"); |
347 | } |
347 | } |
348 | |
348 | |
349 | void print_repolist(struct cgit_repolist *list) |
349 | void print_repolist(struct cgit_repolist *list) |
350 | { |
350 | { |
351 | int i; |
351 | int i; |
352 | |
352 | |
353 | for(i = 0; i < list->count; i++) |
353 | for(i = 0; i < list->count; i++) |
354 | print_repo(&list->repos[i]); |
354 | print_repo(&list->repos[i]); |
355 | } |
355 | } |
356 | |
356 | |
357 | |
357 | |
358 | static void cgit_parse_args(int argc, const char **argv) |
358 | static void cgit_parse_args(int argc, const char **argv) |
359 | { |
359 | { |
360 | int i; |
360 | int i; |
361 | int scan = 0; |
361 | int scan = 0; |
362 | |
362 | |
363 | for (i = 1; i < argc; i++) { |
363 | for (i = 1; i < argc; i++) { |
364 | if (!strncmp(argv[i], "--cache=", 8)) { |
364 | if (!strncmp(argv[i], "--cache=", 8)) { |
365 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
365 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
366 | } |
366 | } |
367 | if (!strcmp(argv[i], "--nocache")) { |
367 | if (!strcmp(argv[i], "--nocache")) { |
368 | ctx.cfg.nocache = 1; |
368 | ctx.cfg.nocache = 1; |
369 | } |
369 | } |
370 | if (!strncmp(argv[i], "--query=", 8)) { |
370 | if (!strncmp(argv[i], "--query=", 8)) { |
371 | ctx.qry.raw = xstrdup(argv[i]+8); |
371 | ctx.qry.raw = xstrdup(argv[i]+8); |
372 | } |
372 | } |
373 | if (!strncmp(argv[i], "--repo=", 7)) { |
373 | if (!strncmp(argv[i], "--repo=", 7)) { |
374 | ctx.qry.repo = xstrdup(argv[i]+7); |
374 | ctx.qry.repo = xstrdup(argv[i]+7); |
375 | } |
375 | } |
376 | if (!strncmp(argv[i], "--page=", 7)) { |
376 | if (!strncmp(argv[i], "--page=", 7)) { |
377 | ctx.qry.page = xstrdup(argv[i]+7); |
377 | ctx.qry.page = xstrdup(argv[i]+7); |
378 | } |
378 | } |
379 | if (!strncmp(argv[i], "--head=", 7)) { |
379 | if (!strncmp(argv[i], "--head=", 7)) { |
380 | ctx.qry.head = xstrdup(argv[i]+7); |
380 | ctx.qry.head = xstrdup(argv[i]+7); |
381 | ctx.qry.has_symref = 1; |
381 | ctx.qry.has_symref = 1; |
382 | } |
382 | } |
383 | if (!strncmp(argv[i], "--sha1=", 7)) { |
383 | if (!strncmp(argv[i], "--sha1=", 7)) { |
384 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
384 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
385 | ctx.qry.has_sha1 = 1; |
385 | ctx.qry.has_sha1 = 1; |
386 | } |
386 | } |
387 | if (!strncmp(argv[i], "--ofs=", 6)) { |
387 | if (!strncmp(argv[i], "--ofs=", 6)) { |
388 | ctx.qry.ofs = atoi(argv[i]+6); |
388 | ctx.qry.ofs = atoi(argv[i]+6); |
389 | } |
389 | } |
390 | if (!strncmp(argv[i], "--scan-tree=", 12)) { |
390 | if (!strncmp(argv[i], "--scan-tree=", 12)) { |
391 | scan++; |
391 | scan++; |
392 | scan_tree(argv[i] + 12); |
392 | scan_tree(argv[i] + 12); |
393 | } |
393 | } |
394 | } |
394 | } |
395 | if (scan) { |
395 | if (scan) { |
396 | qsort(cgit_repolist.repos, cgit_repolist.count, |
396 | qsort(cgit_repolist.repos, cgit_repolist.count, |
397 | sizeof(struct cgit_repo), cmp_repos); |
397 | sizeof(struct cgit_repo), cmp_repos); |
398 | print_repolist(&cgit_repolist); |
398 | print_repolist(&cgit_repolist); |
399 | exit(0); |
399 | exit(0); |
400 | } |
400 | } |
401 | } |
401 | } |
402 | |
402 | |
403 | static int calc_ttl() |
403 | static int calc_ttl() |
404 | { |
404 | { |
405 | if (!ctx.repo) |
405 | if (!ctx.repo) |
406 | return ctx.cfg.cache_root_ttl; |
406 | return ctx.cfg.cache_root_ttl; |
407 | |
407 | |
408 | if (!ctx.qry.page) |
408 | if (!ctx.qry.page) |
409 | return ctx.cfg.cache_repo_ttl; |
409 | return ctx.cfg.cache_repo_ttl; |
410 | |
410 | |
411 | if (ctx.qry.has_symref) |
411 | if (ctx.qry.has_symref) |
412 | return ctx.cfg.cache_dynamic_ttl; |
412 | return ctx.cfg.cache_dynamic_ttl; |
413 | |
413 | |
414 | if (ctx.qry.has_sha1) |
414 | if (ctx.qry.has_sha1) |
415 | return ctx.cfg.cache_static_ttl; |
415 | return ctx.cfg.cache_static_ttl; |
416 | |
416 | |
417 | return ctx.cfg.cache_repo_ttl; |
417 | return ctx.cfg.cache_repo_ttl; |
418 | } |
418 | } |
419 | |
419 | |
420 | int main(int argc, const char **argv) |
420 | int main(int argc, const char **argv) |
421 | { |
421 | { |
422 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
422 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
423 | const char *path; |
423 | const char *path; |
424 | char *qry; |
424 | char *qry; |
425 | int err, ttl; |
425 | int err, ttl; |
426 | |
426 | |
427 | prepare_context(&ctx); |
427 | prepare_context(&ctx); |
428 | cgit_repolist.length = 0; |
428 | cgit_repolist.length = 0; |
429 | cgit_repolist.count = 0; |
429 | cgit_repolist.count = 0; |
430 | cgit_repolist.repos = NULL; |
430 | cgit_repolist.repos = NULL; |
431 | |
431 | |
432 | if (getenv("SCRIPT_NAME")) |
432 | if (getenv("SCRIPT_NAME")) |
433 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
433 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
434 | if (getenv("QUERY_STRING")) |
434 | if (getenv("QUERY_STRING")) |
435 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
435 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
436 | cgit_parse_args(argc, argv); |
436 | cgit_parse_args(argc, argv); |
437 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
437 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
438 | config_cb); |
438 | config_cb); |
439 | ctx.repo = NULL; |
439 | ctx.repo = NULL; |
440 | http_parse_querystring(ctx.qry.raw, querystring_cb); |
440 | http_parse_querystring(ctx.qry.raw, querystring_cb); |
441 | |
441 | |
442 | /* If virtual-root isn't specified in cgitrc and no url |
442 | /* If virtual-root isn't specified in cgitrc, lets pretend |
443 | * parameter is specified on the querystring, lets pretend |
443 | * that virtual-root equals SCRIPT_NAME. |
444 | * that virtualroot equals SCRIPT_NAME and use PATH_INFO as |
| |
445 | * url. This allows cgit to work with virtual urls without |
| |
446 | * the need for rewriterules in the webserver (as long as |
| |
447 | * PATH_INFO is included in the cache lookup key). |
| |
448 | */ |
444 | */ |
449 | if (!ctx.cfg.virtual_root && !ctx.qry.url) { |
445 | if (!ctx.cfg.virtual_root) |
450 | ctx.cfg.virtual_root = ctx.cfg.script_name; |
446 | ctx.cfg.virtual_root = ctx.cfg.script_name; |
451 | path = getenv("PATH_INFO"); |
447 | |
452 | if (path) { |
448 | /* If no url parameter is specified on the querystring, lets |
453 | if (path[0] == '/') |
449 | * use PATH_INFO as url. This allows cgit to work with virtual |
454 | path++; |
450 | * urls without the need for rewriterules in the webserver (as |
455 | ctx.qry.url = xstrdup(path); |
451 | * long as PATH_INFO is included in the cache lookup key). |
456 | if (ctx.qry.raw) { |
452 | */ |
457 | qry = ctx.qry.raw; |
453 | path = getenv("PATH_INFO"); |
458 | ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); |
454 | if (!ctx.qry.url && path) { |
459 | free(qry); |
455 | if (path[0] == '/') |
460 | } else |
456 | path++; |
461 | ctx.qry.raw = ctx.qry.url; |
457 | ctx.qry.url = xstrdup(path); |
462 | cgit_parse_url(ctx.qry.url); |
458 | if (ctx.qry.raw) { |
463 | } |
459 | qry = ctx.qry.raw; |
| |
460 | ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); |
| |
461 | free(qry); |
| |
462 | } else |
| |
463 | ctx.qry.raw = ctx.qry.url; |
| |
464 | cgit_parse_url(ctx.qry.url); |
464 | } |
465 | } |
465 | |
466 | |
466 | ttl = calc_ttl(); |
467 | ttl = calc_ttl(); |
467 | ctx.page.expires += ttl*60; |
468 | ctx.page.expires += ttl*60; |
468 | if (ctx.cfg.nocache) |
469 | if (ctx.cfg.nocache) |
469 | ctx.cfg.cache_size = 0; |
470 | ctx.cfg.cache_size = 0; |
470 | err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, |
471 | err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, |
471 | ctx.qry.raw, ttl, process_request, &ctx); |
472 | ctx.qry.raw, ttl, process_request, &ctx); |
472 | if (err) |
473 | if (err) |
473 | cgit_print_error(fmt("Error processing page: %s (%d)", |
474 | cgit_print_error(fmt("Error processing page: %s (%d)", |
474 | strerror(err), err)); |
475 | strerror(err), err)); |
475 | return err; |
476 | return err; |
476 | } |
477 | } |
|