blob: 12981c286eec20cd505ef772ad83eda40ea54051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
/*
 * The entry point every request passes through. It reads cgitrc and the query
 * string into the global ctx, works out how long the answer may be cached,
 * and then hands over to the cache, which either serves a copy it already has
 * or calls back here to render one. Rendering means authenticating the
 * request, resolving the repository and the branch it names, and dispatching
 * to the page handler cmd.c maps the page name to. Run from a shell rather
 * than from a web server the same binary instead prints what it was built
 * with, or scans a directory tree and writes out the repositories it found in
 * cgitrc form.
 */

#define USE_THE_REPOSITORY_VARIABLE

#include "cache.h"
#include "cgit.h"
#include "cmd.h"
#include "config.h"
#include "filter.h"
#include "html.h"
#include "parsing.h"
#include "scan-tree.h"
#include "shared.h"
#include "ui-blob.h"
#include "ui-diff.h"
#include "ui-empty.h"
#include "ui-shared.h"
#include "ui-snapshot.h"
#include "ui-stats.h"

// Deliberately small. The whole body is read at once and handed to the auth
// filter, so a filter never has to defend itself against a huge or a
// dribbled-out POST.
#define MAX_AUTHENTICATION_POST_BYTES 4096

// The filter bar matches the search against every item a page lists, so this
// bounds both the work one request can ask for and the cache key the query
// lands in.
#define MAX_SEARCH_LEN 512

// Furthest into a listing a request may ask to start. A walk has to step over
// every row it skips, so this bounds the work an offset alone can buy.
#define MAX_QUERY_OFFSET 100000

// cgit knows fewer than eight archive formats, so this stands in for every bit
// a snapshots mask can carry.
#define ALL_SNAPSHOT_FORMATS 0xFF

/*
 * The first branch found is the fallback, so a repository whose default branch
 * does not exist still has something to show.
 */
struct refmatch {
	char *wanted;
	char *first;
	int found;
};

const char *cgit_version = CGIT_VERSION;

/*
 * Isolate git from the calling user's configuration, so a snapshot cannot be
 * broken by something like a core.excludesfile pointing at a "~" path that git
 * can no longer expand once HOME is unset below. Called from cmd_main rather
 * than from a constructor attribute, because git-compat-util.h defines
 * __attribute__ away on a compiler that does not support it, which would leave
 * this silently never running.
 */
static void isolate_git_environment(void)
{
	setenv("GIT_CONFIG_NOSYSTEM", "1", 1);
	setenv("GIT_CONFIG_GLOBAL", "/dev/null", 1);
	setenv("GIT_ATTR_NOSYSTEM", "1", 1);
	unsetenv("HOME");
	unsetenv("XDG_CONFIG_HOME");
}

/*
 * Turn a die from anywhere inside git into a rendered page, since a CGI that
 * produced no output leaves the visitor with whatever the web server makes of
 * it.
 */
static NORETURN void die_routine(const char *msg, va_list params)
{
	// The error page abandons any cache fill in progress, so the message
	// reaches the visitor rather than the cache file stdout points at.
	cgit_vprint_error_page(400, "Bad request", msg, params);
	exit(0);
}

static void prepare_context(void)
{
	memset(&ctx, 0, sizeof(ctx));
	ctx.cfg.agefile = "info/web/last-modified";
	ctx.cfg.cache_size = 0;
	ctx.cfg.cache_root = CGIT_CACHE_ROOT;
	ctx.cfg.cache_about_ttl = 15;
	ctx.cfg.cache_snapshot_ttl = 5;
	ctx.cfg.cache_repo_ttl = 5;
	ctx.cfg.cache_root_ttl = 5;
	ctx.cfg.cache_scanrc_ttl = 15;
	ctx.cfg.cache_dynamic_ttl = 5;
	ctx.cfg.cache_static_ttl = -1;
	ctx.cfg.case_sensitive_sort = 1;
	ctx.cfg.branch_sort = 0;
	ctx.cfg.commit_sort = 0;
	ctx.cfg.logo = "/cgit.png";
	ctx.cfg.favicon = "/favicon.ico";
	ctx.cfg.local_time = 0;
	ctx.cfg.date_mode = date_mode_from_type(DATE_SHORT);
	ctx.cfg.enable_relative_dates = 1;
	ctx.cfg.enable_http_clone = 1;
	ctx.cfg.enable_index_owner = 1;
	ctx.cfg.enable_tree_linenumbers = 1;
	ctx.cfg.enable_git_config = 0;
	ctx.cfg.max_repo_count = 50;
	ctx.cfg.max_commit_count = 50;
	ctx.cfg.max_patch_count = 50;
	ctx.cfg.max_diff_files = 200;
	ctx.cfg.max_diff_lines = 1000;
	ctx.cfg.max_msg_len = 80;
	ctx.cfg.max_ref_count = 200;
	ctx.cfg.max_repodesc_len = 80;
	// Counted in kilobytes, so ten megabytes, which bounds the memory one
	// request can be made to allocate for a blob.
	ctx.cfg.max_blob_size = 10 * 1024;
	ctx.cfg.max_stats = 0;
	ctx.cfg.project_list = NULL;
	ctx.cfg.renamelimit = -1;
	ctx.cfg.remove_suffix = 0;
	ctx.cfg.robots = "index, nofollow";
	ctx.cfg.root_title = "Git repository browser";
	ctx.cfg.root_desc = "a fast webinterface for the git dscm";
	ctx.cfg.scan_hidden_path = 0;
	ctx.cfg.script_name = CGIT_SCRIPT_NAME;
	ctx.cfg.section = "";
	ctx.cfg.repository_sort = "name";
	ctx.cfg.section_sort = 0;
	ctx.cfg.summary_branches = 10;
	ctx.cfg.summary_log = 10;
	ctx.cfg.summary_tags = 10;
	ctx.cfg.max_atom_items = 10;
	ctx.cfg.difftype = DIFF_UNIFIED;
	ctx.env.cgit_config = getenv("CGIT_CONFIG");
	ctx.env.http_host = getenv("HTTP_HOST");
	ctx.env.https = getenv("HTTPS");
	ctx.env.no_http = getenv("NO_HTTP");
	ctx.env.path_info = getenv("PATH_INFO");
	ctx.env.query_string = getenv("QUERY_STRING");
	ctx.env.request_method = getenv("REQUEST_METHOD");
	ctx.env.script_name = getenv("SCRIPT_NAME");
	ctx.env.server_name = getenv("SERVER_NAME");
	ctx.env.server_port = getenv("SERVER_PORT");
	ctx.env.http_cookie = getenv("HTTP_COOKIE");
	ctx.env.http_referer = getenv("HTTP_REFERER");
	ctx.env.content_length = getenv("CONTENT_LENGTH") ?
		strtoul(getenv("CONTENT_LENGTH"), NULL, 10) : 0;
	ctx.env.authenticated = 0;
	ctx.page.mimetype = "text/html";
	ctx.page.charset = PAGE_ENCODING;
	ctx.page.filename = NULL;
	ctx.page.size = 0;
	string_list_init_dup(&ctx.cfg.mimetypes);
	if (ctx.env.script_name)
		ctx.cfg.script_name = xstrdup(ctx.env.script_name);
	if (ctx.env.query_string)
		ctx.qry.raw = xstrdup(ctx.env.query_string);
	if (!ctx.env.cgit_config)
		ctx.env.cgit_config = CGIT_CONFIG;
}

