/* * One thunk per page, each pulling the parts of the request its renderer * needs out of the global ctx, plus the table that maps a page name onto its * thunk. Keeping the thunks here lets every renderer take ordinary arguments * instead of reaching into the request state itself. */ #include "cache.h" #include "cgit.h" #include "cmd.h" #include "html.h" #include "ui-atom.h" #include "ui-blame.h" #include "ui-blob.h" #include "ui-clone.h" #include "ui-commit.h" #include "ui-diff.h" #include "ui-log.h" #include "ui-patch.h" #include "ui-plain.h" #include "ui-refs.h" #include "ui-repolist.h" #include "ui-shared.h" #include "ui-snapshot.h" #include "ui-stats.h" #include "ui-summary.h" #include "ui-tag.h" #include "ui-tree.h" static void head_fn(void) { cgit_clone_head(); } static void about_fn(void) { char *currenturl, *redirect; size_t path_info_len; if (!ctx.repo) { cgit_print_site_readme(); return; } // The about page resolves relative links against its own URL, so it // only works with a trailing slash. path_info_len = ctx.env.path_info ? strlen(ctx.env.path_info) : 0; if (!ctx.qry.path && (!ctx.qry.url || !*ctx.qry.url || ctx.qry.url[strlen(ctx.qry.url) - 1] != '/') && (!path_info_len || ctx.env.path_info[path_info_len - 1] != '/')) { currenturl = cgit_currenturl(); redirect = cgit_fmtalloc("%s/", currenturl); cgit_redirect(redirect, true); free(currenturl); free(redirect); } else if (ctx.repo->readme.nr) { cgit_print_repo_readme(ctx.qry.path); } else if (ctx.repo->homepage) { cgit_redirect(ctx.repo->homepage, false); } else { currenturl = cgit_currenturl(); redirect = cgit_fmtalloc("%s../", currenturl); cgit_redirect(redirect, false); free(currenturl); free(redirect); } } static void atom_fn(void) { cgit_print_atom(ctx.qry.head, ctx.qry.path, ctx.cfg.max_atom_items); } static void blame_fn(void) { if (ctx.repo->enable_blame) cgit_print_blame(); else cgit_print_error_page(403, "Forbidden", "Blame is disabled"); } static void blob_fn(void) { cgit_print_blob(ctx.qry.oid, ctx.qry.path, ctx.qry.head, 0); } static void commit_fn(void) { cgit_print_commit(ctx.qry.oid, ctx.qry.path); } static void diff_fn(void) { cgit_print_diff(ctx.qry.oid, ctx.qry.oid2, ctx.qry.path, 1, 0); } static void info_fn(void) { cgit_clone_info(); } static void log_fn(void) { cgit_print_log(ctx.qry.oid, ctx.qry.ofs, ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, ctx.qry.path, 1, ctx.repo->enable_commit_graph, ctx.repo->commit_sort); } static void ls_cache_fn(void) { // The listing names the cache path and the URLs other visitors asked // for, so it stays off until an administrator opts in. if (!ctx.cfg.enable_cache_list) { cgit_print_error_page(404, "Not found", "Not found"); return; } ctx.page.mimetype = "text/plain"; ctx.page.filename = "ls-cache.txt"; cgit_print_http_headers(); cache_ls(ctx.cfg.cache_root); } static void objects_fn(void) { cgit_clone_objects(); } static void patch_fn(void) { cgit_print_patch(ctx.qry.oid, ctx.qry.oid2, ctx.qry.path); } static void plain_fn(void) { cgit_print_plain(); } static void rawdiff_fn(void) { cgit_print_diff(ctx.qry.oid, ctx.qry.oid2, ctx.qry.path, 1, 1); } static void refs_fn(void) { cgit_print_refs(); } static void repolist_fn(void) { cgit_print_repolist(); } static void snapshot_fn(void) { cgit_print_snapshot(ctx.qry.head, ctx.qry.oid, ctx.qry.path, ctx.qry.nohead); } static void stats_fn(void) { if (ctx.repo->enable_stats) cgit_show_stats(); else cgit_print_error_page(404, "Not found", "Stats are disabled"); } static void summary_fn(void) { cgit_print_summary(); } static void tag_fn(void) { cgit_print_tag(ctx.qry.oid); } static void tree_fn(void) { cgit_print_tree(ctx.qry.oid, ctx.qry.path); } // The name is what a request spells as p=, so these strings are part of the // URL space and cannot be renamed. static const struct cgit_cmd commands[] = { { .name = "HEAD", .fn = head_fn, .want_repo = 1, .is_clone = 1 }, { .name = "about", .fn = about_fn }, { .name = "atom", .fn = atom_fn, .want_repo = 1 }, { .name = "blame", .fn = blame_fn, .want_repo = 1, .want_vpath = 1 }, { .name = "blob", .fn = blob_fn, .want_repo = 1 }, { .name = "commit", .fn = commit_fn, .want_repo = 1, .want_vpath = 1 }, { .name = "diff", .fn = diff_fn, .want_repo = 1, .want_vpath = 1 }, { .name = "info", .fn = info_fn, .want_repo = 1, .is_clone = 1 }, { .name = "log", .fn = log_fn, .want_repo = 1, .want_vpath = 1 }, { .name = "ls_cache", .fn = ls_cache_fn }, { .name = "objects", .fn = objects_fn, .want_repo = 1, .is_clone = 1 }, { .name = "patch", .fn = patch_fn, .want_repo = 1, .want_vpath = 1 }, { .name = "plain", .fn = plain_fn, .want_repo = 1 }, { .name = "rawdiff", .fn = rawdiff_fn, .want_repo = 1, .want_vpath = 1 }, { .name = "refs", .fn = refs_fn, .want_repo = 1 }, { .name = "repolist", .fn = repolist_fn }, { .name = "snapshot", .fn = snapshot_fn, .want_repo = 1 }, { .name = "stats", .fn = stats_fn, .want_repo = 1, .want_vpath = 1 }, { .name = "summary", .fn = summary_fn, .want_repo = 1 }, { .name = "tag", .fn = tag_fn, .want_repo = 1 }, { .name = "tree", .fn = tree_fn, .want_repo = 1, .want_vpath = 1 }, }; const struct cgit_cmd *cgit_get_cmd(void) { size_t i; if (ctx.qry.page == NULL) { if (ctx.repo) ctx.qry.page = "summary"; else ctx.qry.page = "repolist"; } for (i = 0; i < ARRAY_SIZE(commands); i++) if (!strcmp(ctx.qry.page, commands[i].name)) return &commands[i]; return NULL; }