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)
13 #include "ui-shared.h"
15 struct walk_tree_context
{
20 static char *get_mimetype_from_file(const char *filename
, const char *ext
)
22 static const char *delimiters
;
32 fd
= fopen(filename
, "r");
36 delimiters
= " \t\r\n";
39 /* loop over all lines in the file */
40 while (!result
&& fgets(line
, sizeof(line
), fd
)) {
41 mimetype
= strtok(line
, delimiters
);
43 /* skip empty lines and comment lines */
44 if (!mimetype
|| (mimetype
[0] == '#'))
47 /* loop over all extensions of mimetype */
48 while ((token
= strtok(NULL
, delimiters
))) {
49 if (!strcasecmp(ext
, token
)) {
50 result
= xstrdup(mimetype
);
60 static int print_object(const unsigned char *sha1
, const char *path
)
62 enum object_type type
;
65 struct string_list_item
*mime
;
68 type
= sha1_object_info(sha1
, &size
);
69 if (type
== OBJ_BAD
) {
70 html_status(404, "Not found", 0);
74 buf
= read_sha1_file(sha1
, &type
, &size
);
76 html_status(404, "Not found", 0);
79 ctx
.page
.mimetype
= NULL
;
80 ext
= strrchr(path
, '.');
82 if (ext
&& *(++ext
)) {
83 mime
= string_list_lookup(&ctx
.cfg
.mimetypes
, ext
);
85 ctx
.page
.mimetype
= (char *)mime
->util
;
87 ctx
.page
.mimetype
= get_mimetype_from_file(ctx
.cfg
.mimetype_file
, ext
);
88 if (ctx
.page
.mimetype
)
92 if (!ctx
.page
.mimetype
) {
93 if (buffer_is_binary(buf
, size
))
94 ctx
.page
.mimetype
= "application/octet-stream";
96 ctx
.page
.mimetype
= "text/plain";
98 ctx
.page
.filename
= fmt("%s", path
);
100 ctx
.page
.etag
= sha1_to_hex(sha1
);
101 cgit_print_http_headers(&ctx
);
103 /* If we allocated this, then casting away const is safe. */
105 free((char*) ctx
.page
.mimetype
);
109 static char *buildpath(const char *base
, int baselen
, const char *path
)
112 return fmt("%.*s%s/", baselen
, base
, path
);
114 return fmt("%.*s/", baselen
, base
);
117 static void print_dir(const unsigned char *sha1
, const char *base
,
118 int baselen
, const char *path
)
120 char *fullpath
, *slash
;
123 fullpath
= buildpath(base
, baselen
, path
);
124 slash
= (fullpath
[0] == '/' ? "" : "/");
125 ctx
.page
.etag
= sha1_to_hex(sha1
);
126 cgit_print_http_headers(&ctx
);
127 htmlf("<html><head><title>%s", slash
);
129 htmlf("</title></head>\n<body>\n<h2>%s", slash
);
131 html("</h2>\n<ul>\n");
132 len
= strlen(fullpath
);
134 fullpath
[len
- 1] = 0;
135 slash
= strrchr(fullpath
, '/');
141 cgit_plain_link("../", NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
147 static void print_dir_entry(const unsigned char *sha1
, const char *base
,
148 int baselen
, const char *path
, unsigned mode
)
152 fullpath
= buildpath(base
, baselen
, path
);
153 if (!S_ISDIR(mode
) && !S_ISGITLINK(mode
))
154 fullpath
[strlen(fullpath
) - 1] = 0;
156 if (S_ISGITLINK(mode
)) {
157 cgit_submodule_link(NULL
, fullpath
, sha1_to_hex(sha1
));
159 cgit_plain_link(path
, NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
164 static void print_dir_tail(void)
166 html(" </ul>\n</body></html>\n");
169 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
170 const char *pathname
, unsigned mode
, int stage
,
173 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
175 if (baselen
== walk_tree_ctx
->match_baselen
) {
177 if (print_object(sha1
, pathname
))
178 walk_tree_ctx
->match
= 1;
179 } else if (S_ISDIR(mode
)) {
180 print_dir(sha1
, base
, baselen
, pathname
);
181 walk_tree_ctx
->match
= 2;
182 return READ_TREE_RECURSIVE
;
184 } else if (baselen
> walk_tree_ctx
->match_baselen
) {
185 print_dir_entry(sha1
, base
, baselen
, pathname
, mode
);
186 walk_tree_ctx
->match
= 2;
187 } else if (S_ISDIR(mode
)) {
188 return READ_TREE_RECURSIVE
;
194 static int basedir_len(const char *path
)
196 char *p
= strrchr(path
, '/');
202 void cgit_print_plain(struct cgit_context
*ctx
)
204 const char *rev
= ctx
->qry
.sha1
;
205 unsigned char sha1
[20];
206 struct commit
*commit
;
207 struct pathspec_item path_items
= {
208 .match
= ctx
->qry
.path
,
209 .len
= ctx
->qry
.path
? strlen(ctx
->qry
.path
) : 0
211 struct pathspec paths
= {
215 struct walk_tree_context walk_tree_ctx
= {
222 if (get_sha1(rev
, sha1
)) {
223 html_status(404, "Not found", 0);
226 commit
= lookup_commit_reference(sha1
);
227 if (!commit
|| parse_commit(commit
)) {
228 html_status(404, "Not found", 0);
231 if (!path_items
.match
) {
232 path_items
.match
= "";
233 walk_tree_ctx
.match_baselen
= -1;
234 print_dir(commit
->tree
->object
.sha1
, "", 0, "");
235 walk_tree_ctx
.match
= 2;
238 walk_tree_ctx
.match_baselen
= basedir_len(path_items
.match
);
239 read_tree_recursive(commit
->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
240 if (!walk_tree_ctx
.match
)
241 html_status(404, "Not found", 0);
242 else if (walk_tree_ctx
.match
== 2)