static void print_version(void)
{
	printf("CGit %s | https://github.com/brycekwon/cgit\n\nCompiled in features:\n", CGIT_VERSION);
#ifdef NO_LUA
	printf("[-] ");
#else
	printf("[+] ");
#endif
	printf("Lua scripting\n");
#ifndef HAVE_LINUX_SENDFILE
	printf("[-] ");
#else
	printf("[+] ");
#endif
	printf("Linux sendfile() usage\n");
}

static int cmp_repos(const void *a, const void *b)
{
	const struct cgit_repo *repo_a = a, *repo_b = b;
	return strcmp(repo_a->url, repo_b->url);
}

static char *build_snapshot_setting(int mask)
{
	const struct cgit_snapshot_format *format;
	struct strbuf result = STRBUF_INIT;

	for (format = cgit_snapshot_formats; format->suffix; format++) {
		if (cgit_snapshot_format_bit(format) & mask) {
			if (result.len)
				strbuf_addch(&result, ' ');
			strbuf_addstr(&result, format->suffix);
		}
	}
	return strbuf_detach(&result, NULL);
}

static void print_repo(FILE *f, struct cgit_repo *repo)
{
	struct string_list_item *item;

	fprintf(f, "repo.url=%s\n", repo->url);
	fprintf(f, "repo.name=%s\n", repo->name);
	fprintf(f, "repo.path=%s\n", repo->path);
	if (repo->owner)
		fprintf(f, "repo.owner=%s\n", repo->owner);
	if (repo->desc)
		fprintf(f, "repo.desc=%s\n", repo->desc);
	for_each_string_list_item(item, &repo->readme) {
		if (item->util)
			fprintf(f, "repo.readme=%s:%s\n", (char *)item->util, item->string);
		else
			fprintf(f, "repo.readme=%s\n", item->string);
	}
	if (repo->defbranch)
		fprintf(f, "repo.defbranch=%s\n", repo->defbranch);
	if (repo->extra_head_content)
		fprintf(f, "repo.extra-head-content=%s\n", repo->extra_head_content);
	if (repo->module_link)
		fprintf(f, "repo.module-link=%s\n", repo->module_link);
	if (repo->section)
		fprintf(f, "repo.section=%s\n", repo->section);
	if (repo->clone_url)
		fprintf(f, "repo.clone-url=%s\n", repo->clone_url);
	fprintf(f, "repo.enable-blame=%d\n", repo->enable_blame);
	fprintf(f, "repo.enable-commit-graph=%d\n", repo->enable_commit_graph);
	fprintf(f, "repo.enable-follow-links=%d\n", repo->enable_follow_links);
	fprintf(f, "repo.enable-log-filecount=%d\n", repo->enable_log_filecount);
	fprintf(f, "repo.enable-log-linecount=%d\n", repo->enable_log_linecount);
	if (repo->about_filter && repo->about_filter != ctx.cfg.about_filter)
		cgit_fprintf_filter(repo->about_filter, f, "repo.about-filter=");
	if (repo->commit_filter && repo->commit_filter != ctx.cfg.commit_filter)
		cgit_fprintf_filter(repo->commit_filter, f, "repo.commit-filter=");
	if (repo->source_filter && repo->source_filter != ctx.cfg.source_filter)
		cgit_fprintf_filter(repo->source_filter, f, "repo.source-filter=");
	if (repo->email_filter && repo->email_filter != ctx.cfg.email_filter)
		cgit_fprintf_filter(repo->email_filter, f, "repo.email-filter=");
	if (repo->snapshots != ctx.cfg.snapshots) {
		char *formats = build_snapshot_setting(repo->snapshots);
		fprintf(f, "repo.snapshots=%s\n", formats ? formats : "");
		free(formats);
	}
	if (repo->snapshot_prefix)
		fprintf(f, "repo.snapshot-prefix=%s\n", repo->snapshot_prefix);
	if (repo->enable_stats != ctx.cfg.enable_stats)
		fprintf(f, "repo.enable-stats=%d\n", repo->enable_stats);
	if (repo->max_stats != ctx.cfg.max_stats)
		fprintf(f, "repo.max-stats=%s\n",
			cgit_find_stats_periodname(repo->max_stats));
	if (repo->logo)
		fprintf(f, "repo.logo=%s\n", repo->logo);
	if (repo->logo_link)
		fprintf(f, "repo.logo-link=%s\n", repo->logo_link);
	fprintf(f, "repo.enable-remote-branches=%d\n", repo->enable_remote_branches);
	fprintf(f, "repo.enable-subject-links=%d\n", repo->enable_subject_links);
	fprintf(f, "repo.enable-html-serving=%d\n", repo->enable_html_serving);
	if (repo->branch_sort == 1)
		fprintf(f, "repo.branch-sort=age\n");
	if (repo->commit_sort) {
		if (repo->commit_sort == 1)
			fprintf(f, "repo.commit-sort=date\n");
		else if (repo->commit_sort == 2)
			fprintf(f, "repo.commit-sort=topo\n");
	}
	fprintf(f, "repo.hide=%d\n", repo->hide);
	fprintf(f, "repo.ignore=%d\n", repo->ignore);
	fprintf(f, "\n");
}

