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