blob: b787bc3c776b01587c297a211cb1e3638eb6de66 [file] [log] [blame]
Lars Hjemlie5da4bc2008-08-06 10:53:50 +02001/* ui-plain.c: functions for output of plain blobs by path
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemlie5da4bc2008-08-06 10:53:50 +02004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Ferry Hubertsd01c6002011-07-19 10:51:58 +02009#include <stdio.h>
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020010#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010011#include "ui-plain.h"
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020012#include "html.h"
13#include "ui-shared.h"
14
Lukas Fleischerb1db30c2013-03-03 17:27:54 +010015struct walk_tree_context {
16 int match_baselen;
17 int match;
18};
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020019
Ferry Hubertsd01c6002011-07-19 10:51:58 +020020static char *get_mimetype_from_file(const char *filename, const char *ext)
21{
22 static const char *delimiters;
23 char *result;
24 FILE *fd;
25 char line[1024];
26 char *mimetype;
27 char *token;
28
29 if (!filename)
30 return NULL;
31
32 fd = fopen(filename, "r");
33 if (!fd)
34 return NULL;
35
36 delimiters = " \t\r\n";
37 result = NULL;
38
39 /* loop over all lines in the file */
40 while (!result && fgets(line, sizeof(line), fd)) {
41 mimetype = strtok(line, delimiters);
42
43 /* skip empty lines and comment lines */
44 if (!mimetype || (mimetype[0] == '#'))
45 continue;
46
47 /* loop over all extensions of mimetype */
48 while ((token = strtok(NULL, delimiters))) {
49 if (!strcasecmp(ext, token)) {
50 result = xstrdup(mimetype);
51 break;
52 }
53 }
54 }
55 fclose(fd);
56
57 return result;
58}
59
Lukas Fleischera6317502013-03-03 17:10:19 +010060static int print_object(const unsigned char *sha1, const char *path)
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020061{
62 enum object_type type;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010063 char *buf, *ext;
Ramsay Jonesbdd4a562008-11-04 19:22:08 +000064 unsigned long size;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010065 struct string_list_item *mime;
Ferry Hubertsd01c6002011-07-19 10:51:58 +020066 int freemime;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020067
68 type = sha1_object_info(sha1, &size);
69 if (type == OBJ_BAD) {
Lars Hjemli885096c2008-08-06 22:57:44 +020070 html_status(404, "Not found", 0);
Lukas Fleischera6317502013-03-03 17:10:19 +010071 return 0;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020072 }
73
74 buf = read_sha1_file(sha1, &type, &size);
75 if (!buf) {
Lars Hjemli885096c2008-08-06 22:57:44 +020076 html_status(404, "Not found", 0);
Lukas Fleischera6317502013-03-03 17:10:19 +010077 return 0;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020078 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010079 ctx.page.mimetype = NULL;
80 ext = strrchr(path, '.');
Ferry Hubertsd01c6002011-07-19 10:51:58 +020081 freemime = 0;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010082 if (ext && *(++ext)) {
Lars Hjemli6d7552b2010-08-22 13:29:57 +020083 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
Ferry Hubertsd01c6002011-07-19 10:51:58 +020084 if (mime) {
Lars Hjemlic4d46c72009-02-13 20:43:30 +010085 ctx.page.mimetype = (char *)mime->util;
John Keeping407f71c2013-10-06 12:14:41 +010086 ctx.page.charset = NULL;
Ferry Hubertsd01c6002011-07-19 10:51:58 +020087 } else {
88 ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
John Keeping407f71c2013-10-06 12:14:41 +010089 if (ctx.page.mimetype) {
Ferry Hubertsd01c6002011-07-19 10:51:58 +020090 freemime = 1;
John Keeping407f71c2013-10-06 12:14:41 +010091 ctx.page.charset = NULL;
92 }
Ferry Hubertsd01c6002011-07-19 10:51:58 +020093 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010094 }
95 if (!ctx.page.mimetype) {
John Keeping407f71c2013-10-06 12:14:41 +010096 if (buffer_is_binary(buf, size)) {
Lars Hjemlic4d46c72009-02-13 20:43:30 +010097 ctx.page.mimetype = "application/octet-stream";
John Keeping407f71c2013-10-06 12:14:41 +010098 ctx.page.charset = NULL;
99 } else {
Lars Hjemlic4d46c72009-02-13 20:43:30 +0100100 ctx.page.mimetype = "text/plain";
John Keeping407f71c2013-10-06 12:14:41 +0100101 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +0100102 }
John Keeping42d54762013-04-06 10:49:22 +0100103 ctx.page.filename = path;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200104 ctx.page.size = size;
Lars Hjemli488a2142009-02-19 22:38:36 +0100105 ctx.page.etag = sha1_to_hex(sha1);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100106 cgit_print_http_headers();
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200107 html_raw(buf, size);
John Keeping57d09bf2013-04-07 12:46:45 +0100108 /* If we allocated this, then casting away const is safe. */
Ferry Hubertsd01c6002011-07-19 10:51:58 +0200109 if (freemime)
John Keeping57d09bf2013-04-07 12:46:45 +0100110 free((char*) ctx.page.mimetype);
Lukas Fleischera6317502013-03-03 17:10:19 +0100111 return 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200112}
113
Lars Hjemli7f88d202011-06-12 20:49:35 +0000114static char *buildpath(const char *base, int baselen, const char *path)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500115{
Lars Hjemli7f88d202011-06-12 20:49:35 +0000116 if (path[0])
John Keepingfb3655d2013-04-06 10:28:57 +0100117 return fmtalloc("%.*s%s/", baselen, base, path);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500118 else
John Keepingfb3655d2013-04-06 10:28:57 +0100119 return fmtalloc("%.*s/", baselen, base);
Lars Hjemli7f88d202011-06-12 20:49:35 +0000120}
121
122static void print_dir(const unsigned char *sha1, const char *base,
123 int baselen, const char *path)
124{
125 char *fullpath, *slash;
126 size_t len;
127
128 fullpath = buildpath(base, baselen, path);
129 slash = (fullpath[0] == '/' ? "" : "/");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500130 ctx.page.etag = sha1_to_hex(sha1);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100131 cgit_print_http_headers();
Lars Hjemli7f88d202011-06-12 20:49:35 +0000132 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");
149 }
John Keepingfb3655d2013-04-06 10:28:57 +0100150 free(fullpath);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500151}
152
Lars Hjemli7f88d202011-06-12 20:49:35 +0000153static void print_dir_entry(const unsigned char *sha1, const char *base,
154 int baselen, const char *path, unsigned mode)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500155{
Lars Hjemli7f88d202011-06-12 20:49:35 +0000156 char *fullpath;
157
158 fullpath = buildpath(base, baselen, path);
Lars Hjemli74218572011-06-15 10:10:41 +0200159 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
Lars Hjemli7f88d202011-06-12 20:49:35 +0000160 fullpath[strlen(fullpath) - 1] = 0;
161 html(" <li>");
Lars Hjemli74218572011-06-15 10:10:41 +0200162 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);
Lars Hjemli7f88d202011-06-12 20:49:35 +0000167 html("</li>\n");
John Keepingfb3655d2013-04-06 10:28:57 +0100168 free(fullpath);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500169}
170
171static void print_dir_tail(void)
172{
173 html(" </ul>\n</body></html>\n");
174}
175
Christian Hesse7358f632015-02-07 14:18:28 +0100176static int walk_tree(const unsigned char *sha1, struct strbuf *base,
177 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200178{
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100179 struct walk_tree_context *walk_tree_ctx = cbdata;
180
Christian Hesse7358f632015-02-07 14:18:28 +0100181 if (base->len == walk_tree_ctx->match_baselen) {
Lukas Fleischera6317502013-03-03 17:10:19 +0100182 if (S_ISREG(mode)) {
183 if (print_object(sha1, pathname))
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100184 walk_tree_ctx->match = 1;
Lukas Fleischera6317502013-03-03 17:10:19 +0100185 } else if (S_ISDIR(mode)) {
Christian Hesse7358f632015-02-07 14:18:28 +0100186 print_dir(sha1, base->buf, base->len, pathname);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100187 walk_tree_ctx->match = 2;
Mark Lodato6c1a7362010-01-31 14:25:03 -0500188 return READ_TREE_RECURSIVE;
189 }
Christian Hesse7358f632015-02-07 14:18:28 +0100190 } else if (base->len > walk_tree_ctx->match_baselen) {
191 print_dir_entry(sha1, base->buf, base->len, pathname, mode);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100192 walk_tree_ctx->match = 2;
Lukas Fleischera6317502013-03-03 17:10:19 +0100193 } else if (S_ISDIR(mode)) {
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200194 return READ_TREE_RECURSIVE;
Lukas Fleischera6317502013-03-03 17:10:19 +0100195 }
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200196
Mark Lodato74ebf822010-01-31 01:07:41 -0500197 return 0;
198}
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200199
Mark Lodato74ebf822010-01-31 01:07:41 -0500200static int basedir_len(const char *path)
201{
202 char *p = strrchr(path, '/');
203 if (p)
204 return p - path + 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200205 return 0;
206}
207
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100208void cgit_print_plain(void)
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200209{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100210 const char *rev = ctx.qry.sha1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200211 unsigned char sha1[20];
212 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000213 struct pathspec_item path_items = {
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100214 .match = ctx.qry.path,
215 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
John Keepingc1633c62013-03-02 12:32:11 +0000216 };
217 struct pathspec paths = {
218 .nr = 1,
219 .items = &path_items
220 };
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100221 struct walk_tree_context walk_tree_ctx = {
222 .match = 0
223 };
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200224
225 if (!rev)
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100226 rev = ctx.qry.head;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200227
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200228 if (get_sha1(rev, sha1)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200229 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200230 return;
231 }
232 commit = lookup_commit_reference(sha1);
233 if (!commit || parse_commit(commit)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200234 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200235 return;
236 }
John Keepingc1633c62013-03-02 12:32:11 +0000237 if (!path_items.match) {
238 path_items.match = "";
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100239 walk_tree_ctx.match_baselen = -1;
Lars Hjemli7f88d202011-06-12 20:49:35 +0000240 print_dir(commit->tree->object.sha1, "", 0, "");
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100241 walk_tree_ctx.match = 2;
Mark Lodato6c1a7362010-01-31 14:25:03 -0500242 }
243 else
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100244 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
245 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
246 if (!walk_tree_ctx.match)
Lars Hjemli885096c2008-08-06 22:57:44 +0200247 html_status(404, "Not found", 0);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100248 else if (walk_tree_ctx.match == 2)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500249 print_dir_tail();
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200250}