static void print_repolist(FILE *f, struct cgit_repolist *list, int start)
{
	int i;

	for (i = start; i < list->count; i++)
		print_repo(f, &list->repos[i]);
}

static void parse_args(int argc, const char **argv)
{
	int i;
	const char *arg;
	int scanned = 0;

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "--version")) {
			print_version();
			exit(0);
		}
		if (skip_prefix(argv[i], "--cache=", &arg)) {
			ctx.cfg.cache_root = xstrdup(arg);
		} else if (!strcmp(argv[i], "--nohttp")) {
			ctx.env.no_http = "1";
		} else if (skip_prefix(argv[i], "--query=", &arg)) {
			ctx.qry.raw = xstrdup(arg);
		} else if (skip_prefix(argv[i], "--repo=", &arg)) {
			ctx.qry.repo = xstrdup(arg);
		} else if (skip_prefix(argv[i], "--page=", &arg)) {
			ctx.qry.page = xstrdup(arg);
		} else if (skip_prefix(argv[i], "--head=", &arg)) {
			ctx.qry.head = xstrdup(arg);
			ctx.qry.has_symref = 1;
		} else if (skip_prefix(argv[i], "--oid=", &arg)) {
			ctx.qry.oid = xstrdup(arg);
			ctx.qry.has_oid = 1;
		} else if (skip_prefix(argv[i], "--ofs=", &arg)) {
			ctx.qry.ofs = atoi(arg);
		} else if (skip_prefix(argv[i], "--scan-tree=", &arg) ||
		           skip_prefix(argv[i], "--scan-path=", &arg)) {
			// A repository's own snapshots setting is masked with
			// the global one, which normally comes from cgitrc.
			// That has not been read yet here, so an empty mask
			// would discard whatever the repository asked for.
			ctx.cfg.snapshots = ALL_SNAPSHOT_FORMATS;
			scanned++;
			scan_tree(arg);
		}
	}
	if (scanned) {
		qsort(cgit_repolist.repos, cgit_repolist.count,
			sizeof(struct cgit_repo), cmp_repos);
		print_repolist(stdout, &cgit_repolist, 0);
		exit(0);
	}
}

static int generate_cached_repolist(const char *path, const char *cached_rc)
{
	struct strbuf locked_rc = STRBUF_INIT;
	int err = 0;
	int first;
	FILE *f;

	strbuf_addf(&locked_rc, "%s.lock", cached_rc);
	f = fopen(locked_rc.buf, "wx");
	if (!f) {
		// An existing lock file only means concurrent requests, which
		// is not worth a line in the server log.
		err = errno;
		if (err != EEXIST)
			fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
				locked_rc.buf, strerror(err), err);
		goto out;
	}
	first = cgit_repolist.count;
	if (ctx.cfg.project_list)
		scan_projects(path, ctx.cfg.project_list);
	else
		scan_tree(path);
	print_repolist(f, &cgit_repolist, first);
	// Closed before the rename, because print_repolist writes through stdio
	// and a rename over the live file would otherwise publish a repolist
	// that stops wherever the buffer happened to end.
	if (fclose(f)) {
		err = errno;
		fprintf(stderr, "[cgit] Error writing %s: %s (%d)\n",
			locked_rc.buf, strerror(err), err);
		unlink(locked_rc.buf);
		goto out;
	}
	if (rename(locked_rc.buf, cached_rc)) {
		err = errno;
		fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
			locked_rc.buf, cached_rc, strerror(err), err);
		unlink(locked_rc.buf);
	}
out:
	strbuf_release(&locked_rc);
	return err;
}

// A cached repolist is itself a config file, so these two call each other.
static void apply_config(const char *name, const char *value);

static void process_cached_repolist(const char *path)
{
	struct stat st;
	struct strbuf cached_rc = STRBUF_INIT;
	time_t age;
	unsigned long hash;
	int devnull;

	hash = cache_hash_str(path);
	if (ctx.cfg.project_list)
		hash += cache_hash_str(ctx.cfg.project_list);
	strbuf_addf(&cached_rc, "%s/rc-%8lx", ctx.cfg.cache_root, hash);

	if (stat(cached_rc.buf, &st)) {
		// Nothing is cached yet, so this request scans in its own
		// process, leaving no copy behind when it cannot take the lock.
		if (generate_cached_repolist(path, cached_rc.buf)) {
			if (ctx.cfg.project_list)
				scan_projects(path, ctx.cfg.project_list);
			else
				scan_tree(path);
		}
		goto out;
	}

	config_file_parse(cached_rc.buf, apply_config);

	age = time(NULL) - st.st_mtime;
	if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
		goto out;

	// The list just parsed is stale but usable, so a child rebuilds it
	// while this request answers from what it already has.
	if (fork())
		goto out;

	// The child inherits the descriptors of the request, and the web server
	// reads stdout until every holder of it is gone, so leaving them in
	// place would keep the visitor waiting for the whole scan after their
	// page was written. Anything the scan prints would land on that
	// response as well.
	devnull = open("/dev/null", O_RDWR);
	if (devnull >= 0) {
		dup2(devnull, STDIN_FILENO);
		dup2(devnull, STDOUT_FILENO);
		dup2(devnull, STDERR_FILENO);
		if (devnull > STDERR_FILENO)
			close(devnull);
	}
	// _exit rather than exit, so the handlers the request registered do
	// not run a second time in the child.
	_exit(generate_cached_repolist(path, cached_rc.buf));
out:
	strbuf_release(&cached_rc);
}

static void add_mimetype(const char *name, const char *value)
{
	struct string_list_item *item;

	item = string_list_insert(&ctx.cfg.mimetypes, name);
	item->util = xstrdup(value);
}

