ui-blob: Do not accept mimetype from user
[cgit.git] / ui-plain.c
blob58addab946af588fcb458026f9e7f5f731e6c7f5
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)
7 */
9 #include "cgit.h"
10 #include "ui-plain.h"
11 #include "html.h"
12 #include "ui-shared.h"
14 struct walk_tree_context {
15 int match_baselen;
16 int match;
19 static int print_object(const unsigned char *sha1, const char *path)
21 enum object_type type;
22 char *buf, *mimetype;
23 unsigned long size;
25 type = sha1_object_info(sha1, &size);
26 if (type == OBJ_BAD) {
27 cgit_print_error_page(404, "Not found", "Not found");
28 return 0;
31 buf = read_sha1_file(sha1, &type, &size);
32 if (!buf) {
33 cgit_print_error_page(404, "Not found", "Not found");
34 return 0;
37 mimetype = get_mimetype_for_filename(path);
38 ctx.page.mimetype = mimetype;
40 if (!ctx.page.mimetype) {
41 if (buffer_is_binary(buf, size)) {
42 ctx.page.mimetype = "application/octet-stream";
43 ctx.page.charset = NULL;
44 } else {
45 ctx.page.mimetype = "text/plain";
48 ctx.page.filename = path;
49 ctx.page.size = size;
50 ctx.page.etag = sha1_to_hex(sha1);
51 cgit_print_http_headers();
52 html_raw(buf, size);
53 free(mimetype);
54 free(buf);
55 return 1;
58 static char *buildpath(const char *base, int baselen, const char *path)
60 if (path[0])
61 return fmtalloc("%.*s%s/", baselen, base, path);
62 else
63 return fmtalloc("%.*s/", baselen, base);
66 static void print_dir(const unsigned char *sha1, const char *base,
67 int baselen, const char *path)
69 char *fullpath, *slash;
70 size_t len;
72 fullpath = buildpath(base, baselen, path);
73 slash = (fullpath[0] == '/' ? "" : "/");
74 ctx.page.etag = sha1_to_hex(sha1);
75 cgit_print_http_headers();
76 htmlf("<html><head><title>%s", slash);
77 html_txt(fullpath);
78 htmlf("</title></head>\n<body>\n<h2>%s", slash);
79 html_txt(fullpath);
80 html("</h2>\n<ul>\n");
81 len = strlen(fullpath);
82 if (len > 1) {
83 fullpath[len - 1] = 0;
84 slash = strrchr(fullpath, '/');
85 if (slash)
86 *(slash + 1) = 0;
87 else {
88 free(fullpath);
89 fullpath = NULL;
91 html("<li>");
92 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
93 fullpath);
94 html("</li>\n");
96 free(fullpath);
99 static void print_dir_entry(const unsigned char *sha1, const char *base,
100 int baselen, const char *path, unsigned mode)
102 char *fullpath;
104 fullpath = buildpath(base, baselen, path);
105 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
106 fullpath[strlen(fullpath) - 1] = 0;
107 html(" <li>");
108 if (S_ISGITLINK(mode)) {
109 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
110 } else
111 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
112 fullpath);
113 html("</li>\n");
114 free(fullpath);
117 static void print_dir_tail(void)
119 html(" </ul>\n</body></html>\n");
122 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
123 const char *pathname, unsigned mode, int stage, void *cbdata)
125 struct walk_tree_context *walk_tree_ctx = cbdata;
127 if (base->len == walk_tree_ctx->match_baselen) {
128 if (S_ISREG(mode)) {
129 if (print_object(sha1, pathname))
130 walk_tree_ctx->match = 1;
131 } else if (S_ISDIR(mode)) {
132 print_dir(sha1, base->buf, base->len, pathname);
133 walk_tree_ctx->match = 2;
134 return READ_TREE_RECURSIVE;
136 } else if (base->len > walk_tree_ctx->match_baselen) {
137 print_dir_entry(sha1, base->buf, base->len, pathname, mode);
138 walk_tree_ctx->match = 2;
139 } else if (S_ISDIR(mode)) {
140 return READ_TREE_RECURSIVE;
143 return 0;
146 static int basedir_len(const char *path)
148 char *p = strrchr(path, '/');
149 if (p)
150 return p - path + 1;
151 return 0;
154 void cgit_print_plain(void)
156 const char *rev = ctx.qry.sha1;
157 unsigned char sha1[20];
158 struct commit *commit;
159 struct pathspec_item path_items = {
160 .match = ctx.qry.path,
161 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
163 struct pathspec paths = {
164 .nr = 1,
165 .items = &path_items
167 struct walk_tree_context walk_tree_ctx = {
168 .match = 0
171 if (!rev)
172 rev = ctx.qry.head;
174 if (get_sha1(rev, sha1)) {
175 cgit_print_error_page(404, "Not found", "Not found");
176 return;
178 commit = lookup_commit_reference(sha1);
179 if (!commit || parse_commit(commit)) {
180 cgit_print_error_page(404, "Not found", "Not found");
181 return;
183 if (!path_items.match) {
184 path_items.match = "";
185 walk_tree_ctx.match_baselen = -1;
186 print_dir(commit->tree->object.oid.hash, "", 0, "");
187 walk_tree_ctx.match = 2;
189 else
190 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
191 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
192 if (!walk_tree_ctx.match)
193 cgit_print_error_page(404, "Not found", "Not found");
194 else if (walk_tree_ctx.match == 2)
195 print_dir_tail();