1 /* ui-plain.c: functions for output of plain blobs by path
3 * Copyright (C) 2008 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
14 struct walk_tree_context
{
19 static char *get_mimetype_from_file(const char *filename
, const char *ext
)
21 static const char *delimiters
;
31 fd
= fopen(filename
, "r");
35 delimiters
= " \t\r\n";
38 /* loop over all lines in the file */
39 while (!result
&& fgets(line
, sizeof(line
), fd
)) {
40 mimetype
= strtok(line
, delimiters
);
42 /* skip empty lines and comment lines */
43 if (!mimetype
|| (mimetype
[0] == '#'))
46 /* loop over all extensions of mimetype */
47 while ((token
= strtok(NULL
, delimiters
))) {
48 if (!strcasecmp(ext
, token
)) {
49 result
= xstrdup(mimetype
);
59 static int print_object(const unsigned char *sha1
, const char *path
)
61 enum object_type type
;
64 struct string_list_item
*mime
;
67 type
= sha1_object_info(sha1
, &size
);
68 if (type
== OBJ_BAD
) {
69 html_status(404, "Not found", 0);
73 buf
= read_sha1_file(sha1
, &type
, &size
);
75 html_status(404, "Not found", 0);
78 ctx
.page
.mimetype
= NULL
;
79 ext
= strrchr(path
, '.');
81 if (ext
&& *(++ext
)) {
82 mime
= string_list_lookup(&ctx
.cfg
.mimetypes
, ext
);
84 ctx
.page
.mimetype
= (char *)mime
->util
;
86 ctx
.page
.mimetype
= get_mimetype_from_file(ctx
.cfg
.mimetype_file
, ext
);
87 if (ctx
.page
.mimetype
)
91 if (!ctx
.page
.mimetype
) {
92 if (buffer_is_binary(buf
, size
))
93 ctx
.page
.mimetype
= "application/octet-stream";
95 ctx
.page
.mimetype
= "text/plain";
97 ctx
.page
.filename
= fmt("%s", path
);
99 ctx
.page
.etag
= sha1_to_hex(sha1
);
100 cgit_print_http_headers(&ctx
);
103 free(ctx
.page
.mimetype
);
107 static char *buildpath(const char *base
, int baselen
, const char *path
)
110 return fmt("%.*s%s/", baselen
, base
, path
);
112 return fmt("%.*s/", baselen
, base
);
115 static void print_dir(const unsigned char *sha1
, const char *base
,
116 int baselen
, const char *path
)
118 char *fullpath
, *slash
;
121 fullpath
= buildpath(base
, baselen
, path
);
122 slash
= (fullpath
[0] == '/' ? "" : "/");
123 ctx
.page
.etag
= sha1_to_hex(sha1
);
124 cgit_print_http_headers(&ctx
);
125 htmlf("<html><head><title>%s", slash
);
127 htmlf("</title></head>\n<body>\n<h2>%s", slash
);
129 html("</h2>\n<ul>\n");
130 len
= strlen(fullpath
);
132 fullpath
[len
- 1] = 0;
133 slash
= strrchr(fullpath
, '/');
139 cgit_plain_link("../", NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
145 static void print_dir_entry(const unsigned char *sha1
, const char *base
,
146 int baselen
, const char *path
, unsigned mode
)
150 fullpath
= buildpath(base
, baselen
, path
);
151 if (!S_ISDIR(mode
) && !S_ISGITLINK(mode
))
152 fullpath
[strlen(fullpath
) - 1] = 0;
154 if (S_ISGITLINK(mode
)) {
155 cgit_submodule_link(NULL
, fullpath
, sha1_to_hex(sha1
));
157 cgit_plain_link(path
, NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
162 static void print_dir_tail(void)
164 html(" </ul>\n</body></html>\n");
167 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
168 const char *pathname
, unsigned mode
, int stage
,
171 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
173 if (baselen
== walk_tree_ctx
->match_baselen
) {
175 if (print_object(sha1
, pathname
))
176 walk_tree_ctx
->match
= 1;
177 } else if (S_ISDIR(mode
)) {
178 print_dir(sha1
, base
, baselen
, pathname
);
179 walk_tree_ctx
->match
= 2;
180 return READ_TREE_RECURSIVE
;
182 } else if (baselen
> walk_tree_ctx
->match_baselen
) {
183 print_dir_entry(sha1
, base
, baselen
, pathname
, mode
);
184 walk_tree_ctx
->match
= 2;
185 } else if (S_ISDIR(mode
)) {
186 return READ_TREE_RECURSIVE
;
192 static int basedir_len(const char *path
)
194 char *p
= strrchr(path
, '/');
200 void cgit_print_plain(struct cgit_context
*ctx
)
202 const char *rev
= ctx
->qry
.sha1
;
203 unsigned char sha1
[20];
204 struct commit
*commit
;
205 struct pathspec_item path_items
= {
206 .match
= ctx
->qry
.path
,
207 .len
= ctx
->qry
.path
? strlen(ctx
->qry
.path
) : 0
209 struct pathspec paths
= {
213 struct walk_tree_context walk_tree_ctx
= {
220 if (get_sha1(rev
, sha1
)) {
221 html_status(404, "Not found", 0);
224 commit
= lookup_commit_reference(sha1
);
225 if (!commit
|| parse_commit(commit
)) {
226 html_status(404, "Not found", 0);
229 if (!path_items
.match
) {
230 path_items
.match
= "";
231 walk_tree_ctx
.match_baselen
= -1;
232 print_dir(commit
->tree
->object
.sha1
, "", 0, "");
233 walk_tree_ctx
.match
= 2;
236 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
)
239 html_status(404, "Not found", 0);
240 else if (walk_tree_ctx
.match
== 2)