static void apply_config(const char *name, const char *value)
{
	const char *arg;

	if (!strcmp(name, "section"))
		ctx.cfg.section = cgit_strdup_first_line(value);
	else if (!strcmp(name, "repo.url"))
		ctx.repo = cgit_add_repo(value);
	else if (ctx.repo && !strcmp(name, "repo.path"))
		ctx.repo->path = cgit_trim_end(value, '/');
	else if (ctx.repo && skip_prefix(name, "repo.", &arg))
		cgit_repo_config(ctx.repo, arg, value);
	else if (!strcmp(name, "readme"))
		string_list_append(&ctx.cfg.readme, cgit_strdup_first_line(value));
	else if (!strcmp(name, "root-title"))
		ctx.cfg.root_title = cgit_strdup_first_line(value);
	else if (!strcmp(name, "root-desc"))
		ctx.cfg.root_desc = cgit_strdup_first_line(value);
	else if (!strcmp(name, "root-readme"))
		ctx.cfg.root_readme = cgit_strdup_first_line(value);
	else if (!strcmp(name, "css"))
		string_list_append(&ctx.cfg.css, cgit_strdup_first_line(value));
	else if (!strcmp(name, "js"))
		string_list_append(&ctx.cfg.js, cgit_strdup_first_line(value));
	else if (!strcmp(name, "favicon"))
		ctx.cfg.favicon = cgit_strdup_first_line(value);
	else if (!strcmp(name, "footer"))
		ctx.cfg.footer = cgit_strdup_first_line(value);
	else if (!strcmp(name, "head-include"))
		ctx.cfg.head_include = cgit_strdup_first_line(value);
	else if (!strcmp(name, "header"))
		ctx.cfg.header = cgit_strdup_first_line(value);
	else if (!strcmp(name, "logo"))
		ctx.cfg.logo = cgit_strdup_first_line(value);
	else if (!strcmp(name, "logo-link"))
		ctx.cfg.logo_link = cgit_strdup_first_line(value);
	else if (!strcmp(name, "module-link"))
		ctx.cfg.module_link = cgit_strdup_first_line(value);
	else if (!strcmp(name, "strict-export"))
		ctx.cfg.strict_export = cgit_strdup_first_line(value);
	else if (!strcmp(name, "virtual-root"))
		ctx.cfg.virtual_root = cgit_ensure_end(value, '/');
	else if (!strcmp(name, "noplainemail"))
		ctx.cfg.noplainemail = atoi(value);
	else if (!strcmp(name, "noheader"))
		ctx.cfg.noheader = atoi(value);
	else if (!strcmp(name, "snapshots"))
		ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
	else if (!strcmp(name, "enable-filter-overrides"))
		ctx.cfg.enable_filter_overrides = atoi(value);
	else if (!strcmp(name, "enable-follow-links"))
		ctx.cfg.enable_follow_links = atoi(value);
	else if (!strcmp(name, "enable-stats"))
		ctx.cfg.enable_stats = atoi(value);
	else if (!strcmp(name, "enable-http-clone"))
		ctx.cfg.enable_http_clone = atoi(value);
	else if (!strcmp(name, "enable-index-links"))
		ctx.cfg.enable_index_links = atoi(value);
	else if (!strcmp(name, "enable-index-owner"))
		ctx.cfg.enable_index_owner = atoi(value);
	else if (!strcmp(name, "enable-blame"))
		ctx.cfg.enable_blame = atoi(value);
	else if (!strcmp(name, "enable-commit-graph"))
		ctx.cfg.enable_commit_graph = atoi(value);
	else if (!strcmp(name, "enable-log-filecount"))
		ctx.cfg.enable_log_filecount = atoi(value);
	else if (!strcmp(name, "enable-log-linecount"))
		ctx.cfg.enable_log_linecount = atoi(value);
	else if (!strcmp(name, "enable-relative-dates"))
		ctx.cfg.enable_relative_dates = atoi(value);
	else if (!strcmp(name, "enable-remote-branches"))
		ctx.cfg.enable_remote_branches = atoi(value);
	else if (!strcmp(name, "enable-subject-links"))
		ctx.cfg.enable_subject_links = atoi(value);
	else if (!strcmp(name, "enable-html-serving"))
		ctx.cfg.enable_html_serving = atoi(value);
	else if (!strcmp(name, "enable-tree-linenumbers"))
		ctx.cfg.enable_tree_linenumbers = atoi(value);
	else if (!strcmp(name, "enable-tree-group-dirs"))
		ctx.cfg.enable_tree_group_dirs = atoi(value);
	else if (!strcmp(name, "enable-git-config"))
		ctx.cfg.enable_git_config = atoi(value);
	else if (!strcmp(name, "enable-cache-list"))
		ctx.cfg.enable_cache_list = atoi(value);
	else if (!strcmp(name, "max-stats"))
		ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
	else if (!strcmp(name, "cache-size"))
		ctx.cfg.cache_size = atoi(value);
	else if (!strcmp(name, "cache-root"))
		ctx.cfg.cache_root = cgit_strdup_first_line(cgit_expand_macros(value));
	else if (!strcmp(name, "cache-root-ttl"))
		ctx.cfg.cache_root_ttl = atoi(value);
	else if (!strcmp(name, "cache-repo-ttl"))
		ctx.cfg.cache_repo_ttl = atoi(value);
	else if (!strcmp(name, "cache-scanrc-ttl"))
		ctx.cfg.cache_scanrc_ttl = atoi(value);
	else if (!strcmp(name, "cache-static-ttl"))
		ctx.cfg.cache_static_ttl = atoi(value);
	else if (!strcmp(name, "cache-dynamic-ttl"))
		ctx.cfg.cache_dynamic_ttl = atoi(value);
	else if (!strcmp(name, "cache-about-ttl"))
		ctx.cfg.cache_about_ttl = atoi(value);
	else if (!strcmp(name, "cache-snapshot-ttl"))
		ctx.cfg.cache_snapshot_ttl = atoi(value);
	else if (!strcmp(name, "case-sensitive-sort"))
		ctx.cfg.case_sensitive_sort = atoi(value);
	else if (!strcmp(name, "about-filter"))
		ctx.cfg.about_filter = cgit_new_filter(value, ABOUT);
	else if (!strcmp(name, "commit-filter"))
		ctx.cfg.commit_filter = cgit_new_filter(value, COMMIT);
	else if (!strcmp(name, "email-filter"))
		ctx.cfg.email_filter = cgit_new_filter(value, EMAIL);
	else if (!strcmp(name, "auth-filter"))
		ctx.cfg.auth_filter = cgit_new_filter(value, AUTH);
	else if (!strcmp(name, "embedded"))
		ctx.cfg.embedded = atoi(value);
	else if (!strcmp(name, "max-atom-items"))
		ctx.cfg.max_atom_items = atoi(value);
	else if (!strcmp(name, "max-message-length"))
		ctx.cfg.max_msg_len = atoi(value);
	else if (!strcmp(name, "max-repodesc-length"))
		ctx.cfg.max_repodesc_len = atoi(value);
	else if (!strcmp(name, "max-blob-size"))
		ctx.cfg.max_blob_size = atoi(value);
	else if (!strcmp(name, "max-diff-files"))
		ctx.cfg.max_diff_files = atoi(value);
	else if (!strcmp(name, "max-diff-lines"))
		ctx.cfg.max_diff_lines = atoi(value);
	else if (!strcmp(name, "max-ref-count"))
		ctx.cfg.max_ref_count = atoi(value);
	else if (!strcmp(name, "max-repo-count")) {
		ctx.cfg.max_repo_count = atoi(value);
		if (ctx.cfg.max_repo_count <= 0)
			ctx.cfg.max_repo_count = INT_MAX;
	} else if (!strcmp(name, "max-commit-count"))
		ctx.cfg.max_commit_count = atoi(value);
	else if (!strcmp(name, "max-patch-count"))
		ctx.cfg.max_patch_count = atoi(value);
	else if (!strcmp(name, "project-list"))
		ctx.cfg.project_list = cgit_strdup_first_line(cgit_expand_macros(value));
	else if (!strcmp(name, "scan-path")) {
		if (ctx.cfg.cache_size)
			process_cached_repolist(cgit_expand_macros(value));
		else if (ctx.cfg.project_list)
			scan_projects(cgit_expand_macros(value),
				      ctx.cfg.project_list);
		else
			scan_tree(cgit_expand_macros(value));
	} else if (!strcmp(name, "scan-hidden-path"))
		ctx.cfg.scan_hidden_path = atoi(value);
	else if (!strcmp(name, "section-from-path"))
		ctx.cfg.section_from_path = atoi(value);
	else if (!strcmp(name, "repository-sort"))
		ctx.cfg.repository_sort = cgit_strdup_first_line(value);
	else if (!strcmp(name, "section-sort"))
		ctx.cfg.section_sort = atoi(value);
	else if (!strcmp(name, "source-filter"))
		ctx.cfg.source_filter = cgit_new_filter(value, SOURCE);
	else if (!strcmp(name, "summary-log"))
		ctx.cfg.summary_log = atoi(value);
	else if (!strcmp(name, "summary-branches"))
		ctx.cfg.summary_branches = atoi(value);
	else if (!strcmp(name, "summary-tags"))
		ctx.cfg.summary_tags = atoi(value);
	else if (!strcmp(name, "side-by-side-diffs"))
		ctx.cfg.difftype = atoi(value) ? DIFF_SSDIFF : DIFF_UNIFIED;
	else if (!strcmp(name, "agefile"))
		ctx.cfg.agefile = cgit_strdup_first_line(value);
	else if (!strcmp(name, "mimetype-file"))
		ctx.cfg.mimetype_file = cgit_strdup_first_line(value);
	else if (!strcmp(name, "renamelimit"))
		ctx.cfg.renamelimit = atoi(value);
	else if (!strcmp(name, "remove-suffix"))
		ctx.cfg.remove_suffix = atoi(value);
	else if (!strcmp(name, "robots"))
		ctx.cfg.robots = cgit_strdup_first_line(value);
	else if (!strcmp(name, "clone-prefix"))
		ctx.cfg.clone_prefix = cgit_strdup_first_line(value);
	else if (!strcmp(name, "clone-url"))
		ctx.cfg.clone_url = cgit_strdup_first_line(value);
	else if (!strcmp(name, "local-time"))
		ctx.cfg.local_time = atoi(value);
	else if (!strcmp(name, "date-format"))
		cgit_parse_date_format(value, &ctx.cfg.date_mode);
	else if (!strcmp(name, "commit-sort")) {
		if (!strcmp(value, "date"))
			ctx.cfg.commit_sort = 1;
		if (!strcmp(value, "topo"))
			ctx.cfg.commit_sort = 2;
	} else if (!strcmp(name, "branch-sort")) {
		if (!strcmp(value, "age"))
			ctx.cfg.branch_sort = 1;
		if (!strcmp(value, "name"))
			ctx.cfg.branch_sort = 0;
	} else if (skip_prefix(name, "mimetype.", &arg))
		add_mimetype(arg, value);
	else if (!strcmp(name, "include"))
		config_file_parse(cgit_expand_macros(value), apply_config);
}

