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