1 /* ui-plain.c: functions for output of plain blobs by path
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
16 struct walk_tree_context {
21 static int print_object(const struct object_id *oid, const char *path)
23 enum object_type type;
27 type = oid_object_info(the_repository, oid, &size);
28 if (type == OBJ_BAD) {
29 cgit_print_error_page(404, "Not found", "Not found");
33 buf = repo_read_object_file(the_repository, oid, &type, &size);
35 cgit_print_error_page(404, "Not found", "Not found");
39 mimetype = get_mimetype_for_filename(path);
40 ctx.page.mimetype = mimetype;
42 if (!ctx.repo->enable_html_serving) {
43 html("X-Content-Type-Options: nosniff\n");
44 html("Content-Security-Policy: default-src 'none'\n");
46 /* Built-in white list allows PDF and everything that isn't text/ and application/ */
47 if ((!strncmp(mimetype, "text/", 5) || !strncmp(mimetype, "application/", 12)) && strcmp(mimetype, "application/pdf"))
48 ctx.page.mimetype = NULL;
52 if (!ctx.page.mimetype) {
53 if (buffer_is_binary(buf, size)) {
54 ctx.page.mimetype = "application/octet-stream";
55 ctx.page.charset = NULL;
57 ctx.page.mimetype = "text/plain";
60 ctx.page.filename = path;
62 ctx.page.etag = oid_to_hex(oid);
63 cgit_print_http_headers();
70 static char *buildpath(const char *base, int baselen, const char *path)
73 return fmtalloc("%.*s%s/", baselen, base, path);
75 return fmtalloc("%.*s/", baselen, base);
78 static void print_dir(const struct object_id *oid, const char *base,
79 int baselen, const char *path)
81 char *fullpath, *slash;
84 fullpath = buildpath(base, baselen, path);
85 slash = (fullpath[0] == '/' ? "" : "/");
86 ctx.page.etag = oid_to_hex(oid);
87 cgit_print_http_headers();
88 htmlf("<html><head><title>%s", slash);
90 htmlf("</title></head>\n<body>\n<h2>%s", slash);
92 html("</h2>\n<ul>\n");
93 len = strlen(fullpath);
95 fullpath[len - 1] = 0;
96 slash = strrchr(fullpath, '/');
104 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.oid,
111 static void print_dir_entry(const struct object_id *oid, const char *base,
112 int baselen, const char *path, unsigned mode)
116 fullpath = buildpath(base, baselen, path);
117 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
118 fullpath[strlen(fullpath) - 1] = 0;
120 if (S_ISGITLINK(mode)) {
121 cgit_submodule_link(NULL, fullpath, oid_to_hex(oid));
123 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.oid,
129 static void print_dir_tail(void)
131 html(" </ul>\n</body></html>\n");
134 static int walk_tree(const struct object_id *oid, struct strbuf *base,
135 const char *pathname, unsigned mode, void *cbdata)
137 struct walk_tree_context *walk_tree_ctx = cbdata;
139 if (base->len == walk_tree_ctx->match_baselen) {
140 if (S_ISREG(mode) || S_ISLNK(mode)) {
141 if (print_object(oid, pathname))
142 walk_tree_ctx->match = 1;
143 } else if (S_ISDIR(mode)) {
144 print_dir(oid, base->buf, base->len, pathname);
145 walk_tree_ctx->match = 2;
146 return READ_TREE_RECURSIVE;
148 } else if (base->len < INT_MAX && (int)base->len > walk_tree_ctx->match_baselen) {
149 print_dir_entry(oid, base->buf, base->len, pathname, mode);
150 walk_tree_ctx->match = 2;
151 } else if (S_ISDIR(mode)) {
152 return READ_TREE_RECURSIVE;
158 static int basedir_len(const char *path)
160 char *p = strrchr(path, '/');
166 void cgit_print_plain(void)
168 const char *rev = ctx.qry.oid;
169 struct object_id oid;
170 struct commit *commit;
171 struct pathspec_item path_items = {
172 .match = ctx.qry.path,
173 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
175 struct pathspec paths = {
179 struct walk_tree_context walk_tree_ctx = {
186 if (repo_get_oid(the_repository, rev, &oid)) {
187 cgit_print_error_page(404, "Not found", "Not found");
190 commit = lookup_commit_reference(the_repository, &oid);
191 if (!commit || repo_parse_commit(the_repository, commit)) {
192 cgit_print_error_page(404, "Not found", "Not found");
195 if (!path_items.match) {
196 path_items.match = "";
197 walk_tree_ctx.match_baselen = -1;
198 print_dir(get_commit_tree_oid(commit), "", 0, "");
199 walk_tree_ctx.match = 2;
202 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
203 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
204 &paths, walk_tree, &walk_tree_ctx);
205 if (!walk_tree_ctx.match)
206 cgit_print_error_page(404, "Not found", "Not found");
207 else if (walk_tree_ctx.match == 2)