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