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;
17 static struct string_list
*query_params
;
21 const char *config_name
;
25 static struct rpc_service rpc_service
[] = {
26 { "upload-pack", "uploadpack", 1 },
27 { "receive-pack", "receivepack", -1 },
30 static struct string_list
*get_parameters(void)
33 const char *query
= getenv("QUERY_STRING");
35 query_params
= xcalloc(1, sizeof(*query_params
));
36 while (query
&& *query
) {
37 char *name
= url_decode_parameter_name(&query
);
38 char *value
= url_decode_parameter_value(&query
);
39 struct string_list_item
*i
;
41 i
= string_list_lookup(query_params
, name
);
43 i
= string_list_insert(query_params
, name
);
52 static const char *get_parameter(const char *name
)
54 struct string_list_item
*i
;
55 i
= string_list_lookup(get_parameters(), name
);
56 return i
? i
->util
: NULL
;
59 __attribute__((format (printf
, 2, 3)))
60 static void format_write(int fd
, const char *fmt
, ...)
62 static char buffer
[1024];
68 n
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
70 if (n
>= sizeof(buffer
))
71 die("protocol error: impossibly long line");
73 write_or_die(fd
, buffer
, n
);
76 static void http_status(unsigned code
, const char *msg
)
78 format_write(1, "Status: %u %s\r\n", code
, msg
);
81 static void hdr_str(const char *name
, const char *value
)
83 format_write(1, "%s: %s\r\n", name
, value
);
86 static void hdr_int(const char *name
, uintmax_t value
)
88 format_write(1, "%s: %" PRIuMAX
"\r\n", name
, value
);
91 static void hdr_date(const char *name
, unsigned long when
)
93 const char *value
= show_date(when
, 0, DATE_RFC2822
);
97 static void hdr_nocache(void)
99 hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
100 hdr_str("Pragma", "no-cache");
101 hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
104 static void hdr_cache_forever(void)
106 unsigned long now
= time(NULL
);
107 hdr_date("Date", now
);
108 hdr_date("Expires", now
+ 31536000);
109 hdr_str("Cache-Control", "public, max-age=31536000");
112 static void end_headers(void)
114 write_or_die(1, "\r\n", 2);
117 __attribute__((format (printf
, 1, 2)))
118 static NORETURN
void not_found(const char *err
, ...)
122 http_status(404, "Not Found");
126 va_start(params
, err
);
128 vfprintf(stderr
, err
, params
);
133 __attribute__((format (printf
, 1, 2)))
134 static NORETURN
void forbidden(const char *err
, ...)
138 http_status(403, "Forbidden");
142 va_start(params
, err
);
144 vfprintf(stderr
, err
, params
);
149 static void select_getanyfile(void)
152 forbidden("Unsupported service: getanyfile");
155 static void send_strbuf(const char *type
, struct strbuf
*buf
)
157 hdr_int(content_length
, buf
->len
);
158 hdr_str(content_type
, type
);
160 write_or_die(1, buf
->buf
, buf
->len
);
163 static void send_local_file(const char *the_type
, const char *name
)
165 const char *p
= git_path("%s", name
);
166 size_t buf_alloc
= 8192;
167 char *buf
= xmalloc(buf_alloc
);
171 fd
= open(p
, O_RDONLY
);
173 not_found("Cannot open '%s': %s", p
, strerror(errno
));
174 if (fstat(fd
, &sb
) < 0)
175 die_errno("Cannot stat '%s'", p
);
177 hdr_int(content_length
, sb
.st_size
);
178 hdr_str(content_type
, the_type
);
179 hdr_date(last_modified
, sb
.st_mtime
);
183 ssize_t n
= xread(fd
, buf
, buf_alloc
);
185 die_errno("Cannot read '%s'", p
);
188 write_or_die(1, buf
, n
);
194 static void get_text_file(char *name
)
198 send_local_file("text/plain", name
);
201 static void get_loose_object(char *name
)
205 send_local_file("application/x-git-loose-object", name
);
208 static void get_pack_file(char *name
)
212 send_local_file("application/x-git-packed-objects", name
);
215 static void get_idx_file(char *name
)
219 send_local_file("application/x-git-packed-objects-toc", name
);
222 static void http_config(void)
225 struct strbuf var
= STRBUF_INIT
;
227 git_config_get_bool("http.getanyfile", &getanyfile
);
229 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
230 struct rpc_service
*svc
= &rpc_service
[i
];
231 strbuf_addf(&var
, "http.%s", svc
->config_name
);
232 if (!git_config_get_bool(var
.buf
, &value
))
233 svc
->enabled
= value
;
237 strbuf_release(&var
);
240 static struct rpc_service
*select_service(const char *name
)
242 const char *svc_name
;
243 struct rpc_service
*svc
= NULL
;
246 if (!skip_prefix(name
, "git-", &svc_name
))
247 forbidden("Unsupported service: '%s'", name
);
249 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
250 struct rpc_service
*s
= &rpc_service
[i
];
251 if (!strcmp(s
->name
, svc_name
)) {
258 forbidden("Unsupported service: '%s'", name
);
260 if (svc
->enabled
< 0) {
261 const char *user
= getenv("REMOTE_USER");
262 svc
->enabled
= (user
&& *user
) ? 1 : 0;
265 forbidden("Service not enabled: '%s'", svc
->name
);
269 static void inflate_request(const char *prog_name
, int out
)
272 unsigned char in_buf
[8192];
273 unsigned char out_buf
[8192];
274 unsigned long cnt
= 0;
276 memset(&stream
, 0, sizeof(stream
));
277 git_inflate_init_gzip_only(&stream
);
280 ssize_t n
= xread(0, in_buf
, sizeof(in_buf
));
282 die("request ended in the middle of the gzip stream");
284 stream
.next_in
= in_buf
;
287 while (0 < stream
.avail_in
) {
290 stream
.next_out
= out_buf
;
291 stream
.avail_out
= sizeof(out_buf
);
293 ret
= git_inflate(&stream
, Z_NO_FLUSH
);
294 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
295 die("zlib error inflating request, result %d", ret
);
297 n
= stream
.total_out
- cnt
;
298 if (write_in_full(out
, out_buf
, n
) != n
)
299 die("%s aborted reading request", prog_name
);
302 if (ret
== Z_STREAM_END
)
308 git_inflate_end(&stream
);
312 static void run_service(const char **argv
)
314 const char *encoding
= getenv("HTTP_CONTENT_ENCODING");
315 const char *user
= getenv("REMOTE_USER");
316 const char *host
= getenv("REMOTE_ADDR");
317 int gzipped_request
= 0;
318 struct child_process cld
= CHILD_PROCESS_INIT
;
320 if (encoding
&& !strcmp(encoding
, "gzip"))
322 else if (encoding
&& !strcmp(encoding
, "x-gzip"))
330 if (!getenv("GIT_COMMITTER_NAME"))
331 argv_array_pushf(&cld
.env_array
, "GIT_COMMITTER_NAME=%s", user
);
332 if (!getenv("GIT_COMMITTER_EMAIL"))
333 argv_array_pushf(&cld
.env_array
,
334 "GIT_COMMITTER_EMAIL=%s@http.%s", user
, host
);
340 if (start_command(&cld
))
345 inflate_request(argv
[0], cld
.in
);
349 if (finish_command(&cld
))
353 static int show_text_ref(const char *name
, const unsigned char *sha1
,
354 int flag
, void *cb_data
)
356 const char *name_nons
= strip_namespace(name
);
357 struct strbuf
*buf
= cb_data
;
358 struct object
*o
= parse_object(sha1
);
362 strbuf_addf(buf
, "%s\t%s\n", sha1_to_hex(sha1
), name_nons
);
363 if (o
->type
== OBJ_TAG
) {
364 o
= deref_tag(o
, name
, 0);
367 strbuf_addf(buf
, "%s\t%s^{}\n", sha1_to_hex(o
->sha1
),
373 static void get_info_refs(char *arg
)
375 const char *service_name
= get_parameter("service");
376 struct strbuf buf
= STRBUF_INIT
;
381 const char *argv
[] = {NULL
/* service name */,
382 "--stateless-rpc", "--advertise-refs",
384 struct rpc_service
*svc
= select_service(service_name
);
386 strbuf_addf(&buf
, "application/x-git-%s-advertisement",
388 hdr_str(content_type
, buf
.buf
);
391 packet_write(1, "# service=git-%s\n", svc
->name
);
399 for_each_namespaced_ref(show_text_ref
, &buf
);
400 send_strbuf("text/plain", &buf
);
402 strbuf_release(&buf
);
405 static int show_head_ref(const char *refname
, const unsigned char *sha1
,
406 int flag
, void *cb_data
)
408 struct strbuf
*buf
= cb_data
;
410 if (flag
& REF_ISSYMREF
) {
411 unsigned char unused
[20];
412 const char *target
= resolve_ref_unsafe(refname
,
415 const char *target_nons
= strip_namespace(target
);
417 strbuf_addf(buf
, "ref: %s\n", target_nons
);
419 strbuf_addf(buf
, "%s\n", sha1_to_hex(sha1
));
425 static void get_head(char *arg
)
427 struct strbuf buf
= STRBUF_INIT
;
430 head_ref_namespaced(show_head_ref
, &buf
);
431 send_strbuf("text/plain", &buf
);
432 strbuf_release(&buf
);
435 static void get_info_packs(char *arg
)
437 size_t objdirlen
= strlen(get_object_directory());
438 struct strbuf buf
= STRBUF_INIT
;
439 struct packed_git
*p
;
443 prepare_packed_git();
444 for (p
= packed_git
; p
; p
= p
->next
) {
449 strbuf_grow(&buf
, cnt
* 53 + 2);
450 for (p
= packed_git
; p
; p
= p
->next
) {
452 strbuf_addf(&buf
, "P %s\n", p
->pack_name
+ objdirlen
+ 6);
454 strbuf_addch(&buf
, '\n');
457 send_strbuf("text/plain; charset=utf-8", &buf
);
458 strbuf_release(&buf
);
461 static void check_content_type(const char *accepted_type
)
463 const char *actual_type
= getenv("CONTENT_TYPE");
468 if (strcmp(actual_type
, accepted_type
)) {
469 http_status(415, "Unsupported Media Type");
473 "Expected POST with Content-Type '%s',"
474 " but received '%s' instead.\n",
475 accepted_type
, actual_type
);
480 static void service_rpc(char *service_name
)
482 const char *argv
[] = {NULL
, "--stateless-rpc", ".", NULL
};
483 struct rpc_service
*svc
= select_service(service_name
);
484 struct strbuf buf
= STRBUF_INIT
;
487 strbuf_addf(&buf
, "application/x-git-%s-request", svc
->name
);
488 check_content_type(buf
.buf
);
493 strbuf_addf(&buf
, "application/x-git-%s-result", svc
->name
);
494 hdr_str(content_type
, buf
.buf
);
500 strbuf_release(&buf
);
504 static NORETURN
void die_webcgi(const char *err
, va_list params
)
507 vreportf("fatal: ", err
, params
);
509 http_status(500, "Internal Server Error");
513 exit(0); /* we successfully reported a failure ;-) */
516 static int die_webcgi_recursing(void)
521 static char* getdir(void)
523 struct strbuf buf
= STRBUF_INIT
;
524 char *pathinfo
= getenv("PATH_INFO");
525 char *root
= getenv("GIT_PROJECT_ROOT");
526 char *path
= getenv("PATH_TRANSLATED");
529 if (!pathinfo
|| !*pathinfo
)
530 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
531 if (daemon_avoid_alias(pathinfo
))
532 die("'%s': aliased", pathinfo
);
533 end_url_with_slash(&buf
, root
);
534 if (pathinfo
[0] == '/')
536 strbuf_addstr(&buf
, pathinfo
);
537 return strbuf_detach(&buf
, NULL
);
538 } else if (path
&& *path
) {
539 return xstrdup(path
);
541 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
545 static struct service_cmd
{
550 {"GET", "/HEAD$", get_head
},
551 {"GET", "/info/refs$", get_info_refs
},
552 {"GET", "/objects/info/alternates$", get_text_file
},
553 {"GET", "/objects/info/http-alternates$", get_text_file
},
554 {"GET", "/objects/info/packs$", get_info_packs
},
555 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object
},
556 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file
},
557 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file
},
559 {"POST", "/git-upload-pack$", service_rpc
},
560 {"POST", "/git-receive-pack$", service_rpc
}
563 int main(int argc
, char **argv
)
565 char *method
= getenv("REQUEST_METHOD");
567 struct service_cmd
*cmd
= NULL
;
568 char *cmd_arg
= NULL
;
573 git_extract_argv0_path(argv
[0]);
574 set_die_routine(die_webcgi
);
575 set_die_is_recursing_routine(die_webcgi_recursing
);
578 die("No REQUEST_METHOD from server");
579 if (!strcmp(method
, "HEAD"))
583 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
584 struct service_cmd
*c
= &services
[i
];
588 if (regcomp(&re
, c
->pattern
, REG_EXTENDED
))
589 die("Bogus regex in service table: %s", c
->pattern
);
590 if (!regexec(&re
, dir
, 1, out
, 0)) {
593 if (strcmp(method
, c
->method
)) {
594 const char *proto
= getenv("SERVER_PROTOCOL");
595 if (proto
&& !strcmp(proto
, "HTTP/1.1")) {
596 http_status(405, "Method Not Allowed");
597 hdr_str("Allow", !strcmp(c
->method
, "GET") ?
598 "GET, HEAD" : c
->method
);
600 http_status(400, "Bad Request");
607 n
= out
[0].rm_eo
- out
[0].rm_so
;
608 cmd_arg
= xmemdupz(dir
+ out
[0].rm_so
+ 1, n
- 1);
609 dir
[out
[0].rm_so
] = 0;
616 not_found("Request not supported: '%s'", dir
);
619 if (!enter_repo(dir
, 0))
620 not_found("Not a git repository: '%s'", dir
);
621 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
622 access("git-daemon-export-ok", F_OK
) )
623 not_found("Repository not exported: '%s'", dir
);