From b4362df095ae1baa05fe3f83079dcdd131f5411b Mon Sep 17 00:00:00 2001 From: dongsheng Date: Wed, 16 Jul 2008 04:07:22 +0000 Subject: [PATCH] MDL-15349, add download function, which can download multi-file in paralle. --- repository/curl.class.php | 111 +++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 32 deletions(-) diff --git a/repository/curl.class.php b/repository/curl.class.php index 93b59f4fdc..ada6378b1b 100644 --- a/repository/curl.class.php +++ b/repository/curl.class.php @@ -5,6 +5,13 @@ * This is a wrapper class for curl, it is quite easy to use: * * $c = new curl(); + * // enable cache + * $c = new curl(array('cache'=>true)); + * // enable cookie + * $c = new curl(array('cookie'=>true)); + * // enable proxy + * $c = new curl(array('proxy'=>true)); + * * // HTTP GET Method * $html = $c->get('http://example.com'); * // HTTP POST Method @@ -13,7 +20,7 @@ * $html = $c->put('http://example.com/', array('file'=>'/var/www/test.txt'); * * @author Dongsheng Cai - * @version 0.2 dev + * @version 0.4 dev * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ @@ -26,7 +33,7 @@ class curl { private $proxy_type = ''; private $debug = false; private $cookie = false; - public $version = '0.2 dev'; + public $version = '0.4 dev'; public $response = array(); public $header = array(); public $info; @@ -205,24 +212,25 @@ class curl { } /** - * Formatting HTTP Response Header - * + * Set options for individual curl instance */ - protected function request($url, $options = array()){ - if (!preg_match('#^https?://#i', $url)) { - $this->error = 'Invalid protocol specified in url'; - return false; - } + private function apply_opt($curl, $options) { // Clean up $this->cleanopt(); - // create curl instance - $curl = curl_init($url); - $this->setopt(array('url'=>$url)); - // reset before set options - curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader')); + // set cookie + if(!empty($this->cookie) || !empty($options['cookie'])) { + $this->setopt(array('cookiejar'=>$this->cookie, + 'cookiefile'=>$this->cookie + )); + } + // set proxy + if(!empty($this->proxy) || !empty($options['proxy'])) { + $this->setopt($this->proxy); + } $this->setopt($options); - + // reset before set options + curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader')); // set headers if(empty($this->header)){ $this->setHeader(array( @@ -233,16 +241,11 @@ class curl { } curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header); - // set cookie - if(!empty($this->cookie)) { - $this->setopt(array('cookiejar'=>$this->cookie, - 'cookiefile'=>$this->cookie - )); - } - - // set proxy - if(!empty($this->proxy)) { - $this->setopt($this->proxy); + if($this->debug){ + echo '

Options

'; + var_dump($this->options); + echo '

Header

'; + var_dump($this->header); } // set options @@ -252,14 +255,58 @@ class curl { } curl_setopt($curl, $name, $val); } - - if($this->debug){ - echo '

Options

'; - var_dump($this->options); - echo '

Header

'; - var_dump($this->header); + return $curl; + } + /* + * Download mulit files in parallel + * $c = new curl; + * $c->download(array( + * array('url'=>'http://localhost/', 'file'=>fopen('a', 'wb')), + * array('url'=>'http://localhost/20/', 'file'=>fopen('b', 'wb')) + * )); + */ + public function download($requests, $options = array()) { + $options['returntransfer'] = false; + return $this->mulit_request($requests, $options); + } + /* + * Mulit HTTP Requests + * This function could run mulit-request in parallel. + */ + protected function mulit_request($requests, $options = array()) { + $count = count($requests); + $handles = array(); + $results = array(); + $main = curl_multi_init(); + for($i = 0; $i < $count; $i++) { + $url = $requests[$i]; + foreach($url as $n=>$v){ + $options[$n] = $url[$n]; + } + $handles[$i] = curl_init($url['url']); + $this->apply_opt($handles[$i], $options); + curl_multi_add_handle($main, $handles[$i]); } - + $running = 0; + do { + curl_multi_exec($main, $running); + } while($running > 0); + for($i = 0; $i < $count; $i++) + { + $results[] = curl_multi_getcontent($handles[$i]); + curl_multi_remove_handle($main, $handles[$i]); + } + curl_multi_close($main); + return $results; + } + /** + * Single HTTP Request + */ + protected function request($url, $options = array()){ + // create curl instance + $curl = curl_init($url); + $options['url'] = $url; + $this->apply_opt($curl, $options); if($this->cache && $ret = $this->cache->get($this->options)) { return $ret; } else { -- 2.39.5