blob: 2abd210c0f5afd1871b00e6d53eaddb929b39ff0 [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
9#include "cgit.h"
10#include "html.h"
11#include "ui-shared.h"
12
Mark Lodato74ebf822010-01-31 01:07:41 -050013int match_baselen;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020014int match;
15
16static void print_object(const unsigned char *sha1, const char *path)
17{
18 enum object_type type;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010019 char *buf, *ext;
Ramsay Jonesbdd4a562008-11-04 19:22:08 +000020 unsigned long size;
Lars Hjemlic4d46c72009-02-13 20:43:30 +010021 struct string_list_item *mime;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020022
23 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) {
Lars Hjemli885096c2008-08-06 22:57:44 +020025 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020026 return;
27 }
28
29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) {
Lars Hjemli885096c2008-08-06 22:57:44 +020031 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020032 return;
33 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010034 ctx.page.mimetype = NULL;
35 ext = strrchr(path, '.');
36 if (ext && *(++ext)) {
Lars Hjemli6d7552b2010-08-22 13:29:57 +020037 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
Lars Hjemlic4d46c72009-02-13 20:43:30 +010038 if (mime)
39 ctx.page.mimetype = (char *)mime->util;
40 }
41 if (!ctx.page.mimetype) {
42 if (buffer_is_binary(buf, size))
43 ctx.page.mimetype = "application/octet-stream";
44 else
45 ctx.page.mimetype = "text/plain";
46 }
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020047 ctx.page.filename = fmt("%s", path);
48 ctx.page.size = size;
Lars Hjemli488a2142009-02-19 22:38:36 +010049 ctx.page.etag = sha1_to_hex(sha1);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020050 cgit_print_http_headers(&ctx);
51 html_raw(buf, size);
52 match = 1;
53}
54
Lars Hjemli7f88d202011-06-12 20:49:35 +000055static char *buildpath(const char *base, int baselen, const char *path)
Mark Lodato6c1a7362010-01-31 14:25:03 -050056{
Lars Hjemli7f88d202011-06-12 20:49:35 +000057 if (path[0])
58 return fmt("%.*s%s/", baselen, base, path);
Mark Lodato6c1a7362010-01-31 14:25:03 -050059 else
Lars Hjemli7f88d202011-06-12 20:49:35 +000060 return fmt("%.*s/", baselen, base);
61}
62
63static void print_dir(const unsigned char *sha1, const char *base,
64 int baselen, const char *path)
65{
66 char *fullpath, *slash;
67 size_t len;
68
69 fullpath = buildpath(base, baselen, path);
70 slash = (fullpath[0] == '/' ? "" : "/");
Mark Lodato6c1a7362010-01-31 14:25:03 -050071 ctx.page.etag = sha1_to_hex(sha1);
72 cgit_print_http_headers(&ctx);
Lars Hjemli7f88d202011-06-12 20:49:35 +000073 htmlf("<html><head><title>%s", slash);
74 html_txt(fullpath);
75 htmlf("</title></head>\n<body>\n<h2>%s", slash);
76 html_txt(fullpath);
77 html("</h2>\n<ul>\n");
78 len = strlen(fullpath);
79 if (len > 1) {
80 fullpath[len - 1] = 0;
81 slash = strrchr(fullpath, '/');
82 if (slash)
83 *(slash + 1) = 0;
84 else
85 fullpath = NULL;
86 html("<li>");
87 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
88 fullpath);
89 html("</li>\n");
90 }
Mark Lodato6c1a7362010-01-31 14:25:03 -050091 match = 2;
92}
93
Lars Hjemli7f88d202011-06-12 20:49:35 +000094static void print_dir_entry(const unsigned char *sha1, const char *base,
95 int baselen, const char *path, unsigned mode)
Mark Lodato6c1a7362010-01-31 14:25:03 -050096{
Lars Hjemli7f88d202011-06-12 20:49:35 +000097 char *fullpath;
98
99 fullpath = buildpath(base, baselen, path);
Lars Hjemli74218572011-06-15 10:10:41 +0200100 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
Lars Hjemli7f88d202011-06-12 20:49:35 +0000101 fullpath[strlen(fullpath) - 1] = 0;
102 html(" <li>");
Lars Hjemli74218572011-06-15 10:10:41 +0200103 if (S_ISGITLINK(mode)) {
104 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
105 } else
106 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
107 fullpath);
Lars Hjemli7f88d202011-06-12 20:49:35 +0000108 html("</li>\n");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500109 match = 2;
110}
111
112static void print_dir_tail(void)
113{
114 html(" </ul>\n</body></html>\n");
115}
116
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200117static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
118 const char *pathname, unsigned mode, int stage,
119 void *cbdata)
120{
Mark Lodato74ebf822010-01-31 01:07:41 -0500121 if (baselen == match_baselen) {
122 if (S_ISREG(mode))
123 print_object(sha1, pathname);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500124 else if (S_ISDIR(mode)) {
Lars Hjemli7f88d202011-06-12 20:49:35 +0000125 print_dir(sha1, base, baselen, pathname);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500126 return READ_TREE_RECURSIVE;
127 }
Mark Lodato74ebf822010-01-31 01:07:41 -0500128 }
Mark Lodato6c1a7362010-01-31 14:25:03 -0500129 else if (baselen > match_baselen)
Lars Hjemli7f88d202011-06-12 20:49:35 +0000130 print_dir_entry(sha1, base, baselen, pathname, mode);
Mark Lodato74ebf822010-01-31 01:07:41 -0500131 else if (S_ISDIR(mode))
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200132 return READ_TREE_RECURSIVE;
133
Mark Lodato74ebf822010-01-31 01:07:41 -0500134 return 0;
135}
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200136
Mark Lodato74ebf822010-01-31 01:07:41 -0500137static int basedir_len(const char *path)
138{
139 char *p = strrchr(path, '/');
140 if (p)
141 return p - path + 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200142 return 0;
143}
144
145void cgit_print_plain(struct cgit_context *ctx)
146{
147 const char *rev = ctx->qry.sha1;
148 unsigned char sha1[20];
149 struct commit *commit;
150 const char *paths[] = {ctx->qry.path, NULL};
151
152 if (!rev)
153 rev = ctx->qry.head;
154
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200155 if (get_sha1(rev, sha1)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200156 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200157 return;
158 }
159 commit = lookup_commit_reference(sha1);
160 if (!commit || parse_commit(commit)) {
Lars Hjemli885096c2008-08-06 22:57:44 +0200161 html_status(404, "Not found", 0);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200162 return;
163 }
Mark Lodato6c1a7362010-01-31 14:25:03 -0500164 if (!paths[0]) {
165 paths[0] = "";
166 match_baselen = -1;
Lars Hjemli7f88d202011-06-12 20:49:35 +0000167 print_dir(commit->tree->object.sha1, "", 0, "");
Mark Lodato6c1a7362010-01-31 14:25:03 -0500168 }
169 else
170 match_baselen = basedir_len(paths[0]);
Martins Polakovs24538b02009-12-10 17:34:42 +0159171 read_tree_recursive(commit->tree, "", 0, 0, paths, walk_tree, NULL);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200172 if (!match)
Lars Hjemli885096c2008-08-06 22:57:44 +0200173 html_status(404, "Not found", 0);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500174 else if (match == 2)
175 print_dir_tail();
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200176}