config: make empty js= omit script tag
[cgit.git] / ui-blob.c
blobc10ae42ebd054ef84dfbe01f3c99005518a078e5
1 /* ui-blob.c: show blob content
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
9 #include "cgit.h"
10 #include "ui-blob.h"
11 #include "html.h"
12 #include "ui-shared.h"
14 struct walk_tree_context {
15 const char *match_path;
16 struct object_id *matched_oid;
17 unsigned int found_path:1;
18 unsigned int file_only:1;
21 static int walk_tree(const struct object_id *oid, struct strbuf *base,
22 const char *pathname, unsigned mode, void *cbdata)
24 struct walk_tree_context *walk_tree_ctx = cbdata;
26 if (walk_tree_ctx->file_only && !S_ISREG(mode))
27 return READ_TREE_RECURSIVE;
28 if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
29 || strcmp(walk_tree_ctx->match_path + base->len, pathname))
30 return READ_TREE_RECURSIVE;
31 oidcpy(walk_tree_ctx->matched_oid, oid);
32 walk_tree_ctx->found_path = 1;
33 return 0;
36 int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
38 struct object_id oid;
39 unsigned long size;
40 struct pathspec_item path_items = {
41 .match = xstrdup(path),
42 .len = strlen(path)
44 struct pathspec paths = {
45 .nr = 1,
46 .items = &path_items
48 struct walk_tree_context walk_tree_ctx = {
49 .match_path = path,
50 .matched_oid = &oid,
51 .found_path = 0,
52 .file_only = file_only
55 if (get_oid(ref, &oid))
56 goto done;
57 if (oid_object_info(the_repository, &oid, &size) != OBJ_COMMIT)
58 goto done;
59 read_tree(the_repository,
60 repo_get_commit_tree(the_repository, lookup_commit_reference(the_repository, &oid)),
61 &paths, walk_tree, &walk_tree_ctx);
63 done:
64 free(path_items.match);
65 return walk_tree_ctx.found_path;
68 int cgit_print_file(char *path, const char *head, int file_only)
70 struct object_id oid;
71 enum object_type type;
72 char *buf;
73 unsigned long size;
74 struct commit *commit;
75 struct pathspec_item path_items = {
76 .match = path,
77 .len = strlen(path)
79 struct pathspec paths = {
80 .nr = 1,
81 .items = &path_items
83 struct walk_tree_context walk_tree_ctx = {
84 .match_path = path,
85 .matched_oid = &oid,
86 .found_path = 0,
87 .file_only = file_only
90 if (get_oid(head, &oid))
91 return -1;
92 type = oid_object_info(the_repository, &oid, &size);
93 if (type == OBJ_COMMIT) {
94 commit = lookup_commit_reference(the_repository, &oid);
95 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
96 &paths, walk_tree, &walk_tree_ctx);
97 if (!walk_tree_ctx.found_path)
98 return -1;
99 type = oid_object_info(the_repository, &oid, &size);
101 if (type == OBJ_BAD)
102 return -1;
103 buf = read_object_file(&oid, &type, &size);
104 if (!buf)
105 return -1;
106 buf[size] = '\0';
107 html_raw(buf, size);
108 free(buf);
109 return 0;
112 void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
114 struct object_id oid;
115 enum object_type type;
116 char *buf;
117 unsigned long size;
118 struct commit *commit;
119 struct pathspec_item path_items = {
120 .match = path,
121 .len = path ? strlen(path) : 0
123 struct pathspec paths = {
124 .nr = 1,
125 .items = &path_items
127 struct walk_tree_context walk_tree_ctx = {
128 .match_path = path,
129 .matched_oid = &oid,
130 .found_path = 0,
131 .file_only = file_only
134 if (hex) {
135 if (get_oid_hex(hex, &oid)) {
136 cgit_print_error_page(400, "Bad request",
137 "Bad hex value: %s", hex);
138 return;
140 } else {
141 if (get_oid(head, &oid)) {
142 cgit_print_error_page(404, "Not found",
143 "Bad ref: %s", head);
144 return;
148 type = oid_object_info(the_repository, &oid, &size);
150 if ((!hex) && type == OBJ_COMMIT && path) {
151 commit = lookup_commit_reference(the_repository, &oid);
152 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
153 &paths, walk_tree, &walk_tree_ctx);
154 type = oid_object_info(the_repository, &oid, &size);
157 if (type == OBJ_BAD) {
158 cgit_print_error_page(404, "Not found",
159 "Bad object name: %s", hex);
160 return;
163 buf = read_object_file(&oid, &type, &size);
164 if (!buf) {
165 cgit_print_error_page(500, "Internal server error",
166 "Error reading object %s", hex);
167 return;
170 buf[size] = '\0';
171 if (buffer_is_binary(buf, size))
172 ctx.page.mimetype = "application/octet-stream";
173 else
174 ctx.page.mimetype = "text/plain";
175 ctx.page.filename = path;
177 html("X-Content-Type-Options: nosniff\n");
178 html("Content-Security-Policy: default-src 'none'\n");
179 cgit_print_http_headers();
180 html_raw(buf, size);
181 free(buf);