/*
 * Read a whole number a request supplied, clamped into the range the caller
 * accepts. strtol rather than atoi, because atoi has no defined behaviour once
 * the digits overflow and every value here arrives straight from the query
 * string.
 */
static int query_int(const char *value, int min, int max)
{
	long number = strtol(value, NULL, 10);

	if (number < min)
		return min;
	if (number > max)
		return max;
	return number;
}

static void apply_query_param(const char *name, const char *value)
{
	if (!value)
		value = "";

	if (!strcmp(name,"r")) {
		ctx.qry.repo = xstrdup(value);
		ctx.repo = cgit_get_repoinfo(value);
	} else if (!strcmp(name, "p")) {
		ctx.qry.page = xstrdup(value);
	} else if (!strcmp(name, "url")) {
		// Every leading slash goes, not just one. What is left is
		// joined onto the virtual root, so a value like //example.com
		// would otherwise survive as /example.com and make that join a
		// scheme-relative link to another host.
		while (*value == '/')
			value++;
		ctx.qry.url = xstrdup(value);
		cgit_parse_url(value);
	} else if (!strcmp(name, "qt")) {
		ctx.qry.grep = xstrdup(value);
	} else if (!strcmp(name, "q")) {
		if (strlen(value) > MAX_SEARCH_LEN)
			ctx.qry.search = xstrndup(value, MAX_SEARCH_LEN);
		else
			ctx.qry.search = xstrdup(value);
	} else if (!strcmp(name, "h")) {
		ctx.qry.head = xstrdup(value);
		ctx.qry.has_symref = 1;
	} else if (!strcmp(name, "id")) {
		ctx.qry.oid = xstrdup(value);
		ctx.qry.has_oid = 1;
	} else if (!strcmp(name, "id2")) {
		ctx.qry.oid2 = xstrdup(value);
		ctx.qry.has_oid = 1;
	} else if (!strcmp(name, "ofs")) {
		// Bounded above so a crafted value cannot force a walk over the
		// whole history. Negatives stop at -1 rather than at zero,
		// because the offset is overloaded, the stats page submits -1
		// for all authors, and the log skip loop floors a negative
		// itself.
		ctx.qry.ofs = query_int(value, -1, MAX_QUERY_OFFSET);
	} else if (!strcmp(name, "path")) {
		ctx.qry.path = cgit_trim_end(value, '/');
	} else if (!strcmp(name, "s")) {
		ctx.qry.sort = xstrdup(value);
	} else if (!strcmp(name, "showmsg")) {
		ctx.qry.showmsg = query_int(value, INT_MIN, INT_MAX);
	} else if (!strcmp(name, "period")) {
		ctx.qry.period = xstrdup(value);
	} else if (!strcmp(name, "dt")) {
		ctx.qry.difftype = query_int(value, INT_MIN, INT_MAX);
		ctx.qry.has_difftype = 1;
	} else if (!strcmp(name, "ss")) {
		// No longer generated, but old links still carry it.
		ctx.qry.difftype = query_int(value, INT_MIN, INT_MAX) ?
			DIFF_SSDIFF : DIFF_UNIFIED;
		ctx.qry.has_difftype = 1;
	} else if (!strcmp(name, "all")) {
		ctx.qry.show_all = query_int(value, INT_MIN, INT_MAX);
	} else if (!strcmp(name, "context")) {
		// Context lines are not counted against max-diff-lines, so an
		// unbounded width turns a whole blob into context and renders
		// it in full however small the change was.
		ctx.qry.context = query_int(value, 0, MAX_DIFF_CONTEXT_LINES);
	} else if (!strcmp(name, "ignorews")) {
		ctx.qry.ignorews = query_int(value, INT_MIN, INT_MAX);
	} else if (!strcmp(name, "follow")) {
		ctx.qry.follow = query_int(value, INT_MIN, INT_MAX);
	}
}

