Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 1 | /* ui-blob.c: show blob content |
| 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 | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 9 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 10 | #include "ui-blob.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 11 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 12 | #include "ui-shared.h" |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 13 | |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 14 | struct walk_tree_context { |
Jason A. Donenfeld | cd4c77d | 2013-05-25 16:32:37 +0200 | [diff] [blame] | 15 | const char *match_path; |
Christian Hesse | c330a2e | 2016-10-11 08:55:34 +0200 | [diff] [blame] | 16 | struct object_id *matched_oid; |
John Keeping | 12d3d4c | 2015-03-08 16:32:17 +0000 | [diff] [blame] | 17 | unsigned int found_path:1; |
| 18 | unsigned int file_only:1; |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 19 | }; |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 20 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 21 | static int walk_tree(const unsigned char *sha1, struct strbuf *base, |
| 22 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 23 | { |
| 24 | struct walk_tree_context *walk_tree_ctx = cbdata; |
| 25 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 26 | if (walk_tree_ctx->file_only && !S_ISREG(mode)) |
| 27 | return READ_TREE_RECURSIVE; |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 28 | if (strncmp(base->buf, walk_tree_ctx->match_path, base->len) |
| 29 | || strcmp(walk_tree_ctx->match_path + base->len, pathname)) |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 30 | return READ_TREE_RECURSIVE; |
Christian Hesse | c330a2e | 2016-10-11 08:55:34 +0200 | [diff] [blame] | 31 | hashcpy(walk_tree_ctx->matched_oid->hash, sha1); |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 32 | walk_tree_ctx->found_path = 1; |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 36 | int cgit_ref_path_exists(const char *path, const char *ref, int file_only) |
Jason A. Donenfeld | cd4c77d | 2013-05-25 16:32:37 +0200 | [diff] [blame] | 37 | { |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 38 | struct object_id oid; |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 39 | unsigned long size; |
| 40 | struct pathspec_item path_items = { |
Christian Hesse | 3d33b46 | 2017-07-24 17:22:52 +0200 | [diff] [blame^] | 41 | .match = xstrdup(path), |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 42 | .len = strlen(path) |
| 43 | }; |
| 44 | struct pathspec paths = { |
| 45 | .nr = 1, |
| 46 | .items = &path_items |
| 47 | }; |
| 48 | struct walk_tree_context walk_tree_ctx = { |
| 49 | .match_path = path, |
Christian Hesse | c330a2e | 2016-10-11 08:55:34 +0200 | [diff] [blame] | 50 | .matched_oid = &oid, |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 51 | .found_path = 0, |
| 52 | .file_only = file_only |
| 53 | }; |
Jason A. Donenfeld | cd4c77d | 2013-05-25 16:32:37 +0200 | [diff] [blame] | 54 | |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 55 | if (get_oid(ref, &oid)) |
Christian Hesse | 3d33b46 | 2017-07-24 17:22:52 +0200 | [diff] [blame^] | 56 | goto done; |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 57 | if (sha1_object_info(oid.hash, &size) != OBJ_COMMIT) |
Christian Hesse | 3d33b46 | 2017-07-24 17:22:52 +0200 | [diff] [blame^] | 58 | goto done; |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 59 | read_tree_recursive(lookup_commit_reference(oid.hash)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
Christian Hesse | 3d33b46 | 2017-07-24 17:22:52 +0200 | [diff] [blame^] | 60 | |
| 61 | done: |
| 62 | free(path_items.match); |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 63 | return walk_tree_ctx.found_path; |
Jason A. Donenfeld | cd4c77d | 2013-05-25 16:32:37 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 66 | int cgit_print_file(char *path, const char *head, int file_only) |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 67 | { |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 68 | struct object_id oid; |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 69 | enum object_type type; |
| 70 | char *buf; |
| 71 | unsigned long size; |
| 72 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 73 | struct pathspec_item path_items = { |
| 74 | .match = path, |
| 75 | .len = strlen(path) |
| 76 | }; |
| 77 | struct pathspec paths = { |
| 78 | .nr = 1, |
| 79 | .items = &path_items |
| 80 | }; |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 81 | struct walk_tree_context walk_tree_ctx = { |
| 82 | .match_path = path, |
Christian Hesse | c330a2e | 2016-10-11 08:55:34 +0200 | [diff] [blame] | 83 | .matched_oid = &oid, |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 84 | .found_path = 0, |
| 85 | .file_only = file_only |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 88 | if (get_oid(head, &oid)) |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 89 | return -1; |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 90 | type = sha1_object_info(oid.hash, &size); |
John Keeping | 509488d | 2015-10-08 23:23:58 +0100 | [diff] [blame] | 91 | if (type == OBJ_COMMIT) { |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 92 | commit = lookup_commit_reference(oid.hash); |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 93 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
| 94 | if (!walk_tree_ctx.found_path) |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 95 | return -1; |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 96 | type = sha1_object_info(oid.hash, &size); |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 97 | } |
| 98 | if (type == OBJ_BAD) |
| 99 | return -1; |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 100 | buf = read_sha1_file(oid.hash, &type, &size); |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 101 | if (!buf) |
| 102 | return -1; |
| 103 | buf[size] = '\0'; |
Mark Lodato | d187b98 | 2010-09-04 14:18:16 -0400 | [diff] [blame] | 104 | html_raw(buf, size); |
Christian Hesse | 3080212 | 2015-10-10 16:56:24 +0200 | [diff] [blame] | 105 | free(buf); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 109 | void cgit_print_blob(const char *hex, char *path, const char *head, int file_only) |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 110 | { |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 111 | struct object_id oid; |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 112 | enum object_type type; |
Lars Hjemli | 596eb8f | 2009-07-25 11:51:19 +0200 | [diff] [blame] | 113 | char *buf; |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 114 | unsigned long size; |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 115 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 116 | struct pathspec_item path_items = { |
| 117 | .match = path, |
John Keeping | 1c32e00 | 2013-04-07 15:06:23 +0100 | [diff] [blame] | 118 | .len = path ? strlen(path) : 0 |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 119 | }; |
| 120 | struct pathspec paths = { |
| 121 | .nr = 1, |
| 122 | .items = &path_items |
| 123 | }; |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 124 | struct walk_tree_context walk_tree_ctx = { |
| 125 | .match_path = path, |
Christian Hesse | c330a2e | 2016-10-11 08:55:34 +0200 | [diff] [blame] | 126 | .matched_oid = &oid, |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 127 | .found_path = 0, |
| 128 | .file_only = file_only |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 129 | }; |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 130 | |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 131 | if (hex) { |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 132 | if (get_oid_hex(hex, &oid)) { |
John Keeping | 9a06211 | 2015-08-14 12:47:06 +0100 | [diff] [blame] | 133 | cgit_print_error_page(400, "Bad request", |
| 134 | "Bad hex value: %s", hex); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | } else { |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 138 | if (get_oid(head, &oid)) { |
John Keeping | 9a06211 | 2015-08-14 12:47:06 +0100 | [diff] [blame] | 139 | cgit_print_error_page(404, "Not found", |
| 140 | "Bad ref: %s", head); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 141 | return; |
| 142 | } |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 145 | type = sha1_object_info(oid.hash, &size); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 146 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 147 | if ((!hex) && type == OBJ_COMMIT && path) { |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 148 | commit = lookup_commit_reference(oid.hash); |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 149 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 150 | type = sha1_object_info(oid.hash, &size); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 153 | if (type == OBJ_BAD) { |
John Keeping | 9a06211 | 2015-08-14 12:47:06 +0100 | [diff] [blame] | 154 | cgit_print_error_page(404, "Not found", |
| 155 | "Bad object name: %s", hex); |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
Christian Hesse | 6e4b7b6 | 2016-09-29 21:38:49 +0200 | [diff] [blame] | 159 | buf = read_sha1_file(oid.hash, &type, &size); |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 160 | if (!buf) { |
John Keeping | 9a06211 | 2015-08-14 12:47:06 +0100 | [diff] [blame] | 161 | cgit_print_error_page(500, "Internal server error", |
| 162 | "Error reading object %s", hex); |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
| 166 | buf[size] = '\0'; |
Jason A. Donenfeld | 92996ac | 2016-01-14 14:31:53 +0100 | [diff] [blame] | 167 | if (buffer_is_binary(buf, size)) |
| 168 | ctx.page.mimetype = "application/octet-stream"; |
| 169 | else |
| 170 | ctx.page.mimetype = "text/plain"; |
Lars Hjemli | f3c1a18 | 2008-03-24 00:51:19 +0100 | [diff] [blame] | 171 | ctx.page.filename = path; |
Jason A. Donenfeld | 9ca2566 | 2016-01-14 14:43:43 +0100 | [diff] [blame] | 172 | |
| 173 | html("X-Content-Type-Options: nosniff\n"); |
| 174 | html("Content-Security-Policy: default-src 'none'\n"); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 175 | cgit_print_http_headers(); |
Mark Lodato | d187b98 | 2010-09-04 14:18:16 -0400 | [diff] [blame] | 176 | html_raw(buf, size); |
Christian Hesse | 7320bfa | 2015-10-10 16:56:23 +0200 | [diff] [blame] | 177 | free(buf); |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 178 | } |