]> git.ipfire.org Git - thirdparty/git.git/blame - http-backend.c
treewide: remove unnecessary includes for wrapper.h
[thirdparty/git.git] / http-backend.c
CommitLineData
d530c04e 1#include "git-compat-util.h"
36bf1958 2#include "alloc.h"
b2141fc1 3#include "config.h"
32a8f510 4#include "environment.h"
d88dbaa7 5#include "git-zlib.h"
41771fa4 6#include "hex.h"
c339932b 7#include "path.h"
a80d72db 8#include "repository.h"
2f4038ab
SP
9#include "refs.h"
10#include "pkt-line.h"
11#include "object.h"
12#include "tag.h"
d807c4a0 13#include "exec-cmd.h"
556cfa3b
SP
14#include "run-command.h"
15#include "string-list.h"
638794cd 16#include "url.h"
dbbcd44f 17#include "strvec.h"
0abe14f6 18#include "packfile.h"
a034e910 19#include "object-store-ll.h"
237ffedd 20#include "protocol.h"
88c7b4c3 21#include "date.h"
d48be35c 22#include "write-or-die.h"
2f4038ab
SP
23
24static const char content_type[] = "Content-Type";
25static const char content_length[] = "Content-Length";
26static const char last_modified[] = "Last-Modified";
5abb013b 27static int getanyfile = 1;
6bc0cb51 28static unsigned long max_request_buffer = 10 * 1024 * 1024;
2f4038ab 29
556cfa3b
SP
30static struct string_list *query_params;
31
32struct rpc_service {
33 const char *name;
34 const char *config_name;
6bc0cb51 35 unsigned buffer_input : 1;
556cfa3b
SP
36 signed enabled : 2;
37};
38
39static struct rpc_service rpc_service[] = {
6bc0cb51
JK
40 { "upload-pack", "uploadpack", 1, 1 },
41 { "receive-pack", "receivepack", 0, -1 },
556cfa3b
SP
42};
43
556cfa3b
SP
44static struct string_list *get_parameters(void)
45{
46 if (!query_params) {
47 const char *query = getenv("QUERY_STRING");
48
ca56dadb 49 CALLOC_ARRAY(query_params, 1);
556cfa3b 50 while (query && *query) {
638794cd
JK
51 char *name = url_decode_parameter_name(&query);
52 char *value = url_decode_parameter_value(&query);
556cfa3b
SP
53 struct string_list_item *i;
54
e8c8b713 55 i = string_list_lookup(query_params, name);
556cfa3b 56 if (!i)
78a395d3 57 i = string_list_insert(query_params, name);
556cfa3b
SP
58 else
59 free(i->util);
60 i->util = value;
61 }
62 }
63 return query_params;
64}
65
66static const char *get_parameter(const char *name)
67{
68 struct string_list_item *i;
e8c8b713 69 i = string_list_lookup(get_parameters(), name);
556cfa3b
SP
70 return i ? i->util : NULL;
71}
72
35487017 73__attribute__((format (printf, 2, 3)))
2f4038ab
SP
74static void format_write(int fd, const char *fmt, ...)
75{
76 static char buffer[1024];
77
78 va_list args;
79 unsigned n;
80
81 va_start(args, fmt);
82 n = vsnprintf(buffer, sizeof(buffer), fmt, args);
83 va_end(args);
84 if (n >= sizeof(buffer))
85 die("protocol error: impossibly long line");
86
cdf4fb8e 87 write_or_die(fd, buffer, n);
2f4038ab
SP
88}
89
b36045c1 90static void http_status(struct strbuf *hdr, unsigned code, const char *msg)
2f4038ab 91{
b36045c1 92 strbuf_addf(hdr, "Status: %u %s\r\n", code, msg);
2f4038ab
SP
93}
94
b36045c1 95static void hdr_str(struct strbuf *hdr, const char *name, const char *value)
2f4038ab 96{
b36045c1 97 strbuf_addf(hdr, "%s: %s\r\n", name, value);
2f4038ab
SP
98}
99
b36045c1 100static void hdr_int(struct strbuf *hdr, const char *name, uintmax_t value)
2f4038ab 101{
b36045c1 102 strbuf_addf(hdr, "%s: %" PRIuMAX "\r\n", name, value);
2f4038ab
SP
103}
104
dddbad72 105static void hdr_date(struct strbuf *hdr, const char *name, timestamp_t when)
2f4038ab 106{
a5481a6c 107 const char *value = show_date(when, 0, DATE_MODE(RFC2822));
b36045c1 108 hdr_str(hdr, name, value);
2f4038ab
SP
109}
110
b36045c1 111static void hdr_nocache(struct strbuf *hdr)
2f4038ab 112{
b36045c1
EW
113 hdr_str(hdr, "Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
114 hdr_str(hdr, "Pragma", "no-cache");
115 hdr_str(hdr, "Cache-Control", "no-cache, max-age=0, must-revalidate");
2f4038ab
SP
116}
117
b36045c1 118static void hdr_cache_forever(struct strbuf *hdr)
2f4038ab 119{
dddbad72 120 timestamp_t now = time(NULL);
b36045c1
EW
121 hdr_date(hdr, "Date", now);
122 hdr_date(hdr, "Expires", now + 31536000);
123 hdr_str(hdr, "Cache-Control", "public, max-age=31536000");
2f4038ab
SP
124}
125
b36045c1 126static void end_headers(struct strbuf *hdr)
2f4038ab 127{
b36045c1
EW
128 strbuf_add(hdr, "\r\n", 2);
129 write_or_die(1, hdr->buf, hdr->len);
130 strbuf_release(hdr);
2f4038ab
SP
131}
132
b36045c1
EW
133__attribute__((format (printf, 2, 3)))
134static NORETURN void not_found(struct strbuf *hdr, const char *err, ...)
2f4038ab
SP
135{
136 va_list params;
137
b36045c1
EW
138 http_status(hdr, 404, "Not Found");
139 hdr_nocache(hdr);
140 end_headers(hdr);
2f4038ab
SP
141
142 va_start(params, err);
143 if (err && *err)
144 vfprintf(stderr, err, params);
145 va_end(params);
146 exit(0);
147}
148
b36045c1
EW
149__attribute__((format (printf, 2, 3)))
150static NORETURN void forbidden(struct strbuf *hdr, const char *err, ...)
556cfa3b
SP
151{
152 va_list params;
153
b36045c1
EW
154 http_status(hdr, 403, "Forbidden");
155 hdr_nocache(hdr);
156 end_headers(hdr);
556cfa3b
SP
157
158 va_start(params, err);
159 if (err && *err)
160 vfprintf(stderr, err, params);
161 va_end(params);
162 exit(0);
163}
164
b36045c1 165static void select_getanyfile(struct strbuf *hdr)
5abb013b
SP
166{
167 if (!getanyfile)
b36045c1 168 forbidden(hdr, "Unsupported service: getanyfile");
5abb013b
SP
169}
170
b36045c1
EW
171static void send_strbuf(struct strbuf *hdr,
172 const char *type, struct strbuf *buf)
2f4038ab 173{
b36045c1
EW
174 hdr_int(hdr, content_length, buf->len);
175 hdr_str(hdr, content_type, type);
176 end_headers(hdr);
cdf4fb8e 177 write_or_die(1, buf->buf, buf->len);
2f4038ab
SP
178}
179
b36045c1
EW
180static void send_local_file(struct strbuf *hdr, const char *the_type,
181 const char *name)
2f4038ab 182{
fcd12db6 183 char *p = git_pathdup("%s", name);
2f4038ab
SP
184 size_t buf_alloc = 8192;
185 char *buf = xmalloc(buf_alloc);
186 int fd;
187 struct stat sb;
2f4038ab
SP
188
189 fd = open(p, O_RDONLY);
190 if (fd < 0)
b36045c1 191 not_found(hdr, "Cannot open '%s': %s", p, strerror(errno));
2f4038ab
SP
192 if (fstat(fd, &sb) < 0)
193 die_errno("Cannot stat '%s'", p);
194
b36045c1
EW
195 hdr_int(hdr, content_length, sb.st_size);
196 hdr_str(hdr, content_type, the_type);
197 hdr_date(hdr, last_modified, sb.st_mtime);
198 end_headers(hdr);
2f4038ab 199
4a5328d6 200 for (;;) {
2f4038ab
SP
201 ssize_t n = xread(fd, buf, buf_alloc);
202 if (n < 0)
203 die_errno("Cannot read '%s'", p);
204 if (!n)
205 break;
cdf4fb8e 206 write_or_die(1, buf, n);
2f4038ab
SP
207 }
208 close(fd);
209 free(buf);
fcd12db6 210 free(p);
2f4038ab
SP
211}
212
b36045c1 213static void get_text_file(struct strbuf *hdr, char *name)
2f4038ab 214{
b36045c1
EW
215 select_getanyfile(hdr);
216 hdr_nocache(hdr);
217 send_local_file(hdr, "text/plain", name);
2f4038ab
SP
218}
219
b36045c1 220static void get_loose_object(struct strbuf *hdr, char *name)
2f4038ab 221{
b36045c1
EW
222 select_getanyfile(hdr);
223 hdr_cache_forever(hdr);
224 send_local_file(hdr, "application/x-git-loose-object", name);
2f4038ab
SP
225}
226
b36045c1 227static void get_pack_file(struct strbuf *hdr, char *name)
2f4038ab 228{
b36045c1
EW
229 select_getanyfile(hdr);
230 hdr_cache_forever(hdr);
231 send_local_file(hdr, "application/x-git-packed-objects", name);
2f4038ab
SP
232}
233
b36045c1 234static void get_idx_file(struct strbuf *hdr, char *name)
2f4038ab 235{
b36045c1
EW
236 select_getanyfile(hdr);
237 hdr_cache_forever(hdr);
238 send_local_file(hdr, "application/x-git-packed-objects-toc", name);
2f4038ab
SP
239}
240
6881f0cc 241static void http_config(void)
556cfa3b 242{
6881f0cc
TA
243 int i, value = 0;
244 struct strbuf var = STRBUF_INIT;
ae021d87 245
6881f0cc 246 git_config_get_bool("http.getanyfile", &getanyfile);
6bc0cb51 247 git_config_get_ulong("http.maxrequestbuffer", &max_request_buffer);
556cfa3b 248
6881f0cc
TA
249 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
250 struct rpc_service *svc = &rpc_service[i];
251 strbuf_addf(&var, "http.%s", svc->config_name);
252 if (!git_config_get_bool(var.buf, &value))
253 svc->enabled = value;
254 strbuf_reset(&var);
5abb013b
SP
255 }
256
6881f0cc 257 strbuf_release(&var);
556cfa3b
SP
258}
259
b36045c1 260static struct rpc_service *select_service(struct strbuf *hdr, const char *name)
556cfa3b 261{
ae021d87 262 const char *svc_name;
556cfa3b
SP
263 struct rpc_service *svc = NULL;
264 int i;
265
ae021d87 266 if (!skip_prefix(name, "git-", &svc_name))
b36045c1 267 forbidden(hdr, "Unsupported service: '%s'", name);
556cfa3b
SP
268
269 for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
270 struct rpc_service *s = &rpc_service[i];
ae021d87 271 if (!strcmp(s->name, svc_name)) {
556cfa3b
SP
272 svc = s;
273 break;
274 }
275 }
276
277 if (!svc)
b36045c1 278 forbidden(hdr, "Unsupported service: '%s'", name);
556cfa3b 279
556cfa3b
SP
280 if (svc->enabled < 0) {
281 const char *user = getenv("REMOTE_USER");
282 svc->enabled = (user && *user) ? 1 : 0;
283 }
284 if (!svc->enabled)
b36045c1 285 forbidden(hdr, "Service not enabled: '%s'", svc->name);
556cfa3b
SP
286 return svc;
287}
288
6b1fae1d
MK
289static void write_to_child(int out, const unsigned char *buf, ssize_t len, const char *prog_name)
290{
291 if (write_in_full(out, buf, len) < 0)
292 die("unable to write to '%s'", prog_name);
293}
294
6bc0cb51
JK
295/*
296 * This is basically strbuf_read(), except that if we
297 * hit max_request_buffer we die (we'd rather reject a
298 * maliciously large request than chew up infinite memory).
299 */
c79edf73 300static ssize_t read_request_eof(int fd, unsigned char **out)
6bc0cb51
JK
301{
302 size_t len = 0, alloc = 8192;
303 unsigned char *buf = xmalloc(alloc);
304
305 if (max_request_buffer < alloc)
306 max_request_buffer = alloc;
307
308 while (1) {
309 ssize_t cnt;
310
311 cnt = read_in_full(fd, buf + len, alloc - len);
312 if (cnt < 0) {
313 free(buf);
314 return -1;
315 }
316
317 /* partial read from read_in_full means we hit EOF */
318 len += cnt;
319 if (len < alloc) {
320 *out = buf;
321 return len;
322 }
323
324 /* otherwise, grow and try again (if we can) */
325 if (alloc == max_request_buffer)
326 die("request was larger than our maximum size (%lu);"
327 " try setting GIT_HTTP_MAX_REQUEST_BUFFER",
328 max_request_buffer);
329
330 alloc = alloc_nr(alloc);
331 if (alloc > max_request_buffer)
332 alloc = max_request_buffer;
333 REALLOC_ARRAY(buf, alloc);
334 }
335}
336
c79edf73
MK
337static ssize_t read_request_fixed_len(int fd, ssize_t req_len, unsigned char **out)
338{
339 unsigned char *buf = NULL;
340 ssize_t cnt = 0;
341
342 if (max_request_buffer < req_len) {
343 die("request was larger than our maximum size (%lu): "
344 "%" PRIuMAX "; try setting GIT_HTTP_MAX_REQUEST_BUFFER",
345 max_request_buffer, (uintmax_t)req_len);
346 }
347
348 buf = xmalloc(req_len);
349 cnt = read_in_full(fd, buf, req_len);
350 if (cnt < 0) {
351 free(buf);
352 return -1;
353 }
354 *out = buf;
355 return cnt;
356}
357
358static ssize_t get_content_length(void)
359{
360 ssize_t val = -1;
361 const char *str = getenv("CONTENT_LENGTH");
362
574c513e 363 if (str && *str && !git_parse_ssize_t(str, &val))
c79edf73
MK
364 die("failed to parse CONTENT_LENGTH: %s", str);
365 return val;
366}
367
368static ssize_t read_request(int fd, unsigned char **out, ssize_t req_len)
369{
370 if (req_len < 0)
371 return read_request_eof(fd, out);
372 else
373 return read_request_fixed_len(fd, req_len, out);
374}
375
376static void inflate_request(const char *prog_name, int out, int buffer_input, ssize_t req_len)
556cfa3b 377{
ef49a7a0 378 git_zstream stream;
6bc0cb51 379 unsigned char *full_request = NULL;
556cfa3b
SP
380 unsigned char in_buf[8192];
381 unsigned char out_buf[8192];
382 unsigned long cnt = 0;
6c213e86
MK
383 int req_len_defined = req_len >= 0;
384 size_t req_remaining_len = req_len;
556cfa3b
SP
385
386 memset(&stream, 0, sizeof(stream));
5e86c1fb 387 git_inflate_init_gzip_only(&stream);
556cfa3b
SP
388
389 while (1) {
6bc0cb51
JK
390 ssize_t n;
391
392 if (buffer_input) {
393 if (full_request)
394 n = 0; /* nothing left to read */
395 else
c79edf73 396 n = read_request(0, &full_request, req_len);
6bc0cb51
JK
397 stream.next_in = full_request;
398 } else {
6c213e86
MK
399 ssize_t buffer_len;
400 if (req_len_defined && req_remaining_len <= sizeof(in_buf))
401 buffer_len = req_remaining_len;
402 else
403 buffer_len = sizeof(in_buf);
404 n = xread(0, in_buf, buffer_len);
6bc0cb51 405 stream.next_in = in_buf;
6c213e86
MK
406 if (req_len_defined && n > 0)
407 req_remaining_len -= n;
6bc0cb51
JK
408 }
409
556cfa3b
SP
410 if (n <= 0)
411 die("request ended in the middle of the gzip stream");
556cfa3b
SP
412 stream.avail_in = n;
413
414 while (0 < stream.avail_in) {
415 int ret;
416
417 stream.next_out = out_buf;
418 stream.avail_out = sizeof(out_buf);
419
9e7e5ca3 420 ret = git_inflate(&stream, Z_NO_FLUSH);
556cfa3b
SP
421 if (ret != Z_OK && ret != Z_STREAM_END)
422 die("zlib error inflating request, result %d", ret);
423
424 n = stream.total_out - cnt;
6b1fae1d
MK
425 write_to_child(out, out_buf, stream.total_out - cnt, prog_name);
426 cnt = stream.total_out;
556cfa3b
SP
427
428 if (ret == Z_STREAM_END)
429 goto done;
430 }
431 }
432
433done:
9e7e5ca3 434 git_inflate_end(&stream);
556cfa3b 435 close(out);
6bc0cb51
JK
436 free(full_request);
437}
438
c79edf73 439static void copy_request(const char *prog_name, int out, ssize_t req_len)
6bc0cb51
JK
440{
441 unsigned char *buf;
c79edf73 442 ssize_t n = read_request(0, &buf, req_len);
6bc0cb51
JK
443 if (n < 0)
444 die_errno("error reading request body");
6b1fae1d 445 write_to_child(out, buf, n, prog_name);
6bc0cb51
JK
446 close(out);
447 free(buf);
556cfa3b
SP
448}
449
6c213e86
MK
450static void pipe_fixed_length(const char *prog_name, int out, size_t req_len)
451{
452 unsigned char buf[8192];
453 size_t remaining_len = req_len;
454
455 while (remaining_len > 0) {
456 size_t chunk_length = remaining_len > sizeof(buf) ? sizeof(buf) : remaining_len;
457 ssize_t n = xread(0, buf, chunk_length);
458 if (n < 0)
459 die_errno("Reading request failed");
460 write_to_child(out, buf, n, prog_name);
461 remaining_len -= n;
462 }
463
464 close(out);
465}
466
6bc0cb51 467static void run_service(const char **argv, int buffer_input)
556cfa3b
SP
468{
469 const char *encoding = getenv("HTTP_CONTENT_ENCODING");
470 const char *user = getenv("REMOTE_USER");
471 const char *host = getenv("REMOTE_ADDR");
556cfa3b 472 int gzipped_request = 0;
d3180279 473 struct child_process cld = CHILD_PROCESS_INIT;
c79edf73 474 ssize_t req_len = get_content_length();
556cfa3b 475
12144e8f 476 if (encoding && (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")))
556cfa3b
SP
477 gzipped_request = 1;
478
479 if (!user || !*user)
480 user = "anonymous";
481 if (!host || !*host)
482 host = "(none)";
483
e32a4581 484 if (!getenv("GIT_COMMITTER_NAME"))
29fda24d 485 strvec_pushf(&cld.env, "GIT_COMMITTER_NAME=%s", user);
e32a4581 486 if (!getenv("GIT_COMMITTER_EMAIL"))
29fda24d 487 strvec_pushf(&cld.env,
f6d8942b 488 "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
556cfa3b 489
6def0ff8 490 strvec_pushv(&cld.args, argv);
6c213e86 491 if (buffer_input || gzipped_request || req_len >= 0)
556cfa3b
SP
492 cld.in = -1;
493 cld.git_cmd = 1;
02818a98
MK
494 cld.clean_on_exit = 1;
495 cld.wait_after_clean = 1;
556cfa3b
SP
496 if (start_command(&cld))
497 exit(1);
498
499 close(1);
500 if (gzipped_request)
c79edf73 501 inflate_request(argv[0], cld.in, buffer_input, req_len);
6bc0cb51 502 else if (buffer_input)
c79edf73 503 copy_request(argv[0], cld.in, req_len);
6c213e86
MK
504 else if (req_len >= 0)
505 pipe_fixed_length(argv[0], cld.in, req_len);
556cfa3b
SP
506 else
507 close(0);
508
509 if (finish_command(&cld))
510 exit(1);
556cfa3b
SP
511}
512
f72f5421 513static int show_text_ref(const char *name, const struct object_id *oid,
5cf88fd8 514 int flag UNUSED, void *cb_data)
2f4038ab 515{
6130f86d 516 const char *name_nons = strip_namespace(name);
2f4038ab 517 struct strbuf *buf = cb_data;
109cd76d 518 struct object *o = parse_object(the_repository, oid);
2f4038ab
SP
519 if (!o)
520 return 0;
521
f72f5421 522 strbuf_addf(buf, "%s\t%s\n", oid_to_hex(oid), name_nons);
2f4038ab 523 if (o->type == OBJ_TAG) {
a74093da 524 o = deref_tag(the_repository, o, name, 0);
2f4038ab
SP
525 if (!o)
526 return 0;
f2fd0760 527 strbuf_addf(buf, "%s\t%s^{}\n", oid_to_hex(&o->oid),
6130f86d 528 name_nons);
2f4038ab
SP
529 }
530 return 0;
531}
532
2be1506a 533static void get_info_refs(struct strbuf *hdr, char *arg UNUSED)
2f4038ab 534{
556cfa3b 535 const char *service_name = get_parameter("service");
2f4038ab
SP
536 struct strbuf buf = STRBUF_INIT;
537
b36045c1 538 hdr_nocache(hdr);
556cfa3b
SP
539
540 if (service_name) {
541 const char *argv[] = {NULL /* service name */,
98e2d9d6 542 "--http-backend-info-refs",
556cfa3b 543 ".", NULL};
b36045c1 544 struct rpc_service *svc = select_service(hdr, service_name);
556cfa3b
SP
545
546 strbuf_addf(&buf, "application/x-git-%s-advertisement",
547 svc->name);
b36045c1
EW
548 hdr_str(hdr, content_type, buf.buf);
549 end_headers(hdr);
556cfa3b 550
237ffedd
BW
551
552 if (determine_protocol_version_server() != protocol_v2) {
553 packet_write_fmt(1, "# service=git-%s\n", svc->name);
554 packet_flush(1);
555 }
556cfa3b
SP
556
557 argv[0] = svc->name;
6bc0cb51 558 run_service(argv, 0);
556cfa3b
SP
559
560 } else {
b36045c1 561 select_getanyfile(hdr);
6130f86d 562 for_each_namespaced_ref(show_text_ref, &buf);
b36045c1 563 send_strbuf(hdr, "text/plain", &buf);
556cfa3b 564 }
2f4038ab
SP
565 strbuf_release(&buf);
566}
567
f72f5421
MH
568static int show_head_ref(const char *refname, const struct object_id *oid,
569 int flag, void *cb_data)
6130f86d
JK
570{
571 struct strbuf *buf = cb_data;
572
573 if (flag & REF_ISSYMREF) {
7695d118
RS
574 const char *target = resolve_ref_unsafe(refname,
575 RESOLVE_REF_READING,
744c040b 576 NULL, NULL);
6130f86d 577
7fd12bfb
MH
578 if (target)
579 strbuf_addf(buf, "ref: %s\n", strip_namespace(target));
6130f86d 580 } else {
f72f5421 581 strbuf_addf(buf, "%s\n", oid_to_hex(oid));
6130f86d
JK
582 }
583
584 return 0;
585}
586
2be1506a 587static void get_head(struct strbuf *hdr, char *arg UNUSED)
6130f86d
JK
588{
589 struct strbuf buf = STRBUF_INIT;
590
b36045c1 591 select_getanyfile(hdr);
6130f86d 592 head_ref_namespaced(show_head_ref, &buf);
b36045c1 593 send_strbuf(hdr, "text/plain", &buf);
6130f86d
JK
594 strbuf_release(&buf);
595}
596
2be1506a 597static void get_info_packs(struct strbuf *hdr, char *arg UNUSED)
2f4038ab
SP
598{
599 size_t objdirlen = strlen(get_object_directory());
600 struct strbuf buf = STRBUF_INIT;
601 struct packed_git *p;
602 size_t cnt = 0;
603
b36045c1 604 select_getanyfile(hdr);
454ea2e4 605 for (p = get_all_packs(the_repository); p; p = p->next) {
2f4038ab
SP
606 if (p->pack_local)
607 cnt++;
608 }
609
610 strbuf_grow(&buf, cnt * 53 + 2);
454ea2e4 611 for (p = get_all_packs(the_repository); p; p = p->next) {
2f4038ab
SP
612 if (p->pack_local)
613 strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
614 }
615 strbuf_addch(&buf, '\n');
616
b36045c1
EW
617 hdr_nocache(hdr);
618 send_strbuf(hdr, "text/plain; charset=utf-8", &buf);
2f4038ab
SP
619 strbuf_release(&buf);
620}
621
b36045c1 622static void check_content_type(struct strbuf *hdr, const char *accepted_type)
556cfa3b
SP
623{
624 const char *actual_type = getenv("CONTENT_TYPE");
625
626 if (!actual_type)
627 actual_type = "";
628
629 if (strcmp(actual_type, accepted_type)) {
b36045c1
EW
630 http_status(hdr, 415, "Unsupported Media Type");
631 hdr_nocache(hdr);
632 end_headers(hdr);
556cfa3b
SP
633 format_write(1,
634 "Expected POST with Content-Type '%s',"
635 " but received '%s' instead.\n",
636 accepted_type, actual_type);
637 exit(0);
638 }
639}
640
b36045c1 641static void service_rpc(struct strbuf *hdr, char *service_name)
556cfa3b
SP
642{
643 const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
b36045c1 644 struct rpc_service *svc = select_service(hdr, service_name);
556cfa3b
SP
645 struct strbuf buf = STRBUF_INIT;
646
647 strbuf_reset(&buf);
648 strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
b36045c1 649 check_content_type(hdr, buf.buf);
556cfa3b 650
b36045c1 651 hdr_nocache(hdr);
556cfa3b
SP
652
653 strbuf_reset(&buf);
654 strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
b36045c1 655 hdr_str(hdr, content_type, buf.buf);
556cfa3b 656
b36045c1 657 end_headers(hdr);
556cfa3b
SP
658
659 argv[0] = svc->name;
6bc0cb51 660 run_service(argv, svc->buffer_input);
556cfa3b
SP
661 strbuf_release(&buf);
662}
663
7253a023 664static int dead;
2f4038ab
SP
665static NORETURN void die_webcgi(const char *err, va_list params)
666{
7253a023 667 if (dead <= 1) {
b36045c1 668 struct strbuf hdr = STRBUF_INIT;
e081a7c3 669 report_fn die_message_fn = get_die_message_routine();
b36045c1 670
e081a7c3 671 die_message_fn(err, params);
2f4038ab 672
b36045c1
EW
673 http_status(&hdr, 500, "Internal Server Error");
674 hdr_nocache(&hdr);
675 end_headers(&hdr);
5856b5f5
SP
676 }
677 exit(0); /* we successfully reported a failure ;-) */
2f4038ab
SP
678}
679
7253a023
JK
680static int die_webcgi_recursing(void)
681{
682 return dead++ > 1;
683}
684
917adc03
ML
685static char* getdir(void)
686{
687 struct strbuf buf = STRBUF_INIT;
688 char *pathinfo = getenv("PATH_INFO");
689 char *root = getenv("GIT_PROJECT_ROOT");
690 char *path = getenv("PATH_TRANSLATED");
691
692 if (root && *root) {
693 if (!pathinfo || !*pathinfo)
694 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
34b6cb8b
SP
695 if (daemon_avoid_alias(pathinfo))
696 die("'%s': aliased", pathinfo);
cf688cc2 697 end_url_with_slash(&buf, root);
34b6cb8b
SP
698 if (pathinfo[0] == '/')
699 pathinfo++;
917adc03
ML
700 strbuf_addstr(&buf, pathinfo);
701 return strbuf_detach(&buf, NULL);
702 } else if (path && *path) {
703 return xstrdup(path);
704 } else
705 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
706 return NULL;
707}
708
2f4038ab
SP
709static struct service_cmd {
710 const char *method;
711 const char *pattern;
b36045c1 712 void (*imp)(struct strbuf *, char *);
2f4038ab 713} services[] = {
6130f86d 714 {"GET", "/HEAD$", get_head},
2f4038ab
SP
715 {"GET", "/info/refs$", get_info_refs},
716 {"GET", "/objects/info/alternates$", get_text_file},
717 {"GET", "/objects/info/http-alternates$", get_text_file},
718 {"GET", "/objects/info/packs$", get_info_packs},
719 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
f786ae9f 720 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{62}$", get_loose_object},
2f4038ab 721 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
f786ae9f 722 {"GET", "/objects/pack/pack-[0-9a-f]{64}\\.pack$", get_pack_file},
556cfa3b 723 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
f786ae9f 724 {"GET", "/objects/pack/pack-[0-9a-f]{64}\\.idx$", get_idx_file},
556cfa3b
SP
725
726 {"POST", "/git-upload-pack$", service_rpc},
727 {"POST", "/git-receive-pack$", service_rpc}
2f4038ab
SP
728};
729
b36045c1
EW
730static int bad_request(struct strbuf *hdr, const struct service_cmd *c)
731{
732 const char *proto = getenv("SERVER_PROTOCOL");
733
734 if (proto && !strcmp(proto, "HTTP/1.1")) {
735 http_status(hdr, 405, "Method Not Allowed");
736 hdr_str(hdr, "Allow",
737 !strcmp(c->method, "GET") ? "GET, HEAD" : c->method);
738 } else
739 http_status(hdr, 400, "Bad Request");
740 hdr_nocache(hdr);
741 end_headers(hdr);
742 return 0;
743}
744
77ef8b0e 745int cmd_main(int argc UNUSED, const char **argv UNUSED)
2f4038ab
SP
746{
747 char *method = getenv("REQUEST_METHOD");
ff6a37c9 748 const char *proto_header;
917adc03 749 char *dir;
2f4038ab
SP
750 struct service_cmd *cmd = NULL;
751 char *cmd_arg = NULL;
752 int i;
b36045c1 753 struct strbuf hdr = STRBUF_INIT;
2f4038ab 754
2f4038ab 755 set_die_routine(die_webcgi);
7253a023 756 set_die_is_recursing_routine(die_webcgi_recursing);
2f4038ab
SP
757
758 if (!method)
759 die("No REQUEST_METHOD from server");
760 if (!strcmp(method, "HEAD"))
761 method = "GET";
917adc03 762 dir = getdir();
2f4038ab
SP
763
764 for (i = 0; i < ARRAY_SIZE(services); i++) {
765 struct service_cmd *c = &services[i];
766 regex_t re;
767 regmatch_t out[1];
2139bd02 768 int ret;
2f4038ab
SP
769
770 if (regcomp(&re, c->pattern, REG_EXTENDED))
771 die("Bogus regex in service table: %s", c->pattern);
2139bd02
ÆAB
772 ret = regexec(&re, dir, 1, out, 0);
773 regfree(&re);
774
775 if (!ret) {
48aec1b1 776 size_t n;
2f4038ab 777
b36045c1
EW
778 if (strcmp(method, c->method))
779 return bad_request(&hdr, c);
2f4038ab
SP
780
781 cmd = c;
48aec1b1 782 n = out[0].rm_eo - out[0].rm_so;
5c0b13f8 783 cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);
2f4038ab
SP
784 dir[out[0].rm_so] = 0;
785 break;
786 }
2f4038ab
SP
787 }
788
789 if (!cmd)
b36045c1 790 not_found(&hdr, "Request not supported: '%s'", dir);
2f4038ab
SP
791
792 setup_path();
793 if (!enter_repo(dir, 0))
b36045c1 794 not_found(&hdr, "Not a git repository: '%s'", dir);
8b2bd7cd
TC
795 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
796 access("git-daemon-export-ok", F_OK) )
b36045c1 797 not_found(&hdr, "Repository not exported: '%s'", dir);
eef75d24 798 free(dir);
2f4038ab 799
6881f0cc 800 http_config();
6bc0cb51
JK
801 max_request_buffer = git_env_ulong("GIT_HTTP_MAX_REQUEST_BUFFER",
802 max_request_buffer);
ff6a37c9
JK
803 proto_header = getenv("HTTP_GIT_PROTOCOL");
804 if (proto_header)
805 setenv(GIT_PROTOCOL_ENVIRONMENT, proto_header, 0);
6bc0cb51 806
b36045c1 807 cmd->imp(&hdr, cmd_arg);
eef75d24 808 free(cmd_arg);
2f4038ab
SP
809 return 0;
810}