Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 1 | /* ui-plain.c: functions for output of plain blobs by path |
| 2 | * |
Lukas Fleischer | f7f26f8 | 2014-01-08 15:10:49 +0100 | [diff] [blame] | 3 | * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 10 | #include "ui-plain.h" |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 11 | #include "html.h" |
| 12 | #include "ui-shared.h" |
| 13 | |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 14 | struct walk_tree_context { |
| 15 | int match_baselen; |
| 16 | int match; |
| 17 | }; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 18 | |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 19 | static int print_object(const unsigned char *sha1, const char *path) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 20 | { |
| 21 | enum object_type type; |
Christian Hesse | aa943bc | 2015-08-16 14:53:52 +0200 | [diff] [blame] | 22 | char *buf, *mimetype; |
Ramsay Jones | bdd4a56 | 2008-11-04 19:22:08 +0000 | [diff] [blame] | 23 | unsigned long size; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 24 | |
| 25 | type = sha1_object_info(sha1, &size); |
| 26 | if (type == OBJ_BAD) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 27 | cgit_print_error_page(404, "Not found", "Not found"); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 28 | return 0; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | buf = read_sha1_file(sha1, &type, &size); |
| 32 | if (!buf) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 33 | cgit_print_error_page(404, "Not found", "Not found"); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 34 | return 0; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 35 | } |
Christian Hesse | aa943bc | 2015-08-16 14:53:52 +0200 | [diff] [blame] | 36 | |
| 37 | mimetype = get_mimetype_for_filename(path); |
| 38 | ctx.page.mimetype = mimetype; |
| 39 | |
Jason A. Donenfeld | c326f3e | 2016-01-14 14:53:28 +0100 | [diff] [blame] | 40 | if (!ctx.repo->enable_html_serving) { |
| 41 | html("X-Content-Type-Options: nosniff\n"); |
| 42 | html("Content-Security-Policy: default-src 'none'\n"); |
| 43 | if (mimetype) { |
| 44 | /* Built-in white list allows PDF and everything that isn't text/ and application/ */ |
| 45 | if ((!strncmp(mimetype, "text/", 5) || !strncmp(mimetype, "application/", 12)) && strcmp(mimetype, "application/pdf")) |
| 46 | ctx.page.mimetype = NULL; |
| 47 | } |
| 48 | } |
| 49 | |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 50 | if (!ctx.page.mimetype) { |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 51 | if (buffer_is_binary(buf, size)) { |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 52 | ctx.page.mimetype = "application/octet-stream"; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 53 | ctx.page.charset = NULL; |
| 54 | } else { |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 55 | ctx.page.mimetype = "text/plain"; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 56 | } |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 57 | } |
John Keeping | 42d5476 | 2013-04-06 10:49:22 +0100 | [diff] [blame] | 58 | ctx.page.filename = path; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 59 | ctx.page.size = size; |
Lars Hjemli | 488a214 | 2009-02-19 22:38:36 +0100 | [diff] [blame] | 60 | ctx.page.etag = sha1_to_hex(sha1); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 61 | cgit_print_http_headers(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 62 | html_raw(buf, size); |
Christian Hesse | aa943bc | 2015-08-16 14:53:52 +0200 | [diff] [blame] | 63 | free(mimetype); |
Christian Hesse | 979db79 | 2015-10-09 14:55:49 +0200 | [diff] [blame] | 64 | free(buf); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 65 | return 1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 68 | static char *buildpath(const char *base, int baselen, const char *path) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 69 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 70 | if (path[0]) |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 71 | return fmtalloc("%.*s%s/", baselen, base, path); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 72 | else |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 73 | return fmtalloc("%.*s/", baselen, base); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void print_dir(const unsigned char *sha1, const char *base, |
| 77 | int baselen, const char *path) |
| 78 | { |
| 79 | char *fullpath, *slash; |
| 80 | size_t len; |
| 81 | |
| 82 | fullpath = buildpath(base, baselen, path); |
| 83 | slash = (fullpath[0] == '/' ? "" : "/"); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 84 | ctx.page.etag = sha1_to_hex(sha1); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 85 | cgit_print_http_headers(); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 86 | htmlf("<html><head><title>%s", slash); |
| 87 | html_txt(fullpath); |
| 88 | htmlf("</title></head>\n<body>\n<h2>%s", slash); |
| 89 | html_txt(fullpath); |
| 90 | html("</h2>\n<ul>\n"); |
| 91 | len = strlen(fullpath); |
| 92 | if (len > 1) { |
| 93 | fullpath[len - 1] = 0; |
| 94 | slash = strrchr(fullpath, '/'); |
| 95 | if (slash) |
| 96 | *(slash + 1) = 0; |
Christian Hesse | 08a2b81 | 2015-10-09 14:55:50 +0200 | [diff] [blame] | 97 | else { |
| 98 | free(fullpath); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 99 | fullpath = NULL; |
Christian Hesse | 08a2b81 | 2015-10-09 14:55:50 +0200 | [diff] [blame] | 100 | } |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 101 | html("<li>"); |
| 102 | cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 103 | fullpath); |
| 104 | html("</li>\n"); |
| 105 | } |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 106 | free(fullpath); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 109 | static void print_dir_entry(const unsigned char *sha1, const char *base, |
| 110 | int baselen, const char *path, unsigned mode) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 111 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 112 | char *fullpath; |
| 113 | |
| 114 | fullpath = buildpath(base, baselen, path); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame] | 115 | if (!S_ISDIR(mode) && !S_ISGITLINK(mode)) |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 116 | fullpath[strlen(fullpath) - 1] = 0; |
| 117 | html(" <li>"); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame] | 118 | if (S_ISGITLINK(mode)) { |
| 119 | cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1)); |
| 120 | } else |
| 121 | cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 122 | fullpath); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 123 | html("</li>\n"); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 124 | free(fullpath); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void print_dir_tail(void) |
| 128 | { |
| 129 | html(" </ul>\n</body></html>\n"); |
| 130 | } |
| 131 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 132 | static int walk_tree(const unsigned char *sha1, struct strbuf *base, |
| 133 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 134 | { |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 135 | struct walk_tree_context *walk_tree_ctx = cbdata; |
| 136 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 137 | if (base->len == walk_tree_ctx->match_baselen) { |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 138 | if (S_ISREG(mode)) { |
| 139 | if (print_object(sha1, pathname)) |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 140 | walk_tree_ctx->match = 1; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 141 | } else if (S_ISDIR(mode)) { |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 142 | print_dir(sha1, base->buf, base->len, pathname); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 143 | walk_tree_ctx->match = 2; |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 144 | return READ_TREE_RECURSIVE; |
| 145 | } |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 146 | } else if (base->len > walk_tree_ctx->match_baselen) { |
| 147 | print_dir_entry(sha1, base->buf, base->len, pathname, mode); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 148 | walk_tree_ctx->match = 2; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 149 | } else if (S_ISDIR(mode)) { |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 150 | return READ_TREE_RECURSIVE; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 151 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 152 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 155 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 156 | static int basedir_len(const char *path) |
| 157 | { |
| 158 | char *p = strrchr(path, '/'); |
| 159 | if (p) |
| 160 | return p - path + 1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 164 | void cgit_print_plain(void) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 165 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 166 | const char *rev = ctx.qry.sha1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 167 | unsigned char sha1[20]; |
| 168 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 169 | struct pathspec_item path_items = { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 170 | .match = ctx.qry.path, |
| 171 | .len = ctx.qry.path ? strlen(ctx.qry.path) : 0 |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 172 | }; |
| 173 | struct pathspec paths = { |
| 174 | .nr = 1, |
| 175 | .items = &path_items |
| 176 | }; |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 177 | struct walk_tree_context walk_tree_ctx = { |
| 178 | .match = 0 |
| 179 | }; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 180 | |
| 181 | if (!rev) |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 182 | rev = ctx.qry.head; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 183 | |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 184 | if (get_sha1(rev, sha1)) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 185 | cgit_print_error_page(404, "Not found", "Not found"); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | commit = lookup_commit_reference(sha1); |
| 189 | if (!commit || parse_commit(commit)) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 190 | cgit_print_error_page(404, "Not found", "Not found"); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 191 | return; |
| 192 | } |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 193 | if (!path_items.match) { |
| 194 | path_items.match = ""; |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 195 | walk_tree_ctx.match_baselen = -1; |
Christian Hesse | 559ab5e | 2016-01-05 07:38:53 +0100 | [diff] [blame] | 196 | print_dir(commit->tree->object.oid.hash, "", 0, ""); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 197 | walk_tree_ctx.match = 2; |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 198 | } |
| 199 | else |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 200 | walk_tree_ctx.match_baselen = basedir_len(path_items.match); |
| 201 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
| 202 | if (!walk_tree_ctx.match) |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 203 | cgit_print_error_page(404, "Not found", "Not found"); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 204 | else if (walk_tree_ctx.match == 2) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 205 | print_dir_tail(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 206 | } |