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