Merge branch 'maint-1.6.5' into maint-1.6.6
[git/mjg.git] / http-backend.c
blobf729488fc5f787c0f4997aacdd1bb14732d717f7
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "object.h"
5 #include "tag.h"
6 #include "exec_cmd.h"
7 #include "run-command.h"
8 #include "string-list.h"
10 static const char content_type[] = "Content-Type";
11 static const char content_length[] = "Content-Length";
12 static const char last_modified[] = "Last-Modified";
13 static int getanyfile = 1;
15 static struct string_list *query_params;
17 struct rpc_service {
18 const char *name;
19 const char *config_name;
20 signed enabled : 2;
23 static struct rpc_service rpc_service[] = {
24 { "upload-pack", "uploadpack", 1 },
25 { "receive-pack", "receivepack", -1 },
28 static int decode_char(const char *q)
30 int i;
31 unsigned char val = 0;
32 for (i = 0; i < 2; i++) {
33 unsigned char c = *q++;
34 val <<= 4;
35 if (c >= '0' && c <= '9')
36 val += c - '0';
37 else if (c >= 'a' && c <= 'f')
38 val += c - 'a' + 10;
39 else if (c >= 'A' && c <= 'F')
40 val += c - 'A' + 10;
41 else
42 return -1;
44 return val;
47 static char *decode_parameter(const char **query, int is_name)
49 const char *q = *query;
50 struct strbuf out;
52 strbuf_init(&out, 16);
53 do {
54 unsigned char c = *q;
56 if (!c)
57 break;
58 if (c == '&' || (is_name && c == '=')) {
59 q++;
60 break;
63 if (c == '%') {
64 int val = decode_char(q + 1);
65 if (0 <= val) {
66 strbuf_addch(&out, val);
67 q += 3;
68 continue;
72 if (c == '+')
73 strbuf_addch(&out, ' ');
74 else
75 strbuf_addch(&out, c);
76 q++;
77 } while (1);
78 *query = q;
79 return strbuf_detach(&out, NULL);
82 static struct string_list *get_parameters(void)
84 if (!query_params) {
85 const char *query = getenv("QUERY_STRING");
87 query_params = xcalloc(1, sizeof(*query_params));
88 while (query && *query) {
89 char *name = decode_parameter(&query, 1);
90 char *value = decode_parameter(&query, 0);
91 struct string_list_item *i;
93 i = string_list_lookup(name, query_params);
94 if (!i)
95 i = string_list_insert(name, query_params);
96 else
97 free(i->util);
98 i->util = value;
101 return query_params;
104 static const char *get_parameter(const char *name)
106 struct string_list_item *i;
107 i = string_list_lookup(name, get_parameters());
108 return i ? i->util : NULL;
111 __attribute__((format (printf, 2, 3)))
112 static void format_write(int fd, const char *fmt, ...)
114 static char buffer[1024];
116 va_list args;
117 unsigned n;
119 va_start(args, fmt);
120 n = vsnprintf(buffer, sizeof(buffer), fmt, args);
121 va_end(args);
122 if (n >= sizeof(buffer))
123 die("protocol error: impossibly long line");
125 safe_write(fd, buffer, n);
128 static void http_status(unsigned code, const char *msg)
130 format_write(1, "Status: %u %s\r\n", code, msg);
133 static void hdr_str(const char *name, const char *value)
135 format_write(1, "%s: %s\r\n", name, value);
138 static void hdr_int(const char *name, uintmax_t value)
140 format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
143 static void hdr_date(const char *name, unsigned long when)
145 const char *value = show_date(when, 0, DATE_RFC2822);
146 hdr_str(name, value);
149 static void hdr_nocache(void)
151 hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
152 hdr_str("Pragma", "no-cache");
153 hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
156 static void hdr_cache_forever(void)
158 unsigned long now = time(NULL);
159 hdr_date("Date", now);
160 hdr_date("Expires", now + 31536000);
161 hdr_str("Cache-Control", "public, max-age=31536000");
164 static void end_headers(void)
166 safe_write(1, "\r\n", 2);
169 __attribute__((format (printf, 1, 2)))
170 static NORETURN void not_found(const char *err, ...)
172 va_list params;
174 http_status(404, "Not Found");
175 hdr_nocache();
176 end_headers();
178 va_start(params, err);
179 if (err && *err)
180 vfprintf(stderr, err, params);
181 va_end(params);
182 exit(0);
185 __attribute__((format (printf, 1, 2)))
186 static NORETURN void forbidden(const char *err, ...)
188 va_list params;
190 http_status(403, "Forbidden");
191 hdr_nocache();
192 end_headers();
194 va_start(params, err);
195 if (err && *err)
196 vfprintf(stderr, err, params);
197 va_end(params);
198 exit(0);
201 static void select_getanyfile(void)
203 if (!getanyfile)
204 forbidden("Unsupported service: getanyfile");
207 static void send_strbuf(const char *type, struct strbuf *buf)
209 hdr_int(content_length, buf->len);
210 hdr_str(content_type, type);
211 end_headers();
212 safe_write(1, buf->buf, buf->len);
215 static void send_local_file(const char *the_type, const char *name)
217 const char *p = git_path("%s", name);
218 size_t buf_alloc = 8192;
219 char *buf = xmalloc(buf_alloc);
220 int fd;
221 struct stat sb;
223 fd = open(p, O_RDONLY);
224 if (fd < 0)
225 not_found("Cannot open '%s': %s", p, strerror(errno));
226 if (fstat(fd, &sb) < 0)
227 die_errno("Cannot stat '%s'", p);
229 hdr_int(content_length, sb.st_size);
230 hdr_str(content_type, the_type);
231 hdr_date(last_modified, sb.st_mtime);
232 end_headers();
234 for (;;) {
235 ssize_t n = xread(fd, buf, buf_alloc);
236 if (n < 0)
237 die_errno("Cannot read '%s'", p);
238 if (!n)
239 break;
240 safe_write(1, buf, n);
242 close(fd);
243 free(buf);
246 static void get_text_file(char *name)
248 select_getanyfile();
249 hdr_nocache();
250 send_local_file("text/plain", name);
253 static void get_loose_object(char *name)
255 select_getanyfile();
256 hdr_cache_forever();
257 send_local_file("application/x-git-loose-object", name);
260 static void get_pack_file(char *name)
262 select_getanyfile();
263 hdr_cache_forever();
264 send_local_file("application/x-git-packed-objects", name);
267 static void get_idx_file(char *name)
269 select_getanyfile();
270 hdr_cache_forever();
271 send_local_file("application/x-git-packed-objects-toc", name);
274 static int http_config(const char *var, const char *value, void *cb)
276 if (!strcmp(var, "http.getanyfile")) {
277 getanyfile = git_config_bool(var, value);
278 return 0;
281 if (!prefixcmp(var, "http.")) {
282 int i;
284 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
285 struct rpc_service *svc = &rpc_service[i];
286 if (!strcmp(var + 5, svc->config_name)) {
287 svc->enabled = git_config_bool(var, value);
288 return 0;
293 /* we are not interested in parsing any other configuration here */
294 return 0;
297 static struct rpc_service *select_service(const char *name)
299 struct rpc_service *svc = NULL;
300 int i;
302 if (prefixcmp(name, "git-"))
303 forbidden("Unsupported service: '%s'", name);
305 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
306 struct rpc_service *s = &rpc_service[i];
307 if (!strcmp(s->name, name + 4)) {
308 svc = s;
309 break;
313 if (!svc)
314 forbidden("Unsupported service: '%s'", name);
316 if (svc->enabled < 0) {
317 const char *user = getenv("REMOTE_USER");
318 svc->enabled = (user && *user) ? 1 : 0;
320 if (!svc->enabled)
321 forbidden("Service not enabled: '%s'", svc->name);
322 return svc;
325 static void inflate_request(const char *prog_name, int out)
327 z_stream stream;
328 unsigned char in_buf[8192];
329 unsigned char out_buf[8192];
330 unsigned long cnt = 0;
331 int ret;
333 memset(&stream, 0, sizeof(stream));
334 ret = inflateInit2(&stream, (15 + 16));
335 if (ret != Z_OK)
336 die("cannot start zlib inflater, zlib err %d", ret);
338 while (1) {
339 ssize_t n = xread(0, in_buf, sizeof(in_buf));
340 if (n <= 0)
341 die("request ended in the middle of the gzip stream");
343 stream.next_in = in_buf;
344 stream.avail_in = n;
346 while (0 < stream.avail_in) {
347 int ret;
349 stream.next_out = out_buf;
350 stream.avail_out = sizeof(out_buf);
352 ret = inflate(&stream, Z_NO_FLUSH);
353 if (ret != Z_OK && ret != Z_STREAM_END)
354 die("zlib error inflating request, result %d", ret);
356 n = stream.total_out - cnt;
357 if (write_in_full(out, out_buf, n) != n)
358 die("%s aborted reading request", prog_name);
359 cnt += n;
361 if (ret == Z_STREAM_END)
362 goto done;
366 done:
367 inflateEnd(&stream);
368 close(out);
371 static void run_service(const char **argv)
373 const char *encoding = getenv("HTTP_CONTENT_ENCODING");
374 const char *user = getenv("REMOTE_USER");
375 const char *host = getenv("REMOTE_ADDR");
376 char *env[3];
377 struct strbuf buf = STRBUF_INIT;
378 int gzipped_request = 0;
379 struct child_process cld;
381 if (encoding && !strcmp(encoding, "gzip"))
382 gzipped_request = 1;
383 else if (encoding && !strcmp(encoding, "x-gzip"))
384 gzipped_request = 1;
386 if (!user || !*user)
387 user = "anonymous";
388 if (!host || !*host)
389 host = "(none)";
391 memset(&env, 0, sizeof(env));
392 strbuf_addf(&buf, "GIT_COMMITTER_NAME=%s", user);
393 env[0] = strbuf_detach(&buf, NULL);
395 strbuf_addf(&buf, "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
396 env[1] = strbuf_detach(&buf, NULL);
397 env[2] = NULL;
399 memset(&cld, 0, sizeof(cld));
400 cld.argv = argv;
401 cld.env = (const char *const *)env;
402 if (gzipped_request)
403 cld.in = -1;
404 cld.git_cmd = 1;
405 if (start_command(&cld))
406 exit(1);
408 close(1);
409 if (gzipped_request)
410 inflate_request(argv[0], cld.in);
411 else
412 close(0);
414 if (finish_command(&cld))
415 exit(1);
416 free(env[0]);
417 free(env[1]);
418 strbuf_release(&buf);
421 static int show_text_ref(const char *name, const unsigned char *sha1,
422 int flag, void *cb_data)
424 struct strbuf *buf = cb_data;
425 struct object *o = parse_object(sha1);
426 if (!o)
427 return 0;
429 strbuf_addf(buf, "%s\t%s\n", sha1_to_hex(sha1), name);
430 if (o->type == OBJ_TAG) {
431 o = deref_tag(o, name, 0);
432 if (!o)
433 return 0;
434 strbuf_addf(buf, "%s\t%s^{}\n", sha1_to_hex(o->sha1), name);
436 return 0;
439 static void get_info_refs(char *arg)
441 const char *service_name = get_parameter("service");
442 struct strbuf buf = STRBUF_INIT;
444 hdr_nocache();
446 if (service_name) {
447 const char *argv[] = {NULL /* service name */,
448 "--stateless-rpc", "--advertise-refs",
449 ".", NULL};
450 struct rpc_service *svc = select_service(service_name);
452 strbuf_addf(&buf, "application/x-git-%s-advertisement",
453 svc->name);
454 hdr_str(content_type, buf.buf);
455 end_headers();
457 packet_write(1, "# service=git-%s\n", svc->name);
458 packet_flush(1);
460 argv[0] = svc->name;
461 run_service(argv);
463 } else {
464 select_getanyfile();
465 for_each_ref(show_text_ref, &buf);
466 send_strbuf("text/plain", &buf);
468 strbuf_release(&buf);
471 static void get_info_packs(char *arg)
473 size_t objdirlen = strlen(get_object_directory());
474 struct strbuf buf = STRBUF_INIT;
475 struct packed_git *p;
476 size_t cnt = 0;
478 select_getanyfile();
479 prepare_packed_git();
480 for (p = packed_git; p; p = p->next) {
481 if (p->pack_local)
482 cnt++;
485 strbuf_grow(&buf, cnt * 53 + 2);
486 for (p = packed_git; p; p = p->next) {
487 if (p->pack_local)
488 strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
490 strbuf_addch(&buf, '\n');
492 hdr_nocache();
493 send_strbuf("text/plain; charset=utf-8", &buf);
494 strbuf_release(&buf);
497 static void check_content_type(const char *accepted_type)
499 const char *actual_type = getenv("CONTENT_TYPE");
501 if (!actual_type)
502 actual_type = "";
504 if (strcmp(actual_type, accepted_type)) {
505 http_status(415, "Unsupported Media Type");
506 hdr_nocache();
507 end_headers();
508 format_write(1,
509 "Expected POST with Content-Type '%s',"
510 " but received '%s' instead.\n",
511 accepted_type, actual_type);
512 exit(0);
516 static void service_rpc(char *service_name)
518 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
519 struct rpc_service *svc = select_service(service_name);
520 struct strbuf buf = STRBUF_INIT;
522 strbuf_reset(&buf);
523 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
524 check_content_type(buf.buf);
526 hdr_nocache();
528 strbuf_reset(&buf);
529 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
530 hdr_str(content_type, buf.buf);
532 end_headers();
534 argv[0] = svc->name;
535 run_service(argv);
536 strbuf_release(&buf);
539 static NORETURN void die_webcgi(const char *err, va_list params)
541 char buffer[1000];
543 http_status(500, "Internal Server Error");
544 hdr_nocache();
545 end_headers();
547 vsnprintf(buffer, sizeof(buffer), err, params);
548 fprintf(stderr, "fatal: %s\n", buffer);
549 exit(0);
552 static char* getdir(void)
554 struct strbuf buf = STRBUF_INIT;
555 char *pathinfo = getenv("PATH_INFO");
556 char *root = getenv("GIT_PROJECT_ROOT");
557 char *path = getenv("PATH_TRANSLATED");
559 if (root && *root) {
560 if (!pathinfo || !*pathinfo)
561 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
562 if (daemon_avoid_alias(pathinfo))
563 die("'%s': aliased", pathinfo);
564 strbuf_addstr(&buf, root);
565 if (buf.buf[buf.len - 1] != '/')
566 strbuf_addch(&buf, '/');
567 if (pathinfo[0] == '/')
568 pathinfo++;
569 strbuf_addstr(&buf, pathinfo);
570 return strbuf_detach(&buf, NULL);
571 } else if (path && *path) {
572 return xstrdup(path);
573 } else
574 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
575 return NULL;
578 static struct service_cmd {
579 const char *method;
580 const char *pattern;
581 void (*imp)(char *);
582 } services[] = {
583 {"GET", "/HEAD$", get_text_file},
584 {"GET", "/info/refs$", get_info_refs},
585 {"GET", "/objects/info/alternates$", get_text_file},
586 {"GET", "/objects/info/http-alternates$", get_text_file},
587 {"GET", "/objects/info/packs$", get_info_packs},
588 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
589 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
590 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
592 {"POST", "/git-upload-pack$", service_rpc},
593 {"POST", "/git-receive-pack$", service_rpc}
596 int main(int argc, char **argv)
598 char *method = getenv("REQUEST_METHOD");
599 char *dir;
600 struct service_cmd *cmd = NULL;
601 char *cmd_arg = NULL;
602 int i;
604 git_extract_argv0_path(argv[0]);
605 set_die_routine(die_webcgi);
607 if (!method)
608 die("No REQUEST_METHOD from server");
609 if (!strcmp(method, "HEAD"))
610 method = "GET";
611 dir = getdir();
613 for (i = 0; i < ARRAY_SIZE(services); i++) {
614 struct service_cmd *c = &services[i];
615 regex_t re;
616 regmatch_t out[1];
618 if (regcomp(&re, c->pattern, REG_EXTENDED))
619 die("Bogus regex in service table: %s", c->pattern);
620 if (!regexec(&re, dir, 1, out, 0)) {
621 size_t n;
623 if (strcmp(method, c->method)) {
624 const char *proto = getenv("SERVER_PROTOCOL");
625 if (proto && !strcmp(proto, "HTTP/1.1"))
626 http_status(405, "Method Not Allowed");
627 else
628 http_status(400, "Bad Request");
629 hdr_nocache();
630 end_headers();
631 return 0;
634 cmd = c;
635 n = out[0].rm_eo - out[0].rm_so;
636 cmd_arg = xmalloc(n);
637 memcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);
638 cmd_arg[n-1] = '\0';
639 dir[out[0].rm_so] = 0;
640 break;
642 regfree(&re);
645 if (!cmd)
646 not_found("Request not supported: '%s'", dir);
648 setup_path();
649 if (!enter_repo(dir, 0))
650 not_found("Not a git repository: '%s'", dir);
652 git_config(http_config, NULL);
653 cmd->imp(cmd_arg);
654 return 0;