]> git.cameronkatri.com Git - cgit.git/blob - ui-plain.c
ui-plain.c: fix html and links generated by print_dir() and print_dir_entry()
[cgit.git] / ui-plain.c
1 /* 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
13 int match_baselen;
14 int match;
15
16 static void print_object(const unsigned char *sha1, const char *path)
17 {
18 enum object_type type;
19 char *buf, *ext;
20 unsigned long size;
21 struct string_list_item *mime;
22
23 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) {
25 html_status(404, "Not found", 0);
26 return;
27 }
28
29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) {
31 html_status(404, "Not found", 0);
32 return;
33 }
34 ctx.page.mimetype = NULL;
35 ext = strrchr(path, '.');
36 if (ext && *(++ext)) {
37 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
38 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 }
47 ctx.page.filename = fmt("%s", path);
48 ctx.page.size = size;
49 ctx.page.etag = sha1_to_hex(sha1);
50 cgit_print_http_headers(&ctx);
51 html_raw(buf, size);
52 match = 1;
53 }
54
55 static char *buildpath(const char *base, int baselen, const char *path)
56 {
57 if (path[0])
58 return fmt("%.*s%s/", baselen, base, path);
59 else
60 return fmt("%.*s/", baselen, base);
61 }
62
63 static 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] == '/' ? "" : "/");
71 ctx.page.etag = sha1_to_hex(sha1);
72 cgit_print_http_headers(&ctx);
73 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 }
91 match = 2;
92 }
93
94 static void print_dir_entry(const unsigned char *sha1, const char *base,
95 int baselen, const char *path, unsigned mode)
96 {
97 char *fullpath;
98
99 fullpath = buildpath(base, baselen, path);
100 if (!S_ISDIR(mode))
101 fullpath[strlen(fullpath) - 1] = 0;
102 html(" <li>");
103 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
104 fullpath);
105 html("</li>\n");
106 match = 2;
107 }
108
109 static void print_dir_tail(void)
110 {
111 html(" </ul>\n</body></html>\n");
112 }
113
114 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
115 const char *pathname, unsigned mode, int stage,
116 void *cbdata)
117 {
118 if (baselen == match_baselen) {
119 if (S_ISREG(mode))
120 print_object(sha1, pathname);
121 else if (S_ISDIR(mode)) {
122 print_dir(sha1, base, baselen, pathname);
123 return READ_TREE_RECURSIVE;
124 }
125 }
126 else if (baselen > match_baselen)
127 print_dir_entry(sha1, base, baselen, pathname, mode);
128 else if (S_ISDIR(mode))
129 return READ_TREE_RECURSIVE;
130
131 return 0;
132 }
133
134 static int basedir_len(const char *path)
135 {
136 char *p = strrchr(path, '/');
137 if (p)
138 return p - path + 1;
139 return 0;
140 }
141
142 void cgit_print_plain(struct cgit_context *ctx)
143 {
144 const char *rev = ctx->qry.sha1;
145 unsigned char sha1[20];
146 struct commit *commit;
147 const char *paths[] = {ctx->qry.path, NULL};
148
149 if (!rev)
150 rev = ctx->qry.head;
151
152 if (get_sha1(rev, sha1)) {
153 html_status(404, "Not found", 0);
154 return;
155 }
156 commit = lookup_commit_reference(sha1);
157 if (!commit || parse_commit(commit)) {
158 html_status(404, "Not found", 0);
159 return;
160 }
161 if (!paths[0]) {
162 paths[0] = "";
163 match_baselen = -1;
164 print_dir(commit->tree->object.sha1, "", 0, "");
165 }
166 else
167 match_baselen = basedir_len(paths[0]);
168 read_tree_recursive(commit->tree, "", 0, 0, paths, walk_tree, NULL);
169 if (!match)
170 html_status(404, "Not found", 0);
171 else if (match == 2)
172 print_dir_tail();
173 }