4 function goWebGitBackEnd()
6 if(!isset($_SERVER["PHP_AUTH_USER"])) {
7 header('WWW-Authenticate: Basic realm="My Realm"');
8 header('HTTP/1.0 401 Unauthorized');
12 // well, i cant believe it, but this actually freaking works... for pulls/clones anyways... commits are not working...
13 // but wow thats better then what i had hoped for
15 $fh = fopen('php://input', "r");
17 $k1 = print_r($_REQUEST, true);
18 $k2 = print_r($_SERVER, true);
19 $k3 = print_r($_POST, true);
20 $k4 = print_r($_GET, true);
25 error_log($_SERVER["REQUEST_URI"]);
27 $ruri = $_SERVER["REQUEST_URI"];
28 $euri = str_replace("test.git", "", $_REQUEST["q"]);
30 $rmeth = $_SERVER["REQUEST_METHOD"];
33 foreach($_REQUEST as $key => $var) {
35 //error_log("adding, $var from $key");
36 if($qs == "") $qs.="$key=$var";
37 else $qs.="&$key=$var";
45 // this is where the fun, it ends.
52 if(isset($procenv)) unset($procenv);
53 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
54 $procenv["PATH_TRANSLATED"] = "/var/cache/git/test.git$euri";
55 $procenv["REQUEST_METHOD"] = "$rmeth";
56 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
57 $procenv["QUERY_STRING"] = "$qs";
58 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
59 $procenv["REMOTE_USER"] = "user";
60 $procenv["REMOTE_ADDR"] = "1.2.3.4";
61 $procenv["AUTH_TYPE"] = "Basic";
63 if(isset($_SERVER["CONTENT_TYPE"])) {
64 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
66 //$procenv["CONTENT_TYPE"] = "";
68 if(isset($_SERVER["CONTENT_LENGTH"])) {
69 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
74 $pwd = "/var/cache/git";
76 //error_log("openproc");
77 $proc = proc_open("/usr/lib/git-core/git-http-backend", array(array("pipe","rb"),array("pipe","wb"),array("file","/tmp/err", "a")), $pipes, $pwd, $procenv);
78 //$proc = proc_open("/home/paulr/src/eclipse-workspace/gwvp/datacatch/../datacatcher.sh", array(array("pipe","r"),array("pipe","w"),array("file","/tmp/err", "a")), $pipes, $pwd, $procenv);
79 //error_log("openproc2, $proc");
81 //error_log("openproc3");
83 //if($rmeth == "POST") {
87 //error_log("openproc4");
90 while(!$untilblank&&!feof($pipes[1])) {
91 $lines_t = fgets($pipes[1]);
92 $lines = trim($lines_t);
93 error_log("got line: $lines");
94 if($lines_t == "\r\n") {
96 error_log("now blank");
97 } else header($lines);
98 if($lines === false) {
99 error_log("got an unexpexted exit...");
105 //error_log("openproc5");
106 // if would seam that post and output must be synchronised together somehow... i think
108 $fh = fopen('php://input', 'rb');
111 $s = fread($fh, 1024);
113 error_log("unexpected eror on input read");
115 fwrite($pipes[0], $s);
121 $fl = fopen("/tmp/pushpipe", "ab");
122 while(!feof($pipes[1])) {
123 $d = fread($pipes[1], 1024);
124 error_log("got read, $d");
126 errog_log("got unexpected false on reads");
133 // oh god, something goes wrong with all this data and i dont know what it is
134 // but i think its cause proc_open doesnt deal with binary data properly
137 //$fl = fopen("/tmp/pushpipe", "w");
139 if(!stream_set_blocking($fh,0)) {
140 error_log("cant set input non-blocking");
142 // problem no 1 - dont do this
143 //if(!stream_set_blocking($pipes[0],0)) {
144 //error_log("cant set pipe 0 non-blocking");
146 if(!stream_set_blocking($pipes[1],0)) {
147 error_log("cant set pipe1 non-blocking");
150 // i was going to use stream_select, but i feel this works better like this
154 $from_client_data = fread($fh,8192);
155 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
157 //fwrite($fl, $from_client_data);
158 $client_len = strlen($from_client_data);
160 error_log("client end");
165 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
166 // but i do know that the fgets for the headers above should have comsued that
167 if(!feof($pipes[1])) {
168 $from_cgi_data_t = fread($pipes[1],8192);
169 $from_cgi_data = $from_cgi_data_t;
171 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
172 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
173 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
175 if(strlen($from_cgi_data_t)>0) {
176 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
181 if($from_cgi_data !== false) {
185 $cgi_len = strlen($from_cgi_data);
187 error_log("cgi end");
191 if(feof($pipes[1])) $continue = false;
193 if($client_len == 0 && $cgi_len == 0) {
195 error_log("sleep tick");
197 error_log("sizes: $client_len, $cgi_len");
199 error_log("from cgi: \"$from_cgi_data\"");
212 //error_log("openproc6");