7 #include "run-command.h"
8 #include "string-list.h"
10 #include "argv-array.h"
12 static const char content_type
[] = "Content-Type";
13 static const char content_length
[] = "Content-Length";
14 static const char last_modified
[] = "Last-Modified";
15 static int getanyfile
= 1;
16 static unsigned long max_request_buffer
= 10 * 1024 * 1024;
18 static struct string_list
*query_params
;
22 const char *config_name
;
23 unsigned buffer_input
: 1;
27 static struct rpc_service rpc_service
[] = {
28 { "upload-pack", "uploadpack", 1, 1 },
29 { "receive-pack", "receivepack", 0, -1 },
32 static struct string_list
*get_parameters(void)
35 const char *query
= getenv("QUERY_STRING");
37 query_params
= xcalloc(1, sizeof(*query_params
));
38 while (query
&& *query
) {
39 char *name
= url_decode_parameter_name(&query
);
40 char *value
= url_decode_parameter_value(&query
);
41 struct string_list_item
*i
;
43 i
= string_list_lookup(query_params
, name
);
45 i
= string_list_insert(query_params
, name
);
54 static const char *get_parameter(const char *name
)
56 struct string_list_item
*i
;
57 i
= string_list_lookup(get_parameters(), name
);
58 return i
? i
->util
: NULL
;
61 __attribute__((format (printf
, 2, 3)))
62 static void format_write(int fd
, const char *fmt
, ...)
64 static char buffer
[1024];
70 n
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
72 if (n
>= sizeof(buffer
))
73 die("protocol error: impossibly long line");
75 write_or_die(fd
, buffer
, n
);
78 static void http_status(struct strbuf
*hdr
, unsigned code
, const char *msg
)
80 strbuf_addf(hdr
, "Status: %u %s\r\n", code
, msg
);
83 static void hdr_str(struct strbuf
*hdr
, const char *name
, const char *value
)
85 strbuf_addf(hdr
, "%s: %s\r\n", name
, value
);
88 static void hdr_int(struct strbuf
*hdr
, const char *name
, uintmax_t value
)
90 strbuf_addf(hdr
, "%s: %" PRIuMAX
"\r\n", name
, value
);
93 static void hdr_date(struct strbuf
*hdr
, const char *name
, unsigned long when
)
95 const char *value
= show_date(when
, 0, DATE_MODE(RFC2822
));
96 hdr_str(hdr
, name
, value
);
99 static void hdr_nocache(struct strbuf
*hdr
)
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");
106 static void hdr_cache_forever(struct strbuf
*hdr
)
108 unsigned long now
= time(NULL
);
109 hdr_date(hdr
, "Date", now
);
110 hdr_date(hdr
, "Expires", now
+ 31536000);
111 hdr_str(hdr
, "Cache-Control", "public, max-age=31536000");
114 static void end_headers(struct strbuf
*hdr
)
116 strbuf_add(hdr
, "\r\n", 2);
117 write_or_die(1, hdr
->buf
, hdr
->len
);
121 __attribute__((format (printf
, 2, 3)))
122 static NORETURN
void not_found(struct strbuf
*hdr
, const char *err
, ...)
126 http_status(hdr
, 404, "Not Found");
130 va_start(params
, err
);
132 vfprintf(stderr
, err
, params
);
137 __attribute__((format (printf
, 2, 3)))
138 static NORETURN
void forbidden(struct strbuf
*hdr
, const char *err
, ...)
142 http_status(hdr
, 403, "Forbidden");
146 va_start(params
, err
);
148 vfprintf(stderr
, err
, params
);
153 static void select_getanyfile(struct strbuf
*hdr
)
156 forbidden(hdr
, "Unsupported service: getanyfile");
159 static void send_strbuf(struct strbuf
*hdr
,
160 const char *type
, struct strbuf
*buf
)
162 hdr_int(hdr
, content_length
, buf
->len
);
163 hdr_str(hdr
, content_type
, type
);
165 write_or_die(1, buf
->buf
, buf
->len
);
168 static void send_local_file(struct strbuf
*hdr
, const char *the_type
,
171 char *p
= git_pathdup("%s", name
);
172 size_t buf_alloc
= 8192;
173 char *buf
= xmalloc(buf_alloc
);
177 fd
= open(p
, O_RDONLY
);
179 not_found(hdr
, "Cannot open '%s': %s", p
, strerror(errno
));
180 if (fstat(fd
, &sb
) < 0)
181 die_errno("Cannot stat '%s'", p
);
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
);
189 ssize_t n
= xread(fd
, buf
, buf_alloc
);
191 die_errno("Cannot read '%s'", p
);
194 write_or_die(1, buf
, n
);
201 static void get_text_file(struct strbuf
*hdr
, char *name
)
203 select_getanyfile(hdr
);
205 send_local_file(hdr
, "text/plain", name
);
208 static void get_loose_object(struct strbuf
*hdr
, char *name
)
210 select_getanyfile(hdr
);
211 hdr_cache_forever(hdr
);
212 send_local_file(hdr
, "application/x-git-loose-object", name
);
215 static void get_pack_file(struct strbuf
*hdr
, char *name
)
217 select_getanyfile(hdr
);
218 hdr_cache_forever(hdr
);
219 send_local_file(hdr
, "application/x-git-packed-objects", name
);
222 static void get_idx_file(struct strbuf
*hdr
, char *name
)
224 select_getanyfile(hdr
);
225 hdr_cache_forever(hdr
);
226 send_local_file(hdr
, "application/x-git-packed-objects-toc", name
);
229 static void http_config(void)
232 struct strbuf var
= STRBUF_INIT
;
234 git_config_get_bool("http.getanyfile", &getanyfile
);
235 git_config_get_ulong("http.maxrequestbuffer", &max_request_buffer
);
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
;
245 strbuf_release(&var
);
248 static struct rpc_service
*select_service(struct strbuf
*hdr
, const char *name
)
250 const char *svc_name
;
251 struct rpc_service
*svc
= NULL
;
254 if (!skip_prefix(name
, "git-", &svc_name
))
255 forbidden(hdr
, "Unsupported service: '%s'", name
);
257 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
258 struct rpc_service
*s
= &rpc_service
[i
];
259 if (!strcmp(s
->name
, svc_name
)) {
266 forbidden(hdr
, "Unsupported service: '%s'", name
);
268 if (svc
->enabled
< 0) {
269 const char *user
= getenv("REMOTE_USER");
270 svc
->enabled
= (user
&& *user
) ? 1 : 0;
273 forbidden(hdr
, "Service not enabled: '%s'", svc
->name
);
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).
282 static ssize_t
read_request(int fd
, unsigned char **out
)
284 size_t len
= 0, alloc
= 8192;
285 unsigned char *buf
= xmalloc(alloc
);
287 if (max_request_buffer
< alloc
)
288 max_request_buffer
= alloc
;
293 cnt
= read_in_full(fd
, buf
+ len
, alloc
- len
);
299 /* partial read from read_in_full means we hit EOF */
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",
312 alloc
= alloc_nr(alloc
);
313 if (alloc
> max_request_buffer
)
314 alloc
= max_request_buffer
;
315 REALLOC_ARRAY(buf
, alloc
);
319 static void inflate_request(const char *prog_name
, int out
, int buffer_input
)
322 unsigned char *full_request
= NULL
;
323 unsigned char in_buf
[8192];
324 unsigned char out_buf
[8192];
325 unsigned long cnt
= 0;
327 memset(&stream
, 0, sizeof(stream
));
328 git_inflate_init_gzip_only(&stream
);
335 n
= 0; /* nothing left to read */
337 n
= read_request(0, &full_request
);
338 stream
.next_in
= full_request
;
340 n
= xread(0, in_buf
, sizeof(in_buf
));
341 stream
.next_in
= in_buf
;
345 die("request ended in the middle of the gzip stream");
348 while (0 < stream
.avail_in
) {
351 stream
.next_out
= out_buf
;
352 stream
.avail_out
= sizeof(out_buf
);
354 ret
= git_inflate(&stream
, Z_NO_FLUSH
);
355 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
356 die("zlib error inflating request, result %d", ret
);
358 n
= stream
.total_out
- cnt
;
359 if (write_in_full(out
, out_buf
, n
) != n
)
360 die("%s aborted reading request", prog_name
);
363 if (ret
== Z_STREAM_END
)
369 git_inflate_end(&stream
);
374 static void copy_request(const char *prog_name
, int out
)
377 ssize_t n
= read_request(0, &buf
);
379 die_errno("error reading request body");
380 if (write_in_full(out
, buf
, n
) != n
)
381 die("%s aborted reading request", prog_name
);
386 static void run_service(const char **argv
, int buffer_input
)
388 const char *encoding
= getenv("HTTP_CONTENT_ENCODING");
389 const char *user
= getenv("REMOTE_USER");
390 const char *host
= getenv("REMOTE_ADDR");
391 int gzipped_request
= 0;
392 struct child_process cld
= CHILD_PROCESS_INIT
;
394 if (encoding
&& !strcmp(encoding
, "gzip"))
396 else if (encoding
&& !strcmp(encoding
, "x-gzip"))
404 if (!getenv("GIT_COMMITTER_NAME"))
405 argv_array_pushf(&cld
.env_array
, "GIT_COMMITTER_NAME=%s", user
);
406 if (!getenv("GIT_COMMITTER_EMAIL"))
407 argv_array_pushf(&cld
.env_array
,
408 "GIT_COMMITTER_EMAIL=%s@http.%s", user
, host
);
411 if (buffer_input
|| gzipped_request
)
414 if (start_command(&cld
))
419 inflate_request(argv
[0], cld
.in
, buffer_input
);
420 else if (buffer_input
)
421 copy_request(argv
[0], cld
.in
);
425 if (finish_command(&cld
))
429 static int show_text_ref(const char *name
, const struct object_id
*oid
,
430 int flag
, void *cb_data
)
432 const char *name_nons
= strip_namespace(name
);
433 struct strbuf
*buf
= cb_data
;
434 struct object
*o
= parse_object(oid
->hash
);
438 strbuf_addf(buf
, "%s\t%s\n", oid_to_hex(oid
), name_nons
);
439 if (o
->type
== OBJ_TAG
) {
440 o
= deref_tag(o
, name
, 0);
443 strbuf_addf(buf
, "%s\t%s^{}\n", oid_to_hex(&o
->oid
),
449 static void get_info_refs(struct strbuf
*hdr
, char *arg
)
451 const char *service_name
= get_parameter("service");
452 struct strbuf buf
= STRBUF_INIT
;
457 const char *argv
[] = {NULL
/* service name */,
458 "--stateless-rpc", "--advertise-refs",
460 struct rpc_service
*svc
= select_service(hdr
, service_name
);
462 strbuf_addf(&buf
, "application/x-git-%s-advertisement",
464 hdr_str(hdr
, content_type
, buf
.buf
);
467 packet_write_fmt(1, "# service=git-%s\n", svc
->name
);
471 run_service(argv
, 0);
474 select_getanyfile(hdr
);
475 for_each_namespaced_ref(show_text_ref
, &buf
);
476 send_strbuf(hdr
, "text/plain", &buf
);
478 strbuf_release(&buf
);
481 static int show_head_ref(const char *refname
, const struct object_id
*oid
,
482 int flag
, void *cb_data
)
484 struct strbuf
*buf
= cb_data
;
486 if (flag
& REF_ISSYMREF
) {
487 struct object_id unused
;
488 const char *target
= resolve_ref_unsafe(refname
,
493 strbuf_addf(buf
, "ref: %s\n", strip_namespace(target
));
495 strbuf_addf(buf
, "%s\n", oid_to_hex(oid
));
501 static void get_head(struct strbuf
*hdr
, char *arg
)
503 struct strbuf buf
= STRBUF_INIT
;
505 select_getanyfile(hdr
);
506 head_ref_namespaced(show_head_ref
, &buf
);
507 send_strbuf(hdr
, "text/plain", &buf
);
508 strbuf_release(&buf
);
511 static void get_info_packs(struct strbuf
*hdr
, char *arg
)
513 size_t objdirlen
= strlen(get_object_directory());
514 struct strbuf buf
= STRBUF_INIT
;
515 struct packed_git
*p
;
518 select_getanyfile(hdr
);
519 prepare_packed_git();
520 for (p
= packed_git
; p
; p
= p
->next
) {
525 strbuf_grow(&buf
, cnt
* 53 + 2);
526 for (p
= packed_git
; p
; p
= p
->next
) {
528 strbuf_addf(&buf
, "P %s\n", p
->pack_name
+ objdirlen
+ 6);
530 strbuf_addch(&buf
, '\n');
533 send_strbuf(hdr
, "text/plain; charset=utf-8", &buf
);
534 strbuf_release(&buf
);
537 static void check_content_type(struct strbuf
*hdr
, const char *accepted_type
)
539 const char *actual_type
= getenv("CONTENT_TYPE");
544 if (strcmp(actual_type
, accepted_type
)) {
545 http_status(hdr
, 415, "Unsupported Media Type");
549 "Expected POST with Content-Type '%s',"
550 " but received '%s' instead.\n",
551 accepted_type
, actual_type
);
556 static void service_rpc(struct strbuf
*hdr
, char *service_name
)
558 const char *argv
[] = {NULL
, "--stateless-rpc", ".", NULL
};
559 struct rpc_service
*svc
= select_service(hdr
, service_name
);
560 struct strbuf buf
= STRBUF_INIT
;
563 strbuf_addf(&buf
, "application/x-git-%s-request", svc
->name
);
564 check_content_type(hdr
, buf
.buf
);
569 strbuf_addf(&buf
, "application/x-git-%s-result", svc
->name
);
570 hdr_str(hdr
, content_type
, buf
.buf
);
575 run_service(argv
, svc
->buffer_input
);
576 strbuf_release(&buf
);
580 static NORETURN
void die_webcgi(const char *err
, va_list params
)
583 struct strbuf hdr
= STRBUF_INIT
;
585 vreportf("fatal: ", err
, params
);
587 http_status(&hdr
, 500, "Internal Server Error");
591 exit(0); /* we successfully reported a failure ;-) */
594 static int die_webcgi_recursing(void)
599 static char* getdir(void)
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");
607 if (!pathinfo
|| !*pathinfo
)
608 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
609 if (daemon_avoid_alias(pathinfo
))
610 die("'%s': aliased", pathinfo
);
611 end_url_with_slash(&buf
, root
);
612 if (pathinfo
[0] == '/')
614 strbuf_addstr(&buf
, pathinfo
);
615 return strbuf_detach(&buf
, NULL
);
616 } else if (path
&& *path
) {
617 return xstrdup(path
);
619 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
623 static struct service_cmd
{
626 void (*imp
)(struct strbuf
*, char *);
628 {"GET", "/HEAD$", get_head
},
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
},
635 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file
},
637 {"POST", "/git-upload-pack$", service_rpc
},
638 {"POST", "/git-receive-pack$", service_rpc
}
641 static int bad_request(struct strbuf
*hdr
, const struct service_cmd
*c
)
643 const char *proto
= getenv("SERVER_PROTOCOL");
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
);
650 http_status(hdr
, 400, "Bad Request");
656 int cmd_main(int argc
, const char **argv
)
658 char *method
= getenv("REQUEST_METHOD");
660 struct service_cmd
*cmd
= NULL
;
661 char *cmd_arg
= NULL
;
663 struct strbuf hdr
= STRBUF_INIT
;
665 set_die_routine(die_webcgi
);
666 set_die_is_recursing_routine(die_webcgi_recursing
);
669 die("No REQUEST_METHOD from server");
670 if (!strcmp(method
, "HEAD"))
674 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
675 struct service_cmd
*c
= &services
[i
];
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)) {
684 if (strcmp(method
, c
->method
))
685 return bad_request(&hdr
, c
);
688 n
= out
[0].rm_eo
- out
[0].rm_so
;
689 cmd_arg
= xmemdupz(dir
+ out
[0].rm_so
+ 1, n
- 1);
690 dir
[out
[0].rm_so
] = 0;
697 not_found(&hdr
, "Request not supported: '%s'", dir
);
700 if (!enter_repo(dir
, 0))
701 not_found(&hdr
, "Not a git repository: '%s'", dir
);
702 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
703 access("git-daemon-export-ok", F_OK
) )
704 not_found(&hdr
, "Repository not exported: '%s'", dir
);
707 max_request_buffer
= git_env_ulong("GIT_HTTP_MAX_REQUEST_BUFFER",
710 cmd
->imp(&hdr
, cmd_arg
);