email-gravatar: do not scale icons up
[cgit.git] / ui-plain.c
blob68e0387ff800720778abc206537b794d5d45267f
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 <stdio.h>
10 #include "cgit.h"
11 #include "ui-plain.h"
12 #include "html.h"
13 #include "ui-shared.h"
15 struct walk_tree_context {
16 int match_baselen;
17 int match;
20 static char *get_mimetype_from_file(const char *filename, const char *ext)
22 static const char *delimiters;
23 char *result;
24 FILE *fd;
25 char line[1024];
26 char *mimetype;
27 char *token;
29 if (!filename)
30 return NULL;
32 fd = fopen(filename, "r");
33 if (!fd)
34 return NULL;
36 delimiters = " \t\r\n";
37 result = NULL;
39 /* loop over all lines in the file */
40 while (!result && fgets(line, sizeof(line), fd)) {
41 mimetype = strtok(line, delimiters);
43 /* skip empty lines and comment lines */
44 if (!mimetype || (mimetype[0] == '#'))
45 continue;
47 /* loop over all extensions of mimetype */
48 while ((token = strtok(NULL, delimiters))) {
49 if (!strcasecmp(ext, token)) {
50 result = xstrdup(mimetype);
51 break;
55 fclose(fd);
57 return result;
60 static int print_object(const unsigned char *sha1, const char *path)
62 enum object_type type;
63 char *buf, *ext;
64 unsigned long size;
65 struct string_list_item *mime;
66 int freemime;
68 type = sha1_object_info(sha1, &size);
69 if (type == OBJ_BAD) {
70 html_status(404, "Not found", 0);
71 return 0;
74 buf = read_sha1_file(sha1, &type, &size);
75 if (!buf) {
76 html_status(404, "Not found", 0);
77 return 0;
79 ctx.page.mimetype = NULL;
80 ext = strrchr(path, '.');
81 freemime = 0;
82 if (ext && *(++ext)) {
83 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
84 if (mime) {
85 ctx.page.mimetype = (char *)mime->util;
86 ctx.page.charset = NULL;
87 } else {
88 ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
89 if (ctx.page.mimetype) {
90 freemime = 1;
91 ctx.page.charset = NULL;
95 if (!ctx.page.mimetype) {
96 if (buffer_is_binary(buf, size)) {
97 ctx.page.mimetype = "application/octet-stream";
98 ctx.page.charset = NULL;
99 } else {
100 ctx.page.mimetype = "text/plain";
103 ctx.page.filename = path;
104 ctx.page.size = size;
105 ctx.page.etag = sha1_to_hex(sha1);
106 cgit_print_http_headers(&ctx);
107 html_raw(buf, size);
108 /* If we allocated this, then casting away const is safe. */
109 if (freemime)
110 free((char*) ctx.page.mimetype);
111 return 1;
114 static char *buildpath(const char *base, int baselen, const char *path)
116 if (path[0])
117 return fmtalloc("%.*s%s/", baselen, base, path);
118 else
119 return fmtalloc("%.*s/", baselen, base);
122 static void print_dir(const unsigned char *sha1, const char *base,
123 int baselen, const char *path)
125 char *fullpath, *slash;
126 size_t len;
128 fullpath = buildpath(base, baselen, path);
129 slash = (fullpath[0] == '/' ? "" : "/");
130 ctx.page.etag = sha1_to_hex(sha1);
131 cgit_print_http_headers(&ctx);
132 htmlf("<html><head><title>%s", slash);
133 html_txt(fullpath);
134 htmlf("</title></head>\n<body>\n<h2>%s", slash);
135 html_txt(fullpath);
136 html("</h2>\n<ul>\n");
137 len = strlen(fullpath);
138 if (len > 1) {
139 fullpath[len - 1] = 0;
140 slash = strrchr(fullpath, '/');
141 if (slash)
142 *(slash + 1) = 0;
143 else
144 fullpath = NULL;
145 html("<li>");
146 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
147 fullpath);
148 html("</li>\n");
150 free(fullpath);
153 static void print_dir_entry(const unsigned char *sha1, const char *base,
154 int baselen, const char *path, unsigned mode)
156 char *fullpath;
158 fullpath = buildpath(base, baselen, path);
159 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
160 fullpath[strlen(fullpath) - 1] = 0;
161 html(" <li>");
162 if (S_ISGITLINK(mode)) {
163 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
164 } else
165 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
166 fullpath);
167 html("</li>\n");
168 free(fullpath);
171 static void print_dir_tail(void)
173 html(" </ul>\n</body></html>\n");
176 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
177 const char *pathname, unsigned mode, int stage,
178 void *cbdata)
180 struct walk_tree_context *walk_tree_ctx = cbdata;
182 if (baselen == walk_tree_ctx->match_baselen) {
183 if (S_ISREG(mode)) {
184 if (print_object(sha1, pathname))
185 walk_tree_ctx->match = 1;
186 } else if (S_ISDIR(mode)) {
187 print_dir(sha1, base, baselen, pathname);
188 walk_tree_ctx->match = 2;
189 return READ_TREE_RECURSIVE;
191 } else if (baselen > walk_tree_ctx->match_baselen) {
192 print_dir_entry(sha1, base, baselen, pathname, mode);
193 walk_tree_ctx->match = 2;
194 } else if (S_ISDIR(mode)) {
195 return READ_TREE_RECURSIVE;
198 return 0;
201 static int basedir_len(const char *path)
203 char *p = strrchr(path, '/');
204 if (p)
205 return p - path + 1;
206 return 0;
209 void cgit_print_plain(struct cgit_context *ctx)
211 const char *rev = ctx->qry.sha1;
212 unsigned char sha1[20];
213 struct commit *commit;
214 struct pathspec_item path_items = {
215 .match = ctx->qry.path,
216 .len = ctx->qry.path ? strlen(ctx->qry.path) : 0
218 struct pathspec paths = {
219 .nr = 1,
220 .items = &path_items
222 struct walk_tree_context walk_tree_ctx = {
223 .match = 0
226 if (!rev)
227 rev = ctx->qry.head;
229 if (get_sha1(rev, sha1)) {
230 html_status(404, "Not found", 0);
231 return;
233 commit = lookup_commit_reference(sha1);
234 if (!commit || parse_commit(commit)) {
235 html_status(404, "Not found", 0);
236 return;
238 if (!path_items.match) {
239 path_items.match = "";
240 walk_tree_ctx.match_baselen = -1;
241 print_dir(commit->tree->object.sha1, "", 0, "");
242 walk_tree_ctx.match = 2;
244 else
245 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
246 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
247 if (!walk_tree_ctx.match)
248 html_status(404, "Not found", 0);
249 else if (walk_tree_ctx.match == 2)
250 print_dir_tail();