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