static void open_auth_filter(const char *action)
{
	cgit_open_filter(ctx.cfg.auth_filter, action,
		ctx.env.http_cookie ? ctx.env.http_cookie : "",
		ctx.env.request_method ? ctx.env.request_method : "",
		ctx.env.query_string ? ctx.env.query_string : "",
		ctx.env.http_referer ? ctx.env.http_referer : "",
		ctx.env.path_info ? ctx.env.path_info : "",
		ctx.env.http_host ? ctx.env.http_host : "",
		ctx.env.https ? ctx.env.https : "",
		ctx.qry.repo ? ctx.qry.repo : "",
		ctx.qry.page ? ctx.qry.page : "",
		cgit_currentfullurl(),
		cgit_loginurl());
}

/*
 * The filter answers the login POST itself, writing the status line and every
 * header, so nothing here prints any and the process ends before this returns.
 */
static void authenticate_post(void)
{
	char buffer[MAX_AUTHENTICATION_POST_BYTES];
	size_t len;
	ssize_t got;

	open_auth_filter("authenticate-post");
	len = ctx.env.content_length;
	if (len > MAX_AUTHENTICATION_POST_BYTES)
		len = MAX_AUTHENTICATION_POST_BYTES;
	if ((got = read(STDIN_FILENO, buffer, len)) < 0)
		die_errno("Could not read POST from stdin");
	if (write(STDOUT_FILENO, buffer, got) < 0)
		die_errno("Could not write POST to stdout");
	cgit_close_filter(ctx.cfg.auth_filter);
	exit(0);
}

static void authenticate_cookie(void)
{
	if (!ctx.cfg.auth_filter) {
		ctx.env.authenticated = 1;
		return;
	}

	if (ctx.env.request_method && ctx.qry.page && !ctx.repo &&
	    !strcmp(ctx.env.request_method, "POST") &&
	    !strcmp(ctx.qry.page, "login")) {
		authenticate_post();
		return;
	}

	open_auth_filter("authenticate-cookie");
	ctx.env.authenticated = cgit_close_filter(ctx.cfg.auth_filter);
}

// Every cache-*-ttl setting is written in minutes, and so is this.
static int calc_ttl(void)
{
	if (!ctx.repo)
		return ctx.cfg.cache_root_ttl;

	if (!ctx.qry.page)
		return ctx.cfg.cache_repo_ttl;

	if (!strcmp(ctx.qry.page, "about"))
		return ctx.cfg.cache_about_ttl;

	if (!strcmp(ctx.qry.page, "snapshot"))
		return ctx.cfg.cache_snapshot_ttl;

	if (ctx.qry.has_oid)
		return ctx.cfg.cache_static_ttl;

	if (ctx.qry.has_symref)
		return ctx.cfg.cache_dynamic_ttl;

	return ctx.cfg.cache_repo_ttl;
}

/*
 * The scheme and the host are folded in because the absolute urls a page
 * carries, its clone urls and atom links, are built from them, so a request
 * arriving with a spoofed Host must not poison the page served to a visitor
 * who came in on the real one.
 */
static void build_cache_key(struct strbuf *key)
{
	char *hosturl = cgit_hosturl();
	const char *parts[] = {
		cgit_httpscheme(),
		hosturl ? hosturl : "",
		ctx.env.path_info ? ctx.env.path_info : "",
		ctx.env.query_string ? ctx.env.query_string : "",
	};
	size_t i;

	// Each part is written behind its own length, so nothing a value
	// contains can make two different requests spell one key. The path and
	// the query come from the environment rather than the query string cgit
	// rebuilds, since that rebuild folds the two together and would let the
	// PATH_INFO and QUERY_STRING forms of one request share a slot.
	for (i = 0; i < ARRAY_SIZE(parts); i++)
		strbuf_addf(key, "%zu|%s", strlen(parts[i]), parts[i]);
	free(hosturl);
}

// Returning non-zero ends git's walk, so the search stops at the first hit.
static int find_current_ref(const struct reference *ref, void *data)
{
	struct refmatch *match = data;

	if (!strcmp(ref->name, match->wanted))
		match->found = 1;
	if (!match->first)
		match->first = xstrdup(ref->name);
	return match->found;
}

