blob: 7fecc32e1a970fdb3c51575e44072036222718d2 [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
Mark Lodato74ebf822010-01-31 01:07:41 -050014int match_baselen;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020015int match;
16
Ferry Hubertsd01c6002011-07-19 10:51:58 +020017static char *get_mimetype_from_file(const char *filename, const char *ext)
18{
19 static const char *delimiters;
20 char *result;
21 FILE *fd;
22 char line[1024];
23 char *mimetype;
24 char *token;
25
26 if (!filename)
27 return NULL;
28
29 fd = fopen(filename, "r");
30 if (!fd)
31 return NULL;
32
33 delimiters = " \t\r\n";
34 result = NULL;
35
36 /* loop over all lines in the file */
37 while (!result && fgets(line, sizeof(line), fd)) {
38 mimetype = strtok(line, delimiters);
39
40 /* skip empty lines and comment lines */
41 if (!mimetype || (mimetype[0] == '#'))
42 continue;
43
44 /* loop over all extensions of mimetype */
45 while ((token = strtok(NULL, delimiters))) {
46 if (!strcasecmp(ext, token)) {
47 result = xstrdup(mimetype);
48 break;
49 }
50 }
51 }
52 fclose(fd);
53
54 return result;
55}
56
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020057static void print_object(const unsigned char *sha1, const char *path)
58{
59 enum object_type type;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010060 char *buf, *ext;
Ramsay Jonesbdd4a562008-11-04 19:22:08 +000061 unsigned long size;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010062 struct string_list_item *mime;
Ferry Hubertsd01c6002011-07-19 10:51:58 +020063 int freemime;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020064
65 type = sha1_object_info(sha1, &size);
66 if (type == OBJ_BAD) {
Lars Hjemli885096c2008-08-06 22:57:44 +020067 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020068 return;
69 }
70
71 buf = read_sha1_file(sha1, &type, &size);
72 if (!buf) {
Lars Hjemli885096c2008-08-06 22:57:44 +020073 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020074 return;
75 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010076 ctx.page.mimetype = NULL;
77 ext = strrchr(path, '.');
Ferry Hubertsd01c6002011-07-19 10:51:58 +020078 freemime = 0;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010079 if (ext && *(++ext)) {
Lars Hjemli6d7552b2010-08-22 13:29:57 +020080 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
Ferry Hubertsd01c6002011-07-19 10:51:58 +020081 if (mime) {
Lars Hjemlic4d46c72009-02-13 20:43:30 +010082 ctx.page.mimetype = (char *)mime->util;
Ferry Hubertsd01c6002011-07-19 10:51:58 +020083 } else {
84 ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
85 if (ctx.page.mimetype)
86 freemime = 1;
87 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010088 }
89 if (!ctx.page.mimetype) {
90 if (buffer_is_binary(buf, size))
91 ctx.page.mimetype = "application/octet-stream";
92 else
93 ctx.page.mimetype = "text/plain";
94 }
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020095 ctx.page.filename = fmt("%s", path);
96 ctx.page.size = size;
Lars Hjemli488a2142009-02-19 22:38:36 +010097 ctx.page.etag = sha1_to_hex(sha1);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020098 cgit_print_http_headers(&ctx);
99 html_raw(buf, size);
100 match = 1;
Ferry Hubertsd01c6002011-07-19 10:51:58 +0200101 if (freemime)
102 free(ctx.page.mimetype);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200103}
104
Lars Hjemli7f88d202011-06-12 20:49:35 +0000105static char *buildpath(const char *base, int baselen, const char *path)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500106{
Lars Hjemli7f88d202011-06-12 20:49:35 +0000107 if (path[0])
108 return fmt("%.*s%s/", baselen, base, path);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500109 else
Lars Hjemli7f88d202011-06-12 20:49:35 +0000110 return fmt("%.*s/", baselen, base);
111}
112
113static void print_dir(const unsigned char *sha1, const char *base,
114 int baselen, const char *path)
115{
116 char *fullpath, *slash;
117 size_t len;
118
119 fullpath = buildpath(base, baselen, path);
120 slash = (fullpath[0] == '/' ? "" : "/");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500121 ctx.page.etag = sha1_to_hex(sha1);
122 cgit_print_http_headers(&ctx);
Lars Hjemli7f88d202011-06-12 20:49:35 +0000123 htmlf("<html><head><title>%s", slash);
124 html_txt(fullpath);
125 htmlf("</title></head>\n<body>\n<h2>%s", slash);
126 html_txt(fullpath);
127 html("</h2>\n<ul>\n");
128 len = strlen(fullpath);
129 if (len > 1) {
130 fullpath[len - 1] = 0;
131 slash = strrchr(fullpath, '/');
132 if (slash)
133 *(slash + 1) = 0;
134 else
135 fullpath = NULL;
136 html("<li>");
137 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
138 fullpath);
139 html("</li>\n");
140 }
Mark Lodato6c1a7362010-01-31 14:25:03 -0500141 match = 2;
142}
143
Lars Hjemli7f88d202011-06-12 20:49:35 +0000144static void print_dir_entry(const unsigned char *sha1, const char *base,
145 int baselen, const char *path, unsigned mode)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500146{
Lars Hjemli7f88d202011-06-12 20:49:35 +0000147 char *fullpath;
148
149 fullpath = buildpath(base, baselen, path);
150 if (!S_ISDIR(mode))
151 fullpath[strlen(fullpath) - 1] = 0;
152 html(" <li>");
153 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
154 fullpath);
155 html("</li>\n");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500156 match = 2;
157}
158
159static void print_dir_tail(void)
160{
161 html(" </ul>\n</body></html>\n");
162}
163
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200164static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
165 const char *pathname, unsigned mode, int stage,
166 void *cbdata)
167{
Mark Lodato74ebf822010-01-31 01:07:41 -0500168 if (baselen == match_baselen) {
169 if (S_ISREG(mode))
170 print_object(sha1, pathname);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500171 else if (S_ISDIR(mode)) {
Lars Hjemli7f88d202011-06-12 20:49:35 +0000172 print_dir(sha1, base, baselen, pathname);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500173 return READ_TREE_RECURSIVE;
174 }
Mark Lodato74ebf822010-01-31 01:07:41 -0500175 }
Mark Lodato6c1a7362010-01-31 14:25:03 -0500176 else if (baselen > match_baselen)
Lars Hjemli7f88d202011-06-12 20:49:35 +0000177 print_dir_entry(sha1, base, baselen, pathname, mode);
Mark Lodato74ebf822010-01-31 01:07:41 -0500178 else if (S_ISDIR(mode))
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200179 return READ_TREE_RECURSIVE;
180
Mark Lodato74ebf822010-01-31 01:07:41 -0500181 return 0;
182}
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200183
Mark Lodato74ebf822010-01-31 01:07:41 -0500184static int basedir_len(const char *path)
185{
186 char *p = strrchr(path, '/');
187 if (p)
188 return p - path + 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200189 return 0;
190}
191
192void cgit_print_plain(struct cgit_context *ctx)
193{
194 const char *rev = ctx->qry.sha1;
195 unsigned char sha1[20];
196 struct commit *commit;
197 const char *paths[] = {ctx->qry.path, NULL};
198
199 if (!rev)
200 rev = ctx->qry.head;
201
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200202 if (get_sha1(rev, sha1)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200203 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200204 return;
205 }
206 commit = lookup_commit_reference(sha1);
207 if (!commit || parse_commit(commit)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200208 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200209 return;
210 }
Mark Lodato6c1a7362010-01-31 14:25:03 -0500211 if (!paths[0]) {
212 paths[0] = "";
213 match_baselen = -1;
Lars Hjemli7f88d202011-06-12 20:49:35 +0000214 print_dir(commit->tree->object.sha1, "", 0, "");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500215 }
216 else
217 match_baselen = basedir_len(paths[0]);
Martins Polakovs24538b02009-12-10 17:34:42 +0159218 read_tree_recursive(commit->tree, "", 0, 0, paths, walk_tree, NULL);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200219 if (!match)
Lars Hjemli885096c2008-08-06 22:57:44 +0200220 html_status(404, "Not found", 0);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500221 else if (match == 2)
222 print_dir_tail();
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200223}