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