blob: 8ef4ec6ba525351a6ec6d906e5e3e5e795139c6b [file] [log] [blame]
Lars Hjemlie5da4bc2008-08-06 10:53:50 +02001/* ui-plain.c: functions for output of plain blobs by path
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
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"
11#include "html.h"
12#include "ui-shared.h"
13
Lukas Fleischerb1db30c2013-03-03 17:27:54 +010014struct walk_tree_context {
15 int match_baselen;
16 int match;
17};
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020018
Ferry Hubertsd01c6002011-07-19 10:51:58 +020019static 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
Lukas Fleischera6317502013-03-03 17:10:19 +010059static int print_object(const unsigned char *sha1, const char *path)
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020060{
61 enum object_type type;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010062 char *buf, *ext;
Ramsay Jonesbdd4a562008-11-04 19:22:08 +000063 unsigned long size;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010064 struct string_list_item *mime;
Ferry Hubertsd01c6002011-07-19 10:51:58 +020065 int freemime;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020066
67 type = sha1_object_info(sha1, &size);
68 if (type == OBJ_BAD) {
Lars Hjemli885096c2008-08-06 22:57:44 +020069 html_status(404, "Not found", 0);
Lukas Fleischera6317502013-03-03 17:10:19 +010070 return 0;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020071 }
72
73 buf = read_sha1_file(sha1, &type, &size);
74 if (!buf) {
Lars Hjemli885096c2008-08-06 22:57:44 +020075 html_status(404, "Not found", 0);
Lukas Fleischera6317502013-03-03 17:10:19 +010076 return 0;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020077 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010078 ctx.page.mimetype = NULL;
79 ext = strrchr(path, '.');
Ferry Hubertsd01c6002011-07-19 10:51:58 +020080 freemime = 0;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010081 if (ext && *(++ext)) {
Lars Hjemli6d7552b2010-08-22 13:29:57 +020082 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
Ferry Hubertsd01c6002011-07-19 10:51:58 +020083 if (mime) {
Lars Hjemlic4d46c72009-02-13 20:43:30 +010084 ctx.page.mimetype = (char *)mime->util;
Ferry Hubertsd01c6002011-07-19 10:51:58 +020085 } else {
86 ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
87 if (ctx.page.mimetype)
88 freemime = 1;
89 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010090 }
91 if (!ctx.page.mimetype) {
92 if (buffer_is_binary(buf, size))
93 ctx.page.mimetype = "application/octet-stream";
94 else
95 ctx.page.mimetype = "text/plain";
96 }
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020097 ctx.page.filename = fmt("%s", path);
98 ctx.page.size = size;
Lars Hjemli488a2142009-02-19 22:38:36 +010099 ctx.page.etag = sha1_to_hex(sha1);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200100 cgit_print_http_headers(&ctx);
101 html_raw(buf, size);
Ferry Hubertsd01c6002011-07-19 10:51:58 +0200102 if (freemime)
103 free(ctx.page.mimetype);
Lukas Fleischera6317502013-03-03 17:10:19 +0100104 return 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200105}
106
Lars Hjemli7f88d202011-06-12 20:49:35 +0000107static char *buildpath(const char *base, int baselen, const char *path)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500108{
Lars Hjemli7f88d202011-06-12 20:49:35 +0000109 if (path[0])
110 return fmt("%.*s%s/", baselen, base, path);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500111 else
Lars Hjemli7f88d202011-06-12 20:49:35 +0000112 return fmt("%.*s/", baselen, base);
113}
114
115static void print_dir(const unsigned char *sha1, const char *base,
116 int baselen, const char *path)
117{
118 char *fullpath, *slash;
119 size_t len;
120
121 fullpath = buildpath(base, baselen, path);
122 slash = (fullpath[0] == '/' ? "" : "/");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500123 ctx.page.etag = sha1_to_hex(sha1);
124 cgit_print_http_headers(&ctx);
Lars Hjemli7f88d202011-06-12 20:49:35 +0000125 htmlf("<html><head><title>%s", slash);
126 html_txt(fullpath);
127 htmlf("</title></head>\n<body>\n<h2>%s", slash);
128 html_txt(fullpath);
129 html("</h2>\n<ul>\n");
130 len = strlen(fullpath);
131 if (len > 1) {
132 fullpath[len - 1] = 0;
133 slash = strrchr(fullpath, '/');
134 if (slash)
135 *(slash + 1) = 0;
136 else
137 fullpath = NULL;
138 html("<li>");
139 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
140 fullpath);
141 html("</li>\n");
142 }
Mark Lodato6c1a7362010-01-31 14:25:03 -0500143}
144
Lars Hjemli7f88d202011-06-12 20:49:35 +0000145static void print_dir_entry(const unsigned char *sha1, const char *base,
146 int baselen, const char *path, unsigned mode)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500147{
Lars Hjemli7f88d202011-06-12 20:49:35 +0000148 char *fullpath;
149
150 fullpath = buildpath(base, baselen, path);
Lars Hjemli74218572011-06-15 10:10:41 +0200151 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
Lars Hjemli7f88d202011-06-12 20:49:35 +0000152 fullpath[strlen(fullpath) - 1] = 0;
153 html(" <li>");
Lars Hjemli74218572011-06-15 10:10:41 +0200154 if (S_ISGITLINK(mode)) {
155 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
156 } else
157 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
158 fullpath);
Lars Hjemli7f88d202011-06-12 20:49:35 +0000159 html("</li>\n");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500160}
161
162static void print_dir_tail(void)
163{
164 html(" </ul>\n</body></html>\n");
165}
166
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200167static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
168 const char *pathname, unsigned mode, int stage,
169 void *cbdata)
170{
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100171 struct walk_tree_context *walk_tree_ctx = cbdata;
172
173 if (baselen == walk_tree_ctx->match_baselen) {
Lukas Fleischera6317502013-03-03 17:10:19 +0100174 if (S_ISREG(mode)) {
175 if (print_object(sha1, pathname))
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100176 walk_tree_ctx->match = 1;
Lukas Fleischera6317502013-03-03 17:10:19 +0100177 } else if (S_ISDIR(mode)) {
Lars Hjemli7f88d202011-06-12 20:49:35 +0000178 print_dir(sha1, base, baselen, pathname);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100179 walk_tree_ctx->match = 2;
Mark Lodato6c1a7362010-01-31 14:25:03 -0500180 return READ_TREE_RECURSIVE;
181 }
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100182 } else if (baselen > walk_tree_ctx->match_baselen) {
Lars Hjemli7f88d202011-06-12 20:49:35 +0000183 print_dir_entry(sha1, base, baselen, pathname, mode);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100184 walk_tree_ctx->match = 2;
Lukas Fleischera6317502013-03-03 17:10:19 +0100185 } else if (S_ISDIR(mode)) {
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200186 return READ_TREE_RECURSIVE;
Lukas Fleischera6317502013-03-03 17:10:19 +0100187 }
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200188
Mark Lodato74ebf822010-01-31 01:07:41 -0500189 return 0;
190}
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200191
Mark Lodato74ebf822010-01-31 01:07:41 -0500192static int basedir_len(const char *path)
193{
194 char *p = strrchr(path, '/');
195 if (p)
196 return p - path + 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200197 return 0;
198}
199
200void cgit_print_plain(struct cgit_context *ctx)
201{
202 const char *rev = ctx->qry.sha1;
203 unsigned char sha1[20];
204 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000205 struct pathspec_item path_items = {
206 .match = ctx->qry.path,
207 .len = ctx->qry.path ? strlen(ctx->qry.path) : 0
208 };
209 struct pathspec paths = {
210 .nr = 1,
211 .items = &path_items
212 };
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100213 struct walk_tree_context walk_tree_ctx = {
214 .match = 0
215 };
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200216
217 if (!rev)
218 rev = ctx->qry.head;
219
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200220 if (get_sha1(rev, sha1)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200221 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200222 return;
223 }
224 commit = lookup_commit_reference(sha1);
225 if (!commit || parse_commit(commit)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200226 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200227 return;
228 }
John Keepingc1633c62013-03-02 12:32:11 +0000229 if (!path_items.match) {
230 path_items.match = "";
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100231 walk_tree_ctx.match_baselen = -1;
Lars Hjemli7f88d202011-06-12 20:49:35 +0000232 print_dir(commit->tree->object.sha1, "", 0, "");
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100233 walk_tree_ctx.match = 2;
Mark Lodato6c1a7362010-01-31 14:25:03 -0500234 }
235 else
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100236 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
237 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
238 if (!walk_tree_ctx.match)
Lars Hjemli885096c2008-08-06 22:57:44 +0200239 html_status(404, "Not found", 0);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100240 else if (walk_tree_ctx.match == 2)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500241 print_dir_tail();
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200242}