diff options
Diffstat (limited to 'buildscripts/phing/classes/phing/tasks/ext/HttpGetTask.php')
-rwxr-xr-x | buildscripts/phing/classes/phing/tasks/ext/HttpGetTask.php | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/buildscripts/phing/classes/phing/tasks/ext/HttpGetTask.php b/buildscripts/phing/classes/phing/tasks/ext/HttpGetTask.php new file mode 100755 index 00000000..114b80a1 --- /dev/null +++ b/buildscripts/phing/classes/phing/tasks/ext/HttpGetTask.php @@ -0,0 +1,170 @@ +<?php +/* + * $Id: f3fa317b72e2f70f1e483fa49dbf089094e2a476 $ + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This software consists of voluntary contributions made by many individuals + * and is licensed under the LGPL. For more information please see + * <http://phing.info>. + */ + +require_once 'phing/Task.php'; + +/** + * A HTTP request task. + * Making an HTTP request and try to match the response against an provided + * regular expression. + * + * @package phing.tasks.ext + * @author Ole Markus With <o.with@sportradar.com> + * @version $Id: f3fa317b72e2f70f1e483fa49dbf089094e2a476 $ + */ +class HttpGetTask extends Task +{ + /** + * Holds the request URL + * + * @var string + */ + protected $url = null; + + /** + * Holds the filename to store the output in + * + * @var string + */ + protected $filename = null; + + /** + * Holds the save location + * + * @var string + */ + protected $dir = null; + + /** + * Holds the proxy + * + * @var string + */ + protected $_proxy = null; + + /** + * Load the necessary environment for running this task. + * + * @throws BuildException + */ + public function init() + { + @include_once 'HTTP/Request2.php'; + + if (! class_exists('HTTP_Request2')) { + throw new BuildException( + 'HttpRequestTask depends on HTTP_Request2 being installed ' + . 'and on include_path.', + $this->getLocation() + ); + } + } + + + /** + * Make the GET request + * + * @throws BuildException + */ + public function main() + { + if (!isset($this->url)) { + throw new BuildException("Missing attribute 'url'"); + } + + if (!isset($this->dir)) { + throw new BuildException("Missing attribute 'dir'"); + } + + $config = array(); + if (isset($this->_proxy) && $url = parse_url($this->_proxy)) { + $config['proxy_user'] = $url['user']; + $config['proxy_password'] = $url['pass']; + $config['proxy_host'] = $url['host']; + $config['proxy_port'] = $url['port']; + } + + $this->log("Fetching " . $this->url); + + $request = new HTTP_Request2($this->url, '', $config); + $response = $request->send(); + if ($response->getStatus() != 200) { + throw new BuildException("Request unsuccessful. Response from server: " . $response->getStatus() . " " . $response->getReasonPhrase()); + } + + $content = $response->getBody(); + $disposition = $response->getHeader('content-disposition'); + + if ($this->filename) { + $filename = $this->filename; + } elseif ($disposition && 0 == strpos($disposition, 'attachment') + && preg_match('/filename="([^"]+)"/', $disposition, $m)) { + $filename = basename($m[1]); + } else { + $filename = basename(parse_url($this->url, PHP_URL_PATH)); + } + + if (!is_writable($this->dir)) { + throw new BuildException("Cannot write to directory: " . $this->dir); + } + + $filename = $this->dir . "/" . $filename; + file_put_contents($filename, $content); + + $this->log("Contents from " . $this->url . " saved to $filename"); + } + + /** + * Sets the request URL + * + * @param string $url + */ + public function setUrl($url) { + $this->url = $url; + } + + /** + * Sets the filename to store the output in + * + * @param string $filename + */ + public function setFilename($filename) { + $this->filename = $filename; + } + + /** + * Sets the save location + * + * @param string $dir + */ + public function setDir($dir) { + $this->dir = $dir; + } + + /** + * Sets the proxy + * + * @param string $proxy + */ + public function setProxy($proxy) { + $this->_proxy = $proxy; + } +} |