|
diff --git a/cgit.c b/cgit.c index 5301840..5816f3d 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -16,8 +16,31 @@ |
16 | #include "scan-tree.h" |
16 | #include "scan-tree.h" |
17 | |
17 | |
18 | const char *cgit_version = CGIT_VERSION; |
18 | const char *cgit_version = CGIT_VERSION; |
19 | |
19 | |
| |
20 | void add_mimetype(const char *name, const char *value) |
| |
21 | { |
| |
22 | struct string_list_item *item; |
| |
23 | |
| |
24 | item = string_list_insert(xstrdup(name), &ctx.cfg.mimetypes); |
| |
25 | item->util = xstrdup(value); |
| |
26 | } |
| |
27 | |
| |
28 | struct cgit_filter *new_filter(const char *cmd, int extra_args) |
| |
29 | { |
| |
30 | struct cgit_filter *f; |
| |
31 | |
| |
32 | if (!cmd || !cmd[0]) |
| |
33 | return NULL; |
| |
34 | |
| |
35 | f = xmalloc(sizeof(struct cgit_filter)); |
| |
36 | f->cmd = xstrdup(cmd); |
| |
37 | f->argv = xmalloc((2 + extra_args) * sizeof(char *)); |
| |
38 | f->argv[0] = f->cmd; |
| |
39 | f->argv[1] = NULL; |
| |
40 | return f; |
| |
41 | } |
| |
42 | |
20 | void config_cb(const char *name, const char *value) |
43 | void config_cb(const char *name, const char *value) |
21 | { |
44 | { |
22 | if (!strcmp(name, "root-title")) |
45 | if (!strcmp(name, "root-title")) |
23 | ctx.cfg.root_title = xstrdup(value); |
46 | ctx.cfg.root_title = xstrdup(value); |
@@ -30,8 +53,10 @@ void config_cb(const char *name, const char *value) |
30 | else if (!strcmp(name, "favicon")) |
53 | else if (!strcmp(name, "favicon")) |
31 | ctx.cfg.favicon = xstrdup(value); |
54 | ctx.cfg.favicon = xstrdup(value); |
32 | else if (!strcmp(name, "footer")) |
55 | else if (!strcmp(name, "footer")) |
33 | ctx.cfg.footer = xstrdup(value); |
56 | ctx.cfg.footer = xstrdup(value); |
| |
57 | else if (!strcmp(name, "head-include")) |
| |
58 | ctx.cfg.head_include = xstrdup(value); |
34 | else if (!strcmp(name, "header")) |
59 | else if (!strcmp(name, "header")) |
35 | ctx.cfg.header = xstrdup(value); |
60 | ctx.cfg.header = xstrdup(value); |
36 | else if (!strcmp(name, "logo")) |
61 | else if (!strcmp(name, "logo")) |
37 | ctx.cfg.logo = xstrdup(value); |
62 | ctx.cfg.logo = xstrdup(value); |
@@ -48,8 +73,12 @@ void config_cb(const char *name, const char *value) |
48 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
73 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
49 | ctx.cfg.virtual_root = ""; |
74 | ctx.cfg.virtual_root = ""; |
50 | } else if (!strcmp(name, "nocache")) |
75 | } else if (!strcmp(name, "nocache")) |
51 | ctx.cfg.nocache = atoi(value); |
76 | ctx.cfg.nocache = atoi(value); |
| |
77 | else if (!strcmp(name, "noplainemail")) |
| |
78 | ctx.cfg.noplainemail = atoi(value); |
| |
79 | else if (!strcmp(name, "noheader")) |
| |
80 | ctx.cfg.noheader = atoi(value); |
52 | else if (!strcmp(name, "snapshots")) |
81 | else if (!strcmp(name, "snapshots")) |
53 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
82 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
54 | else if (!strcmp(name, "enable-index-links")) |
83 | else if (!strcmp(name, "enable-index-links")) |
55 | ctx.cfg.enable_index_links = atoi(value); |
84 | ctx.cfg.enable_index_links = atoi(value); |
@@ -70,16 +99,24 @@ void config_cb(const char *name, const char *value) |
70 | else if (!strcmp(name, "cache-static-ttl")) |
99 | else if (!strcmp(name, "cache-static-ttl")) |
71 | ctx.cfg.cache_static_ttl = atoi(value); |
100 | ctx.cfg.cache_static_ttl = atoi(value); |
72 | else if (!strcmp(name, "cache-dynamic-ttl")) |
101 | else if (!strcmp(name, "cache-dynamic-ttl")) |
73 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
102 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
| |
103 | else if (!strcmp(name, "about-filter")) |
| |
104 | ctx.cfg.about_filter = new_filter(value, 0); |
| |
105 | else if (!strcmp(name, "commit-filter")) |
| |
106 | ctx.cfg.commit_filter = new_filter(value, 0); |
| |
107 | else if (!strcmp(name, "embedded")) |
| |
108 | ctx.cfg.embedded = atoi(value); |
74 | else if (!strcmp(name, "max-message-length")) |
109 | else if (!strcmp(name, "max-message-length")) |
75 | ctx.cfg.max_msg_len = atoi(value); |
110 | ctx.cfg.max_msg_len = atoi(value); |
76 | else if (!strcmp(name, "max-repodesc-length")) |
111 | else if (!strcmp(name, "max-repodesc-length")) |
77 | ctx.cfg.max_repodesc_len = atoi(value); |
112 | ctx.cfg.max_repodesc_len = atoi(value); |
78 | else if (!strcmp(name, "max-repo-count")) |
113 | else if (!strcmp(name, "max-repo-count")) |
79 | ctx.cfg.max_repo_count = atoi(value); |
114 | ctx.cfg.max_repo_count = atoi(value); |
80 | else if (!strcmp(name, "max-commit-count")) |
115 | else if (!strcmp(name, "max-commit-count")) |
81 | ctx.cfg.max_commit_count = atoi(value); |
116 | ctx.cfg.max_commit_count = atoi(value); |
| |
117 | else if (!strcmp(name, "source-filter")) |
| |
118 | ctx.cfg.source_filter = new_filter(value, 1); |
82 | else if (!strcmp(name, "summary-log")) |
119 | else if (!strcmp(name, "summary-log")) |
83 | ctx.cfg.summary_log = atoi(value); |
120 | ctx.cfg.summary_log = atoi(value); |
84 | else if (!strcmp(name, "summary-branches")) |
121 | else if (!strcmp(name, "summary-branches")) |
85 | ctx.cfg.summary_branches = atoi(value); |
122 | ctx.cfg.summary_branches = atoi(value); |
@@ -94,8 +131,10 @@ void config_cb(const char *name, const char *value) |
94 | else if (!strcmp(name, "clone-prefix")) |
131 | else if (!strcmp(name, "clone-prefix")) |
95 | ctx.cfg.clone_prefix = xstrdup(value); |
132 | ctx.cfg.clone_prefix = xstrdup(value); |
96 | else if (!strcmp(name, "local-time")) |
133 | else if (!strcmp(name, "local-time")) |
97 | ctx.cfg.local_time = atoi(value); |
134 | ctx.cfg.local_time = atoi(value); |
| |
135 | else if (!prefixcmp(name, "mimetype.")) |
| |
136 | add_mimetype(name + 9, value); |
98 | else if (!strcmp(name, "repo.group")) |
137 | else if (!strcmp(name, "repo.group")) |
99 | ctx.cfg.repo_group = xstrdup(value); |
138 | ctx.cfg.repo_group = xstrdup(value); |
100 | else if (!strcmp(name, "repo.url")) |
139 | else if (!strcmp(name, "repo.url")) |
101 | ctx.repo = cgit_add_repo(value); |
140 | ctx.repo = cgit_add_repo(value); |
@@ -120,8 +159,14 @@ void config_cb(const char *name, const char *value) |
120 | else if (ctx.repo && !strcmp(name, "repo.max-stats")) |
159 | else if (ctx.repo && !strcmp(name, "repo.max-stats")) |
121 | ctx.repo->max_stats = cgit_find_stats_period(value, NULL); |
160 | ctx.repo->max_stats = cgit_find_stats_period(value, NULL); |
122 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
161 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
123 | ctx.repo->module_link= xstrdup(value); |
162 | ctx.repo->module_link= xstrdup(value); |
| |
163 | else if (ctx.repo && !strcmp(name, "repo.about-filter")) |
| |
164 | ctx.repo->about_filter = new_filter(value, 0); |
| |
165 | else if (ctx.repo && !strcmp(name, "repo.commit-filter")) |
| |
166 | ctx.repo->commit_filter = new_filter(value, 0); |
| |
167 | else if (ctx.repo && !strcmp(name, "repo.source-filter")) |
| |
168 | ctx.repo->source_filter = new_filter(value, 1); |
124 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
169 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
125 | if (*value == '/') |
170 | if (*value == '/') |
126 | ctx.repo->readme = xstrdup(value); |
171 | ctx.repo->readme = xstrdup(value); |
127 | else |
172 | else |
@@ -172,8 +217,13 @@ static void querystring_cb(const char *name, const char *value) |
172 | ctx.qry.period = xstrdup(value); |
217 | ctx.qry.period = xstrdup(value); |
173 | } |
218 | } |
174 | } |
219 | } |
175 | |
220 | |
| |
221 | char *xstrdupn(const char *str) |
| |
222 | { |
| |
223 | return (str ? xstrdup(str) : NULL); |
| |
224 | } |
| |
225 | |
176 | static void prepare_context(struct cgit_context *ctx) |
226 | static void prepare_context(struct cgit_context *ctx) |
177 | { |
227 | { |
178 | memset(ctx, 0, sizeof(ctx)); |
228 | memset(ctx, 0, sizeof(ctx)); |
179 | ctx->cfg.agefile = "info/web/last-modified"; |
229 | ctx->cfg.agefile = "info/web/last-modified"; |
@@ -185,9 +235,9 @@ static void prepare_context(struct cgit_context *ctx) |
185 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
235 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
186 | ctx->cfg.cache_root_ttl = 5; |
236 | ctx->cfg.cache_root_ttl = 5; |
187 | ctx->cfg.cache_static_ttl = -1; |
237 | ctx->cfg.cache_static_ttl = -1; |
188 | ctx->cfg.css = "/cgit.css"; |
238 | ctx->cfg.css = "/cgit.css"; |
189 | ctx->cfg.logo = "/git-logo.png"; |
239 | ctx->cfg.logo = "/cgit.png"; |
190 | ctx->cfg.local_time = 0; |
240 | ctx->cfg.local_time = 0; |
191 | ctx->cfg.max_repo_count = 50; |
241 | ctx->cfg.max_repo_count = 50; |
192 | ctx->cfg.max_commit_count = 50; |
242 | ctx->cfg.max_commit_count = 50; |
193 | ctx->cfg.max_lock_attempts = 5; |
243 | ctx->cfg.max_lock_attempts = 5; |
@@ -202,14 +252,32 @@ static void prepare_context(struct cgit_context *ctx) |
202 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
252 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
203 | ctx->cfg.summary_branches = 10; |
253 | ctx->cfg.summary_branches = 10; |
204 | ctx->cfg.summary_log = 10; |
254 | ctx->cfg.summary_log = 10; |
205 | ctx->cfg.summary_tags = 10; |
255 | ctx->cfg.summary_tags = 10; |
| |
256 | ctx->env.cgit_config = xstrdupn(getenv("CGIT_CONFIG")); |
| |
257 | ctx->env.http_host = xstrdupn(getenv("HTTP_HOST")); |
| |
258 | ctx->env.https = xstrdupn(getenv("HTTPS")); |
| |
259 | ctx->env.no_http = xstrdupn(getenv("NO_HTTP")); |
| |
260 | ctx->env.path_info = xstrdupn(getenv("PATH_INFO")); |
| |
261 | ctx->env.query_string = xstrdupn(getenv("QUERY_STRING")); |
| |
262 | ctx->env.request_method = xstrdupn(getenv("REQUEST_METHOD")); |
| |
263 | ctx->env.script_name = xstrdupn(getenv("SCRIPT_NAME")); |
| |
264 | ctx->env.server_name = xstrdupn(getenv("SERVER_NAME")); |
| |
265 | ctx->env.server_port = xstrdupn(getenv("SERVER_PORT")); |
206 | ctx->page.mimetype = "text/html"; |
266 | ctx->page.mimetype = "text/html"; |
207 | ctx->page.charset = PAGE_ENCODING; |
267 | ctx->page.charset = PAGE_ENCODING; |
208 | ctx->page.filename = NULL; |
268 | ctx->page.filename = NULL; |
209 | ctx->page.size = 0; |
269 | ctx->page.size = 0; |
210 | ctx->page.modified = time(NULL); |
270 | ctx->page.modified = time(NULL); |
211 | ctx->page.expires = ctx->page.modified; |
271 | ctx->page.expires = ctx->page.modified; |
| |
272 | ctx->page.etag = NULL; |
| |
273 | memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list)); |
| |
274 | if (ctx->env.script_name) |
| |
275 | ctx->cfg.script_name = ctx->env.script_name; |
| |
276 | if (ctx->env.query_string) |
| |
277 | ctx->qry.raw = ctx->env.query_string; |
| |
278 | if (!ctx->env.cgit_config) |
| |
279 | ctx->env.cgit_config = CGIT_CONFIG; |
212 | } |
280 | } |
213 | |
281 | |
214 | struct refmatch { |
282 | struct refmatch { |
215 | char *req_ref; |
283 | char *req_ref; |
@@ -287,8 +355,10 @@ static int prepare_repo_cmd(struct cgit_context *ctx) |
287 | |
355 | |
288 | if (get_sha1(ctx->qry.head, sha1)) { |
356 | if (get_sha1(ctx->qry.head, sha1)) { |
289 | tmp = xstrdup(ctx->qry.head); |
357 | tmp = xstrdup(ctx->qry.head); |
290 | ctx->qry.head = ctx->repo->defbranch; |
358 | ctx->qry.head = ctx->repo->defbranch; |
| |
359 | ctx->page.status = 404; |
| |
360 | ctx->page.statusmsg = "not found"; |
291 | cgit_print_http_headers(ctx); |
361 | cgit_print_http_headers(ctx); |
292 | cgit_print_docstart(ctx); |
362 | cgit_print_docstart(ctx); |
293 | cgit_print_pageheader(ctx); |
363 | cgit_print_pageheader(ctx); |
294 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
364 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
@@ -378,8 +448,11 @@ static void cgit_parse_args(int argc, const char **argv) |
378 | } |
448 | } |
379 | if (!strcmp(argv[i], "--nocache")) { |
449 | if (!strcmp(argv[i], "--nocache")) { |
380 | ctx.cfg.nocache = 1; |
450 | ctx.cfg.nocache = 1; |
381 | } |
451 | } |
| |
452 | if (!strcmp(argv[i], "--nohttp")) { |
| |
453 | ctx.env.no_http = "1"; |
| |
454 | } |
382 | if (!strncmp(argv[i], "--query=", 8)) { |
455 | if (!strncmp(argv[i], "--query=", 8)) { |
383 | ctx.qry.raw = xstrdup(argv[i]+8); |
456 | ctx.qry.raw = xstrdup(argv[i]+8); |
384 | } |
457 | } |
385 | if (!strncmp(argv[i], "--repo=", 7)) { |
458 | if (!strncmp(argv[i], "--repo=", 7)) { |
@@ -430,9 +503,8 @@ static int calc_ttl() |
430 | } |
503 | } |
431 | |
504 | |
432 | int main(int argc, const char **argv) |
505 | int main(int argc, const char **argv) |
433 | { |
506 | { |
434 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
| |
435 | const char *path; |
507 | const char *path; |
436 | char *qry; |
508 | char *qry; |
437 | int err, ttl; |
509 | int err, ttl; |
438 | |
510 | |
@@ -440,15 +512,10 @@ int main(int argc, const char **argv) |
440 | cgit_repolist.length = 0; |
512 | cgit_repolist.length = 0; |
441 | cgit_repolist.count = 0; |
513 | cgit_repolist.count = 0; |
442 | cgit_repolist.repos = NULL; |
514 | cgit_repolist.repos = NULL; |
443 | |
515 | |
444 | if (getenv("SCRIPT_NAME")) |
| |
445 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
| |
446 | if (getenv("QUERY_STRING")) |
| |
447 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
| |
448 | cgit_parse_args(argc, argv); |
516 | cgit_parse_args(argc, argv); |
449 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
517 | parse_configfile(ctx.env.cgit_config, config_cb); |
450 | config_cb); |
| |
451 | ctx.repo = NULL; |
518 | ctx.repo = NULL; |
452 | http_parse_querystring(ctx.qry.raw, querystring_cb); |
519 | http_parse_querystring(ctx.qry.raw, querystring_cb); |
453 | |
520 | |
454 | /* If virtual-root isn't specified in cgitrc, lets pretend |
521 | /* If virtual-root isn't specified in cgitrc, lets pretend |
@@ -461,9 +528,9 @@ int main(int argc, const char **argv) |
461 | * use PATH_INFO as url. This allows cgit to work with virtual |
528 | * use PATH_INFO as url. This allows cgit to work with virtual |
462 | * urls without the need for rewriterules in the webserver (as |
529 | * urls without the need for rewriterules in the webserver (as |
463 | * long as PATH_INFO is included in the cache lookup key). |
530 | * long as PATH_INFO is included in the cache lookup key). |
464 | */ |
531 | */ |
465 | path = getenv("PATH_INFO"); |
532 | path = ctx.env.path_info; |
466 | if (!ctx.qry.url && path) { |
533 | if (!ctx.qry.url && path) { |
467 | if (path[0] == '/') |
534 | if (path[0] == '/') |
468 | path++; |
535 | path++; |
469 | ctx.qry.url = xstrdup(path); |
536 | ctx.qry.url = xstrdup(path); |
@@ -477,8 +544,10 @@ int main(int argc, const char **argv) |
477 | } |
544 | } |
478 | |
545 | |
479 | ttl = calc_ttl(); |
546 | ttl = calc_ttl(); |
480 | ctx.page.expires += ttl*60; |
547 | ctx.page.expires += ttl*60; |
| |
548 | if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")) |
| |
549 | ctx.cfg.nocache = 1; |
481 | if (ctx.cfg.nocache) |
550 | if (ctx.cfg.nocache) |
482 | ctx.cfg.cache_size = 0; |
551 | ctx.cfg.cache_size = 0; |
483 | err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, |
552 | err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, |
484 | ctx.qry.raw, ttl, process_request, &ctx); |
553 | ctx.qry.raw, ttl, process_request, &ctx); |
|