Smart push over HTTP: client side
[alt-git.git] / http-backend.c
blob7900cda69add0e7c69c5cefe93b9d1f8ec3e6fb0
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";
14 static struct string_list *query_params;
16 struct rpc_service {
17 const char *name;
18 const char *config_name;
19 signed enabled : 2;
22 static struct rpc_service rpc_service[] = {
23 { "upload-pack", "uploadpack", 1 },
24 { "receive-pack", "receivepack", -1 },
27 static int decode_char(const char *q)
29 int i;
30 unsigned char val = 0;
31 for (i = 0; i < 2; i++) {
32 unsigned char c = *q++;
33 val <<= 4;
34 if (c >= '0' && c <= '9')
35 val += c - '0';
36 else if (c >= 'a' && c <= 'f')
37 val += c - 'a' + 10;
38 else if (c >= 'A' && c <= 'F')
39 val += c - 'A' + 10;
40 else
41 return -1;
43 return val;
46 static char *decode_parameter(const char **query, int is_name)
48 const char *q = *query;
49 struct strbuf out;
51 strbuf_init(&out, 16);
52 do {
53 unsigned char c = *q;
55 if (!c)
56 break;
57 if (c == '&' || (is_name && c == '=')) {
58 q++;
59 break;
62 if (c == '%') {
63 int val = decode_char(q + 1);
64 if (0 <= val) {
65 strbuf_addch(&out, val);
66 q += 3;
67 continue;
71 if (c == '+')
72 strbuf_addch(&out, ' ');
73 else
74 strbuf_addch(&out, c);
75 q++;
76 } while (1);
77 *query = q;
78 return strbuf_detach(&out, NULL);
81 static struct string_list *get_parameters(void)
83 if (!query_params) {
84 const char *query = getenv("QUERY_STRING");
86 query_params = xcalloc(1, sizeof(*query_params));
87 while (query && *query) {
88 char *name = decode_parameter(&query, 1);
89 char *value = decode_parameter(&query, 0);
90 struct string_list_item *i;
92 i = string_list_lookup(name, query_params);
93 if (!i)
94 i = string_list_insert(name, query_params);
95 else
96 free(i->util);
97 i->util = value;
100 return query_params;
103 static const char *get_parameter(const char *name)
105 struct string_list_item *i;
106 i = string_list_lookup(name, get_parameters());
107 return i ? i->util : NULL;
110 static void format_write(int fd, const char *fmt, ...)
112 static char buffer[1024];
114 va_list args;
115 unsigned n;
117 va_start(args, fmt);
118 n = vsnprintf(buffer, sizeof(buffer), fmt, args);
119 va_end(args);
120 if (n >= sizeof(buffer))
121 die("protocol error: impossibly long line");
123 safe_write(fd, buffer, n);
126 static void http_status(unsigned code, const char *msg)
128 format_write(1, "Status: %u %s\r\n", code, msg);
131 static void hdr_str(const char *name, const char *value)
133 format_write(1, "%s: %s\r\n", name, value);
136 static void hdr_int(const char *name, size_t value)
138 format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
141 static void hdr_date(const char *name, unsigned long when)
143 const char *value = show_date(when, 0, DATE_RFC2822);
144 hdr_str(name, value);
147 static void hdr_nocache(void)
149 hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
150 hdr_str("Pragma", "no-cache");
151 hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
154 static void hdr_cache_forever(void)
156 unsigned long now = time(NULL);
157 hdr_date("Date", now);
158 hdr_date("Expires", now + 31536000);
159 hdr_str("Cache-Control", "public, max-age=31536000");
162 static void end_headers(void)
164 safe_write(1, "\r\n", 2);
167 static NORETURN void not_found(const char *err, ...)
169 va_list params;
171 http_status(404, "Not Found");
172 hdr_nocache();
173 end_headers();
175 va_start(params, err);
176 if (err && *err)
177 vfprintf(stderr, err, params);
178 va_end(params);
179 exit(0);
182 static NORETURN void forbidden(const char *err, ...)
184 va_list params;
186 http_status(403, "Forbidden");
187 hdr_nocache();
188 end_headers();
190 va_start(params, err);
191 if (err && *err)
192 vfprintf(stderr, err, params);
193 va_end(params);
194 exit(0);
197 static void send_strbuf(const char *type, struct strbuf *buf)
199 hdr_int(content_length, buf->len);
200 hdr_str(content_type, type);
201 end_headers();
202 safe_write(1, buf->buf, buf->len);
205 static void send_file(const char *the_type, const char *name)
207 const char *p = git_path("%s", name);
208 size_t buf_alloc = 8192;
209 char *buf = xmalloc(buf_alloc);
210 int fd;
211 struct stat sb;
212 size_t size;
214 fd = open(p, O_RDONLY);
215 if (fd < 0)
216 not_found("Cannot open '%s': %s", p, strerror(errno));
217 if (fstat(fd, &sb) < 0)
218 die_errno("Cannot stat '%s'", p);
220 size = xsize_t(sb.st_size);
222 hdr_int(content_length, size);
223 hdr_str(content_type, the_type);
224 hdr_date(last_modified, sb.st_mtime);
225 end_headers();
227 while (size) {
228 ssize_t n = xread(fd, buf, buf_alloc);
229 if (n < 0)
230 die_errno("Cannot read '%s'", p);
231 if (!n)
232 break;
233 safe_write(1, buf, n);
235 close(fd);
236 free(buf);
239 static void get_text_file(char *name)
241 hdr_nocache();
242 send_file("text/plain", name);
245 static void get_loose_object(char *name)
247 hdr_cache_forever();
248 send_file("application/x-git-loose-object", name);
251 static void get_pack_file(char *name)
253 hdr_cache_forever();
254 send_file("application/x-git-packed-objects", name);
257 static void get_idx_file(char *name)
259 hdr_cache_forever();
260 send_file("application/x-git-packed-objects-toc", name);
263 static int http_config(const char *var, const char *value, void *cb)
265 struct rpc_service *svc = cb;
267 if (!prefixcmp(var, "http.") &&
268 !strcmp(var + 5, svc->config_name)) {
269 svc->enabled = git_config_bool(var, value);
270 return 0;
273 /* we are not interested in parsing any other configuration here */
274 return 0;
277 static struct rpc_service *select_service(const char *name)
279 struct rpc_service *svc = NULL;
280 int i;
282 if (prefixcmp(name, "git-"))
283 forbidden("Unsupported service: '%s'", name);
285 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
286 struct rpc_service *s = &rpc_service[i];
287 if (!strcmp(s->name, name + 4)) {
288 svc = s;
289 break;
293 if (!svc)
294 forbidden("Unsupported service: '%s'", name);
296 git_config(http_config, svc);
297 if (svc->enabled < 0) {
298 const char *user = getenv("REMOTE_USER");
299 svc->enabled = (user && *user) ? 1 : 0;
301 if (!svc->enabled)
302 forbidden("Service not enabled: '%s'", svc->name);
303 return svc;
306 static void inflate_request(const char *prog_name, int out)
308 z_stream stream;
309 unsigned char in_buf[8192];
310 unsigned char out_buf[8192];
311 unsigned long cnt = 0;
312 int ret;
314 memset(&stream, 0, sizeof(stream));
315 ret = inflateInit2(&stream, (15 + 16));
316 if (ret != Z_OK)
317 die("cannot start zlib inflater, zlib err %d", ret);
319 while (1) {
320 ssize_t n = xread(0, in_buf, sizeof(in_buf));
321 if (n <= 0)
322 die("request ended in the middle of the gzip stream");
324 stream.next_in = in_buf;
325 stream.avail_in = n;
327 while (0 < stream.avail_in) {
328 int ret;
330 stream.next_out = out_buf;
331 stream.avail_out = sizeof(out_buf);
333 ret = inflate(&stream, Z_NO_FLUSH);
334 if (ret != Z_OK && ret != Z_STREAM_END)
335 die("zlib error inflating request, result %d", ret);
337 n = stream.total_out - cnt;
338 if (write_in_full(out, out_buf, n) != n)
339 die("%s aborted reading request", prog_name);
340 cnt += n;
342 if (ret == Z_STREAM_END)
343 goto done;
347 done:
348 inflateEnd(&stream);
349 close(out);
352 static void run_service(const char **argv)
354 const char *encoding = getenv("HTTP_CONTENT_ENCODING");
355 const char *user = getenv("REMOTE_USER");
356 const char *host = getenv("REMOTE_ADDR");
357 char *env[3];
358 struct strbuf buf = STRBUF_INIT;
359 int gzipped_request = 0;
360 struct child_process cld;
362 if (encoding && !strcmp(encoding, "gzip"))
363 gzipped_request = 1;
364 else if (encoding && !strcmp(encoding, "x-gzip"))
365 gzipped_request = 1;
367 if (!user || !*user)
368 user = "anonymous";
369 if (!host || !*host)
370 host = "(none)";
372 memset(&env, 0, sizeof(env));
373 strbuf_addf(&buf, "GIT_COMMITTER_NAME=%s", user);
374 env[0] = strbuf_detach(&buf, NULL);
376 strbuf_addf(&buf, "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
377 env[1] = strbuf_detach(&buf, NULL);
378 env[2] = NULL;
380 memset(&cld, 0, sizeof(cld));
381 cld.argv = argv;
382 cld.env = (const char *const *)env;
383 if (gzipped_request)
384 cld.in = -1;
385 cld.git_cmd = 1;
386 if (start_command(&cld))
387 exit(1);
389 close(1);
390 if (gzipped_request)
391 inflate_request(argv[0], cld.in);
392 else
393 close(0);
395 if (finish_command(&cld))
396 exit(1);
397 free(env[0]);
398 free(env[1]);
399 strbuf_release(&buf);
402 static int show_text_ref(const char *name, const unsigned char *sha1,
403 int flag, void *cb_data)
405 struct strbuf *buf = cb_data;
406 struct object *o = parse_object(sha1);
407 if (!o)
408 return 0;
410 strbuf_addf(buf, "%s\t%s\n", sha1_to_hex(sha1), name);
411 if (o->type == OBJ_TAG) {
412 o = deref_tag(o, name, 0);
413 if (!o)
414 return 0;
415 strbuf_addf(buf, "%s\t%s^{}\n", sha1_to_hex(o->sha1), name);
417 return 0;
420 static void get_info_refs(char *arg)
422 const char *service_name = get_parameter("service");
423 struct strbuf buf = STRBUF_INIT;
425 hdr_nocache();
427 if (service_name) {
428 const char *argv[] = {NULL /* service name */,
429 "--stateless-rpc", "--advertise-refs",
430 ".", NULL};
431 struct rpc_service *svc = select_service(service_name);
433 strbuf_addf(&buf, "application/x-git-%s-advertisement",
434 svc->name);
435 hdr_str(content_type, buf.buf);
436 end_headers();
438 packet_write(1, "# service=git-%s\n", svc->name);
439 packet_flush(1);
441 argv[0] = svc->name;
442 run_service(argv);
444 } else {
445 for_each_ref(show_text_ref, &buf);
446 send_strbuf("text/plain", &buf);
448 strbuf_release(&buf);
451 static void get_info_packs(char *arg)
453 size_t objdirlen = strlen(get_object_directory());
454 struct strbuf buf = STRBUF_INIT;
455 struct packed_git *p;
456 size_t cnt = 0;
458 prepare_packed_git();
459 for (p = packed_git; p; p = p->next) {
460 if (p->pack_local)
461 cnt++;
464 strbuf_grow(&buf, cnt * 53 + 2);
465 for (p = packed_git; p; p = p->next) {
466 if (p->pack_local)
467 strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
469 strbuf_addch(&buf, '\n');
471 hdr_nocache();
472 send_strbuf("text/plain; charset=utf-8", &buf);
473 strbuf_release(&buf);
476 static void check_content_type(const char *accepted_type)
478 const char *actual_type = getenv("CONTENT_TYPE");
480 if (!actual_type)
481 actual_type = "";
483 if (strcmp(actual_type, accepted_type)) {
484 http_status(415, "Unsupported Media Type");
485 hdr_nocache();
486 end_headers();
487 format_write(1,
488 "Expected POST with Content-Type '%s',"
489 " but received '%s' instead.\n",
490 accepted_type, actual_type);
491 exit(0);
495 static void service_rpc(char *service_name)
497 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
498 struct rpc_service *svc = select_service(service_name);
499 struct strbuf buf = STRBUF_INIT;
501 strbuf_reset(&buf);
502 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
503 check_content_type(buf.buf);
505 hdr_nocache();
507 strbuf_reset(&buf);
508 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
509 hdr_str(content_type, buf.buf);
511 end_headers();
513 argv[0] = svc->name;
514 run_service(argv);
515 strbuf_release(&buf);
518 static NORETURN void die_webcgi(const char *err, va_list params)
520 char buffer[1000];
522 http_status(500, "Internal Server Error");
523 hdr_nocache();
524 end_headers();
526 vsnprintf(buffer, sizeof(buffer), err, params);
527 fprintf(stderr, "fatal: %s\n", buffer);
528 exit(0);
531 static char* getdir(void)
533 struct strbuf buf = STRBUF_INIT;
534 char *pathinfo = getenv("PATH_INFO");
535 char *root = getenv("GIT_PROJECT_ROOT");
536 char *path = getenv("PATH_TRANSLATED");
538 if (root && *root) {
539 if (!pathinfo || !*pathinfo)
540 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
541 strbuf_addstr(&buf, root);
542 strbuf_addstr(&buf, pathinfo);
543 return strbuf_detach(&buf, NULL);
544 } else if (path && *path) {
545 return xstrdup(path);
546 } else
547 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
548 return NULL;
551 static struct service_cmd {
552 const char *method;
553 const char *pattern;
554 void (*imp)(char *);
555 } services[] = {
556 {"GET", "/HEAD$", get_text_file},
557 {"GET", "/info/refs$", get_info_refs},
558 {"GET", "/objects/info/alternates$", get_text_file},
559 {"GET", "/objects/info/http-alternates$", get_text_file},
560 {"GET", "/objects/info/packs$", get_info_packs},
561 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
562 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
563 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
565 {"POST", "/git-upload-pack$", service_rpc},
566 {"POST", "/git-receive-pack$", service_rpc}
569 int main(int argc, char **argv)
571 char *method = getenv("REQUEST_METHOD");
572 char *dir;
573 struct service_cmd *cmd = NULL;
574 char *cmd_arg = NULL;
575 int i;
577 git_extract_argv0_path(argv[0]);
578 set_die_routine(die_webcgi);
580 if (!method)
581 die("No REQUEST_METHOD from server");
582 if (!strcmp(method, "HEAD"))
583 method = "GET";
584 dir = getdir();
586 for (i = 0; i < ARRAY_SIZE(services); i++) {
587 struct service_cmd *c = &services[i];
588 regex_t re;
589 regmatch_t out[1];
591 if (regcomp(&re, c->pattern, REG_EXTENDED))
592 die("Bogus regex in service table: %s", c->pattern);
593 if (!regexec(&re, dir, 1, out, 0)) {
594 size_t n = out[0].rm_eo - out[0].rm_so;
596 if (strcmp(method, c->method)) {
597 const char *proto = getenv("SERVER_PROTOCOL");
598 if (proto && !strcmp(proto, "HTTP/1.1"))
599 http_status(405, "Method Not Allowed");
600 else
601 http_status(400, "Bad Request");
602 hdr_nocache();
603 end_headers();
604 return 0;
607 cmd = c;
608 cmd_arg = xmalloc(n);
609 strncpy(cmd_arg, dir + out[0].rm_so + 1, n);
610 cmd_arg[n] = '\0';
611 dir[out[0].rm_so] = 0;
612 break;
614 regfree(&re);
617 if (!cmd)
618 not_found("Request not supported: '%s'", dir);
620 setup_path();
621 if (!enter_repo(dir, 0))
622 not_found("Not a git repository: '%s'", dir);
624 cmd->imp(cmd_arg);
625 return 0;