|
diff --git a/ui-blob.c b/ui-blob.c index 3cda03d..2ccd31d 100644 --- a/ ui-blob.c+++ b/ ui-blob.c |
|
@@ -1,73 +1,79 @@ |
1 | /* ui-blob.c: show blob content |
1 | /* ui-blob.c: show blob content |
2 | * |
2 | * |
3 | * Copyright (C) 2008 Lars Hjemli |
3 | * Copyright (C) 2008 Lars Hjemli |
4 | * |
4 | * |
5 | * Licensed under GNU General Public License v2 |
5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) |
6 | * (see COPYING for full license text) |
7 | */ |
7 | */ |
8 | |
8 | |
9 | #include "cgit.h" |
9 | #include "cgit.h" |
10 | #include "html.h" |
10 | #include "html.h" |
11 | #include "ui-shared.h" |
11 | #include "ui-shared.h" |
12 | |
12 | |
13 | static char *match_path; |
13 | static char *match_path; |
14 | static unsigned char *matched_sha1; |
14 | static unsigned char *matched_sha1; |
15 | |
15 | |
16 | static int walk_tree(const unsigned char *sha1, const char *base,int baselen, |
16 | static int walk_tree(const unsigned char *sha1, const char *base,int baselen, |
17 | const char *pathname, unsigned mode, int stage, void *cbdata) { |
17 | const char *pathname, unsigned mode, int stage, void *cbdata) { |
18 | if(strncmp(base,match_path,baselen) |
18 | if(strncmp(base,match_path,baselen) |
19 | || strcmp(match_path+baselen,pathname) ) |
19 | || strcmp(match_path+baselen,pathname) ) |
20 | return READ_TREE_RECURSIVE; |
20 | return READ_TREE_RECURSIVE; |
21 | memmove(matched_sha1,sha1,20); |
21 | memmove(matched_sha1,sha1,20); |
22 | return 0; |
22 | return 0; |
23 | } |
23 | } |
24 | |
24 | |
25 | void cgit_print_blob(const char *hex, char *path, const char *head) |
25 | void cgit_print_blob(const char *hex, char *path, const char *head) |
26 | { |
26 | { |
27 | |
27 | |
28 | unsigned char sha1[20]; |
28 | unsigned char sha1[20]; |
29 | enum object_type type; |
29 | enum object_type type; |
30 | unsigned char *buf; |
30 | char *buf; |
31 | unsigned long size; |
31 | unsigned long size; |
32 | struct commit *commit; |
32 | struct commit *commit; |
33 | const char *paths[] = {path, NULL}; |
33 | const char *paths[] = {path, NULL}; |
34 | |
34 | |
35 | if (hex) { |
35 | if (hex) { |
36 | if (get_sha1_hex(hex, sha1)){ |
36 | if (get_sha1_hex(hex, sha1)){ |
37 | cgit_print_error(fmt("Bad hex value: %s", hex)); |
37 | cgit_print_error(fmt("Bad hex value: %s", hex)); |
38 | return; |
38 | return; |
39 | } |
39 | } |
40 | } else { |
40 | } else { |
41 | if (get_sha1(head,sha1)) { |
41 | if (get_sha1(head,sha1)) { |
42 | cgit_print_error(fmt("Bad ref: %s", head)); |
42 | cgit_print_error(fmt("Bad ref: %s", head)); |
43 | return; |
43 | return; |
44 | } |
44 | } |
45 | } |
45 | } |
46 | |
46 | |
47 | type = sha1_object_info(sha1, &size); |
47 | type = sha1_object_info(sha1, &size); |
48 | |
48 | |
49 | if((!hex) && type == OBJ_COMMIT && path) { |
49 | if((!hex) && type == OBJ_COMMIT && path) { |
50 | commit = lookup_commit_reference(sha1); |
50 | commit = lookup_commit_reference(sha1); |
51 | match_path = path; |
51 | match_path = path; |
52 | matched_sha1 = sha1; |
52 | matched_sha1 = sha1; |
53 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); |
53 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); |
54 | type = sha1_object_info(sha1,&size); |
54 | type = sha1_object_info(sha1,&size); |
55 | } |
55 | } |
56 | |
56 | |
57 | if (type == OBJ_BAD) { |
57 | if (type == OBJ_BAD) { |
58 | cgit_print_error(fmt("Bad object name: %s", hex)); |
58 | cgit_print_error(fmt("Bad object name: %s", hex)); |
59 | return; |
59 | return; |
60 | } |
60 | } |
61 | |
61 | |
62 | buf = read_sha1_file(sha1, &type, &size); |
62 | buf = read_sha1_file(sha1, &type, &size); |
63 | if (!buf) { |
63 | if (!buf) { |
64 | cgit_print_error(fmt("Error reading object %s", hex)); |
64 | cgit_print_error(fmt("Error reading object %s", hex)); |
65 | return; |
65 | return; |
66 | } |
66 | } |
67 | |
67 | |
68 | buf[size] = '\0'; |
68 | buf[size] = '\0'; |
69 | ctx.page.mimetype = ctx.qry.mimetype; |
69 | ctx.page.mimetype = ctx.qry.mimetype; |
| |
70 | if (!ctx.page.mimetype) { |
| |
71 | if (buffer_is_binary(buf, size)) |
| |
72 | ctx.page.mimetype = "application/octet-stream"; |
| |
73 | else |
| |
74 | ctx.page.mimetype = "text/plain"; |
| |
75 | } |
70 | ctx.page.filename = path; |
76 | ctx.page.filename = path; |
71 | cgit_print_http_headers(&ctx); |
77 | cgit_print_http_headers(&ctx); |
72 | write(htmlfd, buf, size); |
78 | write(htmlfd, buf, size); |
73 | } |
79 | } |
|
|
|
|
@@ -1,79 +1,82 @@ |
1 | /* ui-plain.c: functions for output of plain blobs by path |
1 | /* ui-plain.c: functions for output of plain blobs by path |
2 | * |
2 | * |
3 | * Copyright (C) 2008 Lars Hjemli |
3 | * Copyright (C) 2008 Lars Hjemli |
4 | * |
4 | * |
5 | * Licensed under GNU General Public License v2 |
5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) |
6 | * (see COPYING for full license text) |
7 | */ |
7 | */ |
8 | |
8 | |
9 | #include "cgit.h" |
9 | #include "cgit.h" |
10 | #include "html.h" |
10 | #include "html.h" |
11 | #include "ui-shared.h" |
11 | #include "ui-shared.h" |
12 | |
12 | |
13 | char *curr_rev; |
13 | char *curr_rev; |
14 | char *match_path; |
14 | char *match_path; |
15 | int match; |
15 | int match; |
16 | |
16 | |
17 | static void print_object(const unsigned char *sha1, const char *path) |
17 | static void print_object(const unsigned char *sha1, const char *path) |
18 | { |
18 | { |
19 | enum object_type type; |
19 | enum object_type type; |
20 | char *buf; |
20 | char *buf; |
21 | unsigned long size; |
21 | unsigned long size; |
22 | |
22 | |
23 | type = sha1_object_info(sha1, &size); |
23 | type = sha1_object_info(sha1, &size); |
24 | if (type == OBJ_BAD) { |
24 | if (type == OBJ_BAD) { |
25 | html_status(404, "Not found", 0); |
25 | html_status(404, "Not found", 0); |
26 | return; |
26 | return; |
27 | } |
27 | } |
28 | |
28 | |
29 | buf = read_sha1_file(sha1, &type, &size); |
29 | buf = read_sha1_file(sha1, &type, &size); |
30 | if (!buf) { |
30 | if (!buf) { |
31 | html_status(404, "Not found", 0); |
31 | html_status(404, "Not found", 0); |
32 | return; |
32 | return; |
33 | } |
33 | } |
34 | ctx.page.mimetype = "text/plain"; |
34 | if (buffer_is_binary(buf, size)) |
| |
35 | ctx.page.mimetype = "application/octet-stream"; |
| |
36 | else |
| |
37 | ctx.page.mimetype = "text/plain"; |
35 | ctx.page.filename = fmt("%s", path); |
38 | ctx.page.filename = fmt("%s", path); |
36 | ctx.page.size = size; |
39 | ctx.page.size = size; |
37 | cgit_print_http_headers(&ctx); |
40 | cgit_print_http_headers(&ctx); |
38 | html_raw(buf, size); |
41 | html_raw(buf, size); |
39 | match = 1; |
42 | match = 1; |
40 | } |
43 | } |
41 | |
44 | |
42 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
45 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
43 | const char *pathname, unsigned mode, int stage, |
46 | const char *pathname, unsigned mode, int stage, |
44 | void *cbdata) |
47 | void *cbdata) |
45 | { |
48 | { |
46 | if (S_ISDIR(mode)) |
49 | if (S_ISDIR(mode)) |
47 | return READ_TREE_RECURSIVE; |
50 | return READ_TREE_RECURSIVE; |
48 | |
51 | |
49 | if (S_ISREG(mode)) |
52 | if (S_ISREG(mode)) |
50 | print_object(sha1, pathname); |
53 | print_object(sha1, pathname); |
51 | |
54 | |
52 | return 0; |
55 | return 0; |
53 | } |
56 | } |
54 | |
57 | |
55 | void cgit_print_plain(struct cgit_context *ctx) |
58 | void cgit_print_plain(struct cgit_context *ctx) |
56 | { |
59 | { |
57 | const char *rev = ctx->qry.sha1; |
60 | const char *rev = ctx->qry.sha1; |
58 | unsigned char sha1[20]; |
61 | unsigned char sha1[20]; |
59 | struct commit *commit; |
62 | struct commit *commit; |
60 | const char *paths[] = {ctx->qry.path, NULL}; |
63 | const char *paths[] = {ctx->qry.path, NULL}; |
61 | |
64 | |
62 | if (!rev) |
65 | if (!rev) |
63 | rev = ctx->qry.head; |
66 | rev = ctx->qry.head; |
64 | |
67 | |
65 | curr_rev = xstrdup(rev); |
68 | curr_rev = xstrdup(rev); |
66 | if (get_sha1(rev, sha1)) { |
69 | if (get_sha1(rev, sha1)) { |
67 | html_status(404, "Not found", 0); |
70 | html_status(404, "Not found", 0); |
68 | return; |
71 | return; |
69 | } |
72 | } |
70 | commit = lookup_commit_reference(sha1); |
73 | commit = lookup_commit_reference(sha1); |
71 | if (!commit || parse_commit(commit)) { |
74 | if (!commit || parse_commit(commit)) { |
72 | html_status(404, "Not found", 0); |
75 | html_status(404, "Not found", 0); |
73 | return; |
76 | return; |
74 | } |
77 | } |
75 | match_path = ctx->qry.path; |
78 | match_path = ctx->qry.path; |
76 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); |
79 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); |
77 | if (!match) |
80 | if (!match) |
78 | html_status(404, "Not found", 0); |
81 | html_status(404, "Not found", 0); |
79 | } |
82 | } |
|