]> gitweb.ps.run Git - ps-cgit/blob - ui-plain.c
git: update to v2.46.0
[ps-cgit] / ui-plain.c
1 /* ui-plain.c: functions for output of plain blobs by path
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #define USE_THE_REPOSITORY_VARIABLE
10
11 #include "cgit.h"
12 #include "ui-plain.h"
13 #include "html.h"
14 #include "ui-shared.h"
15
16 struct walk_tree_context {
17         int match_baselen;
18         int match;
19 };
20
21 static int print_object(const struct object_id *oid, const char *path)
22 {
23         enum object_type type;
24         char *buf, *mimetype;
25         unsigned long size;
26
27         type = oid_object_info(the_repository, oid, &size);
28         if (type == OBJ_BAD) {
29                 cgit_print_error_page(404, "Not found", "Not found");
30                 return 0;
31         }
32
33         buf = repo_read_object_file(the_repository, oid, &type, &size);
34         if (!buf) {
35                 cgit_print_error_page(404, "Not found", "Not found");
36                 return 0;
37         }
38
39         mimetype = get_mimetype_for_filename(path);
40         ctx.page.mimetype = mimetype;
41
42         if (!ctx.repo->enable_html_serving) {
43                 html("X-Content-Type-Options: nosniff\n");
44                 html("Content-Security-Policy: default-src 'none'\n");
45                 if (mimetype) {
46                         /* Built-in white list allows PDF and everything that isn't text/ and application/ */
47                         if ((!strncmp(mimetype, "text/", 5) || !strncmp(mimetype, "application/", 12)) && strcmp(mimetype, "application/pdf"))
48                                 ctx.page.mimetype = NULL;
49                 }
50         }
51
52         if (!ctx.page.mimetype) {
53                 if (buffer_is_binary(buf, size)) {
54                         ctx.page.mimetype = "application/octet-stream";
55                         ctx.page.charset = NULL;
56                 } else {
57                         ctx.page.mimetype = "text/plain";
58                 }
59         }
60         ctx.page.filename = path;
61         ctx.page.size = size;
62         ctx.page.etag = oid_to_hex(oid);
63         cgit_print_http_headers();
64         html_raw(buf, size);
65         free(mimetype);
66         free(buf);
67         return 1;
68 }
69
70 static char *buildpath(const char *base, int baselen, const char *path)
71 {
72         if (path[0])
73                 return fmtalloc("%.*s%s/", baselen, base, path);
74         else
75                 return fmtalloc("%.*s/", baselen, base);
76 }
77
78 static void print_dir(const struct object_id *oid, const char *base,
79                       int baselen, const char *path)
80 {
81         char *fullpath, *slash;
82         size_t len;
83
84         fullpath = buildpath(base, baselen, path);
85         slash = (fullpath[0] == '/' ? "" : "/");
86         ctx.page.etag = oid_to_hex(oid);
87         cgit_print_http_headers();
88         htmlf("<html><head><title>%s", slash);
89         html_txt(fullpath);
90         htmlf("</title></head>\n<body>\n<h2>%s", slash);
91         html_txt(fullpath);
92         html("</h2>\n<ul>\n");
93         len = strlen(fullpath);
94         if (len > 1) {
95                 fullpath[len - 1] = 0;
96                 slash = strrchr(fullpath, '/');
97                 if (slash)
98                         *(slash + 1) = 0;
99                 else {
100                         free(fullpath);
101                         fullpath = NULL;
102                 }
103                 html("<li>");
104                 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.oid,
105                                 fullpath);
106                 html("</li>\n");
107         }
108         free(fullpath);
109 }
110
111 static void print_dir_entry(const struct object_id *oid, const char *base,
112                             int baselen, const char *path, unsigned mode)
113 {
114         char *fullpath;
115
116         fullpath = buildpath(base, baselen, path);
117         if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
118                 fullpath[strlen(fullpath) - 1] = 0;
119         html("  <li>");
120         if (S_ISGITLINK(mode)) {
121                 cgit_submodule_link(NULL, fullpath, oid_to_hex(oid));
122         } else
123                 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.oid,
124                                 fullpath);
125         html("</li>\n");
126         free(fullpath);
127 }
128
129 static void print_dir_tail(void)
130 {
131         html(" </ul>\n</body></html>\n");
132 }
133
134 static int walk_tree(const struct object_id *oid, struct strbuf *base,
135                 const char *pathname, unsigned mode, void *cbdata)
136 {
137         struct walk_tree_context *walk_tree_ctx = cbdata;
138
139         if (base->len == walk_tree_ctx->match_baselen) {
140                 if (S_ISREG(mode) || S_ISLNK(mode)) {
141                         if (print_object(oid, pathname))
142                                 walk_tree_ctx->match = 1;
143                 } else if (S_ISDIR(mode)) {
144                         print_dir(oid, base->buf, base->len, pathname);
145                         walk_tree_ctx->match = 2;
146                         return READ_TREE_RECURSIVE;
147                 }
148         } else if (base->len < INT_MAX && (int)base->len > walk_tree_ctx->match_baselen) {
149                 print_dir_entry(oid, base->buf, base->len, pathname, mode);
150                 walk_tree_ctx->match = 2;
151         } else if (S_ISDIR(mode)) {
152                 return READ_TREE_RECURSIVE;
153         }
154
155         return 0;
156 }
157
158 static int basedir_len(const char *path)
159 {
160         char *p = strrchr(path, '/');
161         if (p)
162                 return p - path + 1;
163         return 0;
164 }
165
166 void cgit_print_plain(void)
167 {
168         const char *rev = ctx.qry.oid;
169         struct object_id oid;
170         struct commit *commit;
171         struct pathspec_item path_items = {
172                 .match = ctx.qry.path,
173                 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
174         };
175         struct pathspec paths = {
176                 .nr = 1,
177                 .items = &path_items
178         };
179         struct walk_tree_context walk_tree_ctx = {
180                 .match = 0
181         };
182
183         if (!rev)
184                 rev = ctx.qry.head;
185
186         if (repo_get_oid(the_repository, rev, &oid)) {
187                 cgit_print_error_page(404, "Not found", "Not found");
188                 return;
189         }
190         commit = lookup_commit_reference(the_repository, &oid);
191         if (!commit || repo_parse_commit(the_repository, commit)) {
192                 cgit_print_error_page(404, "Not found", "Not found");
193                 return;
194         }
195         if (!path_items.match) {
196                 path_items.match = "";
197                 walk_tree_ctx.match_baselen = -1;
198                 print_dir(get_commit_tree_oid(commit), "", 0, "");
199                 walk_tree_ctx.match = 2;
200         }
201         else
202                 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
203         read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
204                   &paths, walk_tree, &walk_tree_ctx);
205         if (!walk_tree_ctx.match)
206                 cgit_print_error_page(404, "Not found", "Not found");
207         else if (walk_tree_ctx.match == 2)
208                 print_dir_tail();
209 }