Smart fetch and push over HTTP: server side
[git/git.git] / http-backend.c
CommitLineData
2f4038ab
SP
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"
556cfa3b
SP
7#include "run-command.h"
8#include "string-list.h"
2f4038ab
SP
9
10static const char content_type[] = "Content-Type";
11static const char content_length[] = "Content-Length";
12static const char last_modified[] = "Last-Modified";
13
556cfa3b
SP
14static struct string_list *query_params;
15
16struct rpc_service {
17 const char *name;
18 const char *config_name;
19 signed enabled : 2;
20};
21
22static struct rpc_service rpc_service[] = {
23 { "upload-pack", "uploadpack", 1 },
24 { "receive-pack", "receivepack", -1 },
25};
26
27static int decode_char(const char *q)
28{
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;
42 }
43 return val;
44}
45
46static char *decode_parameter(const char **query, int is_name)
47{
48 const char *q = *query;
49 struct strbuf out;
50
51 strbuf_init(&out, 16);
52 do {
53 unsigned char c = *q;
54
55 if (!c)
56 break;
57 if (c == '&' || (is_name && c == '=')) {
58 q++;
59 break;
60 }
61
62 if (c == '%') {
63 int val = decode_char(q + 1);
64 if (0 <= val) {
65 strbuf_addch(&out, val);
66 q += 3;
67 continue;
68 }
69 }
70
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);
79}
80
81static struct string_list *get_parameters(void)
82{
83 if (!query_params) {
84 const char *query = getenv("QUERY_STRING");
85
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;
91
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;
98 }
99 }
100 return query_params;
101}
102
103static const char *get_parameter(const char *name)
104{
105 struct string_list_item *i;
106 i = string_list_lookup(name, get_parameters());
107 return i ? i->util : NULL;
108}
109
2f4038ab
SP
110static void format_write(int fd, const char *fmt, ...)
111{
112 static char buffer[1024];
113
114 va_list args;
115 unsigned n;
116
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");
122
123 safe_write(fd, buffer, n);
124}
125
126static void http_status(unsigned code, const char *msg)
127{
128 format_write(1, "Status: %u %s\r\n", code, msg);
129}
130
131static void hdr_str(const char *name, const char *value)
132{
133 format_write(1, "%s: %s\r\n", name, value);
134}
135
136static void hdr_int(const char *name, size_t value)
137{
138 format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
139}
140
141static void hdr_date(const char *name, unsigned long when)
142{
143 const char *value = show_date(when, 0, DATE_RFC2822);
144 hdr_str(name, value);
145}
146
147static void hdr_nocache(void)
148{
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");
152}
153
154static void hdr_cache_forever(void)
155{
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");
160}
161
162static void end_headers(void)
163{
164 safe_write(1, "\r\n", 2);
165}
166
167static NORETURN void not_found(const char *err, ...)
168{
169 va_list params;
170
171 http_status(404, "Not Found");
172 hdr_nocache();
173 end_headers();
174
175 va_start(params, err);
176 if (err && *err)
177 vfprintf(stderr, err, params);
178 va_end(params);
179 exit(0);
180}
181
556cfa3b
SP
182static NORETURN void forbidden(const char *err, ...)
183{
184 va_list params;
185
186 http_status(403, "Forbidden");
187 hdr_nocache();
188 end_headers();
189
190 va_start(params, err);
191 if (err && *err)
192 vfprintf(stderr, err, params);
193 va_end(params);
194 exit(0);
195}
196
2f4038ab
SP
197static void send_strbuf(const char *type, struct strbuf *buf)
198{
199 hdr_int(content_length, buf->len);
200 hdr_str(content_type, type);
201 end_headers();
202 safe_write(1, buf->buf, buf->len);
203}
204
205static void send_file(const char *the_type, const char *name)
206{
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;
213
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);
219
220 size = xsize_t(sb.st_size);
221
222 hdr_int(content_length, size);
223 hdr_str(content_type, the_type);
224 hdr_date(last_modified, sb.st_mtime);
225 end_headers();
226
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);
234 }
235 close(fd);
236 free(buf);
237}
238
239static void get_text_file(char *name)
240{
241 hdr_nocache();
242 send_file("text/plain", name);
243}
244
245static void get_loose_object(char *name)
246{
247 hdr_cache_forever();
248 send_file("application/x-git-loose-object", name);
249}
250
251static void get_pack_file(char *name)
252{
253 hdr_cache_forever();
254 send_file("application/x-git-packed-objects", name);
255}
256
257static void get_idx_file(char *name)
258{
259 hdr_cache_forever();
260 send_file("application/x-git-packed-objects-toc", name);
261}
262
556cfa3b
SP
263static int http_config(const char *var, const char *value, void *cb)
264{
265 struct rpc_service *svc = cb;
266
267 if (!prefixcmp(var, "http.") &&
268 !strcmp(var + 5, svc->config_name)) {
269 svc->enabled = git_config_bool(var, value);
270 return 0;
271 }
272
273 /* we are not interested in parsing any other configuration here */
274 return 0;
275}
276
277static struct rpc_service *select_service(const char *name)
278{
279 struct rpc_service *svc = NULL;
280 int i;
281
282 if (prefixcmp(name, "git-"))
283 forbidden("Unsupported service: '%s'", name);
284
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;
290 }
291 }
292
293 if (!svc)
294 forbidden("Unsupported service: '%s'", name);
295
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;
300 }
301 if (!svc->enabled)
302 forbidden("Service not enabled: '%s'", svc->name);
303 return svc;
304}
305
306static void inflate_request(const char *prog_name, int out)
307{
308 z_stream stream;
309 unsigned char in_buf[8192];
310 unsigned char out_buf[8192];
311 unsigned long cnt = 0;
312 int ret;
313
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);
318
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");
323
324 stream.next_in = in_buf;
325 stream.avail_in = n;
326
327 while (0 < stream.avail_in) {
328 int ret;
329
330 stream.next_out = out_buf;
331 stream.avail_out = sizeof(out_buf);
332
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);
336
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;
341
342 if (ret == Z_STREAM_END)
343 goto done;
344 }
345 }
346
347done:
348 inflateEnd(&stream);
349 close(out);
350}
351
352static void run_service(const char **argv)
353{
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;
361
362 if (encoding && !strcmp(encoding, "gzip"))
363 gzipped_request = 1;
364 else if (encoding && !strcmp(encoding, "x-gzip"))
365 gzipped_request = 1;
366
367 if (!user || !*user)
368 user = "anonymous";
369 if (!host || !*host)
370 host = "(none)";
371
372 memset(&env, 0, sizeof(env));
373 strbuf_addf(&buf, "GIT_COMMITTER_NAME=%s", user);
374 env[0] = strbuf_detach(&buf, NULL);
375
376 strbuf_addf(&buf, "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
377 env[1] = strbuf_detach(&buf, NULL);
378 env[2] = NULL;
379
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);
388
389 close(1);
390 if (gzipped_request)
391 inflate_request(argv[0], cld.in);
392 else
393 close(0);
394
395 if (finish_command(&cld))
396 exit(1);
397 free(env[0]);
398 free(env[1]);
399 strbuf_release(&buf);
400}
401
2f4038ab
SP
402static int show_text_ref(const char *name, const unsigned char *sha1,
403 int flag, void *cb_data)
404{
405 struct strbuf *buf = cb_data;
406 struct object *o = parse_object(sha1);
407 if (!o)
408 return 0;
409
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);
416 }
417 return 0;
418}
419
420static void get_info_refs(char *arg)
421{
556cfa3b 422 const char *service_name = get_parameter("service");
2f4038ab
SP
423 struct strbuf buf = STRBUF_INIT;
424
2f4038ab 425 hdr_nocache();
556cfa3b
SP
426
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);
432
433 strbuf_addf(&buf, "application/x-git-%s-advertisement",
434 svc->name);
435 hdr_str(content_type, buf.buf);
436 end_headers();
437
438 packet_write(1, "# service=git-%s\n", svc->name);
439 packet_flush(1);
440
441 argv[0] = svc->name;
442 run_service(argv);
443
444 } else {
445 for_each_ref(show_text_ref, &buf);
446 send_strbuf("text/plain", &buf);
447 }
2f4038ab
SP
448 strbuf_release(&buf);
449}
450
451static void get_info_packs(char *arg)
452{
453 size_t objdirlen = strlen(get_object_directory());
454 struct strbuf buf = STRBUF_INIT;
455 struct packed_git *p;
456 size_t cnt = 0;
457
458 prepare_packed_git();
459 for (p = packed_git; p; p = p->next) {
460 if (p->pack_local)
461 cnt++;
462 }
463
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);
468 }
469 strbuf_addch(&buf, '\n');
470
471 hdr_nocache();
472 send_strbuf("text/plain; charset=utf-8", &buf);
473 strbuf_release(&buf);
474}
475
556cfa3b
SP
476static void check_content_type(const char *accepted_type)
477{
478 const char *actual_type = getenv("CONTENT_TYPE");
479
480 if (!actual_type)
481 actual_type = "";
482
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);
492 }
493}
494
495static void service_rpc(char *service_name)
496{
497 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
498 struct rpc_service *svc = select_service(service_name);
499 struct strbuf buf = STRBUF_INIT;
500
501 strbuf_reset(&buf);
502 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
503 check_content_type(buf.buf);
504
505 hdr_nocache();
506
507 strbuf_reset(&buf);
508 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
509 hdr_str(content_type, buf.buf);
510
511 end_headers();
512
513 argv[0] = svc->name;
514 run_service(argv);
515 strbuf_release(&buf);
516}
517
2f4038ab
SP
518static NORETURN void die_webcgi(const char *err, va_list params)
519{
520 char buffer[1000];
521
522 http_status(500, "Internal Server Error");
523 hdr_nocache();
524 end_headers();
525
526 vsnprintf(buffer, sizeof(buffer), err, params);
527 fprintf(stderr, "fatal: %s\n", buffer);
528 exit(0);
529}
530
531static struct service_cmd {
532 const char *method;
533 const char *pattern;
534 void (*imp)(char *);
535} services[] = {
536 {"GET", "/HEAD$", get_text_file},
537 {"GET", "/info/refs$", get_info_refs},
538 {"GET", "/objects/info/alternates$", get_text_file},
539 {"GET", "/objects/info/http-alternates$", get_text_file},
540 {"GET", "/objects/info/packs$", get_info_packs},
541 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
542 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
556cfa3b
SP
543 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
544
545 {"POST", "/git-upload-pack$", service_rpc},
546 {"POST", "/git-receive-pack$", service_rpc}
2f4038ab
SP
547};
548
549int main(int argc, char **argv)
550{
551 char *method = getenv("REQUEST_METHOD");
552 char *dir = getenv("PATH_TRANSLATED");
553 struct service_cmd *cmd = NULL;
554 char *cmd_arg = NULL;
555 int i;
556
557 git_extract_argv0_path(argv[0]);
558 set_die_routine(die_webcgi);
559
560 if (!method)
561 die("No REQUEST_METHOD from server");
562 if (!strcmp(method, "HEAD"))
563 method = "GET";
564 if (!dir)
565 die("No PATH_TRANSLATED from server");
566
567 for (i = 0; i < ARRAY_SIZE(services); i++) {
568 struct service_cmd *c = &services[i];
569 regex_t re;
570 regmatch_t out[1];
571
572 if (regcomp(&re, c->pattern, REG_EXTENDED))
573 die("Bogus regex in service table: %s", c->pattern);
574 if (!regexec(&re, dir, 1, out, 0)) {
575 size_t n = out[0].rm_eo - out[0].rm_so;
576
577 if (strcmp(method, c->method)) {
578 const char *proto = getenv("SERVER_PROTOCOL");
579 if (proto && !strcmp(proto, "HTTP/1.1"))
580 http_status(405, "Method Not Allowed");
581 else
582 http_status(400, "Bad Request");
583 hdr_nocache();
584 end_headers();
585 return 0;
586 }
587
588 cmd = c;
589 cmd_arg = xmalloc(n);
590 strncpy(cmd_arg, dir + out[0].rm_so + 1, n);
591 cmd_arg[n] = '\0';
592 dir[out[0].rm_so] = 0;
593 break;
594 }
595 regfree(&re);
596 }
597
598 if (!cmd)
599 not_found("Request not supported: '%s'", dir);
600
601 setup_path();
602 if (!enter_repo(dir, 0))
603 not_found("Not a git repository: '%s'", dir);
604
605 cmd->imp(cmd_arg);
606 return 0;
607}