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";
14 static struct string_list
*query_params
;
18 const char *config_name
;
22 static struct rpc_service rpc_service
[] = {
23 { "upload-pack", "uploadpack", 1 },
24 { "receive-pack", "receivepack", -1 },
27 static int decode_char(const char *q
)
30 unsigned char val
= 0;
31 for (i
= 0; i
< 2; i
++) {
32 unsigned char c
= *q
++;
34 if (c
>= '0' && c
<= '9')
36 else if (c
>= 'a' && c
<= 'f')
38 else if (c
>= 'A' && c
<= 'F')
46 static char *decode_parameter(const char **query
, int is_name
)
48 const char *q
= *query
;
51 strbuf_init(&out
, 16);
57 if (c
== '&' || (is_name
&& c
== '=')) {
63 int val
= decode_char(q
+ 1);
65 strbuf_addch(&out
, val
);
72 strbuf_addch(&out
, ' ');
74 strbuf_addch(&out
, c
);
78 return strbuf_detach(&out
, NULL
);
81 static struct string_list
*get_parameters(void)
84 const char *query
= getenv("QUERY_STRING");
86 query_params
= xcalloc(1, sizeof(*query_params
));
87 while (query
&& *query
) {
88 char *name
= decode_parameter(&query
, 1);
89 char *value
= decode_parameter(&query
, 0);
90 struct string_list_item
*i
;
92 i
= string_list_lookup(name
, query_params
);
94 i
= string_list_insert(name
, query_params
);
103 static const char *get_parameter(const char *name
)
105 struct string_list_item
*i
;
106 i
= string_list_lookup(name
, get_parameters());
107 return i
? i
->util
: NULL
;
110 static void format_write(int fd
, const char *fmt
, ...)
112 static char buffer
[1024];
118 n
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
120 if (n
>= sizeof(buffer
))
121 die("protocol error: impossibly long line");
123 safe_write(fd
, buffer
, n
);
126 static void http_status(unsigned code
, const char *msg
)
128 format_write(1, "Status: %u %s\r\n", code
, msg
);
131 static void hdr_str(const char *name
, const char *value
)
133 format_write(1, "%s: %s\r\n", name
, value
);
136 static void hdr_int(const char *name
, size_t value
)
138 format_write(1, "%s: %" PRIuMAX
"\r\n", name
, value
);
141 static void hdr_date(const char *name
, unsigned long when
)
143 const char *value
= show_date(when
, 0, DATE_RFC2822
);
144 hdr_str(name
, value
);
147 static void hdr_nocache(void)
149 hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
150 hdr_str("Pragma", "no-cache");
151 hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
154 static void hdr_cache_forever(void)
156 unsigned long now
= time(NULL
);
157 hdr_date("Date", now
);
158 hdr_date("Expires", now
+ 31536000);
159 hdr_str("Cache-Control", "public, max-age=31536000");
162 static void end_headers(void)
164 safe_write(1, "\r\n", 2);
167 static NORETURN
void not_found(const char *err
, ...)
171 http_status(404, "Not Found");
175 va_start(params
, err
);
177 vfprintf(stderr
, err
, params
);
182 static NORETURN
void forbidden(const char *err
, ...)
186 http_status(403, "Forbidden");
190 va_start(params
, err
);
192 vfprintf(stderr
, err
, params
);
197 static void send_strbuf(const char *type
, struct strbuf
*buf
)
199 hdr_int(content_length
, buf
->len
);
200 hdr_str(content_type
, type
);
202 safe_write(1, buf
->buf
, buf
->len
);
205 static void send_file(const char *the_type
, const char *name
)
207 const char *p
= git_path("%s", name
);
208 size_t buf_alloc
= 8192;
209 char *buf
= xmalloc(buf_alloc
);
214 fd
= open(p
, O_RDONLY
);
216 not_found("Cannot open '%s': %s", p
, strerror(errno
));
217 if (fstat(fd
, &sb
) < 0)
218 die_errno("Cannot stat '%s'", p
);
220 size
= xsize_t(sb
.st_size
);
222 hdr_int(content_length
, size
);
223 hdr_str(content_type
, the_type
);
224 hdr_date(last_modified
, sb
.st_mtime
);
228 ssize_t n
= xread(fd
, buf
, buf_alloc
);
230 die_errno("Cannot read '%s'", p
);
233 safe_write(1, buf
, n
);
239 static void get_text_file(char *name
)
242 send_file("text/plain", name
);
245 static void get_loose_object(char *name
)
248 send_file("application/x-git-loose-object", name
);
251 static void get_pack_file(char *name
)
254 send_file("application/x-git-packed-objects", name
);
257 static void get_idx_file(char *name
)
260 send_file("application/x-git-packed-objects-toc", name
);
263 static int http_config(const char *var
, const char *value
, void *cb
)
265 struct rpc_service
*svc
= cb
;
267 if (!prefixcmp(var
, "http.") &&
268 !strcmp(var
+ 5, svc
->config_name
)) {
269 svc
->enabled
= git_config_bool(var
, value
);
273 /* we are not interested in parsing any other configuration here */
277 static struct rpc_service
*select_service(const char *name
)
279 struct rpc_service
*svc
= NULL
;
282 if (prefixcmp(name
, "git-"))
283 forbidden("Unsupported service: '%s'", name
);
285 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
286 struct rpc_service
*s
= &rpc_service
[i
];
287 if (!strcmp(s
->name
, name
+ 4)) {
294 forbidden("Unsupported service: '%s'", name
);
296 git_config(http_config
, svc
);
297 if (svc
->enabled
< 0) {
298 const char *user
= getenv("REMOTE_USER");
299 svc
->enabled
= (user
&& *user
) ? 1 : 0;
302 forbidden("Service not enabled: '%s'", svc
->name
);
306 static void inflate_request(const char *prog_name
, int out
)
309 unsigned char in_buf
[8192];
310 unsigned char out_buf
[8192];
311 unsigned long cnt
= 0;
314 memset(&stream
, 0, sizeof(stream
));
315 ret
= inflateInit2(&stream
, (15 + 16));
317 die("cannot start zlib inflater, zlib err %d", ret
);
320 ssize_t n
= xread(0, in_buf
, sizeof(in_buf
));
322 die("request ended in the middle of the gzip stream");
324 stream
.next_in
= in_buf
;
327 while (0 < stream
.avail_in
) {
330 stream
.next_out
= out_buf
;
331 stream
.avail_out
= sizeof(out_buf
);
333 ret
= inflate(&stream
, Z_NO_FLUSH
);
334 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
335 die("zlib error inflating request, result %d", ret
);
337 n
= stream
.total_out
- cnt
;
338 if (write_in_full(out
, out_buf
, n
) != n
)
339 die("%s aborted reading request", prog_name
);
342 if (ret
== Z_STREAM_END
)
352 static void run_service(const char **argv
)
354 const char *encoding
= getenv("HTTP_CONTENT_ENCODING");
355 const char *user
= getenv("REMOTE_USER");
356 const char *host
= getenv("REMOTE_ADDR");
358 struct strbuf buf
= STRBUF_INIT
;
359 int gzipped_request
= 0;
360 struct child_process cld
;
362 if (encoding
&& !strcmp(encoding
, "gzip"))
364 else if (encoding
&& !strcmp(encoding
, "x-gzip"))
372 memset(&env
, 0, sizeof(env
));
373 strbuf_addf(&buf
, "GIT_COMMITTER_NAME=%s", user
);
374 env
[0] = strbuf_detach(&buf
, NULL
);
376 strbuf_addf(&buf
, "GIT_COMMITTER_EMAIL=%s@http.%s", user
, host
);
377 env
[1] = strbuf_detach(&buf
, NULL
);
380 memset(&cld
, 0, sizeof(cld
));
382 cld
.env
= (const char *const *)env
;
386 if (start_command(&cld
))
391 inflate_request(argv
[0], cld
.in
);
395 if (finish_command(&cld
))
399 strbuf_release(&buf
);
402 static int show_text_ref(const char *name
, const unsigned char *sha1
,
403 int flag
, void *cb_data
)
405 struct strbuf
*buf
= cb_data
;
406 struct object
*o
= parse_object(sha1
);
410 strbuf_addf(buf
, "%s\t%s\n", sha1_to_hex(sha1
), name
);
411 if (o
->type
== OBJ_TAG
) {
412 o
= deref_tag(o
, name
, 0);
415 strbuf_addf(buf
, "%s\t%s^{}\n", sha1_to_hex(o
->sha1
), name
);
420 static void get_info_refs(char *arg
)
422 const char *service_name
= get_parameter("service");
423 struct strbuf buf
= STRBUF_INIT
;
428 const char *argv
[] = {NULL
/* service name */,
429 "--stateless-rpc", "--advertise-refs",
431 struct rpc_service
*svc
= select_service(service_name
);
433 strbuf_addf(&buf
, "application/x-git-%s-advertisement",
435 hdr_str(content_type
, buf
.buf
);
438 packet_write(1, "# service=git-%s\n", svc
->name
);
445 for_each_ref(show_text_ref
, &buf
);
446 send_strbuf("text/plain", &buf
);
448 strbuf_release(&buf
);
451 static void get_info_packs(char *arg
)
453 size_t objdirlen
= strlen(get_object_directory());
454 struct strbuf buf
= STRBUF_INIT
;
455 struct packed_git
*p
;
458 prepare_packed_git();
459 for (p
= packed_git
; p
; p
= p
->next
) {
464 strbuf_grow(&buf
, cnt
* 53 + 2);
465 for (p
= packed_git
; p
; p
= p
->next
) {
467 strbuf_addf(&buf
, "P %s\n", p
->pack_name
+ objdirlen
+ 6);
469 strbuf_addch(&buf
, '\n');
472 send_strbuf("text/plain; charset=utf-8", &buf
);
473 strbuf_release(&buf
);
476 static void check_content_type(const char *accepted_type
)
478 const char *actual_type
= getenv("CONTENT_TYPE");
483 if (strcmp(actual_type
, accepted_type
)) {
484 http_status(415, "Unsupported Media Type");
488 "Expected POST with Content-Type '%s',"
489 " but received '%s' instead.\n",
490 accepted_type
, actual_type
);
495 static void service_rpc(char *service_name
)
497 const char *argv
[] = {NULL
, "--stateless-rpc", ".", NULL
};
498 struct rpc_service
*svc
= select_service(service_name
);
499 struct strbuf buf
= STRBUF_INIT
;
502 strbuf_addf(&buf
, "application/x-git-%s-request", svc
->name
);
503 check_content_type(buf
.buf
);
508 strbuf_addf(&buf
, "application/x-git-%s-result", svc
->name
);
509 hdr_str(content_type
, buf
.buf
);
515 strbuf_release(&buf
);
518 static NORETURN
void die_webcgi(const char *err
, va_list params
)
522 http_status(500, "Internal Server Error");
526 vsnprintf(buffer
, sizeof(buffer
), err
, params
);
527 fprintf(stderr
, "fatal: %s\n", buffer
);
531 static char* getdir(void)
533 struct strbuf buf
= STRBUF_INIT
;
534 char *pathinfo
= getenv("PATH_INFO");
535 char *root
= getenv("GIT_PROJECT_ROOT");
536 char *path
= getenv("PATH_TRANSLATED");
539 if (!pathinfo
|| !*pathinfo
)
540 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
541 strbuf_addstr(&buf
, root
);
542 strbuf_addstr(&buf
, pathinfo
);
543 return strbuf_detach(&buf
, NULL
);
544 } else if (path
&& *path
) {
545 return xstrdup(path
);
547 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
551 static struct service_cmd
{
556 {"GET", "/HEAD$", get_text_file
},
557 {"GET", "/info/refs$", get_info_refs
},
558 {"GET", "/objects/info/alternates$", get_text_file
},
559 {"GET", "/objects/info/http-alternates$", get_text_file
},
560 {"GET", "/objects/info/packs$", get_info_packs
},
561 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object
},
562 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file
},
563 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file
},
565 {"POST", "/git-upload-pack$", service_rpc
},
566 {"POST", "/git-receive-pack$", service_rpc
}
569 int main(int argc
, char **argv
)
571 char *method
= getenv("REQUEST_METHOD");
573 struct service_cmd
*cmd
= NULL
;
574 char *cmd_arg
= NULL
;
577 git_extract_argv0_path(argv
[0]);
578 set_die_routine(die_webcgi
);
581 die("No REQUEST_METHOD from server");
582 if (!strcmp(method
, "HEAD"))
586 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
587 struct service_cmd
*c
= &services
[i
];
591 if (regcomp(&re
, c
->pattern
, REG_EXTENDED
))
592 die("Bogus regex in service table: %s", c
->pattern
);
593 if (!regexec(&re
, dir
, 1, out
, 0)) {
594 size_t n
= out
[0].rm_eo
- out
[0].rm_so
;
596 if (strcmp(method
, c
->method
)) {
597 const char *proto
= getenv("SERVER_PROTOCOL");
598 if (proto
&& !strcmp(proto
, "HTTP/1.1"))
599 http_status(405, "Method Not Allowed");
601 http_status(400, "Bad Request");
608 cmd_arg
= xmalloc(n
);
609 strncpy(cmd_arg
, dir
+ out
[0].rm_so
+ 1, n
);
611 dir
[out
[0].rm_so
] = 0;
618 not_found("Request not supported: '%s'", dir
);
621 if (!enter_repo(dir
, 0))
622 not_found("Not a git repository: '%s'", dir
);