static char *find_default_branch(struct cgit_repo *repo)
{
	struct refmatch match;
	char *ref;

	match.wanted = repo->defbranch;
	match.first = NULL;
	match.found = 0;
	refs_for_each_branch_ref(get_main_ref_store(the_repository),
				 find_current_ref, &match);
	if (match.found)
		ref = match.wanted;
	else
		ref = match.first;
	if (ref)
		ref = xstrdup(ref);
	free(match.first);

	return ref;
}

static char *guess_defbranch(void)
{
	const char *ref, *refname;
	struct object_id oid;

	ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
				     "HEAD", 0, &oid, NULL);
	if (!ref || !skip_prefix(ref, "refs/heads/", &refname))
		return "master";
	return xstrdup(refname);
}

/*
 * Split one readme setting into the file it names and the ref that file is
 * read from, leaving the ref NULL for a file on disk. The caller frees both.
 */
static void parse_readme(const char *readme, char **filename, char **ref,
			 struct cgit_repo *repo)
{
	const char *colon;

	*filename = NULL;
	*ref = NULL;

	if (!readme || !readme[0])
		return;

	// A colon separates a ref from a path, so a setting carrying one names
	// a file tracked in the repository rather than one on disk.
	colon = strchr(readme, ':');
	if (colon && strlen(colon) > 1) {
		if (colon == readme && ctx.qry.head)
			*ref = xstrdup(ctx.qry.head);
		else if (colon == readme && repo->defbranch)
			*ref = xstrdup(repo->defbranch);
		else
			*ref = xstrndup(readme, colon - readme);
		readme = colon + 1;
	}

	if (!(*ref) && readme[0] != '/')
		*filename = cgit_fmtalloc("%s/%s", repo->path, readme);
	else
		*filename = xstrdup(readme);
}

static void choose_readme(struct cgit_repo *repo)
{
	int found;
	char *filename, *ref;
	struct string_list_item *entry;

	if (!repo->readme.nr)
		return;

	found = 0;
	for_each_string_list_item(entry, &repo->readme) {
		parse_readme(entry->string, &filename, &ref, repo);
		if (!filename) {
			free(filename);
			free(ref);
			continue;
		}
		if (ref) {
			if (cgit_ref_path_exists(filename, ref, 1)) {
				found = 1;
				break;
			}
		}
		else if (!access(filename, R_OK)) {
			found = 1;
			break;
		}
		free(filename);
		free(ref);
	}
	repo->readme.strdup_strings = 1;
	string_list_clear(&repo->readme, 0);
	repo->readme.strdup_strings = 0;
	if (found)
		string_list_append(&repo->readme, filename)->util = ref;
}

static void prepare_repo_env(int *nongit)
{
	setenv("GIT_DIR", ctx.repo->path, 1);

	// Both read configuration out of the repository, with the user's own
	// git configuration already stripped by isolate_git_environment.
	setup_git_directory_gently(the_repository, nongit);
	load_display_notes(NULL);
}

/*
 * Returns non-zero once it has written a complete response of its own, in
 * which case the caller must not render a page over the top of it.
 */
static int prepare_repo_cmd(int nongit)
{
	struct object_id oid;
	int err;

	if (nongit) {
		const char *name = ctx.repo->name;
		err = errno;
		ctx.page.title = cgit_fmtalloc("%s - %s", ctx.cfg.root_title,
						"config error");
		ctx.repo = NULL;
		cgit_print_http_headers();
		cgit_print_docstart();
		cgit_print_pageheader();
		cgit_print_error("Failed to open %s: %s", name,
				 err ? strerror(err) : "Not a valid git repository");
		cgit_print_docend();
		return 1;
	}
	ctx.page.title = cgit_fmtalloc("%s - %s", ctx.repo->name, ctx.repo->desc);

	if (!ctx.repo->defbranch)
		ctx.repo->defbranch = guess_defbranch();

	if (!ctx.qry.head) {
		ctx.qry.nohead = 1;
		ctx.qry.head = find_default_branch(ctx.repo);
	}

	if (!ctx.qry.head) {
		ctx.empty_repo = 1;
		// Before the document starts, since the <head> carries
		// <link rel='vcs-git'> and those clone urls expand macros such
		// as $CGIT_REPO_URL out of this environment.
		cgit_prepare_repo_env(ctx.repo);
		cgit_print_http_headers();
		cgit_print_docstart();
		cgit_print_pageheader();
		cgit_print_empty_repo();
		cgit_print_docend();
		return 1;
	}

	if (repo_get_oid(the_repository, ctx.qry.head, &oid)) {
		char *old_head = ctx.qry.head;
		ctx.qry.head = xstrdup(ctx.repo->defbranch);
		cgit_print_error_page(404, "Not found",
				"Invalid branch: %s", old_head);
		free(old_head);
		return 1;
	}
	string_list_sort(&ctx.repo->submodules);
	cgit_prepare_repo_env(ctx.repo);
	choose_readme(ctx.repo);
	return 0;
}

static void process_request(void)
{
	const struct cgit_cmd *cmd;
	int nongit = 0;

	// An unauthenticated request is answered with the filter's own body
	// whatever page it asked for.
	if (!ctx.env.authenticated) {
		ctx.page.title = "Authentication Required";
		cgit_print_http_headers();
		cgit_print_docstart();
		cgit_print_pageheader();
		open_auth_filter("body");
		cgit_close_filter(ctx.cfg.auth_filter);
		cgit_print_docend();
		return;
	}

	if (ctx.repo)
		prepare_repo_env(&nongit);

	cmd = cgit_get_cmd();
	if (!cmd) {
		ctx.page.title = "cgit error";
		cgit_print_error_page(404, "Not found", "Invalid request");
		return;
	}

	if (!ctx.cfg.enable_http_clone && cmd->is_clone) {
		ctx.page.title = "cgit error";
		cgit_print_error_page(404, "Not found", "Invalid request");
		return;
	}

	if (cmd->want_repo && !ctx.repo) {
		cgit_print_error_page(400, "Bad request",
				"No repository selected");
		return;
	}

	ctx.qry.vpath = cmd->want_vpath ? ctx.qry.path : NULL;

	if (ctx.repo && prepare_repo_cmd(nongit))
		return;

	cmd->fn();
}

