summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/tasks/ext/HttpGetTask.php
blob: 114b80a1ffe830fe5ffb3f054da7ba23d854c742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
    }
}