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