1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Phing\Tasks\System\Condition; |
21
|
|
|
|
22
|
|
|
use Phing\Exception\BuildException; |
23
|
|
|
use Phing\Project; |
24
|
|
|
use Phing\ProjectComponent; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Condition to wait for a HTTP request to succeed. |
28
|
|
|
* |
29
|
|
|
* Attributes are: |
30
|
|
|
* - url - the URL of the request. |
31
|
|
|
* - errorsBeginAt - number at which errors begin at; default=400. |
32
|
|
|
* |
33
|
|
|
* @author Siad Ardroumli <[email protected]> |
34
|
|
|
* @package phing.tasks.system.condition |
35
|
|
|
*/ |
36
|
|
|
class HttpCondition extends ProjectComponent implements Condition |
37
|
|
|
{ |
38
|
|
|
public const DEFAULT_REQUEST_METHOD = 'GET'; |
39
|
|
|
|
40
|
|
|
private $errorsBeginAt = 400; |
41
|
|
|
private $url; |
42
|
|
|
private $quiet = false; |
43
|
|
|
private $requestMethod = self::DEFAULT_REQUEST_METHOD; |
44
|
|
|
private $followRedirects = true; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the url attribute. |
48
|
|
|
* |
49
|
|
|
* @param string $url the url of the request |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function setUrl($url) |
54
|
|
|
{ |
55
|
|
|
$this->url = $url; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the errorsBeginAt attribute. |
60
|
|
|
* |
61
|
|
|
* @param string $errorsBeginAt number at which errors begin at, default is 400 |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function setErrorsBeginAt($errorsBeginAt) |
66
|
|
|
{ |
67
|
|
|
$this->errorsBeginAt = $errorsBeginAt; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets the method to be used when issuing the HTTP request. |
72
|
|
|
* |
73
|
|
|
* @param string $method The HTTP request method to use. Valid values are |
74
|
|
|
* "GET", "HEAD", "TRACE", etc. The default |
75
|
|
|
* if not specified is "GET". |
76
|
|
|
*/ |
77
|
|
|
public function setRequestMethod($method) |
78
|
|
|
{ |
79
|
|
|
$this->requestMethod = $method === null ? self::DEFAULT_REQUEST_METHOD : strtoupper($method); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Whether redirects sent by the server should be followed, |
84
|
|
|
* defaults to true. |
85
|
|
|
* |
86
|
|
|
* @param boolean $f |
87
|
|
|
*/ |
88
|
|
|
public function setFollowRedirects($f) |
89
|
|
|
{ |
90
|
|
|
$this->followRedirects = $f; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set quiet mode, which suppresses warnings if curl_exec() fails. |
95
|
|
|
* |
96
|
|
|
* @param $bool |
97
|
|
|
*/ |
98
|
|
|
public function setQuiet($bool) |
99
|
|
|
{ |
100
|
|
|
$this->quiet = $bool; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
* |
106
|
|
|
* @return true if the HTTP request succeeds |
107
|
|
|
* |
108
|
|
|
* @throws BuildException if an error occurs |
109
|
|
|
*/ |
110
|
|
|
public function evaluate() |
111
|
|
|
{ |
112
|
|
|
if ($this->url === null) { |
113
|
|
|
throw new BuildException("No url specified in http condition"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!filter_var($this->url, FILTER_VALIDATE_URL)) { |
117
|
|
|
$this->log( |
118
|
|
|
"Possible malformed URL: " . $this->url, |
119
|
|
|
$this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->log("Checking for " . $this->url, Project::MSG_VERBOSE); |
124
|
|
|
|
125
|
|
|
$handle = curl_init($this->url); |
126
|
|
|
curl_setopt($handle, CURLOPT_NOBODY, true); |
127
|
|
|
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $this->requestMethod); |
128
|
|
|
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, $this->followRedirects); |
129
|
|
|
|
130
|
|
|
if (!curl_exec($handle)) { |
131
|
|
|
$this->log( |
132
|
|
|
"No response received from URL: " . $this->url, |
133
|
|
|
$this->quiet ? Project::MSG_VERBOSE : Project::MSG_ERR |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
return false; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
140
|
|
|
curl_close($handle); |
141
|
|
|
|
142
|
|
|
$this->log("Result code for " . $this->url . " was " . $httpCode, Project::MSG_VERBOSE); |
143
|
|
|
|
144
|
|
|
$result = false; |
145
|
|
|
if ($httpCode > 0 && $httpCode < $this->errorsBeginAt) { |
146
|
|
|
$result = true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $result; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|