]> git.cameronkatri.com Git - cgit.git/blob - ui-plain.c
plain: use cgit_print_error_page() instead of html_status()
[cgit.git] / ui-plain.c
1 /* ui-plain.c: functions for output of plain blobs by path
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "ui-plain.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 struct walk_tree_context {
15 int match_baselen;
16 int match;
17 };
18
19 static char *get_mimetype_from_file(const char *filename, const char *ext)
20 {
21 static const char *delimiters;
22 char *result;
23 FILE *fd;
24 char line[1024];
25 char *mimetype;
26 char *token;
27
28 if (!filename)
29 return NULL;
30
31 fd = fopen(filename, "r");
32 if (!fd)
33 return NULL;
34
35 delimiters = " \t\r\n";
36 result = NULL;
37
38 /* loop over all lines in the file */
39 while (!result && fgets(line, sizeof(line), fd)) {
40 mimetype = strtok(line, delimiters);
41
42 /* skip empty lines and comment lines */
43 if (!mimetype || (mimetype[0] == '#'))
44 continue;
45
46 /* loop over all extensions of mimetype */
47 while ((token = strtok(NULL, delimiters))) {
48 if (!strcasecmp(ext, token)) {
49 result = xstrdup(mimetype);
50 break;
51 }
52 }
53 }
54 fclose(fd);
55
56 return result;
57 }
58
59 static int print_object(const unsigned char *sha1, const char *path)
60 {
61 enum object_type type;
62 char *buf, *ext;
63 unsigned long size;
64 struct string_list_item *mime;
65 int freemime;
66
67 type = sha1_object_info(sha1, &size);
68 if (type == OBJ_BAD) {
69 cgit_print_error_page(404, "Not found", "Not found");
70 return 0;
71 }
72
73 buf = read_sha1_file(sha1, &type, &size);
74 if (!buf) {
75 cgit_print_error_page(404, "Not found", "Not found");
76 return 0;
77 }
78 ctx.page.mimetype = NULL;
79 ext = strrchr(path, '.');
80 freemime = 0;
81 if (ext && *(++ext)) {
82 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
83 if (mime) {
84 ctx.page.mimetype = (char *)mime->util;
85 ctx.page.charset = NULL;
86 } else {
87 ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
88 if (ctx.page.mimetype) {
89 freemime = 1;
90 ctx.page.charset = NULL;
91 }
92 }
93 }
94 if (!ctx.page.mimetype) {
95 if (buffer_is_binary(buf, size)) {
96 ctx.page.mimetype = "application/octet-stream";
97 ctx.page.charset = NULL;
98 } else {
99 ctx.page.mimetype = "text/plain";
100 }
101 }
102 ctx.page.filename = path;
103 ctx.page.size = size;
104 ctx.page.etag = sha1_to_hex(sha1);
105 cgit_print_http_headers();
106 html_raw(buf, size);
107 /* If we allocated this, then casting away const is safe. */
108 if (freemime)
109 free((char*) ctx.page.mimetype);
110 return 1;
111 }
112
113 static char *buildpath(const char *base, int baselen, const char *path)
114 {
115 if (path[0])
116 return fmtalloc("%.*s%s/", baselen, base, path);
117 else
118 return fmtalloc("%.*s/", baselen, base);
119 }
120
121 static void print_dir(const unsigned char *sha1, const char *base,
122 int baselen, const char *path)
123 {
124 char *fullpath, *slash;
125 size_t len;
126
127 fullpath = buildpath(base, baselen, path);
128 slash = (fullpath[0] == '/' ? "" : "/");
129 ctx.page.etag = sha1_to_hex(sha1);
130 cgit_print_http_headers();
131 htmlf("<html><head><title>%s", slash);
132 html_txt(fullpath);
133 htmlf("</title></head>\n<body>\n<h2>%s", slash);
134 html_txt(fullpath);
135 html("</h2>\n<ul>\n");
136 len = strlen(fullpath);
137 if (len > 1) {
138 fullpath[len - 1] = 0;
139 slash = strrchr(fullpath, '/');
140 if (slash)
141 *(slash + 1) = 0;
142 else
143 fullpath = NULL;
144 html("<li>");
145 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
146 fullpath);
147 html("</li>\n");
148 }
149 free(fullpath);
150 }
151
152 static void print_dir_entry(const unsigned char *sha1, const char *base,
153 int baselen, const char *path, unsigned mode)
154 {
155 char *fullpath;
156
157 fullpath = buildpath(base, baselen, path);
158 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
159 fullpath[strlen(fullpath) - 1] = 0;
160 html(" <li>");
161 if (S_ISGITLINK(mode)) {
162 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
163 } else
164 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
165 fullpath);
166 html("</li>\n");
167 free(fullpath);
168 }
169
170 static void print_dir_tail(void)
171 {
172 html(" </ul>\n</body></html>\n");
173 }
174
175 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
176 const char *pathname, unsigned mode, int stage, void *cbdata)
177 {
178 struct walk_tree_context *walk_tree_ctx = cbdata;
179
180 if (base->len == walk_tree_ctx->match_baselen) {
181 if (S_ISREG(mode)) {
182 if (print_object(sha1, pathname))
183 walk_tree_ctx->match = 1;
184 } else if (S_ISDIR(mode)) {
185 print_dir(sha1, base->buf, base->len, pathname);
186 walk_tree_ctx->match = 2;
187 return READ_TREE_RECURSIVE;
188 }
189 } else if (base->len > walk_tree_ctx->match_baselen) {
190 print_dir_entry(sha1, base->buf, base->len, pathname, mode);
191 walk_tree_ctx->match = 2;
192 } else if (S_ISDIR(mode)) {
193 return READ_TREE_RECURSIVE;
194 }
195
196 return 0;
197 }
198
199 static int basedir_len(const char *path)
200 {
201 char *p = strrchr(path, '/');
202 if (p)
203 return p - path + 1;
204 return 0;
205 }
206
207 void cgit_print_plain(void)
208 {
209 const char *rev = ctx.qry.sha1;
210 unsigned char sha1[20];
211 struct commit *commit;
212 struct pathspec_item path_items = {
213 .match = ctx.qry.path,
214 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
215 };
216 struct pathspec paths = {
217 .nr = 1,
218 .items = &path_items
219 };
220 struct walk_tree_context walk_tree_ctx = {
221 .match = 0
222 };
223
224 if (!rev)
225 rev = ctx.qry.head;
226
227 if (get_sha1(rev, sha1)) {
228 cgit_print_error_page(404, "Not found", "Not found");
229 return;
230 }
231 commit = lookup_commit_reference(sha1);
232 if (!commit || parse_commit(commit)) {
233 cgit_print_error_page(404, "Not found", "Not found");
234 return;
235 }
236 if (!path_items.match) {
237 path_items.match = "";
238 walk_tree_ctx.match_baselen = -1;
239 print_dir(commit->tree->object.sha1, "", 0, "");
240 walk_tree_ctx.match = 2;
241 }
242 else
243 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
244 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
245 if (!walk_tree_ctx.match)
246 cgit_print_error_page(404, "Not found", "Not found");
247 else if (walk_tree_ctx.match == 2)
248 print_dir_tail();
249 }