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