Changeset 8f2c6c5


Ignore:
Timestamp:
01/05/12 11:37:12 (5 months ago)
Author:
mrook
Branches:
master
Children:
8bea7a4, 008aa2e
Parents:
2d6c03b
git-author:
Michiel Rook <mrook@…> (01/05/12 11:37:12)
git-committer:
Michiel Rook <mrook@…> (01/05/12 11:37:12)
Message:

Refs pull request #70 - add proxy attribute to http get and request tasks

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • classes/phing/tasks/ext/HttpGetTask.php

    • Property mode changed from 100644 to 100755
    r84b1411 r8f2c6c5  
    4646     */ 
    4747    protected $dir = null; 
    48  
     48     
     49    /** 
     50     * Holds the proxy 
     51     * 
     52     * @var string 
     53     */ 
     54    protected $_proxy = null; 
    4955 
    5056    /** 
     
    5561    public function init() 
    5662    { 
    57         require_once 'HTTP/Request2.php'; 
     63        @include_once 'HTTP/Request2.php'; 
     64 
     65        if (! class_exists('HTTP_Request2')) { 
     66            throw new BuildException( 
     67                'HttpRequestTask depends on HTTP_Request2 being installed ' 
     68                . 'and on include_path.', 
     69                $this->getLocation() 
     70            ); 
     71        } 
    5872    } 
    5973 
     
    7387            throw new BuildException("Missing attribute 'dir'"); 
    7488        } 
     89         
     90        $config = array(); 
     91        if (isset($this->_proxy) && $url = parse_url($this->_proxy)) { 
     92            $config['proxy_user'] = $url['user']; 
     93            $config['proxy_password'] = $url['pass']; 
     94            $config['proxy_host'] = $url['host']; 
     95            $config['proxy_port'] = $url['port']; 
     96        } 
    7597 
    76         $this->log("Fetching " . $this->url); 
     98            $this->log("Fetching " . $this->url); 
    7799 
    78         $request = new HTTP_Request2($this->url); 
    79         $response =  $request->send(); 
    80         if ($response->getStatus() != 200) { 
    81                 throw new BuildException("Request unsuccessfull. Response from server: " . $response->getStatus() . " " . $response->getReasonPhrase()); 
    82         } 
    83         $content = $response->getBody(); 
    84         if ($this->filename) { 
    85                 $filename = $this->filename; 
    86         } elseif ($disposition = $response->getHeader('content-disposition') 
    87                 && 0 == strpos($disposition, 'attachment') 
    88                 && preg_match('/filename="([^"]+)"/', $disposition, $m)) { 
    89                 $filename = basename($m[1]); 
    90         } else { 
    91                 $filename = basename(parse_url($this->url, PHP_URL_PATH)); 
    92         } 
    93  
    94         if (!is_writable($this->dir)) { 
    95                 throw new BuildException("Cannot write to directory: " . $this->dir); 
    96         } 
    97         $filename = $this->dir . "/" . $filename; 
    98         file_put_contents($filename, $content); 
    99         $this->log("Contents from " . $this->url . " saved to $filename"); 
     100        $request = new HTTP_Request2($this->url, $config); 
     101            $response =  $request->send(); 
     102            if ($response->getStatus() != 200) { 
     103                throw new BuildException("Request unsuccessful. Response from server: " . $response->getStatus() . " " . $response->getReasonPhrase()); 
     104        } 
     105         
     106        $content = $response->getBody(); 
     107        if ($this->filename) { 
     108                $filename = $this->filename; 
     109        } elseif ($disposition = $response->getHeader('content-disposition') 
     110                && 0 == strpos($disposition, 'attachment') 
     111                && preg_match('/filename="([^"]+)"/', $disposition, $m)) { 
     112                $filename = basename($m[1]); 
     113        } else { 
     114                $filename = basename(parse_url($this->url, PHP_URL_PATH)); 
     115        } 
     116     
     117        if (!is_writable($this->dir)) { 
     118                throw new BuildException("Cannot write to directory: " . $this->dir); 
     119        } 
     120         
     121        $filename = $this->dir . "/" . $filename; 
     122        file_put_contents($filename, $content); 
     123         
     124        $this->log("Contents from " . $this->url . " saved to $filename"); 
    100125    } 
    101126 
     
    112137    } 
    113138 
     139    /** 
     140     * Sets the proxy 
     141     *  
     142     * @param string $proxy 
     143     */ 
     144    public function setProxy($proxy) { 
     145        $this->_proxy = $proxy; 
     146    } 
    114147} 
  • classes/phing/tasks/ext/HttpRequestTask.php

    • Property mode changed from 100644 to 100755
    rf410e6e r8f2c6c5  
    4242 
    4343    /** 
     44     * Holds the proxy 
     45     * 
     46     * @var string 
     47     */ 
     48    protected $_proxy = null; 
     49 
     50    /** 
    4451     * Holds the regular expression that should match the response 
    4552     * 
     
    115122 
    116123    /** 
     124     * Sets the proxy 
     125     *  
     126     * @param string $proxy 
     127     */ 
     128    public function setProxy($proxy) { 
     129        $this->_proxy = $proxy; 
     130    } 
     131     
     132    /** 
    117133     * Sets the response regex 
    118134     * 
     
    240256        } 
    241257 
    242         $request = new HTTP_Request2($this->_url); 
     258        $config = array(); 
     259        if (isset($this->_proxy) && $url = parse_url($this->_proxy)) { 
     260            $config['proxy_user'] = $url['user']; 
     261            $config['proxy_password'] = $url['pass']; 
     262            $config['proxy_host'] = $url['host']; 
     263            $config['proxy_port'] = $url['port']; 
     264        } 
     265         
     266        $request = new HTTP_Request2($this->_url, $config); 
    243267 
    244268        // set the authentication data 
  • docs/phing_guide/book/chapters/appendixes/AppendixC-OptionalTasks.html

    • Property mode changed from 100644 to 100755
    r582f22d r8f2c6c5  
    21292129                                <td>No</td> 
    21302130                        </tr> 
    2131                 </tbody> 
     2131            <tr> 
     2132                <td>proxy</td> 
     2133                <td>String</td> 
     2134                <td>The proxy to use, such as <em>http://user:password@hostname:port/</em></td> 
     2135                <td>n/a</td> 
     2136                <td>No</td> 
     2137            </tr> 
     2138                </tbody> 
    21322139        </table> 
    21332140        <h3>Example</h3> 
     
    21622169                                <td>Yes</td> 
    21632170                        </tr> 
     2171            <tr> 
     2172                <td>proxy</td> 
     2173                <td>String</td> 
     2174                <td>The proxy to use, such as <em>http://user:password@hostname:port/</em></td> 
     2175                <td>n/a</td> 
     2176                <td>No</td> 
     2177            </tr> 
    21642178                        <tr> 
    21652179                                <td>responseRegex</td> 
Note: See TracChangeset for help on using the changeset viewer.