http range request ass bandits from redhat for their installer and
authorpaulr <me@pjr.cc>
Tue, 26 Jul 2011 14:55:05 +0000 (00:55 +1000)
committerpaulr <me@pjr.cc>
Tue, 26 Jul 2011 14:55:05 +0000 (00:55 +1000)
the idiotic pain in the ass work-around

libglcas/web.php
plugins/admin.php
plugins/repo.php
plugins/webbase.php
www/index.php

index a8d013a..96d30b1 100644 (file)
@@ -43,7 +43,7 @@ function GLCASMessageBuilder()
        echo "<i>Messages not implemented yet</i>";
 }
 
-function GLCASpageBuilder($bodyClass, $bodyFunction, $title="GLCAS")
+function GLCASpageBuilder($bodyClass, $bodyFunction, $bodycontent, $title="GLCAS")
 {
        global $WEB_ROOT_FS, $BASE_URL;
        
@@ -103,7 +103,9 @@ function GLCASpageBuilder($bodyClass, $bodyFunction, $title="GLCAS")
        
        if($bodyClass != null) {
                $bodyClass->$bodyFunction($url);
-       } else $bodyFunction($url);
+       } else if( $bodyFunction != null) {
+               $bodyFunction($url);
+       } else echo $bodycontent;
        echo "</td></tr></table>";
        
        
