8 #include "run-command.h"
9 #include "string-list.h"
11 #include "argv-array.h"
15 static const char content_type[] = "Content-Type";
16 static const char content_length[] = "Content-Length";
17 static const char last_modified[] = "Last-Modified";
18 static int getanyfile = 1;
19 static unsigned long max_request_buffer = 10 * 1024 * 1024;
21 static struct string_list *query_params;
25 const char *config_name;
26 unsigned buffer_input : 1;
30 static struct rpc_service rpc_service[] = {
31 { "upload-pack", "uploadpack", 1, 1 },
32 { "receive-pack", "receivepack", 0, -1 },
35 static struct string_list *get_parameters(void)
38 const char *query = getenv("QUERY_STRING");
40 query_params = xcalloc(1, sizeof(*query_params));
41 while (query && *query) {
42 char *name = url_decode_parameter_name(&query);
43 char *value = url_decode_parameter_value(&query);
44 struct string_list_item *i;
46 i = string_list_lookup(query_params, name);
48 i = string_list_insert(query_params, name);
57 static const char *get_parameter(const char *name)
59 struct string_list_item *i;
60 i = string_list_lookup(get_parameters(), name);
61 return i ? i->util : NULL;
64 __attribute__((format (printf, 2, 3)))
65 static void format_write(int fd, const char *fmt, ...)
67 static char buffer[1024];
73 n = vsnprintf(buffer, sizeof(buffer), fmt, args);
75 if (n >= sizeof(buffer))
76 die("protocol error: impossibly long line");
78 write_or_die(fd, buffer, n);
81 static void http_status(struct strbuf *hdr, unsigned code, const char *msg)
83 strbuf_addf(hdr, "Status: %u %s\r\n", code, msg);
86 static void hdr_str(struct strbuf *hdr, const char *name, const char *value)
88 strbuf_addf(hdr, "%s: %s\r\n", name, value);
91 static void hdr_int(struct strbuf *hdr, const char *name, uintmax_t value)
93 strbuf_addf(hdr, "%s: %" PRIuMAX "\r\n", name, value);
96 static void hdr_date(struct strbuf *hdr, const char *name, timestamp_t when)
98 const char *value = show_date(when, 0, DATE_MODE(RFC2822));
99 hdr_str(hdr, name, value);
102 static void hdr_nocache(struct strbuf *hdr)
104 hdr_str(hdr, "Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
105 hdr_str(hdr, "Pragma", "no-cache");
106 hdr_str(hdr, "Cache-Control", "no-cache, max-age=0, must-revalidate");
109 static void hdr_cache_forever(struct strbuf *hdr)
111 timestamp_t now = time(NULL);
112 hdr_date(hdr, "Date", now);
113 hdr_date(hdr, "Expires", now + 31536000);
114 hdr_str(hdr, "Cache-Control", "public, max-age=31536000");
117 static void end_headers(struct strbuf *hdr)
119 strbuf_add(hdr, "\r\n", 2);
120 write_or_die(1, hdr->buf, hdr->len);
124 __attribute__((format (printf, 2, 3)))
125 static NORETURN void not_found(struct strbuf *hdr, const char *err, ...)
129 http_status(hdr, 404, "Not Found");
133 va_start(params, err);
135 vfprintf(stderr, err, params);
140 __attribute__((format (printf, 2, 3)))
141 static NORETURN void forbidden(struct strbuf *hdr, const char *err, ...)
145 http_status(hdr, 403, "Forbidden");
149 va_start(params, err);
151 vfprintf(stderr, err, params);
156 static void select_getanyfile(struct strbuf *hdr)
159 forbidden(hdr, "Unsupported service: getanyfile");
162 static void send_strbuf(struct strbuf *hdr,
163 const char *type, struct strbuf *buf)
165 hdr_int(hdr, content_length, buf->len);
166 hdr_str(hdr, content_type, type);
168 write_or_die(1, buf->buf, buf->len);
171 static void send_local_file(struct strbuf *hdr, const char *the_type,
174 char *p = git_pathdup("%s", name);
175 size_t buf_alloc = 8192;
176 char *buf = xmalloc(buf_alloc);
180 fd = open(p, O_RDONLY);
182 not_found(hdr, "Cannot open '%s': %s", p, strerror(errno));
183 if (fstat(fd, &sb) < 0)
184 die_errno("Cannot stat '%s'", p);
186 hdr_int(hdr, content_length, sb.st_size);
187 hdr_str(hdr, content_type, the_type);
188 hdr_date(hdr, last_modified, sb.st_mtime);
192 ssize_t n = xread(fd, buf, buf_alloc);
194 die_errno("Cannot read '%s'", p);
197 write_or_die(1, buf, n);
204 static void get_text_file(struct strbuf *hdr, char *name)
206 select_getanyfile(hdr);
208 send_local_file(hdr, "text/plain", name);
211 static void get_loose_object(struct strbuf *hdr, char *name)
213 select_getanyfile(hdr);
214 hdr_cache_forever(hdr);
215 send_local_file(hdr, "application/x-git-loose-object", name);
218 static void get_pack_file(struct strbuf *hdr, char *name)
220 select_getanyfile(hdr);
221 hdr_cache_forever(hdr);
222 send_local_file(hdr, "application/x-git-packed-objects", name);
225 static void get_idx_file(struct strbuf *hdr, char *name)
227 select_getanyfile(hdr);
228 hdr_cache_forever(hdr);
229 send_local_file(hdr, "application/x-git-packed-objects-toc", name);
232 static void http_config(void)
235 struct strbuf var = STRBUF_INIT;
237 git_config_get_bool("http.getanyfile", &getanyfile);
238 git_config_get_ulong("http.maxrequestbuffer", &max_request_buffer);
240 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
241 struct rpc_service *svc = &rpc_service[i];
242 strbuf_addf(&var, "http.%s", svc->config_name);
243 if (!git_config_get_bool(var.buf, &value))
244 svc->enabled = value;
248 strbuf_release(&var);
251 static struct rpc_service *select_service(struct strbuf *hdr, const char *name)
253 const char *svc_name;
254 struct rpc_service *svc = NULL;
257 if (!skip_prefix(name, "git-", &svc_name))
258 forbidden(hdr, "Unsupported service: '%s'", name);
260 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
261 struct rpc_service *s = &rpc_service[i];
262 if (!strcmp(s->name, svc_name)) {
269 forbidden(hdr, "Unsupported service: '%s'", name);
271 if (svc->enabled < 0) {
272 const char *user = getenv("REMOTE_USER");
273 svc->enabled = (user && *user) ? 1 : 0;
276 forbidden(hdr, "Service not enabled: '%s'", svc->name);
281 * This is basically strbuf_read(), except that if we
282 * hit max_request_buffer we die (we'd rather reject a
283 * maliciously large request than chew up infinite memory).
285 static ssize_t read_request(int fd, unsigned char **out)
287 size_t len = 0, alloc = 8192;
288 unsigned char *buf = xmalloc(alloc);
290 if (max_request_buffer < alloc)
291 max_request_buffer = alloc;
296 cnt = read_in_full(fd, buf + len, alloc - len);
302 /* partial read from read_in_full means we hit EOF */
309 /* otherwise, grow and try again (if we can) */
310 if (alloc == max_request_buffer)
311 die("request was larger than our maximum size (%lu);"
312 " try setting GIT_HTTP_MAX_REQUEST_BUFFER",
315 alloc = alloc_nr(alloc);
316 if (alloc > max_request_buffer)
317 alloc = max_request_buffer;
318 REALLOC_ARRAY(buf, alloc);
322 static void inflate_request(const char *prog_name, int out, int buffer_input)
325 unsigned char *full_request = NULL;
326 unsigned char in_buf[8192];
327 unsigned char out_buf[8192];
328 unsigned long cnt = 0;
330 memset(&stream, 0, sizeof(stream));
331 git_inflate_init_gzip_only(&stream);
338 n = 0; /* nothing left to read */
340 n = read_request(0, &full_request);
341 stream.next_in = full_request;
343 n = xread(0, in_buf, sizeof(in_buf));
344 stream.next_in = in_buf;
348 die("request ended in the middle of the gzip stream");
351 while (0 < stream.avail_in) {
354 stream.next_out = out_buf;
355 stream.avail_out = sizeof(out_buf);
357 ret = git_inflate(&stream, Z_NO_FLUSH);
358 if (ret != Z_OK && ret != Z_STREAM_END)
359 die("zlib error inflating request, result %d", ret);
361 n = stream.total_out - cnt;
362 if (write_in_full(out, out_buf, n) < 0)
363 die("%s aborted reading request", prog_name);
366 if (ret == Z_STREAM_END)
372 git_inflate_end(&stream);
377 static void copy_request(const char *prog_name, int out)
380 ssize_t n = read_request(0, &buf);
382 die_errno("error reading request body");
383 if (write_in_full(out, buf, n) < 0)
384 die("%s aborted reading request", prog_name);
389 static void run_service(const char **argv, int buffer_input)
391 const char *encoding = getenv("HTTP_CONTENT_ENCODING");
392 const char *user = getenv("REMOTE_USER");
393 const char *host = getenv("REMOTE_ADDR");
394 int gzipped_request = 0;
395 struct child_process cld = CHILD_PROCESS_INIT;
397 if (encoding && !strcmp(encoding, "gzip"))
399 else if (encoding && !strcmp(encoding, "x-gzip"))
407 if (!getenv("GIT_COMMITTER_NAME"))
408 argv_array_pushf(&cld.env_array, "GIT_COMMITTER_NAME=%s", user);
409 if (!getenv("GIT_COMMITTER_EMAIL"))
410 argv_array_pushf(&cld.env_array,
411 "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
414 if (buffer_input || gzipped_request)
417 if (start_command(&cld))
422 inflate_request(argv[0], cld.in, buffer_input);
423 else if (buffer_input)
424 copy_request(argv[0], cld.in);
428 if (finish_command(&cld))
432 static int show_text_ref(const char *name, const struct object_id *oid,
433 int flag, void *cb_data)
435 const char *name_nons = strip_namespace(name);
436 struct strbuf *buf = cb_data;
437 struct object *o = parse_object(oid);
441 strbuf_addf(buf, "%s\t%s\n", oid_to_hex(oid), name_nons);
442 if (o->type == OBJ_TAG) {
443 o = deref_tag(o, name, 0);
446 strbuf_addf(buf, "%s\t%s^{}\n", oid_to_hex(&o->oid),
452 static void get_info_refs(struct strbuf *hdr, char *arg)
454 const char *service_name = get_parameter("service");
455 struct strbuf buf = STRBUF_INIT;
460 const char *argv[] = {NULL /* service name */,
461 "--stateless-rpc", "--advertise-refs",
463 struct rpc_service *svc = select_service(hdr, service_name);
465 strbuf_addf(&buf, "application/x-git-%s-advertisement",
467 hdr_str(hdr, content_type, buf.buf);
471 if (determine_protocol_version_server() != protocol_v2) {
472 packet_write_fmt(1, "# service=git-%s\n", svc->name);
477 run_service(argv, 0);
480 select_getanyfile(hdr);
481 for_each_namespaced_ref(show_text_ref, &buf);
482 send_strbuf(hdr, "text/plain", &buf);
484 strbuf_release(&buf);
487 static int show_head_ref(const char *refname, const struct object_id *oid,
488 int flag, void *cb_data)
490 struct strbuf *buf = cb_data;
492 if (flag & REF_ISSYMREF) {
493 const char *target = resolve_ref_unsafe(refname,
498 strbuf_addf(buf, "ref: %s\n", strip_namespace(target));
500 strbuf_addf(buf, "%s\n", oid_to_hex(oid));
506 static void get_head(struct strbuf *hdr, char *arg)
508 struct strbuf buf = STRBUF_INIT;
510 select_getanyfile(hdr);
511 head_ref_namespaced(show_head_ref, &buf);
512 send_strbuf(hdr, "text/plain", &buf);
513 strbuf_release(&buf);
516 static void get_info_packs(struct strbuf *hdr, char *arg)
518 size_t objdirlen = strlen(get_object_directory());
519 struct strbuf buf = STRBUF_INIT;
520 struct packed_git *p;
523 select_getanyfile(hdr);
524 prepare_packed_git();
525 for (p = packed_git; p; p = p->next) {
530 strbuf_grow(&buf, cnt * 53 + 2);
531 for (p = packed_git; p; p = p->next) {
533 strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
535 strbuf_addch(&buf, '\n');
538 send_strbuf(hdr, "text/plain; charset=utf-8", &buf);
539 strbuf_release(&buf);
542 static void check_content_type(struct strbuf *hdr, const char *accepted_type)
544 const char *actual_type = getenv("CONTENT_TYPE");
549 if (strcmp(actual_type, accepted_type)) {
550 http_status(hdr, 415, "Unsupported Media Type");
554 "Expected POST with Content-Type '%s',"
555 " but received '%s' instead.\n",
556 accepted_type, actual_type);
561 static void service_rpc(struct strbuf *hdr, char *service_name)
563 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
564 struct rpc_service *svc = select_service(hdr, service_name);
565 struct strbuf buf = STRBUF_INIT;
568 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
569 check_content_type(hdr, buf.buf);
574 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
575 hdr_str(hdr, content_type, buf.buf);
580 run_service(argv, svc->buffer_input);
581 strbuf_release(&buf);
585 static NORETURN void die_webcgi(const char *err, va_list params)
588 struct strbuf hdr = STRBUF_INIT;
590 vreportf("fatal: ", err, params);
592 http_status(&hdr, 500, "Internal Server Error");
596 exit(0); /* we successfully reported a failure ;-) */
599 static int die_webcgi_recursing(void)
604 static char* getdir(void)
606 struct strbuf buf = STRBUF_INIT;
607 char *pathinfo = getenv("PATH_INFO");
608 char *root = getenv("GIT_PROJECT_ROOT");
609 char *path = getenv("PATH_TRANSLATED");
612 if (!pathinfo || !*pathinfo)
613 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
614 if (daemon_avoid_alias(pathinfo))
615 die("'%s': aliased", pathinfo);
616 end_url_with_slash(&buf, root);
617 if (pathinfo[0] == '/')
619 strbuf_addstr(&buf, pathinfo);
620 return strbuf_detach(&buf, NULL);
621 } else if (path && *path) {
622 return xstrdup(path);
624 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
628 static struct service_cmd {
631 void (*imp)(struct strbuf *, char *);
633 {"GET", "/HEAD$", get_head},
634 {"GET", "/info/refs$", get_info_refs},
635 {"GET", "/objects/info/alternates$", get_text_file},
636 {"GET", "/objects/info/http-alternates$", get_text_file},
637 {"GET", "/objects/info/packs$", get_info_packs},
638 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
639 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
640 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
642 {"POST", "/git-upload-pack$", service_rpc},
643 {"POST", "/git-receive-pack$", service_rpc}
646 static int bad_request(struct strbuf *hdr, const struct service_cmd *c)
648 const char *proto = getenv("SERVER_PROTOCOL");
650 if (proto && !strcmp(proto, "HTTP/1.1")) {
651 http_status(hdr, 405, "Method Not Allowed");
652 hdr_str(hdr, "Allow",
653 !strcmp(c->method, "GET") ? "GET, HEAD" : c->method);
655 http_status(hdr, 400, "Bad Request");
661 int cmd_main(int argc, const char **argv)
663 char *method = getenv("REQUEST_METHOD");
665 struct service_cmd *cmd = NULL;
666 char *cmd_arg = NULL;
668 struct strbuf hdr = STRBUF_INIT;
670 set_die_routine(die_webcgi);
671 set_die_is_recursing_routine(die_webcgi_recursing);
674 die("No REQUEST_METHOD from server");
675 if (!strcmp(method, "HEAD"))
679 for (i = 0; i < ARRAY_SIZE(services); i++) {
680 struct service_cmd *c = &services[i];
684 if (regcomp(&re, c->pattern, REG_EXTENDED))
685 die("Bogus regex in service table: %s", c->pattern);
686 if (!regexec(&re, dir, 1, out, 0)) {
689 if (strcmp(method, c->method))
690 return bad_request(&hdr, c);
693 n = out[0].rm_eo - out[0].rm_so;
694 cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);
695 dir[out[0].rm_so] = 0;
702 not_found(&hdr, "Request not supported: '%s'", dir);
705 if (!enter_repo(dir, 0))
706 not_found(&hdr, "Not a git repository: '%s'", dir);
707 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
708 access("git-daemon-export-ok", F_OK) )
709 not_found(&hdr, "Repository not exported: '%s'", dir);
712 max_request_buffer = git_env_ulong("GIT_HTTP_MAX_REQUEST_BUFFER",
715 cmd->imp(&hdr, cmd_arg);