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 | |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 9 | #include <stdio.h> |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 10 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 11 | #include "ui-plain.h" |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 12 | #include "html.h" |
| 13 | #include "ui-shared.h" |
| 14 | |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 15 | struct walk_tree_context { |
| 16 | int match_baselen; |
| 17 | int match; |
| 18 | }; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 19 | |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 20 | static char *get_mimetype_from_file(const char *filename, const char *ext) |
| 21 | { |
| 22 | static const char *delimiters; |
| 23 | char *result; |
| 24 | FILE *fd; |
| 25 | char line[1024]; |
| 26 | char *mimetype; |
| 27 | char *token; |
| 28 | |
| 29 | if (!filename) |
| 30 | return NULL; |
| 31 | |
| 32 | fd = fopen(filename, "r"); |
| 33 | if (!fd) |
| 34 | return NULL; |
| 35 | |
| 36 | delimiters = " \t\r\n"; |
| 37 | result = NULL; |
| 38 | |
| 39 | /* loop over all lines in the file */ |
| 40 | while (!result && fgets(line, sizeof(line), fd)) { |
| 41 | mimetype = strtok(line, delimiters); |
| 42 | |
| 43 | /* skip empty lines and comment lines */ |
| 44 | if (!mimetype || (mimetype[0] == '#')) |
| 45 | continue; |
| 46 | |
| 47 | /* loop over all extensions of mimetype */ |
| 48 | while ((token = strtok(NULL, delimiters))) { |
| 49 | if (!strcasecmp(ext, token)) { |
| 50 | result = xstrdup(mimetype); |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | fclose(fd); |
| 56 | |
| 57 | return result; |
| 58 | } |
| 59 | |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 60 | static int print_object(const unsigned char *sha1, const char *path) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 61 | { |
| 62 | enum object_type type; |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 63 | char *buf, *ext; |
Ramsay Jones | bdd4a56 | 2008-11-04 19:22:08 +0000 | [diff] [blame] | 64 | unsigned long size; |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 65 | struct string_list_item *mime; |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 66 | int freemime; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 67 | |
| 68 | type = sha1_object_info(sha1, &size); |
| 69 | if (type == OBJ_BAD) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 70 | html_status(404, "Not found", 0); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 71 | return 0; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | buf = read_sha1_file(sha1, &type, &size); |
| 75 | if (!buf) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 76 | html_status(404, "Not found", 0); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 77 | return 0; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 78 | } |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 79 | ctx.page.mimetype = NULL; |
| 80 | ext = strrchr(path, '.'); |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 81 | freemime = 0; |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 82 | if (ext && *(++ext)) { |
Lars Hjemli | 6d7552b | 2010-08-22 13:29:57 +0200 | [diff] [blame] | 83 | mime = string_list_lookup(&ctx.cfg.mimetypes, ext); |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 84 | if (mime) { |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 85 | ctx.page.mimetype = (char *)mime->util; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 86 | ctx.page.charset = NULL; |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 87 | } else { |
| 88 | ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext); |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 89 | if (ctx.page.mimetype) { |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 90 | freemime = 1; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 91 | ctx.page.charset = NULL; |
| 92 | } |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 93 | } |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 94 | } |
| 95 | if (!ctx.page.mimetype) { |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 96 | if (buffer_is_binary(buf, size)) { |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 97 | ctx.page.mimetype = "application/octet-stream"; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 98 | ctx.page.charset = NULL; |
| 99 | } else { |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 100 | ctx.page.mimetype = "text/plain"; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 101 | } |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 102 | } |
John Keeping | 42d5476 | 2013-04-06 10:49:22 +0100 | [diff] [blame] | 103 | ctx.page.filename = path; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 104 | ctx.page.size = size; |
Lars Hjemli | 488a214 | 2009-02-19 22:38:36 +0100 | [diff] [blame] | 105 | ctx.page.etag = sha1_to_hex(sha1); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 106 | cgit_print_http_headers(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 107 | html_raw(buf, size); |
John Keeping | 57d09bf | 2013-04-07 12:46:45 +0100 | [diff] [blame] | 108 | /* If we allocated this, then casting away const is safe. */ |
Ferry Huberts | d01c600 | 2011-07-19 10:51:58 +0200 | [diff] [blame] | 109 | if (freemime) |
John Keeping | 57d09bf | 2013-04-07 12:46:45 +0100 | [diff] [blame] | 110 | free((char*) ctx.page.mimetype); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 111 | return 1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 114 | static char *buildpath(const char *base, int baselen, const char *path) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 115 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 116 | if (path[0]) |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 117 | return fmtalloc("%.*s%s/", baselen, base, path); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 118 | else |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 119 | return fmtalloc("%.*s/", baselen, base); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void print_dir(const unsigned char *sha1, const char *base, |
| 123 | int baselen, const char *path) |
| 124 | { |
| 125 | char *fullpath, *slash; |
| 126 | size_t len; |
| 127 | |
| 128 | fullpath = buildpath(base, baselen, path); |
| 129 | slash = (fullpath[0] == '/' ? "" : "/"); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 130 | ctx.page.etag = sha1_to_hex(sha1); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 131 | cgit_print_http_headers(); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 132 | htmlf("<html><head><title>%s", slash); |
| 133 | html_txt(fullpath); |
| 134 | htmlf("</title></head>\n<body>\n<h2>%s", slash); |
| 135 | html_txt(fullpath); |
| 136 | html("</h2>\n<ul>\n"); |
| 137 | len = strlen(fullpath); |
| 138 | if (len > 1) { |
| 139 | fullpath[len - 1] = 0; |
| 140 | slash = strrchr(fullpath, '/'); |
| 141 | if (slash) |
| 142 | *(slash + 1) = 0; |
| 143 | else |
| 144 | fullpath = NULL; |
| 145 | html("<li>"); |
| 146 | cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 147 | fullpath); |
| 148 | html("</li>\n"); |
| 149 | } |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 150 | free(fullpath); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 153 | static void print_dir_entry(const unsigned char *sha1, const char *base, |
| 154 | int baselen, const char *path, unsigned mode) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 155 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 156 | char *fullpath; |
| 157 | |
| 158 | fullpath = buildpath(base, baselen, path); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame] | 159 | if (!S_ISDIR(mode) && !S_ISGITLINK(mode)) |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 160 | fullpath[strlen(fullpath) - 1] = 0; |
| 161 | html(" <li>"); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame] | 162 | if (S_ISGITLINK(mode)) { |
| 163 | cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1)); |
| 164 | } else |
| 165 | cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 166 | fullpath); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 167 | html("</li>\n"); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 168 | free(fullpath); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void print_dir_tail(void) |
| 172 | { |
| 173 | html(" </ul>\n</body></html>\n"); |
| 174 | } |
| 175 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame^] | 176 | static int walk_tree(const unsigned char *sha1, struct strbuf *base, |
| 177 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 178 | { |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 179 | struct walk_tree_context *walk_tree_ctx = cbdata; |
| 180 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame^] | 181 | if (base->len == walk_tree_ctx->match_baselen) { |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 182 | if (S_ISREG(mode)) { |
| 183 | if (print_object(sha1, pathname)) |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 184 | walk_tree_ctx->match = 1; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 185 | } else if (S_ISDIR(mode)) { |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame^] | 186 | print_dir(sha1, base->buf, base->len, pathname); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 187 | walk_tree_ctx->match = 2; |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 188 | return READ_TREE_RECURSIVE; |
| 189 | } |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame^] | 190 | } else if (base->len > walk_tree_ctx->match_baselen) { |
| 191 | print_dir_entry(sha1, base->buf, base->len, pathname, mode); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 192 | walk_tree_ctx->match = 2; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 193 | } else if (S_ISDIR(mode)) { |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 194 | return READ_TREE_RECURSIVE; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 195 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 196 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 197 | return 0; |
| 198 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 199 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 200 | static int basedir_len(const char *path) |
| 201 | { |
| 202 | char *p = strrchr(path, '/'); |
| 203 | if (p) |
| 204 | return p - path + 1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 208 | void cgit_print_plain(void) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 209 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 210 | const char *rev = ctx.qry.sha1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 211 | unsigned char sha1[20]; |
| 212 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 213 | struct pathspec_item path_items = { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 214 | .match = ctx.qry.path, |
| 215 | .len = ctx.qry.path ? strlen(ctx.qry.path) : 0 |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 216 | }; |
| 217 | struct pathspec paths = { |
| 218 | .nr = 1, |
| 219 | .items = &path_items |
| 220 | }; |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 221 | struct walk_tree_context walk_tree_ctx = { |
| 222 | .match = 0 |
| 223 | }; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 224 | |
| 225 | if (!rev) |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 226 | rev = ctx.qry.head; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 227 | |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 228 | if (get_sha1(rev, sha1)) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 229 | html_status(404, "Not found", 0); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | commit = lookup_commit_reference(sha1); |
| 233 | if (!commit || parse_commit(commit)) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 234 | html_status(404, "Not found", 0); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 235 | return; |
| 236 | } |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 237 | if (!path_items.match) { |
| 238 | path_items.match = ""; |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 239 | walk_tree_ctx.match_baselen = -1; |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 240 | print_dir(commit->tree->object.sha1, "", 0, ""); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 241 | walk_tree_ctx.match = 2; |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 242 | } |
| 243 | else |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 244 | walk_tree_ctx.match_baselen = basedir_len(path_items.match); |
| 245 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
| 246 | if (!walk_tree_ctx.match) |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 247 | html_status(404, "Not found", 0); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 248 | else if (walk_tree_ctx.match == 2) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 249 | print_dir_tail(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 250 | } |