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
|
|
|
/** |
21
|
|
|
* Condition to wait for a HTTP request to succeed. |
22
|
|
|
* |
23
|
|
|
* Attributes are: |
24
|
|
|
* - url - the URL of the request. |
25
|
|
|
* - errorsBeginAt - number at which errors begin at; default=400. |
26
|
|
|
* |
27
|
|
|
* @author Siad Ardroumli <[email protected]> |
28
|
|
|
* @package phing.tasks.system.condition |
29
|
|
|
*/ |
30
|
|
|
class HttpCondition extends ProjectComponent implements Condition |
31
|
|
|
{ |
32
|
|
|
const DEFAULT_REQUEST_METHOD = 'GET'; |
33
|
|
|
|
34
|
|
|
private $errorsBeginAt = 400; |
35
|
|
|
private $url; |
36
|
|
|
private $quiet = false; |
37
|
|
|
private $requestMethod = self::DEFAULT_REQUEST_METHOD; |
38
|
|
|
private $followRedirects = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the url attribute. |
42
|
|
|
* |
43
|
|
|
* @param string $url the url of the request |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
1 |
|
public function setUrl($url) |
48
|
|
|
{ |
49
|
1 |
|
$this->url = $url; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the errorsBeginAt attribute. |
54
|
|
|
* |
55
|
|
|
* @param string $errorsBeginAt number at which errors begin at, default is 400 |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
1 |
|
public function setErrorsBeginAt($errorsBeginAt) |
60
|
|
|
{ |
61
|
1 |
|
$this->errorsBeginAt = $errorsBeginAt; |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets the method to be used when issuing the HTTP request. |
66
|
|
|
* |
67
|
|
|
* @param string $method The HTTP request method to use. Valid values are |
68
|
|
|
* "GET", "HEAD", "TRACE", etc. The default |
69
|
|
|
* if not specified is "GET". |
70
|
|
|
*/ |
71
|
|
|
public function setRequestMethod($method) |
72
|
|
|
{ |
73
|
|
|
$this->requestMethod = $method === null ? self::DEFAULT_REQUEST_METHOD : strtoupper($method); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Whether redirects sent by the server should be followed, |
78
|
|
|
* defaults to true. |
79
|
|
|
* |
80
|
|
|
* @param boolean $f |
81
|
|
|
*/ |
82
|
|
|
public function setFollowRedirects($f) |
83
|
|
|
{ |
84
|
|
|
$this->followRedirects = $f; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set quiet mode, which suppresses warnings if curl_exec() fails. |
89
|
|
|
* |
90
|
|
|
* @param $bool |
91
|
|
|
*/ |
92
|
|
|
public function setQuiet($bool) |
93
|
|
|
{ |
94
|
|
|
$this->quiet = $bool; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
* |
100
|
|
|
* @return true if the HTTP request succeeds |
101
|
|
|
* |
102
|
|
|
* @throws BuildException if an error occurs |
103
|
|
|
*/ |
104
|
1 |
|
public function evaluate() |
105
|
|
|
{ |
106
|
1 |
|
if ($this->url === null) { |
107
|
|
|
throw new BuildException("No url specified in http condition"); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
if (!filter_var($this->url, FILTER_VALIDATE_URL)) { |
111
|
|
|
$this->log( |
112
|
|
|
"Possible malformed URL: " . $this->url, |
113
|
|
|
$this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$this->log("Checking for " . $this->url, Project::MSG_VERBOSE); |
118
|
|
|
|
119
|
1 |
|
$handle = curl_init($this->url); |
120
|
1 |
|
curl_setopt($handle, CURLOPT_NOBODY, true); |
|
|
|
|
121
|
1 |
|
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $this->requestMethod); |
122
|
1 |
|
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, $this->followRedirects); |
123
|
|
|
|
124
|
1 |
|
if (!curl_exec($handle)) { |
|
|
|
|
125
|
|
|
$this->log( |
126
|
|
|
"No response received from URL: " . $this->url, |
127
|
|
|
$this->quiet ? Project::MSG_VERBOSE : Project::MSG_ERR |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return false; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
|
|
|
134
|
1 |
|
curl_close($handle); |
|
|
|
|
135
|
|
|
|
136
|
1 |
|
$this->log("Result code for " . $this->url . " was " . $httpCode, Project::MSG_VERBOSE); |
137
|
|
|
|
138
|
1 |
|
$result = false; |
139
|
1 |
|
if ($httpCode > 0 && $httpCode < $this->errorsBeginAt) { |
140
|
1 |
|
$result = true; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return $result; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|