Merge branch 'jh/maint-do-not-track-non-branches' into maint
[git/jnareb-git.git] / http-backend.c
blob85015048dd466fb2f2afe12086379ecf579d0a56
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"
9 #include "url.h"
11 static const char content_type[] = "Content-Type";
12 static const char content_length[] = "Content-Length";
13 static const char last_modified[] = "Last-Modified";
14 static int getanyfile = 1;
16 static struct string_list *query_params;
18 struct rpc_service {
19 const char *name;
20 const char *config_name;
21 signed enabled : 2;
24 static struct rpc_service rpc_service[] = {
25 { "upload-pack", "uploadpack", 1 },
26 { "receive-pack", "receivepack", -1 },
29 static struct string_list *get_parameters(void)
31 if (!query_params) {
32 const char *query = getenv("QUERY_STRING");
34 query_params = xcalloc(1, sizeof(*query_params));
35 while (query && *query) {
36 char *name = url_decode_parameter_name(&query);
37 char *value = url_decode_parameter_value(&query);
38 struct string_list_item *i;
40 i = string_list_lookup(query_params, name);
41 if (!i)
42 i = string_list_insert(query_params, name);
43 else
44 free(i->util);
45 i->util = value;
48 return query_params;
51 static const char *get_parameter(const char *name)
53 struct string_list_item *i;
54 i = string_list_lookup(get_parameters(), name);
55 return i ? i->util : NULL;
58 __attribute__((format (printf, 2, 3)))
59 static void format_write(int fd, const char *fmt, ...)
61 static char buffer[1024];
63 va_list args;
64 unsigned n;
66 va_start(args, fmt);
67 n = vsnprintf(buffer, sizeof(buffer), fmt, args);
68 va_end(args);
69 if (n >= sizeof(buffer))
70 die("protocol error: impossibly long line");
72 safe_write(fd, buffer, n);
75 static void http_status(unsigned code, const char *msg)
77 format_write(1, "Status: %u %s\r\n", code, msg);
80 static void hdr_str(const char *name, const char *value)
82 format_write(1, "%s: %s\r\n", name, value);
85 static void hdr_int(const char *name, uintmax_t value)
87 format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
90 static void hdr_date(const char *name, unsigned long when)
92 const char *value = show_date(when, 0, DATE_RFC2822);
93 hdr_str(name, value);
96 static void hdr_nocache(void)
98 hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
99 hdr_str("Pragma", "no-cache");
100 hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
103 static void hdr_cache_forever(void)
105 unsigned long now = time(NULL);
106 hdr_date("Date", now);
107 hdr_date("Expires", now + 31536000);
108 hdr_str("Cache-Control", "public, max-age=31536000");
111 static void end_headers(void)
113 safe_write(1, "\r\n", 2);
116 __attribute__((format (printf, 1, 2)))
117 static NORETURN void not_found(const char *err, ...)
119 va_list params;
121 http_status(404, "Not Found");
122 hdr_nocache();
123 end_headers();
125 va_start(params, err);
126 if (err && *err)
127 vfprintf(stderr, err, params);
128 va_end(params);
129 exit(0);
132 __attribute__((format (printf, 1, 2)))
133 static NORETURN void forbidden(const char *err, ...)
135 va_list params;
137 http_status(403, "Forbidden");
138 hdr_nocache();
139 end_headers();
141 va_start(params, err);
142 if (err && *err)
143 vfprintf(stderr, err, params);
144 va_end(params);
145 exit(0);
148 static void select_getanyfile(void)
150 if (!getanyfile)
151 forbidden("Unsupported service: getanyfile");
154 static void send_strbuf(const char *type, struct strbuf *buf)
156 hdr_int(content_length, buf->len);
157 hdr_str(content_type, type);
158 end_headers();
159 safe_write(1, buf->buf, buf->len);
162 static void send_local_file(const char *the_type, const char *name)
164 const char *p = git_path("%s", name);
165 size_t buf_alloc = 8192;
166 char *buf = xmalloc(buf_alloc);
167 int fd;
168 struct stat sb;
170 fd = open(p, O_RDONLY);
171 if (fd < 0)
172 not_found("Cannot open '%s': %s", p, strerror(errno));
173 if (fstat(fd, &sb) < 0)
174 die_errno("Cannot stat '%s'", p);
176 hdr_int(content_length, sb.st_size);
177 hdr_str(content_type, the_type);
178 hdr_date(last_modified, sb.st_mtime);
179 end_headers();
181 for (;;) {
182 ssize_t n = xread(fd, buf, buf_alloc);
183 if (n < 0)
184 die_errno("Cannot read '%s'", p);
185 if (!n)
186 break;
187 safe_write(1, buf, n);
189 close(fd);
190 free(buf);
193 static void get_text_file(char *name)
195 select_getanyfile();
196 hdr_nocache();
197 send_local_file("text/plain", name);
200 static void get_loose_object(char *name)
202 select_getanyfile();
203 hdr_cache_forever();
204 send_local_file("application/x-git-loose-object", name);
207 static void get_pack_file(char *name)
209 select_getanyfile();
210 hdr_cache_forever();
211 send_local_file("application/x-git-packed-objects", name);
214 static void get_idx_file(char *name)
216 select_getanyfile();
217 hdr_cache_forever();
218 send_local_file("application/x-git-packed-objects-toc", name);
221 static int http_config(const char *var, const char *value, void *cb)
223 if (!strcmp(var, "http.getanyfile")) {
224 getanyfile = git_config_bool(var, value);
225 return 0;
228 if (!prefixcmp(var, "http.")) {
229 int i;
231 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
232 struct rpc_service *svc = &rpc_service[i];
233 if (!strcmp(var + 5, svc->config_name)) {
234 svc->enabled = git_config_bool(var, value);
235 return 0;
240 /* we are not interested in parsing any other configuration here */
241 return 0;
244 static struct rpc_service *select_service(const char *name)
246 struct rpc_service *svc = NULL;
247 int i;
249 if (prefixcmp(name, "git-"))
250 forbidden("Unsupported service: '%s'", name);
252 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
253 struct rpc_service *s = &rpc_service[i];
254 if (!strcmp(s->name, name + 4)) {
255 svc = s;
256 break;
260 if (!svc)
261 forbidden("Unsupported service: '%s'", name);
263 if (svc->enabled < 0) {
264 const char *user = getenv("REMOTE_USER");
265 svc->enabled = (user && *user) ? 1 : 0;
267 if (!svc->enabled)
268 forbidden("Service not enabled: '%s'", svc->name);
269 return svc;
272 static void inflate_request(const char *prog_name, int out)
274 z_stream stream;
275 unsigned char in_buf[8192];
276 unsigned char out_buf[8192];
277 unsigned long cnt = 0;
278 int ret;
280 memset(&stream, 0, sizeof(stream));
281 ret = inflateInit2(&stream, (15 + 16));
282 if (ret != Z_OK)
283 die("cannot start zlib inflater, zlib err %d", ret);
285 while (1) {
286 ssize_t n = xread(0, in_buf, sizeof(in_buf));
287 if (n <= 0)
288 die("request ended in the middle of the gzip stream");
290 stream.next_in = in_buf;
291 stream.avail_in = n;
293 while (0 < stream.avail_in) {
294 int ret;
296 stream.next_out = out_buf;
297 stream.avail_out = sizeof(out_buf);
299 ret = inflate(&stream, Z_NO_FLUSH);
300 if (ret != Z_OK && ret != Z_STREAM_END)
301 die("zlib error inflating request, result %d", ret);
303 n = stream.total_out - cnt;
304 if (write_in_full(out, out_buf, n) != n)
305 die("%s aborted reading request", prog_name);
306 cnt += n;
308 if (ret == Z_STREAM_END)
309 goto done;
313 done:
314 inflateEnd(&stream);
315 close(out);
318 static void run_service(const char **argv)
320 const char *encoding = getenv("HTTP_CONTENT_ENCODING");
321 const char *user = getenv("REMOTE_USER");
322 const char *host = getenv("REMOTE_ADDR");
323 char *env[3];
324 struct strbuf buf = STRBUF_INIT;
325 int gzipped_request = 0;
326 struct child_process cld;
328 if (encoding && !strcmp(encoding, "gzip"))
329 gzipped_request = 1;
330 else if (encoding && !strcmp(encoding, "x-gzip"))
331 gzipped_request = 1;
333 if (!user || !*user)
334 user = "anonymous";
335 if (!host || !*host)
336 host = "(none)";
338 memset(&env, 0, sizeof(env));
339 strbuf_addf(&buf, "GIT_COMMITTER_NAME=%s", user);
340 env[0] = strbuf_detach(&buf, NULL);
342 strbuf_addf(&buf, "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
343 env[1] = strbuf_detach(&buf, NULL);
344 env[2] = NULL;
346 memset(&cld, 0, sizeof(cld));
347 cld.argv = argv;
348 cld.env = (const char *const *)env;
349 if (gzipped_request)
350 cld.in = -1;
351 cld.git_cmd = 1;
352 if (start_command(&cld))
353 exit(1);
355 close(1);
356 if (gzipped_request)
357 inflate_request(argv[0], cld.in);
358 else
359 close(0);
361 if (finish_command(&cld))
362 exit(1);
363 free(env[0]);
364 free(env[1]);
365 strbuf_release(&buf);
368 static int show_text_ref(const char *name, const unsigned char *sha1,
369 int flag, void *cb_data)
371 struct strbuf *buf = cb_data;
372 struct object *o = parse_object(sha1);
373 if (!o)
374 return 0;
376 strbuf_addf(buf, "%s\t%s\n", sha1_to_hex(sha1), name);
377 if (o->type == OBJ_TAG) {
378 o = deref_tag(o, name, 0);
379 if (!o)
380 return 0;
381 strbuf_addf(buf, "%s\t%s^{}\n", sha1_to_hex(o->sha1), name);
383 return 0;
386 static void get_info_refs(char *arg)
388 const char *service_name = get_parameter("service");
389 struct strbuf buf = STRBUF_INIT;
391 hdr_nocache();
393 if (service_name) {
394 const char *argv[] = {NULL /* service name */,
395 "--stateless-rpc", "--advertise-refs",
396 ".", NULL};
397 struct rpc_service *svc = select_service(service_name);
399 strbuf_addf(&buf, "application/x-git-%s-advertisement",
400 svc->name);
401 hdr_str(content_type, buf.buf);
402 end_headers();
404 packet_write(1, "# service=git-%s\n", svc->name);
405 packet_flush(1);
407 argv[0] = svc->name;
408 run_service(argv);
410 } else {
411 select_getanyfile();
412 for_each_ref(show_text_ref, &buf);
413 send_strbuf("text/plain", &buf);
415 strbuf_release(&buf);
418 static void get_info_packs(char *arg)
420 size_t objdirlen = strlen(get_object_directory());
421 struct strbuf buf = STRBUF_INIT;
422 struct packed_git *p;
423 size_t cnt = 0;
425 select_getanyfile();
426 prepare_packed_git();
427 for (p = packed_git; p; p = p->next) {
428 if (p->pack_local)
429 cnt++;
432 strbuf_grow(&buf, cnt * 53 + 2);
433 for (p = packed_git; p; p = p->next) {
434 if (p->pack_local)
435 strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
437 strbuf_addch(&buf, '\n');
439 hdr_nocache();
440 send_strbuf("text/plain; charset=utf-8", &buf);
441 strbuf_release(&buf);
444 static void check_content_type(const char *accepted_type)
446 const char *actual_type = getenv("CONTENT_TYPE");
448 if (!actual_type)
449 actual_type = "";
451 if (strcmp(actual_type, accepted_type)) {
452 http_status(415, "Unsupported Media Type");
453 hdr_nocache();
454 end_headers();
455 format_write(1,
456 "Expected POST with Content-Type '%s',"
457 " but received '%s' instead.\n",
458 accepted_type, actual_type);
459 exit(0);
463 static void service_rpc(char *service_name)
465 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
466 struct rpc_service *svc = select_service(service_name);
467 struct strbuf buf = STRBUF_INIT;
469 strbuf_reset(&buf);
470 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
471 check_content_type(buf.buf);
473 hdr_nocache();
475 strbuf_reset(&buf);
476 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
477 hdr_str(content_type, buf.buf);
479 end_headers();
481 argv[0] = svc->name;
482 run_service(argv);
483 strbuf_release(&buf);
486 static NORETURN void die_webcgi(const char *err, va_list params)
488 static int dead;
490 if (!dead) {
491 dead = 1;
492 http_status(500, "Internal Server Error");
493 hdr_nocache();
494 end_headers();
496 vreportf("fatal: ", err, params);
498 exit(0); /* we successfully reported a failure ;-) */
501 static char* getdir(void)
503 struct strbuf buf = STRBUF_INIT;
504 char *pathinfo = getenv("PATH_INFO");
505 char *root = getenv("GIT_PROJECT_ROOT");
506 char *path = getenv("PATH_TRANSLATED");
508 if (root && *root) {
509 if (!pathinfo || !*pathinfo)
510 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
511 if (daemon_avoid_alias(pathinfo))
512 die("'%s': aliased", pathinfo);
513 end_url_with_slash(&buf, root);
514 if (pathinfo[0] == '/')
515 pathinfo++;
516 strbuf_addstr(&buf, pathinfo);
517 return strbuf_detach(&buf, NULL);
518 } else if (path && *path) {
519 return xstrdup(path);
520 } else
521 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
522 return NULL;
525 static struct service_cmd {
526 const char *method;
527 const char *pattern;
528 void (*imp)(char *);
529 } services[] = {
530 {"GET", "/HEAD$", get_text_file},
531 {"GET", "/info/refs$", get_info_refs},
532 {"GET", "/objects/info/alternates$", get_text_file},
533 {"GET", "/objects/info/http-alternates$", get_text_file},
534 {"GET", "/objects/info/packs$", get_info_packs},
535 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
536 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
537 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
539 {"POST", "/git-upload-pack$", service_rpc},
540 {"POST", "/git-receive-pack$", service_rpc}
543 int main(int argc, char **argv)
545 char *method = getenv("REQUEST_METHOD");
546 char *dir;
547 struct service_cmd *cmd = NULL;
548 char *cmd_arg = NULL;
549 int i;
551 git_extract_argv0_path(argv[0]);
552 set_die_routine(die_webcgi);
554 if (!method)
555 die("No REQUEST_METHOD from server");
556 if (!strcmp(method, "HEAD"))
557 method = "GET";
558 dir = getdir();
560 for (i = 0; i < ARRAY_SIZE(services); i++) {
561 struct service_cmd *c = &services[i];
562 regex_t re;
563 regmatch_t out[1];
565 if (regcomp(&re, c->pattern, REG_EXTENDED))
566 die("Bogus regex in service table: %s", c->pattern);
567 if (!regexec(&re, dir, 1, out, 0)) {
568 size_t n;
570 if (strcmp(method, c->method)) {
571 const char *proto = getenv("SERVER_PROTOCOL");
572 if (proto && !strcmp(proto, "HTTP/1.1"))
573 http_status(405, "Method Not Allowed");
574 else
575 http_status(400, "Bad Request");
576 hdr_nocache();
577 end_headers();
578 return 0;
581 cmd = c;
582 n = out[0].rm_eo - out[0].rm_so;
583 cmd_arg = xmalloc(n);
584 memcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);
585 cmd_arg[n-1] = '\0';
586 dir[out[0].rm_so] = 0;
587 break;
589 regfree(&re);
592 if (!cmd)
593 not_found("Request not supported: '%s'", dir);
595 setup_path();
596 if (!enter_repo(dir, 0))
597 not_found("Not a git repository: '%s'", dir);
598 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
599 access("git-daemon-export-ok", F_OK) )
600 not_found("Repository not exported: '%s'", dir);
602 git_config(http_config, NULL);
603 cmd->imp(cmd_arg);
604 return 0;