]> git.ipfire.org Git - thirdparty/git.git/blob - http-backend.c
Merge branch 'js/empty-index-fixes'
[thirdparty/git.git] / http-backend.c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "git-zlib.h"
6 #include "hex.h"
7 #include "path.h"
8 #include "repository.h"
9 #include "refs.h"
10 #include "pkt-line.h"
11 #include "object.h"
12 #include "tag.h"
13 #include "exec-cmd.h"
14 #include "run-command.h"
15 #include "string-list.h"
16 #include "url.h"
17 #include "strvec.h"
18 #include "packfile.h"
19 #include "object-store-ll.h"
20 #include "protocol.h"
21 #include "date.h"
22 #include "wrapper.h"
23 #include "write-or-die.h"
24
25 static const char content_type[] = "Content-Type";
26 static const char content_length[] = "Content-Length";
27 static const char last_modified[] = "Last-Modified";
28 static int getanyfile = 1;
29 static unsigned long max_request_buffer = 10 * 1024 * 1024;
30
31 static struct string_list *query_params;
32
33 struct rpc_service {
34 const char *name;
35 const char *config_name;
36 unsigned buffer_input : 1;
37 signed enabled : 2;
38 };
39
40 static struct rpc_service rpc_service[] = {
41 { "upload-pack", "uploadpack", 1, 1 },
42 { "receive-pack", "receivepack", 0, -1 },
43 };
44
45 static struct string_list *get_parameters(void)
46 {
47 if (!query_params) {
48 const char *query = getenv("QUERY_STRING");
49
50 CALLOC_ARRAY(query_params, 1);
51 while (query && *query) {
52 char *name = url_decode_parameter_name(&query);
53 char *value = url_decode_parameter_value(&query);
54 struct string_list_item *i;
55
56 i = string_list_lookup(query_params, name);
57 if (!i)
58 i = string_list_insert(query_params, name);
59 else
60 free(i->util);
61 i->util = value;
62 }
63 }
64 return query_params;
65 }
66
67 static const char *get_parameter(const char *name)
68 {
69 struct string_list_item *i;
70 i = string_list_lookup(get_parameters(), name);
71 return i ? i->util : NULL;
72 }
73
74 __attribute__((format (printf, 2, 3)))
75 static void format_write(int fd, const char *fmt, ...)
76 {
77 static char buffer[1024];
78
79 va_list args;
80 unsigned n;
81
82 va_start(args, fmt);
83 n = vsnprintf(buffer, sizeof(buffer), fmt, args);
84 va_end(args);
85 if (n >= sizeof(buffer))
86 die("protocol error: impossibly long line");
87
88 write_or_die(fd, buffer, n);
89 }
90
91 static void http_status(struct strbuf *hdr, unsigned code, const char *msg)
92 {
93 strbuf_addf(hdr, "Status: %u %s\r\n", code, msg);
94 }
95
96 static void hdr_str(struct strbuf *hdr, const char *name, const char *value)
97 {
98 strbuf_addf(hdr, "%s: %s\r\n", name, value);
99 }
100
101 static void hdr_int(struct strbuf *hdr, const char *name, uintmax_t value)
102 {
103 strbuf_addf(hdr, "%s: %" PRIuMAX "\r\n", name, value);
104 }
105
106 static void hdr_date(struct strbuf *hdr, const char *name, timestamp_t when)
107 {
108 const char *value = show_date(when, 0, DATE_MODE(RFC2822));
109 hdr_str(hdr, name, value);
110 }
111
112 static void hdr_nocache(struct strbuf *hdr)
113 {
114 hdr_str(hdr, "Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
115 hdr_str(hdr, "Pragma", "no-cache");
116 hdr_str(hdr, "Cache-Control", "no-cache, max-age=0, must-revalidate");
117 }
118
119 static void hdr_cache_forever(struct strbuf *hdr)
120 {
121 timestamp_t now = time(NULL);
122 hdr_date(hdr, "Date", now);
123 hdr_date(hdr, "Expires", now + 31536000);
124 hdr_str(hdr, "Cache-Control", "public, max-age=31536000");
125 }
126
127 static void end_headers(struct strbuf *hdr)
128 {
129 strbuf_add(hdr, "\r\n", 2);
130 write_or_die(1, hdr->buf, hdr->len);
131 strbuf_release(hdr);
132 }
133
134 __attribute__((format (printf, 2, 3)))
135 static NORETURN void not_found(struct strbuf *hdr, const char *err, ...)
136 {
137 va_list params;
138
139 http_status(hdr, 404, "Not Found");
140 hdr_nocache(hdr);
141 end_headers(hdr);
142
143 va_start(params, err);
144 if (err && *err)
145 vfprintf(stderr, err, params);
146 va_end(params);
147 exit(0);
148 }
149
150 __attribute__((format (printf, 2, 3)))
151 static NORETURN void forbidden(struct strbuf *hdr, const char *err, ...)
152 {
153 va_list params;
154
155 http_status(hdr, 403, "Forbidden");
156 hdr_nocache(hdr);
157 end_headers(hdr);
158
159 va_start(params, err);
160 if (err && *err)
161 vfprintf(stderr, err, params);
162 va_end(params);
163 exit(0);
164 }
165
166 static void select_getanyfile(struct strbuf *hdr)
167 {
168 if (!getanyfile)
169 forbidden(hdr, "Unsupported service: getanyfile");
170 }
171
172 static void send_strbuf(struct strbuf *hdr,
173 const char *type, struct strbuf *buf)
174 {
175 hdr_int(hdr, content_length, buf->len);
176 hdr_str(hdr, content_type, type);
177 end_headers(hdr);
178 write_or_die(1, buf->buf, buf->len);
179 }
180
181 static void send_local_file(struct strbuf *hdr, const char *the_type,
182 const char *name)
183 {
184 char *p = git_pathdup("%s", name);
185 size_t buf_alloc = 8192;
186 char *buf = xmalloc(buf_alloc);
187 int fd;
188 struct stat sb;
189
190 fd = open(p, O_RDONLY);
191 if (fd < 0)
192 not_found(hdr, "Cannot open '%s': %s", p, strerror(errno));
193 if (fstat(fd, &sb) < 0)
194 die_errno("Cannot stat '%s'", p);
195
196 hdr_int(hdr, content_length, sb.st_size);
197 hdr_str(hdr, content_type, the_type);
198 hdr_date(hdr, last_modified, sb.st_mtime);
199 end_headers(hdr);
200
201 for (;;) {
202 ssize_t n = xread(fd, buf, buf_alloc);
203 if (n < 0)
204 die_errno("Cannot read '%s'", p);
205 if (!n)
206 break;
207 write_or_die(1, buf, n);
208 }
209 close(fd);
210 free(buf);
211 free(p);
212 }
213
214 static void get_text_file(struct strbuf *hdr, char *name)
215 {
216 select_getanyfile(hdr);
217 hdr_nocache(hdr);
218 send_local_file(hdr, "text/plain", name);
219 }
220
221 static void get_loose_object(struct strbuf *hdr, char *name)
222 {
223 select_getanyfile(hdr);
224 hdr_cache_forever(hdr);
225 send_local_file(hdr, "application/x-git-loose-object", name);
226 }
227
228 static void get_pack_file(struct strbuf *hdr, char *name)
229 {
230 select_getanyfile(hdr);
231 hdr_cache_forever(hdr);
232 send_local_file(hdr, "application/x-git-packed-objects", name);
233 }
234
235 static void get_idx_file(struct strbuf *hdr, char *name)
236 {
237 select_getanyfile(hdr);
238 hdr_cache_forever(hdr);
239 send_local_file(hdr, "application/x-git-packed-objects-toc", name);
240 }
241
242 static void http_config(void)
243 {
244 int i, value = 0;
245 struct strbuf var = STRBUF_INIT;
246
247 git_config_get_bool("http.getanyfile", &getanyfile);
248 git_config_get_ulong("http.maxrequestbuffer", &max_request_buffer);
249
250 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
251 struct rpc_service *svc = &rpc_service[i];
252 strbuf_addf(&var, "http.%s", svc->config_name);
253 if (!git_config_get_bool(var.buf, &value))
254 svc->enabled = value;
255 strbuf_reset(&var);
256 }
257
258 strbuf_release(&var);
259 }
260
261 static struct rpc_service *select_service(struct strbuf *hdr, const char *name)
262 {
263 const char *svc_name;
264 struct rpc_service *svc = NULL;
265 int i;
266
267 if (!skip_prefix(name, "git-", &svc_name))
268 forbidden(hdr, "Unsupported service: '%s'", name);
269
270 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
271 struct rpc_service *s = &rpc_service[i];
272 if (!strcmp(s->name, svc_name)) {
273 svc = s;
274 break;
275 }
276 }
277
278 if (!svc)
279 forbidden(hdr, "Unsupported service: '%s'", name);
280
281 if (svc->enabled < 0) {
282 const char *user = getenv("REMOTE_USER");
283 svc->enabled = (user && *user) ? 1 : 0;
284 }
285 if (!svc->enabled)
286 forbidden(hdr, "Service not enabled: '%s'", svc->name);
287 return svc;
288 }
289
290 static void write_to_child(int out, const unsigned char *buf, ssize_t len, const char *prog_name)
291 {
292 if (write_in_full(out, buf, len) < 0)
293 die("unable to write to '%s'", prog_name);
294 }
295
296 /*
297 * This is basically strbuf_read(), except that if we
298 * hit max_request_buffer we die (we'd rather reject a
299 * maliciously large request than chew up infinite memory).
300 */
301 static ssize_t read_request_eof(int fd, unsigned char **out)
302 {
303 size_t len = 0, alloc = 8192;
304 unsigned char *buf = xmalloc(alloc);
305
306 if (max_request_buffer < alloc)
307 max_request_buffer = alloc;
308
309 while (1) {
310 ssize_t cnt;
311
312 cnt = read_in_full(fd, buf + len, alloc - len);
313 if (cnt < 0) {
314 free(buf);
315 return -1;
316 }
317
318 /* partial read from read_in_full means we hit EOF */
319 len += cnt;
320 if (len < alloc) {
321 *out = buf;
322 return len;
323 }
324
325 /* otherwise, grow and try again (if we can) */
326 if (alloc == max_request_buffer)
327 die("request was larger than our maximum size (%lu);"
328 " try setting GIT_HTTP_MAX_REQUEST_BUFFER",
329 max_request_buffer);
330
331 alloc = alloc_nr(alloc);
332 if (alloc > max_request_buffer)
333 alloc = max_request_buffer;
334 REALLOC_ARRAY(buf, alloc);
335 }
336 }
337
338 static ssize_t read_request_fixed_len(int fd, ssize_t req_len, unsigned char **out)
339 {
340 unsigned char *buf = NULL;
341 ssize_t cnt = 0;
342
343 if (max_request_buffer < req_len) {
344 die("request was larger than our maximum size (%lu): "
345 "%" PRIuMAX "; try setting GIT_HTTP_MAX_REQUEST_BUFFER",
346 max_request_buffer, (uintmax_t)req_len);
347 }
348
349 buf = xmalloc(req_len);
350 cnt = read_in_full(fd, buf, req_len);
351 if (cnt < 0) {
352 free(buf);
353 return -1;
354 }
355 *out = buf;
356 return cnt;
357 }
358
359 static ssize_t get_content_length(void)
360 {
361 ssize_t val = -1;
362 const char *str = getenv("CONTENT_LENGTH");
363
364 if (str && *str && !git_parse_ssize_t(str, &val))
365 die("failed to parse CONTENT_LENGTH: %s", str);
366 return val;
367 }
368
369 static ssize_t read_request(int fd, unsigned char **out, ssize_t req_len)
370 {
371 if (req_len < 0)
372 return read_request_eof(fd, out);
373 else
374 return read_request_fixed_len(fd, req_len, out);
375 }
376
377 static void inflate_request(const char *prog_name, int out, int buffer_input, ssize_t req_len)
378 {
379 git_zstream stream;
380 unsigned char *full_request = NULL;
381 unsigned char in_buf[8192];
382 unsigned char out_buf[8192];
383 unsigned long cnt = 0;
384 int req_len_defined = req_len >= 0;
385 size_t req_remaining_len = req_len;
386
387 memset(&stream, 0, sizeof(stream));
388 git_inflate_init_gzip_only(&stream);
389
390 while (1) {
391 ssize_t n;
392
393 if (buffer_input) {
394 if (full_request)
395 n = 0; /* nothing left to read */
396 else
397 n = read_request(0, &full_request, req_len);
398 stream.next_in = full_request;
399 } else {
400 ssize_t buffer_len;
401 if (req_len_defined && req_remaining_len <= sizeof(in_buf))
402 buffer_len = req_remaining_len;
403 else
404 buffer_len = sizeof(in_buf);
405 n = xread(0, in_buf, buffer_len);
406 stream.next_in = in_buf;
407 if (req_len_defined && n > 0)
408 req_remaining_len -= n;
409 }
410
411 if (n <= 0)
412 die("request ended in the middle of the gzip stream");
413 stream.avail_in = n;
414
415 while (0 < stream.avail_in) {
416 int ret;
417
418 stream.next_out = out_buf;
419 stream.avail_out = sizeof(out_buf);
420
421 ret = git_inflate(&stream, Z_NO_FLUSH);
422 if (ret != Z_OK && ret != Z_STREAM_END)
423 die("zlib error inflating request, result %d", ret);
424
425 n = stream.total_out - cnt;
426 write_to_child(out, out_buf, stream.total_out - cnt, prog_name);
427 cnt = stream.total_out;
428
429 if (ret == Z_STREAM_END)
430 goto done;
431 }
432 }
433
434 done:
435 git_inflate_end(&stream);
436 close(out);
437 free(full_request);
438 }
439
440 static void copy_request(const char *prog_name, int out, ssize_t req_len)
441 {
442 unsigned char *buf;
443 ssize_t n = read_request(0, &buf, req_len);
444 if (n < 0)
445 die_errno("error reading request body");
446 write_to_child(out, buf, n, prog_name);
447 close(out);
448 free(buf);
449 }
450
451 static void pipe_fixed_length(const char *prog_name, int out, size_t req_len)
452 {
453 unsigned char buf[8192];
454 size_t remaining_len = req_len;
455
456 while (remaining_len > 0) {
457 size_t chunk_length = remaining_len > sizeof(buf) ? sizeof(buf) : remaining_len;
458 ssize_t n = xread(0, buf, chunk_length);
459 if (n < 0)
460 die_errno("Reading request failed");
461 write_to_child(out, buf, n, prog_name);
462 remaining_len -= n;
463 }
464
465 close(out);
466 }
467
468 static void run_service(const char **argv, int buffer_input)
469 {
470 const char *encoding = getenv("HTTP_CONTENT_ENCODING");
471 const char *user = getenv("REMOTE_USER");
472 const char *host = getenv("REMOTE_ADDR");
473 int gzipped_request = 0;
474 struct child_process cld = CHILD_PROCESS_INIT;
475 ssize_t req_len = get_content_length();
476
477 if (encoding && (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")))
478 gzipped_request = 1;
479
480 if (!user || !*user)
481 user = "anonymous";
482 if (!host || !*host)
483 host = "(none)";
484
485 if (!getenv("GIT_COMMITTER_NAME"))
486 strvec_pushf(&cld.env, "GIT_COMMITTER_NAME=%s", user);
487 if (!getenv("GIT_COMMITTER_EMAIL"))
488 strvec_pushf(&cld.env,
489 "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
490
491 strvec_pushv(&cld.args, argv);
492 if (buffer_input || gzipped_request || req_len >= 0)
493 cld.in = -1;
494 cld.git_cmd = 1;
495 cld.clean_on_exit = 1;
496 cld.wait_after_clean = 1;
497 if (start_command(&cld))
498 exit(1);
499
500 close(1);
501 if (gzipped_request)
502 inflate_request(argv[0], cld.in, buffer_input, req_len);
503 else if (buffer_input)
504 copy_request(argv[0], cld.in, req_len);
505 else if (req_len >= 0)
506 pipe_fixed_length(argv[0], cld.in, req_len);
507 else
508 close(0);
509
510 if (finish_command(&cld))
511 exit(1);
512 }
513
514 static int show_text_ref(const char *name, const struct object_id *oid,
515 int flag UNUSED, void *cb_data)
516 {
517 const char *name_nons = strip_namespace(name);
518 struct strbuf *buf = cb_data;
519 struct object *o = parse_object(the_repository, oid);
520 if (!o)
521 return 0;
522
523 strbuf_addf(buf, "%s\t%s\n", oid_to_hex(oid), name_nons);
524 if (o->type == OBJ_TAG) {
525 o = deref_tag(the_repository, o, name, 0);
526 if (!o)
527 return 0;
528 strbuf_addf(buf, "%s\t%s^{}\n", oid_to_hex(&o->oid),
529 name_nons);
530 }
531 return 0;
532 }
533
534 static void get_info_refs(struct strbuf *hdr, char *arg UNUSED)
535 {
536 const char *service_name = get_parameter("service");
537 struct strbuf buf = STRBUF_INIT;
538
539 hdr_nocache(hdr);
540
541 if (service_name) {
542 const char *argv[] = {NULL /* service name */,
543 "--http-backend-info-refs",
544 ".", NULL};
545 struct rpc_service *svc = select_service(hdr, service_name);
546
547 strbuf_addf(&buf, "application/x-git-%s-advertisement",
548 svc->name);
549 hdr_str(hdr, content_type, buf.buf);
550 end_headers(hdr);
551
552
553 if (determine_protocol_version_server() != protocol_v2) {
554 packet_write_fmt(1, "# service=git-%s\n", svc->name);
555 packet_flush(1);
556 }
557
558 argv[0] = svc->name;
559 run_service(argv, 0);
560
561 } else {
562 select_getanyfile(hdr);
563 for_each_namespaced_ref(show_text_ref, &buf);
564 send_strbuf(hdr, "text/plain", &buf);
565 }
566 strbuf_release(&buf);
567 }
568
569 static int show_head_ref(const char *refname, const struct object_id *oid,
570 int flag, void *cb_data)
571 {
572 struct strbuf *buf = cb_data;
573
574 if (flag & REF_ISSYMREF) {
575 const char *target = resolve_ref_unsafe(refname,
576 RESOLVE_REF_READING,
577 NULL, NULL);
578
579 if (target)
580 strbuf_addf(buf, "ref: %s\n", strip_namespace(target));
581 } else {
582 strbuf_addf(buf, "%s\n", oid_to_hex(oid));
583 }
584
585 return 0;
586 }
587
588 static void get_head(struct strbuf *hdr, char *arg UNUSED)
589 {
590 struct strbuf buf = STRBUF_INIT;
591
592 select_getanyfile(hdr);
593 head_ref_namespaced(show_head_ref, &buf);
594 send_strbuf(hdr, "text/plain", &buf);
595 strbuf_release(&buf);
596 }
597
598 static void get_info_packs(struct strbuf *hdr, char *arg UNUSED)
599 {
600 size_t objdirlen = strlen(get_object_directory());
601 struct strbuf buf = STRBUF_INIT;
602 struct packed_git *p;
603 size_t cnt = 0;
604
605 select_getanyfile(hdr);
606 for (p = get_all_packs(the_repository); p; p = p->next) {
607 if (p->pack_local)
608 cnt++;
609 }
610
611 strbuf_grow(&buf, cnt * 53 + 2);
612 for (p = get_all_packs(the_repository); p; p = p->next) {
613 if (p->pack_local)
614 strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
615 }
616 strbuf_addch(&buf, '\n');
617
618 hdr_nocache(hdr);
619 send_strbuf(hdr, "text/plain; charset=utf-8", &buf);
620 strbuf_release(&buf);
621 }
622
623 static void check_content_type(struct strbuf *hdr, const char *accepted_type)
624 {
625 const char *actual_type = getenv("CONTENT_TYPE");
626
627 if (!actual_type)
628 actual_type = "";
629
630 if (strcmp(actual_type, accepted_type)) {
631 http_status(hdr, 415, "Unsupported Media Type");
632 hdr_nocache(hdr);
633 end_headers(hdr);
634 format_write(1,
635 "Expected POST with Content-Type '%s',"
636 " but received '%s' instead.\n",
637 accepted_type, actual_type);
638 exit(0);
639 }
640 }
641
642 static void service_rpc(struct strbuf *hdr, char *service_name)
643 {
644 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
645 struct rpc_service *svc = select_service(hdr, service_name);
646 struct strbuf buf = STRBUF_INIT;
647
648 strbuf_reset(&buf);
649 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
650 check_content_type(hdr, buf.buf);
651
652 hdr_nocache(hdr);
653
654 strbuf_reset(&buf);
655 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
656 hdr_str(hdr, content_type, buf.buf);
657
658 end_headers(hdr);
659
660 argv[0] = svc->name;
661 run_service(argv, svc->buffer_input);
662 strbuf_release(&buf);
663 }
664
665 static int dead;
666 static NORETURN void die_webcgi(const char *err, va_list params)
667 {
668 if (dead <= 1) {
669 struct strbuf hdr = STRBUF_INIT;
670 report_fn die_message_fn = get_die_message_routine();
671
672 die_message_fn(err, params);
673
674 http_status(&hdr, 500, "Internal Server Error");
675 hdr_nocache(&hdr);
676 end_headers(&hdr);
677 }
678 exit(0); /* we successfully reported a failure ;-) */
679 }
680
681 static int die_webcgi_recursing(void)
682 {
683 return dead++ > 1;
684 }
685
686 static char* getdir(void)
687 {
688 struct strbuf buf = STRBUF_INIT;
689 char *pathinfo = getenv("PATH_INFO");
690 char *root = getenv("GIT_PROJECT_ROOT");
691 char *path = getenv("PATH_TRANSLATED");
692
693 if (root && *root) {
694 if (!pathinfo || !*pathinfo)
695 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
696 if (daemon_avoid_alias(pathinfo))
697 die("'%s': aliased", pathinfo);
698 end_url_with_slash(&buf, root);
699 if (pathinfo[0] == '/')
700 pathinfo++;
701 strbuf_addstr(&buf, pathinfo);
702 return strbuf_detach(&buf, NULL);
703 } else if (path && *path) {
704 return xstrdup(path);
705 } else
706 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
707 return NULL;
708 }
709
710 static struct service_cmd {
711 const char *method;
712 const char *pattern;
713 void (*imp)(struct strbuf *, char *);
714 } services[] = {
715 {"GET", "/HEAD$", get_head},
716 {"GET", "/info/refs$", get_info_refs},
717 {"GET", "/objects/info/alternates$", get_text_file},
718 {"GET", "/objects/info/http-alternates$", get_text_file},
719 {"GET", "/objects/info/packs$", get_info_packs},
720 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
721 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{62}$", get_loose_object},
722 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
723 {"GET", "/objects/pack/pack-[0-9a-f]{64}\\.pack$", get_pack_file},
724 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
725 {"GET", "/objects/pack/pack-[0-9a-f]{64}\\.idx$", get_idx_file},
726
727 {"POST", "/git-upload-pack$", service_rpc},
728 {"POST", "/git-receive-pack$", service_rpc}
729 };
730
731 static int bad_request(struct strbuf *hdr, const struct service_cmd *c)
732 {
733 const char *proto = getenv("SERVER_PROTOCOL");
734
735 if (proto && !strcmp(proto, "HTTP/1.1")) {
736 http_status(hdr, 405, "Method Not Allowed");
737 hdr_str(hdr, "Allow",
738 !strcmp(c->method, "GET") ? "GET, HEAD" : c->method);
739 } else
740 http_status(hdr, 400, "Bad Request");
741 hdr_nocache(hdr);
742 end_headers(hdr);
743 return 0;
744 }
745
746 int cmd_main(int argc UNUSED, const char **argv UNUSED)
747 {
748 char *method = getenv("REQUEST_METHOD");
749 const char *proto_header;
750 char *dir;
751 struct service_cmd *cmd = NULL;
752 char *cmd_arg = NULL;
753 int i;
754 struct strbuf hdr = STRBUF_INIT;
755
756 set_die_routine(die_webcgi);
757 set_die_is_recursing_routine(die_webcgi_recursing);
758
759 if (!method)
760 die("No REQUEST_METHOD from server");
761 if (!strcmp(method, "HEAD"))
762 method = "GET";
763 dir = getdir();
764
765 for (i = 0; i < ARRAY_SIZE(services); i++) {
766 struct service_cmd *c = &services[i];
767 regex_t re;
768 regmatch_t out[1];
769 int ret;
770
771 if (regcomp(&re, c->pattern, REG_EXTENDED))
772 die("Bogus regex in service table: %s", c->pattern);
773 ret = regexec(&re, dir, 1, out, 0);
774 regfree(&re);
775
776 if (!ret) {
777 size_t n;
778
779 if (strcmp(method, c->method))
780 return bad_request(&hdr, c);
781
782 cmd = c;
783 n = out[0].rm_eo - out[0].rm_so;
784 cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);
785 dir[out[0].rm_so] = 0;
786 break;
787 }
788 }
789
790 if (!cmd)
791 not_found(&hdr, "Request not supported: '%s'", dir);
792
793 setup_path();
794 if (!enter_repo(dir, 0))
795 not_found(&hdr, "Not a git repository: '%s'", dir);
796 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
797 access("git-daemon-export-ok", F_OK) )
798 not_found(&hdr, "Repository not exported: '%s'", dir);
799 free(dir);
800
801 http_config();
802 max_request_buffer = git_env_ulong("GIT_HTTP_MAX_REQUEST_BUFFER",
803 max_request_buffer);
804 proto_header = getenv("HTTP_GIT_PROTOCOL");
805 if (proto_header)
806 setenv(GIT_PROTOCOL_ENVIRONMENT, proto_header, 0);
807
808 cmd->imp(&hdr, cmd_arg);
809 free(cmd_arg);
810 return 0;
811 }