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 | * |
| 3 | * Copyright (C) 2008 Lars Hjemli |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
| 10 | #include "html.h" |
| 11 | #include "ui-shared.h" |
| 12 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 13 | int match_baselen; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 14 | int match; |
| 15 | |
| 16 | static void print_object(const unsigned char *sha1, const char *path) |
| 17 | { |
| 18 | enum object_type type; |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 19 | char *buf, *ext; |
Ramsay Jones | bdd4a56 | 2008-11-04 19:22:08 +0000 | [diff] [blame] | 20 | unsigned long size; |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 21 | struct string_list_item *mime; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 22 | |
| 23 | type = sha1_object_info(sha1, &size); |
| 24 | if (type == OBJ_BAD) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 25 | html_status(404, "Not found", 0); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 26 | return; |
| 27 | } |
| 28 | |
| 29 | buf = read_sha1_file(sha1, &type, &size); |
| 30 | if (!buf) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 31 | html_status(404, "Not found", 0); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 32 | return; |
| 33 | } |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 34 | ctx.page.mimetype = NULL; |
| 35 | ext = strrchr(path, '.'); |
| 36 | if (ext && *(++ext)) { |
Lars Hjemli | 6d7552b | 2010-08-22 13:29:57 +0200 | [diff] [blame] | 37 | mime = string_list_lookup(&ctx.cfg.mimetypes, ext); |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 38 | if (mime) |
| 39 | ctx.page.mimetype = (char *)mime->util; |
| 40 | } |
| 41 | if (!ctx.page.mimetype) { |
| 42 | if (buffer_is_binary(buf, size)) |
| 43 | ctx.page.mimetype = "application/octet-stream"; |
| 44 | else |
| 45 | ctx.page.mimetype = "text/plain"; |
| 46 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 47 | ctx.page.filename = fmt("%s", path); |
| 48 | ctx.page.size = size; |
Lars Hjemli | 488a214 | 2009-02-19 22:38:36 +0100 | [diff] [blame] | 49 | ctx.page.etag = sha1_to_hex(sha1); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 50 | cgit_print_http_headers(&ctx); |
| 51 | html_raw(buf, size); |
| 52 | match = 1; |
| 53 | } |
| 54 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 55 | static char *buildpath(const char *base, int baselen, const char *path) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 56 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 57 | if (path[0]) |
| 58 | return fmt("%.*s%s/", baselen, base, path); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 59 | else |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 60 | return fmt("%.*s/", baselen, base); |
| 61 | } |
| 62 | |
| 63 | static void print_dir(const unsigned char *sha1, const char *base, |
| 64 | int baselen, const char *path) |
| 65 | { |
| 66 | char *fullpath, *slash; |
| 67 | size_t len; |
| 68 | |
| 69 | fullpath = buildpath(base, baselen, path); |
| 70 | slash = (fullpath[0] == '/' ? "" : "/"); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 71 | ctx.page.etag = sha1_to_hex(sha1); |
| 72 | cgit_print_http_headers(&ctx); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 73 | htmlf("<html><head><title>%s", slash); |
| 74 | html_txt(fullpath); |
| 75 | htmlf("</title></head>\n<body>\n<h2>%s", slash); |
| 76 | html_txt(fullpath); |
| 77 | html("</h2>\n<ul>\n"); |
| 78 | len = strlen(fullpath); |
| 79 | if (len > 1) { |
| 80 | fullpath[len - 1] = 0; |
| 81 | slash = strrchr(fullpath, '/'); |
| 82 | if (slash) |
| 83 | *(slash + 1) = 0; |
| 84 | else |
| 85 | fullpath = NULL; |
| 86 | html("<li>"); |
| 87 | cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 88 | fullpath); |
| 89 | html("</li>\n"); |
| 90 | } |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 91 | match = 2; |
| 92 | } |
| 93 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 94 | static void print_dir_entry(const unsigned char *sha1, const char *base, |
| 95 | int baselen, const char *path, unsigned mode) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 96 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 97 | char *fullpath; |
| 98 | |
| 99 | fullpath = buildpath(base, baselen, path); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame^] | 100 | if (!S_ISDIR(mode) && !S_ISGITLINK(mode)) |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 101 | fullpath[strlen(fullpath) - 1] = 0; |
| 102 | html(" <li>"); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame^] | 103 | if (S_ISGITLINK(mode)) { |
| 104 | cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1)); |
| 105 | } else |
| 106 | cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 107 | fullpath); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 108 | html("</li>\n"); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 109 | match = 2; |
| 110 | } |
| 111 | |
| 112 | static void print_dir_tail(void) |
| 113 | { |
| 114 | html(" </ul>\n</body></html>\n"); |
| 115 | } |
| 116 | |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 117 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
| 118 | const char *pathname, unsigned mode, int stage, |
| 119 | void *cbdata) |
| 120 | { |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 121 | if (baselen == match_baselen) { |
| 122 | if (S_ISREG(mode)) |
| 123 | print_object(sha1, pathname); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 124 | else if (S_ISDIR(mode)) { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 125 | print_dir(sha1, base, baselen, pathname); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 126 | return READ_TREE_RECURSIVE; |
| 127 | } |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 128 | } |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 129 | else if (baselen > match_baselen) |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 130 | print_dir_entry(sha1, base, baselen, pathname, mode); |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 131 | else if (S_ISDIR(mode)) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 132 | return READ_TREE_RECURSIVE; |
| 133 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 134 | return 0; |
| 135 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 136 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 137 | static int basedir_len(const char *path) |
| 138 | { |
| 139 | char *p = strrchr(path, '/'); |
| 140 | if (p) |
| 141 | return p - path + 1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | void cgit_print_plain(struct cgit_context *ctx) |
| 146 | { |
| 147 | const char *rev = ctx->qry.sha1; |
| 148 | unsigned char sha1[20]; |
| 149 | struct commit *commit; |
| 150 | const char *paths[] = {ctx->qry.path, NULL}; |
| 151 | |
| 152 | if (!rev) |
| 153 | rev = ctx->qry.head; |
| 154 | |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 155 | if (get_sha1(rev, sha1)) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 156 | html_status(404, "Not found", 0); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | commit = lookup_commit_reference(sha1); |
| 160 | if (!commit || parse_commit(commit)) { |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 161 | html_status(404, "Not found", 0); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 162 | return; |
| 163 | } |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 164 | if (!paths[0]) { |
| 165 | paths[0] = ""; |
| 166 | match_baselen = -1; |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 167 | print_dir(commit->tree->object.sha1, "", 0, ""); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 168 | } |
| 169 | else |
| 170 | match_baselen = basedir_len(paths[0]); |
Martins Polakovs | 24538b0 | 2009-12-10 17:34:42 +0159 | [diff] [blame] | 171 | read_tree_recursive(commit->tree, "", 0, 0, paths, walk_tree, NULL); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 172 | if (!match) |
Lars Hjemli | 885096c | 2008-08-06 22:57:44 +0200 | [diff] [blame] | 173 | html_status(404, "Not found", 0); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 174 | else if (match == 2) |
| 175 | print_dir_tail(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 176 | } |