8 #include "run-command.h"
9 #include "string-list.h"
11 #include "argv-array.h"
14 static const char content_type[] = "Content-Type";
15 static const char content_length[] = "Content-Length";
16 static const char last_modified[] = "Last-Modified";
17 static int getanyfile = 1;
18 static unsigned long max_request_buffer = 10 * 1024 * 1024;
20 static struct string_list *query_params;
24 const char *config_name;
25 unsigned buffer_input : 1;
29 static struct rpc_service rpc_service[] = {
30 { "upload-pack", "uploadpack", 1, 1 },
31 { "receive-pack", "receivepack", 0, -1 },
34 static struct string_list *get_parameters(void)
37 const char *query = getenv("QUERY_STRING");
39 query_params = xcalloc(1, sizeof(*query_params));
40 while (query && *query) {
41 char *name = url_decode_parameter_name(&query);
42 char *value = url_decode_parameter_value(&query);
43 struct string_list_item *i;
45 i = string_list_lookup(query_params, name);
47 i = string_list_insert(query_params, name);
56 static const char *get_parameter(const char *name)
58 struct string_list_item *i;
59 i = string_list_lookup(get_parameters(), name);
60 return i ? i->util : NULL;
63 __attribute__((format (printf, 2, 3)))
64 static void format_write(int fd, const char *fmt, ...)
66 static char buffer[1024];
72 n = vsnprintf(buffer, sizeof(buffer), fmt, args);
74 if (n >= sizeof(buffer))
75 die("protocol error: impossibly long line");
77 write_or_die(fd, buffer, n);
80 static void http_status(struct strbuf *hdr, unsigned code, const char *msg)
82 strbuf_addf(hdr, "Status: %u %s\r\n", code, msg);
85 static void hdr_str(struct strbuf *hdr, const char *name, const char *value)
87 strbuf_addf(hdr, "%s: %s\r\n", name, value);
90 static void hdr_int(struct strbuf *hdr, const char *name, uintmax_t value)
92 strbuf_addf(hdr, "%s: %" PRIuMAX "\r\n", name, value);
95 static void hdr_date(struct strbuf *hdr, const char *name, timestamp_t when)
97 const char *value = show_date(when, 0, DATE_MODE(RFC2822));
98 hdr_str(hdr, name, value);
101 static void hdr_nocache(struct strbuf *hdr)
103 hdr_str(hdr, "Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
104 hdr_str(hdr, "Pragma", "no-cache");
105 hdr_str(hdr, "Cache-Control", "no-cache, max-age=0, must-revalidate");
108 static void hdr_cache_forever(struct strbuf *hdr)
110 timestamp_t now = time(NULL);
111 hdr_date(hdr, "Date", now);
112 hdr_date(hdr, "Expires", now + 31536000);
113 hdr_str(hdr, "Cache-Control", "public, max-age=31536000");
116 static void end_headers(struct strbuf *hdr)
118 strbuf_add(hdr, "\r\n", 2);
119 write_or_die(1, hdr->buf, hdr->len);
123 __attribute__((format (printf, 2, 3)))
124 static NORETURN void not_found(struct strbuf *hdr, const char *err, ...)
128 http_status(hdr, 404, "Not Found");
132 va_start(params, err);
134 vfprintf(stderr, err, params);
139 __attribute__((format (printf, 2, 3)))
140 static NORETURN void forbidden(struct strbuf *hdr, const char *err, ...)
144 http_status(hdr, 403, "Forbidden");
148 va_start(params, err);
150 vfprintf(stderr, err, params);
155 static void select_getanyfile(struct strbuf *hdr)
158 forbidden(hdr, "Unsupported service: getanyfile");
161 static void send_strbuf(struct strbuf *hdr,
162 const char *type, struct strbuf *buf)
164 hdr_int(hdr, content_length, buf->len);
165 hdr_str(hdr, content_type, type);
167 write_or_die(1, buf->buf, buf->len);
170 static void send_local_file(struct strbuf *hdr, const char *the_type,
173 char *p = git_pathdup("%s", name);
174 size_t buf_alloc = 8192;
175 char *buf = xmalloc(buf_alloc);
179 fd = open(p, O_RDONLY);
181 not_found(hdr, "Cannot open '%s': %s", p, strerror(errno));
182 if (fstat(fd, &sb) < 0)
183 die_errno("Cannot stat '%s'", p);
185 hdr_int(hdr, content_length, sb.st_size);
186 hdr_str(hdr, content_type, the_type);
187 hdr_date(hdr, last_modified, sb.st_mtime);
191 ssize_t n = xread(fd, buf, buf_alloc);
193 die_errno("Cannot read '%s'", p);
196 write_or_die(1, buf, n);
203 static void get_text_file(struct strbuf *hdr, char *name)
205 select_getanyfile(hdr);
207 send_local_file(hdr, "text/plain", name);
210 static void get_loose_object(struct strbuf *hdr, char *name)
212 select_getanyfile(hdr);
213 hdr_cache_forever(hdr);
214 send_local_file(hdr, "application/x-git-loose-object", name);
217 static void get_pack_file(struct strbuf *hdr, char *name)
219 select_getanyfile(hdr);
220 hdr_cache_forever(hdr);
221 send_local_file(hdr, "application/x-git-packed-objects", name);
224 static void get_idx_file(struct strbuf *hdr, char *name)
226 select_getanyfile(hdr);
227 hdr_cache_forever(hdr);
228 send_local_file(hdr, "application/x-git-packed-objects-toc", name);
231 static void http_config(void)
234 struct strbuf var = STRBUF_INIT;
236 git_config_get_bool("http.getanyfile", &getanyfile);
237 git_config_get_ulong("http.maxrequestbuffer", &max_request_buffer);
239 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
240 struct rpc_service *svc = &rpc_service[i];
241 strbuf_addf(&var, "http.%s", svc->config_name);
242 if (!git_config_get_bool(var.buf, &value))
243 svc->enabled = value;
247 strbuf_release(&var);
250 static struct rpc_service *select_service(struct strbuf *hdr, const char *name)
252 const char *svc_name;
253 struct rpc_service *svc = NULL;
256 if (!skip_prefix(name, "git-", &svc_name))
257 forbidden(hdr, "Unsupported service: '%s'", name);
259 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
260 struct rpc_service *s = &rpc_service[i];
261 if (!strcmp(s->name, svc_name)) {
268 forbidden(hdr, "Unsupported service: '%s'", name);
270 if (svc->enabled < 0) {
271 const char *user = getenv("REMOTE_USER");
272 svc->enabled = (user && *user) ? 1 : 0;
275 forbidden(hdr, "Service not enabled: '%s'", svc->name);
280 * This is basically strbuf_read(), except that if we
281 * hit max_request_buffer we die (we'd rather reject a
282 * maliciously large request than chew up infinite memory).
284 static ssize_t read_request(int fd, unsigned char **out)
286 size_t len = 0, alloc = 8192;
287 unsigned char *buf = xmalloc(alloc);
289 if (max_request_buffer < alloc)
290 max_request_buffer = alloc;
295 cnt = read_in_full(fd, buf + len, alloc - len);
301 /* partial read from read_in_full means we hit EOF */
308 /* otherwise, grow and try again (if we can) */
309 if (alloc == max_request_buffer)
310 die("request was larger than our maximum size (%lu);"
311 " try setting GIT_HTTP_MAX_REQUEST_BUFFER",
314 alloc = alloc_nr(alloc);
315 if (alloc > max_request_buffer)
316 alloc = max_request_buffer;
317 REALLOC_ARRAY(buf, alloc);
321 static void inflate_request(const char *prog_name, int out, int buffer_input)
324 unsigned char *full_request = NULL;
325 unsigned char in_buf[8192];
326 unsigned char out_buf[8192];
327 unsigned long cnt = 0;
329 memset(&stream, 0, sizeof(stream));
330 git_inflate_init_gzip_only(&stream);
337 n = 0; /* nothing left to read */
339 n = read_request(0, &full_request);
340 stream.next_in = full_request;
342 n = xread(0, in_buf, sizeof(in_buf));
343 stream.next_in = in_buf;
347 die("request ended in the middle of the gzip stream");
350 while (0 < stream.avail_in) {
353 stream.next_out = out_buf;
354 stream.avail_out = sizeof(out_buf);
356 ret = git_inflate(&stream, Z_NO_FLUSH);
357 if (ret != Z_OK && ret != Z_STREAM_END)
358 die("zlib error inflating request, result %d", ret);
360 n = stream.total_out - cnt;
361 if (write_in_full(out, out_buf, n) < 0)
362 die("%s aborted reading request", prog_name);
365 if (ret == Z_STREAM_END)
371 git_inflate_end(&stream);
376 static void copy_request(const char *prog_name, int out)
379 ssize_t n = read_request(0, &buf);
381 die_errno("error reading request body");
382 if (write_in_full(out, buf, n) < 0)
383 die("%s aborted reading request", prog_name);
388 static void run_service(const char **argv, int buffer_input)
390 const char *encoding = getenv("HTTP_CONTENT_ENCODING");
391 const char *user = getenv("REMOTE_USER");
392 const char *host = getenv("REMOTE_ADDR");
393 int gzipped_request = 0;
394 struct child_process cld = CHILD_PROCESS_INIT;
396 if (encoding && !strcmp(encoding, "gzip"))
398 else if (encoding && !strcmp(encoding, "x-gzip"))
406 if (!getenv("GIT_COMMITTER_NAME"))
407 argv_array_pushf(&cld.env_array, "GIT_COMMITTER_NAME=%s", user);
408 if (!getenv("GIT_COMMITTER_EMAIL"))
409 argv_array_pushf(&cld.env_array,
410 "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
413 if (buffer_input || gzipped_request)
416 if (start_command(&cld))
421 inflate_request(argv[0], cld.in, buffer_input);
422 else if (buffer_input)
423 copy_request(argv[0], cld.in);
427 if (finish_command(&cld))
431 static int show_text_ref(const char *name, const struct object_id *oid,
432 int flag, void *cb_data)
434 const char *name_nons = strip_namespace(name);
435 struct strbuf *buf = cb_data;
436 struct object *o = parse_object(oid);
440 strbuf_addf(buf, "%s\t%s\n", oid_to_hex(oid), name_nons);
441 if (o->type == OBJ_TAG) {
442 o = deref_tag(o, name, 0);
445 strbuf_addf(buf, "%s\t%s^{}\n", oid_to_hex(&o->oid),
451 static void get_info_refs(struct strbuf *hdr, char *arg)
453 const char *service_name = get_parameter("service");
454 struct strbuf buf = STRBUF_INIT;
459 const char *argv[] = {NULL /* service name */,
460 "--stateless-rpc", "--advertise-refs",
462 struct rpc_service *svc = select_service(hdr, service_name);
464 strbuf_addf(&buf, "application/x-git-%s-advertisement",
466 hdr_str(hdr, content_type, buf.buf);
469 packet_write_fmt(1, "# service=git-%s\n", svc->name);
473 run_service(argv, 0);
476 select_getanyfile(hdr);
477 for_each_namespaced_ref(show_text_ref, &buf);
478 send_strbuf(hdr, "text/plain", &buf);
480 strbuf_release(&buf);
483 static int show_head_ref(const char *refname, const struct object_id *oid,
484 int flag, void *cb_data)
486 struct strbuf *buf = cb_data;
488 if (flag & REF_ISSYMREF) {
489 struct object_id unused;
490 const char *target = resolve_ref_unsafe(refname,
495 strbuf_addf(buf, "ref: %s\n", strip_namespace(target));
497 strbuf_addf(buf, "%s\n", oid_to_hex(oid));
503 static void get_head(struct strbuf *hdr, char *arg)
505 struct strbuf buf = STRBUF_INIT;
507 select_getanyfile(hdr);
508 head_ref_namespaced(show_head_ref, &buf);
509 send_strbuf(hdr, "text/plain", &buf);
510 strbuf_release(&buf);
513 static void get_info_packs(struct strbuf *hdr, char *arg)
515 size_t objdirlen = strlen(get_object_directory());
516 struct strbuf buf = STRBUF_INIT;
517 struct packed_git *p;
520 select_getanyfile(hdr);
521 prepare_packed_git();
522 for (p = packed_git; p; p = p->next) {
527 strbuf_grow(&buf, cnt * 53 + 2);
528 for (p = packed_git; p; p = p->next) {
530 strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
532 strbuf_addch(&buf, '\n');
535 send_strbuf(hdr, "text/plain; charset=utf-8", &buf);
536 strbuf_release(&buf);
539 static void check_content_type(struct strbuf *hdr, const char *accepted_type)
541 const char *actual_type = getenv("CONTENT_TYPE");
546 if (strcmp(actual_type, accepted_type)) {
547 http_status(hdr, 415, "Unsupported Media Type");
551 "Expected POST with Content-Type '%s',"
552 " but received '%s' instead.\n",
553 accepted_type, actual_type);
558 static void service_rpc(struct strbuf *hdr, char *service_name)
560 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
561 struct rpc_service *svc = select_service(hdr, service_name);
562 struct strbuf buf = STRBUF_INIT;
565 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
566 check_content_type(hdr, buf.buf);
571 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
572 hdr_str(hdr, content_type, buf.buf);
577 run_service(argv, svc->buffer_input);
578 strbuf_release(&buf);
582 static NORETURN void die_webcgi(const char *err, va_list params)
585 struct strbuf hdr = STRBUF_INIT;
587 vreportf("fatal: ", err, params);
589 http_status(&hdr, 500, "Internal Server Error");
593 exit(0); /* we successfully reported a failure ;-) */
596 static int die_webcgi_recursing(void)
601 static char* getdir(void)
603 struct strbuf buf = STRBUF_INIT;
604 char *pathinfo = getenv("PATH_INFO");
605 char *root = getenv("GIT_PROJECT_ROOT");
606 char *path = getenv("PATH_TRANSLATED");
609 if (!pathinfo || !*pathinfo)
610 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
611 if (daemon_avoid_alias(pathinfo))
612 die("'%s': aliased", pathinfo);
613 end_url_with_slash(&buf, root);
614 if (pathinfo[0] == '/')
616 strbuf_addstr(&buf, pathinfo);
617 return strbuf_detach(&buf, NULL);
618 } else if (path && *path) {
619 return xstrdup(path);
621 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
625 static struct service_cmd {
628 void (*imp)(struct strbuf *, char *);
630 {"GET", "/HEAD$", get_head},
631 {"GET", "/info/refs$", get_info_refs},
632 {"GET", "/objects/info/alternates$", get_text_file},
633 {"GET", "/objects/info/http-alternates$", get_text_file},
634 {"GET", "/objects/info/packs$", get_info_packs},
635 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
636 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
637 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
639 {"POST", "/git-upload-pack$", service_rpc},
640 {"POST", "/git-receive-pack$", service_rpc}
643 static int bad_request(struct strbuf *hdr, const struct service_cmd *c)
645 const char *proto = getenv("SERVER_PROTOCOL");
647 if (proto && !strcmp(proto, "HTTP/1.1")) {
648 http_status(hdr, 405, "Method Not Allowed");
649 hdr_str(hdr, "Allow",
650 !strcmp(c->method, "GET") ? "GET, HEAD" : c->method);
652 http_status(hdr, 400, "Bad Request");
658 int cmd_main(int argc, const char **argv)
660 char *method = getenv("REQUEST_METHOD");
662 struct service_cmd *cmd = NULL;
663 char *cmd_arg = NULL;
665 struct strbuf hdr = STRBUF_INIT;
667 set_die_routine(die_webcgi);
668 set_die_is_recursing_routine(die_webcgi_recursing);
671 die("No REQUEST_METHOD from server");
672 if (!strcmp(method, "HEAD"))
676 for (i = 0; i < ARRAY_SIZE(services); i++) {
677 struct service_cmd *c = &services[i];
681 if (regcomp(&re, c->pattern, REG_EXTENDED))
682 die("Bogus regex in service table: %s", c->pattern);
683 if (!regexec(&re, dir, 1, out, 0)) {
686 if (strcmp(method, c->method))
687 return bad_request(&hdr, c);
690 n = out[0].rm_eo - out[0].rm_so;
691 cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);
692 dir[out[0].rm_so] = 0;
699 not_found(&hdr, "Request not supported: '%s'", dir);
702 if (!enter_repo(dir, 0))
703 not_found(&hdr, "Not a git repository: '%s'", dir);
704 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
705 access("git-daemon-export-ok", F_OK) )
706 not_found(&hdr, "Repository not exported: '%s'", dir);
709 max_request_buffer = git_env_ulong("GIT_HTTP_MAX_REQUEST_BUFFER",
712 cmd->imp(&hdr, cmd_arg);