index e6dea41..845d857 100644 (file)
@@ -156,6 +156,8 @@ class GLCASAdmin {
        
        function mainBody($url)
        {
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               
                // first, list available repos
                echo "<h3>Repositories</h3>";
                echo "<br><table border=\"1\">";
@@ -179,7 +181,14 @@ class GLCASAdmin {
                        echo "<tr><td>$desc</td><td>$repotype</td><td>$os</td><td>$version</td><td>$arch</td><td>$other</td><td>$prefix</td><td>$shorturl</td>";
                        
                        // get url
-                       echo "<td><a href=\"/asdf/asdf\">Browse</td><td>";
+                       $browseurl = "$BASE_URL/repo/$rkey";
+                       if($shorturl!="-") {
+                               $brurl = $shorturl;
+                               if($prefix != "-") $brurl = "$prefix/$shorturl";
+                               $browseurl = "$BASE_URL/$brurl/";
+                       }
+                       
+                       echo "<td><a href=\"$browseurl\">Browse</td><td>";
                        
                        // Edit
                        echo "<a href=\"?action=editrepo&repo=$rkey\">Edit</a> ";
index a703429..2584f3d 100644 (file)
@@ -41,6 +41,31 @@ class GLCASRepo {
                echo "i am the repo, $url";
        }
        
+       
+       // TODO: rework this function
+       /*
+        * What i need to do is have a downloader function
+        * that can cope with lots of different shit
+        * but thats a pipe dream
+        * 
+        * what *THIS* function needs to do is
+        * 1) figure out the repo
+        * 2) figure out the file in the repo
+        * 2.1) if its a directory, go to print directory
+        * 3) if the file exists, give it to the user (if a range is specified give the user the range)
+        * 4) if the file does not exist
+        *    - check if a tmp file exists
+        *    - attempt to get an exclusive flock
+        *    - if flock fails, donwload in progress
+        *    - if flock succeeds, truncate file and re-start download
+        *    - if a range request was made, send the range once available
+        *    - if range not available, sleep for 5 and check again.
+        * 
+        * 
+        */
+       
+       
+       // this is a nightmare
        function getRepoForUrl($url)
        {
                // the way we breakdown a url is to explode it
@@ -52,9 +77,22 @@ class GLCASRepo {
                $repostore = $this->config->getConfigVar("storagelocation");
                
                $matched = -1;
+               
+               // first we check for /repo/repoid as a url
                $startat = 0;
+               if($xurl[0] == "repo") {
+                       $repid = $xurl[1];
+                       error_log("trying to get repo for repoid, $repid");
+                       if(isset($uconf[$repid])) {
+                               $matched = ((int)($repid));
+                               error_log("set matched, $matched, $repid");
+                               $startat +=2;
+                       }
+               }
+               
+               
                $prematch = false;
-               foreach($uconf as $key => $var) {
+               if($matched < 0) foreach($uconf as $key => $var) {
                        $pre = $var["prefix"];
                        
                        if($pre!="") {
@@ -68,7 +106,7 @@ class GLCASRepo {
                }
                
                
-               foreach($uconf as $key => $var) {
+               if($matched < 0) foreach($uconf as $key => $var) {
                        // if we matched a pre, then we check against the second url component
                        
                        $short = $var["shorturl"];
@@ -83,8 +121,9 @@ class GLCASRepo {
                        }
                }
                
-               if($matched > -1) {
-                       //echo "Match on $key\n";
+               if($matched < 0) {
+                       echo "No such repo<br>";
+                       return;
                }
                
                
@@ -96,6 +135,7 @@ class GLCASRepo {
                
                // now we want to find repostore/$matched/$file;
                $actualfile = "$repostore/$matched/$file";
+               error_log("Atcualfile is $actualfile");
                //echo "Start file for $actualfile\n";
                
                // first check any directories in $file are in existence
@@ -109,17 +149,57 @@ class GLCASRepo {
                        }
                }
                
+               $reqhead = print_r($_REQUEST, true);
+               $sevhead = print_r($_SERVER, true);
+               
+               error_log("req $reqhead");
+               error_log("sev $sevhead");
+               
+               $rangestart = -1;
+               $rangelength = -1;
+               $rangesstr = "";
+               if(isset($_SERVER["HTTP_RANGE"])) {
+                       // oh shit
+                       $rangesa = explode("=", $_SERVER["HTTP_RANGE"]);
+                       $rangesb = explode(",", $rangesa[1]);
+                       $rangesstr = $rangesb[0];
+                       $ranges = explode("-", $rangesb[0]);
+                       $rangestart = $ranges[0];
+                       $rangelength = $ranges[1] - $ranges[0] +1; 
+                       error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
+               }
                
+               // i have to support http_range cause REDHAT/CENTOS IS annoying as all hell. christ, why do this?
                if(is_file($actualfile)) {
                        // file is stored locally, away we go
-                       header("Content-Length: ".filesize($actualfile));
+                       if($rangelength != -1) {
+                               header("HTTP/1.1 206 Partial Content");
+                               header("Content-Length: ".$rangelength);
+                               header("Content-Range: bytes $rangesstr/".filesize($actualfile));
+                               //header("Content-Length: ".filesize($actualfile));
+                       } else {
+                               header("Content-Length: ".filesize($actualfile));
+                       }
                        $type = mime_content_type($actualfile);
                        header("Content-type: $type");
                        $localfile = fopen($actualfile, "r");
+                       if($rangestart!=-1) fseek($localfile, $rangestart, SEEK_SET);
                        while(!feof($localfile)) {
-                               $data = fread($localfile, 16384);
+                               // cant make this high cause centos is crap
+                               if($rangelength!=-1) {
+                                       $data = fread($localfile, $rangelength);
+                                       error_log("data size was ".strlen($data));
+                               } else {
+                                       $data = fread($localfile, 2048);
+                               }
+                               
                                echo $data;
                                flush();
+                               
+                               if($rangelength!=-1) {
+                                       fclose($localfile);
+                                       exit(0);
+                               }
                        }
                        fclose($localfile);
                } else if(is_dir($actualfile)) {
@@ -134,6 +214,7 @@ class GLCASRepo {
                        // TODO: i should get remote contents with fopen/fread/fwrite as
                        // it should be more memory conservative and we can push to the end client
                        // straight away
+                       ignore_user_abort(true);
                        $rf = fopen($remotefile, "r");
                        error_log("attempting to get remote file $remotefile");
 
@@ -151,6 +232,7 @@ class GLCASRepo {
                                }
                                // get content length form upstream and print
                                if(preg_match("/^Content-Length:.*/", $val)) {
+                                       $clen = $val;
                                        header($val);
                                }
                                // get content type from upstream and print
@@ -164,6 +246,9 @@ class GLCASRepo {
                                header("HTTP/1.0 404 Not Found");
                        } else {
                                $localfile = fopen($actualfile.".tmp.data.deleteme", "w");                              
+                               $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
+                               fwrite($localsizefile, "$clen");
+                               fclose($localsizefile);         
                                while(!feof($rf)) {
                                        $data = fread($rf, 8192);
                                        echo $data;
@@ -173,7 +258,7 @@ class GLCASRepo {
                                fclose($localfile);
                                fclose($rf);
                                rename($actualfile.".tmp.data.deleteme", $actualfile);
-                               error_log("got actualfile, tried to save as $actualfile, did it work?");
+                               //error_log("got actualfile, tried to save as $actualfile, did it work?");
                        }
                }
                
@@ -184,15 +269,19 @@ class GLCASRepo {
        
        function printDir($dir, $localfile, $baseurl)
        {
+               $localfile = preg_replace("/\/\/+/", "/", $localfile);
                $uri = $_SERVER["REQUEST_URI"];
+               $content = "";
                if(is_dir($dir)) {
-                       echo "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
-                       echo "<table>";
+                       $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
+                       $content .= "<table>";
                        $dh = opendir($dir);
                        while(($file = readdir($dh))!==false) {
-                               if($file != "." && $file != "..") echo "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
+                               if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
                        }
-                       echo "</table></body></html>";
+                       $content .= "</table></body></html>";
+                       
+                       GLCASpageBuilder(null, null, $content);
                        
                } else return false;
        }
@@ -233,7 +322,7 @@ class GLCASRepo {
                        
                        // find a version, we assume its going to be something [numbers] and a . (optional)
                        if(preg_match("/^[0-9.]+$/", $comp)>0) {
-                               error_log("version match of $comp");
+                               //error_log("version match of $comp");
                                $glt["version"] = $comp;
                        }
                        
@@ -241,7 +330,7 @@ class GLCASRepo {
                        foreach($kos["arch"] as $archinter => $archname ) {
                                //error_log("Comparing $archinter, $archname with $comp");
                                if(strcasecmp($archname, $comp) == 0) {
-                                       error_log("arch match of $archname with $comp");
+                                       //error_log("arch match of $archname with $comp");
                                        $glt["arch"] = $archname;
                                }
                        }
@@ -339,6 +428,7 @@ class GLCASRepo {
                        mkdir("$repostore/$repokey/repodata");
                }
                
+               //ignore_user_abort(true);
                $actionurl = "$repourl/repodata/repomd.xml";
                $repomdxml = file_get_contents($actionurl);
                file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
index 6fa5943..fea2f2f 100644 (file)
@@ -19,7 +19,44 @@ class GLCASWebBase {
        
        function body($url)
        {
-               echo "i am disturbing, $url";
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               
+               // first, list available repos
+               echo "<h3>Repositories</h3>";
+               echo "<br><table border=\"1\">";
+               echo "<tr><th>Name</th><th>Type</th><th>OS</th><th>Version</th><th>Architecture</th><th>Other</th><th>Prefix</th><th>Short URL</th><th>Browse</th></tr>";
+               
+               // now iterate thru the repos and print them
+               $repo = new GLCASRepo($this->config);
+               $repos = $repo->getRepos();
+               
+               foreach($repos as $rkey => $rval) {
+                       $desc = $rval["desc"];
+                       $os = $rval["os"];
+                       $version = $rval["version"];
+                       $arch = $rval["arch"];
+                       $other = $rval["other"];
+                       $repotype = $rval["repotype"];
+                       $prefix = $rval["prefix"];
+                       $shorturl = $rval["shorturl"];
+                       if($prefix == "") $prefix = "-";
+                       if($shorturl == "") $shorturl = "-";
+                       echo "<tr><td>$desc</td><td>$repotype</td><td>$os</td><td>$version</td><td>$arch</td><td>$other</td><td>$prefix</td><td>$shorturl</td>";
+                       
+                       // get url
+                       $browseurl = "$BASE_URL/repo/$rkey";
+                       if($shorturl!="-") {
+                               $brurl = $shorturl;
+                               if($prefix != "-") $brurl = "$prefix/$shorturl";
+                               $browseurl = "$BASE_URL/$brurl/";
+                       }
+                       echo "<td><a href=\"$browseurl\">Browse</td><td>";
+                       echo "</td>";
+                       echo "</tr>";
+               }
+               
+               echo "</table><br><hr>";
+                       
        }
        
        private $config;
index e6e6f95..47a19a4 100644 (file)
@@ -38,6 +38,7 @@ if(file_exists($WEB_ROOT_FS."/../var")) {
        }
 }
 
+header("Accept-Ranges: none");
 
 $glconfig = new GLCASConfig();
 $glconfig->loadConfig($configpath);