7 #include "run-command.h"
8 #include "string-list.h"
10 static const char content_type
[] = "Content-Type";
11 static const char content_length
[] = "Content-Length";
12 static const char last_modified
[] = "Last-Modified";
13 static int getanyfile
= 1;
15 static struct string_list
*query_params
;
19 const char *config_name
;
23 static struct rpc_service rpc_service
[] = {
24 { "upload-pack", "uploadpack", 1 },
25 { "receive-pack", "receivepack", -1 },
28 static int decode_char(const char *q
)
31 unsigned char val
= 0;
32 for (i
= 0; i
< 2; i
++) {
33 unsigned char c
= *q
++;
35 if (c
>= '0' && c
<= '9')
37 else if (c
>= 'a' && c
<= 'f')
39 else if (c
>= 'A' && c
<= 'F')
47 static char *decode_parameter(const char **query
, int is_name
)
49 const char *q
= *query
;
52 strbuf_init(&out
, 16);
58 if (c
== '&' || (is_name
&& c
== '=')) {
64 int val
= decode_char(q
+ 1);
66 strbuf_addch(&out
, val
);
73 strbuf_addch(&out
, ' ');
75 strbuf_addch(&out
, c
);
79 return strbuf_detach(&out
, NULL
);
82 static struct string_list
*get_parameters(void)
85 const char *query
= getenv("QUERY_STRING");
87 query_params
= xcalloc(1, sizeof(*query_params
));
88 while (query
&& *query
) {
89 char *name
= decode_parameter(&query
, 1);
90 char *value
= decode_parameter(&query
, 0);
91 struct string_list_item
*i
;
93 i
= string_list_lookup(name
, query_params
);
95 i
= string_list_insert(name
, query_params
);
104 static const char *get_parameter(const char *name
)
106 struct string_list_item
*i
;
107 i
= string_list_lookup(name
, get_parameters());
108 return i
? i
->util
: NULL
;
111 static void format_write(int fd
, const char *fmt
, ...)
113 static char buffer
[1024];
119 n
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
121 if (n
>= sizeof(buffer
))
122 die("protocol error: impossibly long line");
124 safe_write(fd
, buffer
, n
);
127 static void http_status(unsigned code
, const char *msg
)
129 format_write(1, "Status: %u %s\r\n", code
, msg
);
132 static void hdr_str(const char *name
, const char *value
)
134 format_write(1, "%s: %s\r\n", name
, value
);
137 static void hdr_int(const char *name
, uintmax_t value
)
139 format_write(1, "%s: %" PRIuMAX
"\r\n", name
, value
);
142 static void hdr_date(const char *name
, unsigned long when
)
144 const char *value
= show_date(when
, 0, DATE_RFC2822
);
145 hdr_str(name
, value
);
148 static void hdr_nocache(void)
150 hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
151 hdr_str("Pragma", "no-cache");
152 hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
155 static void hdr_cache_forever(void)
157 unsigned long now
= time(NULL
);
158 hdr_date("Date", now
);
159 hdr_date("Expires", now
+ 31536000);
160 hdr_str("Cache-Control", "public, max-age=31536000");
163 static void end_headers(void)
165 safe_write(1, "\r\n", 2);
168 static NORETURN
void not_found(const char *err
, ...)
172 http_status(404, "Not Found");
176 va_start(params
, err
);
178 vfprintf(stderr
, err
, params
);
183 static NORETURN
void forbidden(const char *err
, ...)
187 http_status(403, "Forbidden");
191 va_start(params
, err
);
193 vfprintf(stderr
, err
, params
);
198 static void select_getanyfile(void)
201 forbidden("Unsupported service: getanyfile");
204 static void send_strbuf(const char *type
, struct strbuf
*buf
)
206 hdr_int(content_length
, buf
->len
);
207 hdr_str(content_type
, type
);
209 safe_write(1, buf
->buf
, buf
->len
);
212 static void send_local_file(const char *the_type
, const char *name
)
214 const char *p
= git_path("%s", name
);
215 size_t buf_alloc
= 8192;
216 char *buf
= xmalloc(buf_alloc
);
220 fd
= open(p
, O_RDONLY
);
222 not_found("Cannot open '%s': %s", p
, strerror(errno
));
223 if (fstat(fd
, &sb
) < 0)
224 die_errno("Cannot stat '%s'", p
);
226 hdr_int(content_length
, sb
.st_size
);
227 hdr_str(content_type
, the_type
);
228 hdr_date(last_modified
, sb
.st_mtime
);
232 ssize_t n
= xread(fd
, buf
, buf_alloc
);
234 die_errno("Cannot read '%s'", p
);
237 safe_write(1, buf
, n
);
243 static void get_text_file(char *name
)
247 send_local_file("text/plain", name
);
250 static void get_loose_object(char *name
)
254 send_local_file("application/x-git-loose-object", name
);
257 static void get_pack_file(char *name
)
261 send_local_file("application/x-git-packed-objects", name
);
264 static void get_idx_file(char *name
)
268 send_local_file("application/x-git-packed-objects-toc", name
);
271 static int http_config(const char *var
, const char *value
, void *cb
)
273 if (!strcmp(var
, "http.getanyfile")) {
274 getanyfile
= git_config_bool(var
, value
);
278 if (!prefixcmp(var
, "http.")) {
281 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
282 struct rpc_service
*svc
= &rpc_service
[i
];
283 if (!strcmp(var
+ 5, svc
->config_name
)) {
284 svc
->enabled
= git_config_bool(var
, value
);
290 /* we are not interested in parsing any other configuration here */
294 static struct rpc_service
*select_service(const char *name
)
296 struct rpc_service
*svc
= NULL
;
299 if (prefixcmp(name
, "git-"))
300 forbidden("Unsupported service: '%s'", name
);
302 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
303 struct rpc_service
*s
= &rpc_service
[i
];
304 if (!strcmp(s
->name
, name
+ 4)) {
311 forbidden("Unsupported service: '%s'", name
);
313 if (svc
->enabled
< 0) {
314 const char *user
= getenv("REMOTE_USER");
315 svc
->enabled
= (user
&& *user
) ? 1 : 0;
318 forbidden("Service not enabled: '%s'", svc
->name
);
322 static void inflate_request(const char *prog_name
, int out
)
325 unsigned char in_buf
[8192];
326 unsigned char out_buf
[8192];
327 unsigned long cnt
= 0;
330 memset(&stream
, 0, sizeof(stream
));
331 ret
= inflateInit2(&stream
, (15 + 16));
333 die("cannot start zlib inflater, zlib err %d", ret
);
336 ssize_t n
= xread(0, in_buf
, sizeof(in_buf
));
338 die("request ended in the middle of the gzip stream");
340 stream
.next_in
= in_buf
;
343 while (0 < stream
.avail_in
) {
346 stream
.next_out
= out_buf
;
347 stream
.avail_out
= sizeof(out_buf
);
349 ret
= inflate(&stream
, Z_NO_FLUSH
);
350 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
351 die("zlib error inflating request, result %d", ret
);
353 n
= stream
.total_out
- cnt
;
354 if (write_in_full(out
, out_buf
, n
) != n
)
355 die("%s aborted reading request", prog_name
);
358 if (ret
== Z_STREAM_END
)
368 static void run_service(const char **argv
)
370 const char *encoding
= getenv("HTTP_CONTENT_ENCODING");
371 const char *user
= getenv("REMOTE_USER");
372 const char *host
= getenv("REMOTE_ADDR");
374 struct strbuf buf
= STRBUF_INIT
;
375 int gzipped_request
= 0;
376 struct child_process cld
;
378 if (encoding
&& !strcmp(encoding
, "gzip"))
380 else if (encoding
&& !strcmp(encoding
, "x-gzip"))
388 memset(&env
, 0, sizeof(env
));
389 strbuf_addf(&buf
, "GIT_COMMITTER_NAME=%s", user
);
390 env
[0] = strbuf_detach(&buf
, NULL
);
392 strbuf_addf(&buf
, "GIT_COMMITTER_EMAIL=%s@http.%s", user
, host
);
393 env
[1] = strbuf_detach(&buf
, NULL
);
396 memset(&cld
, 0, sizeof(cld
));
398 cld
.env
= (const char *const *)env
;
402 if (start_command(&cld
))
407 inflate_request(argv
[0], cld
.in
);
411 if (finish_command(&cld
))
415 strbuf_release(&buf
);
418 static int show_text_ref(const char *name
, const unsigned char *sha1
,
419 int flag
, void *cb_data
)
421 struct strbuf
*buf
= cb_data
;
422 struct object
*o
= parse_object(sha1
);
426 strbuf_addf(buf
, "%s\t%s\n", sha1_to_hex(sha1
), name
);
427 if (o
->type
== OBJ_TAG
) {
428 o
= deref_tag(o
, name
, 0);
431 strbuf_addf(buf
, "%s\t%s^{}\n", sha1_to_hex(o
->sha1
), name
);
436 static void get_info_refs(char *arg
)
438 const char *service_name
= get_parameter("service");
439 struct strbuf buf
= STRBUF_INIT
;
444 const char *argv
[] = {NULL
/* service name */,
445 "--stateless-rpc", "--advertise-refs",
447 struct rpc_service
*svc
= select_service(service_name
);
449 strbuf_addf(&buf
, "application/x-git-%s-advertisement",
451 hdr_str(content_type
, buf
.buf
);
454 packet_write(1, "# service=git-%s\n", svc
->name
);
462 for_each_ref(show_text_ref
, &buf
);
463 send_strbuf("text/plain", &buf
);
465 strbuf_release(&buf
);
468 static void get_info_packs(char *arg
)
470 size_t objdirlen
= strlen(get_object_directory());
471 struct strbuf buf
= STRBUF_INIT
;
472 struct packed_git
*p
;
476 prepare_packed_git();
477 for (p
= packed_git
; p
; p
= p
->next
) {
482 strbuf_grow(&buf
, cnt
* 53 + 2);
483 for (p
= packed_git
; p
; p
= p
->next
) {
485 strbuf_addf(&buf
, "P %s\n", p
->pack_name
+ objdirlen
+ 6);
487 strbuf_addch(&buf
, '\n');
490 send_strbuf("text/plain; charset=utf-8", &buf
);
491 strbuf_release(&buf
);
494 static void check_content_type(const char *accepted_type
)
496 const char *actual_type
= getenv("CONTENT_TYPE");
501 if (strcmp(actual_type
, accepted_type
)) {
502 http_status(415, "Unsupported Media Type");
506 "Expected POST with Content-Type '%s',"
507 " but received '%s' instead.\n",
508 accepted_type
, actual_type
);
513 static void service_rpc(char *service_name
)
515 const char *argv
[] = {NULL
, "--stateless-rpc", ".", NULL
};
516 struct rpc_service
*svc
= select_service(service_name
);
517 struct strbuf buf
= STRBUF_INIT
;
520 strbuf_addf(&buf
, "application/x-git-%s-request", svc
->name
);
521 check_content_type(buf
.buf
);
526 strbuf_addf(&buf
, "application/x-git-%s-result", svc
->name
);
527 hdr_str(content_type
, buf
.buf
);
533 strbuf_release(&buf
);
536 static NORETURN
void die_webcgi(const char *err
, va_list params
)
540 http_status(500, "Internal Server Error");
544 vsnprintf(buffer
, sizeof(buffer
), err
, params
);
545 fprintf(stderr
, "fatal: %s\n", buffer
);
549 static char* getdir(void)
551 struct strbuf buf
= STRBUF_INIT
;
552 char *pathinfo
= getenv("PATH_INFO");
553 char *root
= getenv("GIT_PROJECT_ROOT");
554 char *path
= getenv("PATH_TRANSLATED");
557 if (!pathinfo
|| !*pathinfo
)
558 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
559 if (daemon_avoid_alias(pathinfo
))
560 die("'%s': aliased", pathinfo
);
561 strbuf_addstr(&buf
, root
);
562 if (buf
.buf
[buf
.len
- 1] != '/')
563 strbuf_addch(&buf
, '/');
564 if (pathinfo
[0] == '/')
566 strbuf_addstr(&buf
, pathinfo
);
567 return strbuf_detach(&buf
, NULL
);
568 } else if (path
&& *path
) {
569 return xstrdup(path
);
571 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
575 static struct service_cmd
{
580 {"GET", "/HEAD$", get_text_file
},
581 {"GET", "/info/refs$", get_info_refs
},
582 {"GET", "/objects/info/alternates$", get_text_file
},
583 {"GET", "/objects/info/http-alternates$", get_text_file
},
584 {"GET", "/objects/info/packs$", get_info_packs
},
585 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object
},
586 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file
},
587 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file
},
589 {"POST", "/git-upload-pack$", service_rpc
},
590 {"POST", "/git-receive-pack$", service_rpc
}
593 int main(int argc
, char **argv
)
595 char *method
= getenv("REQUEST_METHOD");
597 struct service_cmd
*cmd
= NULL
;
598 char *cmd_arg
= NULL
;
601 git_extract_argv0_path(argv
[0]);
602 set_die_routine(die_webcgi
);
605 die("No REQUEST_METHOD from server");
606 if (!strcmp(method
, "HEAD"))
610 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
611 struct service_cmd
*c
= &services
[i
];
615 if (regcomp(&re
, c
->pattern
, REG_EXTENDED
))
616 die("Bogus regex in service table: %s", c
->pattern
);
617 if (!regexec(&re
, dir
, 1, out
, 0)) {
618 size_t n
= out
[0].rm_eo
- out
[0].rm_so
;
620 if (strcmp(method
, c
->method
)) {
621 const char *proto
= getenv("SERVER_PROTOCOL");
622 if (proto
&& !strcmp(proto
, "HTTP/1.1"))
623 http_status(405, "Method Not Allowed");
625 http_status(400, "Bad Request");
632 cmd_arg
= xmalloc(n
);
633 strncpy(cmd_arg
, dir
+ out
[0].rm_so
+ 1, n
);
635 dir
[out
[0].rm_so
] = 0;
642 not_found("Request not supported: '%s'", dir
);
645 if (!enter_repo(dir
, 0))
646 not_found("Not a git repository: '%s'", dir
);
648 git_config(http_config
, NULL
);