void cgit_repo_config(struct cgit_repo *repo, const char *name, const char *value)
{
	const char *path;
	struct string_list_item *item;

	if (!strcmp(name, "name"))
		repo->name = cgit_strdup_first_line(value);
	else if (!strcmp(name, "clone-url"))
		repo->clone_url = cgit_strdup_first_line(value);
	else if (!strcmp(name, "desc"))
		repo->desc = cgit_strdup_first_line(value);
	else if (!strcmp(name, "owner"))
		repo->owner = cgit_strdup_first_line(value);
	else if (!strcmp(name, "defbranch"))
		repo->defbranch = cgit_strdup_first_line(value);
	else if (!strcmp(name, "extra-head-content"))
		repo->extra_head_content = cgit_strdup_first_line(value);
	else if (!strcmp(name, "snapshots"))
		repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value);
	else if (!strcmp(name, "enable-blame"))
		repo->enable_blame = atoi(value);
	else if (!strcmp(name, "enable-commit-graph"))
		repo->enable_commit_graph = atoi(value);
	else if (!strcmp(name, "enable-follow-links"))
		repo->enable_follow_links = atoi(value);
	else if (!strcmp(name, "enable-log-filecount"))
		repo->enable_log_filecount = atoi(value);
	else if (!strcmp(name, "enable-log-linecount"))
		repo->enable_log_linecount = atoi(value);
	else if (!strcmp(name, "enable-remote-branches"))
		repo->enable_remote_branches = atoi(value);
	else if (!strcmp(name, "enable-subject-links"))
		repo->enable_subject_links = atoi(value);
	else if (!strcmp(name, "enable-html-serving"))
		repo->enable_html_serving = atoi(value);
	else if (!strcmp(name, "enable-stats"))
		repo->enable_stats = atoi(value);
	else if (!strcmp(name, "branch-sort")) {
		if (!strcmp(value, "age"))
			repo->branch_sort = 1;
		if (!strcmp(value, "name"))
			repo->branch_sort = 0;
	} else if (!strcmp(name, "commit-sort")) {
		if (!strcmp(value, "date"))
			repo->commit_sort = 1;
		if (!strcmp(value, "topo"))
			repo->commit_sort = 2;
	} else if (!strcmp(name, "max-stats"))
		repo->max_stats = cgit_find_stats_period(value, NULL);
	else if (!strcmp(name, "module-link"))
		repo->module_link = cgit_strdup_first_line(value);
	else if (skip_prefix(name, "module-link.", &path)) {
		item = string_list_append(&repo->submodules,
					  cgit_strdup_first_line(path));
		item->util = cgit_strdup_first_line(value);
	} else if (!strcmp(name, "section"))
		repo->section = cgit_strdup_first_line(value);
	else if (!strcmp(name, "snapshot-prefix"))
		repo->snapshot_prefix = cgit_strdup_first_line(value);
	else if (!strcmp(name, "readme") && value != NULL) {
		if (repo->readme.items == ctx.cfg.readme.items)
			memset(&repo->readme, 0, sizeof(repo->readme));
		string_list_append(&repo->readme, cgit_strdup_first_line(value));
	} else if (!strcmp(name, "logo") && value != NULL)
		repo->logo = cgit_strdup_first_line(value);
	else if (!strcmp(name, "logo-link") && value != NULL)
		repo->logo_link = cgit_strdup_first_line(value);
	else if (!strcmp(name, "hide"))
		repo->hide = atoi(value);
	else if (!strcmp(name, "ignore"))
		repo->ignore = atoi(value);
	else if (ctx.cfg.enable_filter_overrides) {
		if (!strcmp(name, "about-filter"))
			repo->about_filter = cgit_new_filter(value, ABOUT);
		else if (!strcmp(name, "commit-filter"))
			repo->commit_filter = cgit_new_filter(value, COMMIT);
		else if (!strcmp(name, "source-filter"))
			repo->source_filter = cgit_new_filter(value, SOURCE);
		else if (!strcmp(name, "email-filter"))
			repo->email_filter = cgit_new_filter(value, EMAIL);
	}
}

int cmd_main(int argc, const char **argv)
{
	struct strbuf cache_key = STRBUF_INIT;
	const char *path;
	int err, ttl;

	isolate_git_environment();
	cgit_init_filters();
	atexit(cgit_cleanup_filters);
	// Registered second so it runs first, since exit is reached from error
	// paths and from the HEAD shortcut with a page still buffered.
	atexit(html_flush);
	set_die_routine(die_routine);

	prepare_context();
	cgit_repolist.length = 0;
	cgit_repolist.count = 0;
	cgit_repolist.repos = NULL;

	parse_args(argc, argv);
	config_file_parse(cgit_expand_macros(ctx.env.cgit_config), apply_config);
	ctx.repo = NULL;
	http_parse_querystring(ctx.qry.raw, apply_query_param);

	if (!ctx.cfg.virtual_root && ctx.cfg.script_name)
		ctx.cfg.virtual_root = cgit_ensure_end(ctx.cfg.script_name, '/');

	// Falling back to PATH_INFO lets cgit serve virtual urls without a
	// rewrite rule in the web server, and folding it into the raw query
	// string keeps it part of the cache key.
	path = ctx.env.path_info;
	if (!ctx.qry.url && path) {
		// Stripped like the url parameter and for the same reason, so
		// a request for //example.com cannot turn into a link off site.
		while (*path == '/')
			path++;
		ctx.qry.url = xstrdup(path);
		if (ctx.qry.raw) {
			char *path_and_query = cgit_fmtalloc("%s?%s", path, ctx.qry.raw);
			free(ctx.qry.raw);
			ctx.qry.raw = path_and_query;
		} else
			ctx.qry.raw = xstrdup(ctx.qry.url);
		cgit_parse_url(ctx.qry.url);
	}

	authenticate_cookie();

	ttl = calc_ttl();
	// An unauthenticated request gets a body meant for one visitor, and a
	// HEAD request stops after the headers.
	if (!ctx.env.authenticated ||
	    (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")))
		ctx.cfg.cache_size = 0;

	build_cache_key(&cache_key);
	err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
			    cache_key.buf, ttl, process_request);
	strbuf_release(&cache_key);

	cgit_cleanup_filters();
	if (err)
		cgit_print_error("Error processing page: %s (%d)",
				 strerror(err), err);
	return err;
}