File: HttpGetTask.php

package info (click to toggle)
phing 2.8.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,764 kB
  • ctags: 10,110
  • sloc: php: 32,188; xml: 537; makefile: 16; sh: 5
file content (183 lines) | stat: -rw-r--r-- 4,775 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
 * $Id: 6199c8c34ea6f0cc71b5d0e630c68071a23140d4 $
 *
 * 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/tasks/ext/HttpTask.php';

/**
 * A HTTP download task.
 *
 * Downloads a file via HTTP GET method and saves it to a specified directory
 *
 * @package phing.tasks.ext
 * @author  Ole Markus With <o.with@sportradar.com>
 * @version $Id: 6199c8c34ea6f0cc71b5d0e630c68071a23140d4 $
 */
class HttpGetTask extends HttpTask
{
    /**
     * Holds the filename to store the output in
     *
     * @var string
     */
    protected $filename = null;    

    /**
     * Holds the save location
     *
     * @var string
     */
    protected $dir = null;

    /**
     * Holds value for "ssl_verify_peer" option
     *
     * @var boolean
     */
    protected $sslVerifyPeer = true;
    
    /**
     * Holds value for "follow_redirects" option
     *
     * @var null|bool
     */
    protected $followRedirects = null;

    /**
     * Holds the proxy
     *
     * @var string
     */
    protected $proxy = null;

    protected function createRequest()
    {
        if (!isset($this->dir)) {
            throw new BuildException("Required attribute 'dir' is missing");
        }

        $config = array(
            'ssl_verify_peer' => $this->sslVerifyPeer
        );
        if (isset($this->proxy)) {
            $config['proxy'] = $this->proxy;
        }
        if (null !== $this->followRedirects) {
            $config['follow_redirects'] = $this->followRedirects;
        }

        $request = parent::createRequest();
        $request->setConfig($config);

        $this->log("Fetching " . $this->url);

        return $request;
    }

    /**
     * Saves the response body to a specified directory
     *
     * @param HTTP_Request2_Response $response
     * @return void
     * @throws BuildException
     */
    protected function processResponse(HTTP_Request2_Response $response)
    {
        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 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 ssl_verify_peer option
     *
     * @param bool $value
     */
    public function setSslVerifyPeer($value)
    {
        $this->sslVerifyPeer = $value;
    }
    
    /**
     * Sets the follow_redirects option
     *
     * @param bool $value
     */
    public function setFollowRedirects($value)
    {
        $this->followRedirects = $value;
    }

    /**
     * Sets the proxy
     *
     * @param string $proxy
     */
    public function setProxy($proxy)
    {
        $this->proxy = $proxy;
    }
}