]>
git.cameronkatri.com Git - cgit.git/blob - ui-plain.c
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"
17 static char *get_mimetype_from_file(const char *filename
, const char *ext
)
19 static const char *delimiters
;
29 fd
= fopen(filename
, "r");
33 delimiters
= " \t\r\n";
36 /* loop over all lines in the file */
37 while (!result
&& fgets(line
, sizeof(line
), fd
)) {
38 mimetype
= strtok(line
, delimiters
);
40 /* skip empty lines and comment lines */
41 if (!mimetype
|| (mimetype
[0] == '#'))
44 /* loop over all extensions of mimetype */
45 while ((token
= strtok(NULL
, delimiters
))) {
46 if (!strcasecmp(ext
, token
)) {
47 result
= xstrdup(mimetype
);
57 static void print_object(const unsigned char *sha1
, const char *path
)
59 enum object_type type
;
62 struct string_list_item
*mime
;
65 type
= sha1_object_info(sha1
, &size
);
66 if (type
== OBJ_BAD
) {
67 html_status(404, "Not found", 0);
71 buf
= read_sha1_file(sha1
, &type
, &size
);
73 html_status(404, "Not found", 0);
76 ctx
.page
.mimetype
= NULL
;
77 ext
= strrchr(path
, '.');
79 if (ext
&& *(++ext
)) {
80 mime
= string_list_lookup(&ctx
.cfg
.mimetypes
, ext
);
82 ctx
.page
.mimetype
= (char *)mime
->util
;
84 ctx
.page
.mimetype
= get_mimetype_from_file(ctx
.cfg
.mimetype_file
, ext
);
85 if (ctx
.page
.mimetype
)
89 if (!ctx
.page
.mimetype
) {
90 if (buffer_is_binary(buf
, size
))
91 ctx
.page
.mimetype
= "application/octet-stream";
93 ctx
.page
.mimetype
= "text/plain";
95 ctx
.page
.filename
= fmt("%s", path
);
97 ctx
.page
.etag
= sha1_to_hex(sha1
);
98 cgit_print_http_headers(&ctx
);
102 free(ctx
.page
.mimetype
);
105 static char *buildpath(const char *base
, int baselen
, const char *path
)
108 return fmt("%.*s%s/", baselen
, base
, path
);
110 return fmt("%.*s/", baselen
, base
);
113 static void print_dir(const unsigned char *sha1
, const char *base
,
114 int baselen
, const char *path
)
116 char *fullpath
, *slash
;
119 fullpath
= buildpath(base
, baselen
, path
);
120 slash
= (fullpath
[0] == '/' ? "" : "/");
121 ctx
.page
.etag
= sha1_to_hex(sha1
);
122 cgit_print_http_headers(&ctx
);
123 htmlf("<html><head><title>%s", slash
);
125 htmlf("</title></head>\n<body>\n<h2>%s", slash
);
127 html("</h2>\n<ul>\n");
128 len
= strlen(fullpath
);
130 fullpath
[len
- 1] = 0;
131 slash
= strrchr(fullpath
, '/');
137 cgit_plain_link("../", NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
144 static void print_dir_entry(const unsigned char *sha1
, const char *base
,
145 int baselen
, const char *path
, unsigned mode
)
149 fullpath
= buildpath(base
, baselen
, path
);
150 if (!S_ISDIR(mode
) && !S_ISGITLINK(mode
))
151 fullpath
[strlen(fullpath
) - 1] = 0;
153 if (S_ISGITLINK(mode
)) {
154 cgit_submodule_link(NULL
, fullpath
, sha1_to_hex(sha1
));
156 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 if (baselen
== match_baselen
) {
173 print_object(sha1
, pathname
);
174 else if (S_ISDIR(mode
)) {
175 print_dir(sha1
, base
, baselen
, pathname
);
176 return READ_TREE_RECURSIVE
;
179 else if (baselen
> match_baselen
)
180 print_dir_entry(sha1
, base
, baselen
, pathname
, mode
);
181 else if (S_ISDIR(mode
))
182 return READ_TREE_RECURSIVE
;
187 static int basedir_len(const char *path
)
189 char *p
= strrchr(path
, '/');
195 void cgit_print_plain(struct cgit_context
*ctx
)
197 const char *rev
= ctx
->qry
.sha1
;
198 unsigned char sha1
[20];
199 struct commit
*commit
;
200 const char *paths
[] = {ctx
->qry
.path
, NULL
};
205 if (get_sha1(rev
, sha1
)) {
206 html_status(404, "Not found", 0);
209 commit
= lookup_commit_reference(sha1
);
210 if (!commit
|| parse_commit(commit
)) {
211 html_status(404, "Not found", 0);
217 print_dir(commit
->tree
->object
.sha1
, "", 0, "");
220 match_baselen
= basedir_len(paths
[0]);
221 read_tree_recursive(commit
->tree
, "", 0, 0, paths
, walk_tree
, NULL
);
223 html_status(404, "Not found", 0);