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(query_params
, name
);
95 i
= string_list_insert(query_params
, name
);
104 static const char *get_parameter(const char *name
)
106 struct string_list_item
*i
;
107 i
= string_list_lookup(get_parameters(), name
);
108 return i
? i
->util
: NULL
;
111 __attribute__((format (printf
, 2, 3)))
112 static void format_write(int fd
, const char *fmt
, ...)
114 static char buffer
[1024];
120 n
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
122 if (n
>= sizeof(buffer
))
123 die("protocol error: impossibly long line");
125 safe_write(fd
, buffer
, n
);
128 static void http_status(unsigned code
, const char *msg
)
130 format_write(1, "Status: %u %s\r\n", code
, msg
);
133 static void hdr_str(const char *name
, const char *value
)
135 format_write(1, "%s: %s\r\n", name
, value
);
138 static void hdr_int(const char *name
, uintmax_t value
)
140 format_write(1, "%s: %" PRIuMAX
"\r\n", name
, value
);
143 static void hdr_date(const char *name
, unsigned long when
)
145 const char *value
= show_date(when
, 0, DATE_RFC2822
);
146 hdr_str(name
, value
);
149 static void hdr_nocache(void)
151 hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
152 hdr_str("Pragma", "no-cache");
153 hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
156 static void hdr_cache_forever(void)
158 unsigned long now
= time(NULL
);
159 hdr_date("Date", now
);
160 hdr_date("Expires", now
+ 31536000);
161 hdr_str("Cache-Control", "public, max-age=31536000");
164 static void end_headers(void)
166 safe_write(1, "\r\n", 2);
169 __attribute__((format (printf
, 1, 2)))
170 static NORETURN
void not_found(const char *err
, ...)
174 http_status(404, "Not Found");
178 va_start(params
, err
);
180 vfprintf(stderr
, err
, params
);
185 __attribute__((format (printf
, 1, 2)))
186 static NORETURN
void forbidden(const char *err
, ...)
190 http_status(403, "Forbidden");
194 va_start(params
, err
);
196 vfprintf(stderr
, err
, params
);
201 static void select_getanyfile(void)
204 forbidden("Unsupported service: getanyfile");
207 static void send_strbuf(const char *type
, struct strbuf
*buf
)
209 hdr_int(content_length
, buf
->len
);
210 hdr_str(content_type
, type
);
212 safe_write(1, buf
->buf
, buf
->len
);
215 static void send_local_file(const char *the_type
, const char *name
)
217 const char *p
= git_path("%s", name
);
218 size_t buf_alloc
= 8192;
219 char *buf
= xmalloc(buf_alloc
);
223 fd
= open(p
, O_RDONLY
);
225 not_found("Cannot open '%s': %s", p
, strerror(errno
));
226 if (fstat(fd
, &sb
) < 0)
227 die_errno("Cannot stat '%s'", p
);
229 hdr_int(content_length
, sb
.st_size
);
230 hdr_str(content_type
, the_type
);
231 hdr_date(last_modified
, sb
.st_mtime
);
235 ssize_t n
= xread(fd
, buf
, buf_alloc
);
237 die_errno("Cannot read '%s'", p
);
240 safe_write(1, buf
, n
);
246 static void get_text_file(char *name
)
250 send_local_file("text/plain", name
);
253 static void get_loose_object(char *name
)
257 send_local_file("application/x-git-loose-object", name
);
260 static void get_pack_file(char *name
)
264 send_local_file("application/x-git-packed-objects", name
);
267 static void get_idx_file(char *name
)
271 send_local_file("application/x-git-packed-objects-toc", name
);
274 static int http_config(const char *var
, const char *value
, void *cb
)
276 if (!strcmp(var
, "http.getanyfile")) {
277 getanyfile
= git_config_bool(var
, value
);
281 if (!prefixcmp(var
, "http.")) {
284 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
285 struct rpc_service
*svc
= &rpc_service
[i
];
286 if (!strcmp(var
+ 5, svc
->config_name
)) {
287 svc
->enabled
= git_config_bool(var
, value
);
293 /* we are not interested in parsing any other configuration here */
297 static struct rpc_service
*select_service(const char *name
)
299 struct rpc_service
*svc
= NULL
;
302 if (prefixcmp(name
, "git-"))
303 forbidden("Unsupported service: '%s'", name
);
305 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
306 struct rpc_service
*s
= &rpc_service
[i
];
307 if (!strcmp(s
->name
, name
+ 4)) {
314 forbidden("Unsupported service: '%s'", name
);
316 if (svc
->enabled
< 0) {
317 const char *user
= getenv("REMOTE_USER");
318 svc
->enabled
= (user
&& *user
) ? 1 : 0;
321 forbidden("Service not enabled: '%s'", svc
->name
);
325 static void inflate_request(const char *prog_name
, int out
)
328 unsigned char in_buf
[8192];
329 unsigned char out_buf
[8192];
330 unsigned long cnt
= 0;
333 memset(&stream
, 0, sizeof(stream
));
334 ret
= inflateInit2(&stream
, (15 + 16));
336 die("cannot start zlib inflater, zlib err %d", ret
);
339 ssize_t n
= xread(0, in_buf
, sizeof(in_buf
));
341 die("request ended in the middle of the gzip stream");
343 stream
.next_in
= in_buf
;
346 while (0 < stream
.avail_in
) {
349 stream
.next_out
= out_buf
;
350 stream
.avail_out
= sizeof(out_buf
);
352 ret
= inflate(&stream
, Z_NO_FLUSH
);
353 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
354 die("zlib error inflating request, result %d", ret
);
356 n
= stream
.total_out
- cnt
;
357 if (write_in_full(out
, out_buf
, n
) != n
)
358 die("%s aborted reading request", prog_name
);
361 if (ret
== Z_STREAM_END
)
371 static void run_service(const char **argv
)
373 const char *encoding
= getenv("HTTP_CONTENT_ENCODING");
374 const char *user
= getenv("REMOTE_USER");
375 const char *host
= getenv("REMOTE_ADDR");
377 struct strbuf buf
= STRBUF_INIT
;
378 int gzipped_request
= 0;
379 struct child_process cld
;
381 if (encoding
&& !strcmp(encoding
, "gzip"))
383 else if (encoding
&& !strcmp(encoding
, "x-gzip"))
391 memset(&env
, 0, sizeof(env
));
392 strbuf_addf(&buf
, "GIT_COMMITTER_NAME=%s", user
);
393 env
[0] = strbuf_detach(&buf
, NULL
);
395 strbuf_addf(&buf
, "GIT_COMMITTER_EMAIL=%s@http.%s", user
, host
);
396 env
[1] = strbuf_detach(&buf
, NULL
);
399 memset(&cld
, 0, sizeof(cld
));
401 cld
.env
= (const char *const *)env
;
405 if (start_command(&cld
))
410 inflate_request(argv
[0], cld
.in
);
414 if (finish_command(&cld
))
418 strbuf_release(&buf
);
421 static int show_text_ref(const char *name
, const unsigned char *sha1
,
422 int flag
, void *cb_data
)
424 struct strbuf
*buf
= cb_data
;
425 struct object
*o
= parse_object(sha1
);
429 strbuf_addf(buf
, "%s\t%s\n", sha1_to_hex(sha1
), name
);
430 if (o
->type
== OBJ_TAG
) {
431 o
= deref_tag(o
, name
, 0);
434 strbuf_addf(buf
, "%s\t%s^{}\n", sha1_to_hex(o
->sha1
), name
);
439 static void get_info_refs(char *arg
)
441 const char *service_name
= get_parameter("service");
442 struct strbuf buf
= STRBUF_INIT
;
447 const char *argv
[] = {NULL
/* service name */,
448 "--stateless-rpc", "--advertise-refs",
450 struct rpc_service
*svc
= select_service(service_name
);
452 strbuf_addf(&buf
, "application/x-git-%s-advertisement",
454 hdr_str(content_type
, buf
.buf
);
457 packet_write(1, "# service=git-%s\n", svc
->name
);
465 for_each_ref(show_text_ref
, &buf
);
466 send_strbuf("text/plain", &buf
);
468 strbuf_release(&buf
);
471 static void get_info_packs(char *arg
)
473 size_t objdirlen
= strlen(get_object_directory());
474 struct strbuf buf
= STRBUF_INIT
;
475 struct packed_git
*p
;
479 prepare_packed_git();
480 for (p
= packed_git
; p
; p
= p
->next
) {
485 strbuf_grow(&buf
, cnt
* 53 + 2);
486 for (p
= packed_git
; p
; p
= p
->next
) {
488 strbuf_addf(&buf
, "P %s\n", p
->pack_name
+ objdirlen
+ 6);
490 strbuf_addch(&buf
, '\n');
493 send_strbuf("text/plain; charset=utf-8", &buf
);
494 strbuf_release(&buf
);
497 static void check_content_type(const char *accepted_type
)
499 const char *actual_type
= getenv("CONTENT_TYPE");
504 if (strcmp(actual_type
, accepted_type
)) {
505 http_status(415, "Unsupported Media Type");
509 "Expected POST with Content-Type '%s',"
510 " but received '%s' instead.\n",
511 accepted_type
, actual_type
);
516 static void service_rpc(char *service_name
)
518 const char *argv
[] = {NULL
, "--stateless-rpc", ".", NULL
};
519 struct rpc_service
*svc
= select_service(service_name
);
520 struct strbuf buf
= STRBUF_INIT
;
523 strbuf_addf(&buf
, "application/x-git-%s-request", svc
->name
);
524 check_content_type(buf
.buf
);
529 strbuf_addf(&buf
, "application/x-git-%s-result", svc
->name
);
530 hdr_str(content_type
, buf
.buf
);
536 strbuf_release(&buf
);
539 static NORETURN
void die_webcgi(const char *err
, va_list params
)
547 vsnprintf(buffer
, sizeof(buffer
), err
, params
);
548 fprintf(stderr
, "fatal: %s\n", buffer
);
549 http_status(500, "Internal Server Error");
553 exit(0); /* we successfully reported a failure ;-) */
556 static char* getdir(void)
558 struct strbuf buf
= STRBUF_INIT
;
559 char *pathinfo
= getenv("PATH_INFO");
560 char *root
= getenv("GIT_PROJECT_ROOT");
561 char *path
= getenv("PATH_TRANSLATED");
564 if (!pathinfo
|| !*pathinfo
)
565 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
566 if (daemon_avoid_alias(pathinfo
))
567 die("'%s': aliased", pathinfo
);
568 strbuf_addstr(&buf
, root
);
569 if (buf
.buf
[buf
.len
- 1] != '/')
570 strbuf_addch(&buf
, '/');
571 if (pathinfo
[0] == '/')
573 strbuf_addstr(&buf
, pathinfo
);
574 return strbuf_detach(&buf
, NULL
);
575 } else if (path
&& *path
) {
576 return xstrdup(path
);
578 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
582 static struct service_cmd
{
587 {"GET", "/HEAD$", get_text_file
},
588 {"GET", "/info/refs$", get_info_refs
},
589 {"GET", "/objects/info/alternates$", get_text_file
},
590 {"GET", "/objects/info/http-alternates$", get_text_file
},
591 {"GET", "/objects/info/packs$", get_info_packs
},
592 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object
},
593 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file
},
594 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file
},
596 {"POST", "/git-upload-pack$", service_rpc
},
597 {"POST", "/git-receive-pack$", service_rpc
}
600 int main(int argc
, char **argv
)
602 char *method
= getenv("REQUEST_METHOD");
604 struct service_cmd
*cmd
= NULL
;
605 char *cmd_arg
= NULL
;
608 git_extract_argv0_path(argv
[0]);
609 set_die_routine(die_webcgi
);
612 die("No REQUEST_METHOD from server");
613 if (!strcmp(method
, "HEAD"))
617 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
618 struct service_cmd
*c
= &services
[i
];
622 if (regcomp(&re
, c
->pattern
, REG_EXTENDED
))
623 die("Bogus regex in service table: %s", c
->pattern
);
624 if (!regexec(&re
, dir
, 1, out
, 0)) {
627 if (strcmp(method
, c
->method
)) {
628 const char *proto
= getenv("SERVER_PROTOCOL");
629 if (proto
&& !strcmp(proto
, "HTTP/1.1"))
630 http_status(405, "Method Not Allowed");
632 http_status(400, "Bad Request");
639 n
= out
[0].rm_eo
- out
[0].rm_so
;
640 cmd_arg
= xmalloc(n
);
641 memcpy(cmd_arg
, dir
+ out
[0].rm_so
+ 1, n
-1);
643 dir
[out
[0].rm_so
] = 0;
650 not_found("Request not supported: '%s'", dir
);
653 if (!enter_repo(dir
, 0))
654 not_found("Not a git repository: '%s'", dir
);
655 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
656 access("git-daemon-export-ok", F_OK
) )
657 not_found("Repository not exported: '%s'", dir
);
659 git_config(http_config
, NULL
);