blob: 5f30de753737290c1f3e3f4c6a4a861609d907a7 [file] [log] [blame]
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01001/* ui-blob.c: show blob content
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemlica8eb8f2007-05-09 00:48:09 +02009#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-blob.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020013
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010014struct walk_tree_context {
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020015 const char *match_path;
Christian Hessec330a2e2016-10-11 08:55:34 +020016 struct object_id *matched_oid;
John Keeping12d3d4c2015-03-08 16:32:17 +000017 unsigned int found_path:1;
18 unsigned int file_only:1;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010019};
Michael Krelin01d2dce2008-06-24 23:33:24 +020020
Christian Hesse7358f632015-02-07 14:18:28 +010021static int walk_tree(const unsigned char *sha1, struct strbuf *base,
22 const char *pathname, unsigned mode, int stage, void *cbdata)
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010023{
24 struct walk_tree_context *walk_tree_ctx = cbdata;
25
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020026 if (walk_tree_ctx->file_only && !S_ISREG(mode))
27 return READ_TREE_RECURSIVE;
Christian Hesse7358f632015-02-07 14:18:28 +010028 if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
29 || strcmp(walk_tree_ctx->match_path + base->len, pathname))
Michael Krelin01d2dce2008-06-24 23:33:24 +020030 return READ_TREE_RECURSIVE;
Christian Hessec330a2e2016-10-11 08:55:34 +020031 hashcpy(walk_tree_ctx->matched_oid->hash, sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010032 walk_tree_ctx->found_path = 1;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020033 return 0;
34}
35
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020036int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020037{
Christian Hesse6e4b7b62016-09-29 21:38:49 +020038 struct object_id oid;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020039 unsigned long size;
40 struct pathspec_item path_items = {
41 .match = path,
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 Hessec330a2e2016-10-11 08:55:34 +020050 .matched_oid = &oid,
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020051 .found_path = 0,
52 .file_only = file_only
53 };
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020054
Christian Hesse6e4b7b62016-09-29 21:38:49 +020055 if (get_oid(ref, &oid))
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020056 return 0;
Christian Hesse6e4b7b62016-09-29 21:38:49 +020057 if (sha1_object_info(oid.hash, &size) != OBJ_COMMIT)
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020058 return 0;
Christian Hesse6e4b7b62016-09-29 21:38:49 +020059 read_tree_recursive(lookup_commit_reference(oid.hash)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020060 return walk_tree_ctx.found_path;
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020061}
62
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020063int cgit_print_file(char *path, const char *head, int file_only)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020064{
Christian Hesse6e4b7b62016-09-29 21:38:49 +020065 struct object_id oid;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020066 enum object_type type;
67 char *buf;
68 unsigned long size;
69 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +000070 struct pathspec_item path_items = {
71 .match = path,
72 .len = strlen(path)
73 };
74 struct pathspec paths = {
75 .nr = 1,
76 .items = &path_items
77 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010078 struct walk_tree_context walk_tree_ctx = {
79 .match_path = path,
Christian Hessec330a2e2016-10-11 08:55:34 +020080 .matched_oid = &oid,
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020081 .found_path = 0,
82 .file_only = file_only
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010083 };
84
Christian Hesse6e4b7b62016-09-29 21:38:49 +020085 if (get_oid(head, &oid))
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020086 return -1;
Christian Hesse6e4b7b62016-09-29 21:38:49 +020087 type = sha1_object_info(oid.hash, &size);
John Keeping509488d2015-10-08 23:23:58 +010088 if (type == OBJ_COMMIT) {
Christian Hesse6e4b7b62016-09-29 21:38:49 +020089 commit = lookup_commit_reference(oid.hash);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010090 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
91 if (!walk_tree_ctx.found_path)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020092 return -1;
Christian Hesse6e4b7b62016-09-29 21:38:49 +020093 type = sha1_object_info(oid.hash, &size);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020094 }
95 if (type == OBJ_BAD)
96 return -1;
Christian Hesse6e4b7b62016-09-29 21:38:49 +020097 buf = read_sha1_file(oid.hash, &type, &size);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020098 if (!buf)
99 return -1;
100 buf[size] = '\0';
Mark Lodatod187b982010-09-04 14:18:16 -0400101 html_raw(buf, size);
Christian Hesse30802122015-10-10 16:56:24 +0200102 free(buf);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200103 return 0;
104}
105
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200106void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200107{
Christian Hesse6e4b7b62016-09-29 21:38:49 +0200108 struct object_id oid;
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200109 enum object_type type;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200110 char *buf;
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200111 unsigned long size;
Michael Krelin01d2dce2008-06-24 23:33:24 +0200112 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000113 struct pathspec_item path_items = {
114 .match = path,
John Keeping1c32e002013-04-07 15:06:23 +0100115 .len = path ? strlen(path) : 0
John Keepingc1633c62013-03-02 12:32:11 +0000116 };
117 struct pathspec paths = {
118 .nr = 1,
119 .items = &path_items
120 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100121 struct walk_tree_context walk_tree_ctx = {
122 .match_path = path,
Christian Hessec330a2e2016-10-11 08:55:34 +0200123 .matched_oid = &oid,
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200124 .found_path = 0,
125 .file_only = file_only
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100126 };
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200127
Michael Krelin01d2dce2008-06-24 23:33:24 +0200128 if (hex) {
Christian Hesse6e4b7b62016-09-29 21:38:49 +0200129 if (get_oid_hex(hex, &oid)) {
John Keeping9a062112015-08-14 12:47:06 +0100130 cgit_print_error_page(400, "Bad request",
131 "Bad hex value: %s", hex);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200132 return;
133 }
134 } else {
Christian Hesse6e4b7b62016-09-29 21:38:49 +0200135 if (get_oid(head, &oid)) {
John Keeping9a062112015-08-14 12:47:06 +0100136 cgit_print_error_page(404, "Not found",
137 "Bad ref: %s", head);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200138 return;
139 }
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200140 }
141
Christian Hesse6e4b7b62016-09-29 21:38:49 +0200142 type = sha1_object_info(oid.hash, &size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200143
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500144 if ((!hex) && type == OBJ_COMMIT && path) {
Christian Hesse6e4b7b62016-09-29 21:38:49 +0200145 commit = lookup_commit_reference(oid.hash);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100146 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Christian Hesse6e4b7b62016-09-29 21:38:49 +0200147 type = sha1_object_info(oid.hash, &size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200148 }
149
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200150 if (type == OBJ_BAD) {
John Keeping9a062112015-08-14 12:47:06 +0100151 cgit_print_error_page(404, "Not found",
152 "Bad object name: %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200153 return;
154 }
155
Christian Hesse6e4b7b62016-09-29 21:38:49 +0200156 buf = read_sha1_file(oid.hash, &type, &size);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200157 if (!buf) {
John Keeping9a062112015-08-14 12:47:06 +0100158 cgit_print_error_page(500, "Internal server error",
159 "Error reading object %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200160 return;
161 }
162
163 buf[size] = '\0';
Jason A. Donenfeld92996ac2016-01-14 14:31:53 +0100164 if (buffer_is_binary(buf, size))
165 ctx.page.mimetype = "application/octet-stream";
166 else
167 ctx.page.mimetype = "text/plain";
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100168 ctx.page.filename = path;
Jason A. Donenfeld9ca25662016-01-14 14:43:43 +0100169
170 html("X-Content-Type-Options: nosniff\n");
171 html("Content-Security-Policy: default-src 'none'\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100172 cgit_print_http_headers();
Mark Lodatod187b982010-09-04 14:18:16 -0400173 html_raw(buf, size);
Christian Hesse7320bfa2015-10-10 16:56:23 +0200174 free(buf);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200175}