|
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
|
|
|
* Base class for HTTP_Request2-backed tasks |
|
22
|
|
|
* |
|
23
|
|
|
* Handles nested <config /> and <header /> tags, contains a method for |
|
24
|
|
|
* HTTP_Request2 instance creation |
|
25
|
|
|
* |
|
26
|
|
|
* @package phing.tasks.ext |
|
27
|
|
|
* @author Alexey Borzov <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class HttpTask extends Task |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Holds the request URL |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $url = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Prototype HTTP_Request2 object, cloned in createRequest() |
|
40
|
|
|
* |
|
41
|
|
|
* @var HTTP_Request2 |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $requestPrototype = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Holds additional header data |
|
47
|
|
|
* |
|
48
|
|
|
* @var Parameter[] |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $headers = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Holds additional config data for HTTP_Request2 |
|
54
|
|
|
* |
|
55
|
|
|
* @var Parameter[] |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $configData = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Holds the authentication user name |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $authUser = null; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Holds the authentication password |
|
68
|
|
|
* |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $authPassword = ''; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Holds the authentication scheme |
|
75
|
|
|
* |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $authScheme; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Load the necessary environment for running this task. |
|
82
|
|
|
* |
|
83
|
|
|
* @throws BuildException |
|
84
|
|
|
*/ |
|
85
|
16 |
|
public function init() |
|
86
|
|
|
{ |
|
87
|
16 |
|
@include_once 'HTTP/Request2.php'; |
|
88
|
|
|
|
|
89
|
16 |
|
if (!class_exists('HTTP_Request2')) { |
|
90
|
|
|
throw new BuildException( |
|
91
|
|
|
get_class($this) . ' depends on HTTP_Request2 being installed ' |
|
92
|
|
|
. 'and on include_path.', |
|
93
|
|
|
$this->getLocation() |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
16 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Sets the request URL |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $url |
|
102
|
|
|
*/ |
|
103
|
14 |
|
public function setUrl($url) |
|
104
|
|
|
{ |
|
105
|
14 |
|
$this->url = $url; |
|
106
|
14 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Sets the prototype object that will be cloned in createRequest() |
|
110
|
|
|
* |
|
111
|
|
|
* Used in tests to inject an instance of HTTP_Request2 containing a custom adapter |
|
112
|
|
|
* |
|
113
|
|
|
* @param HTTP_Request2 $request |
|
114
|
|
|
*/ |
|
115
|
13 |
|
public function setRequestPrototype(HTTP_Request2 $request) |
|
116
|
|
|
{ |
|
117
|
13 |
|
$this->requestPrototype = $request; |
|
118
|
13 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Creates and configures an instance of HTTP_Request2 |
|
122
|
|
|
* |
|
123
|
|
|
* @return HTTP_Request2 |
|
124
|
|
|
*/ |
|
125
|
13 |
|
protected function createRequest() |
|
126
|
|
|
{ |
|
127
|
13 |
|
if (!$this->requestPrototype) { |
|
128
|
|
|
$request = new HTTP_Request2($this->url); |
|
129
|
|
|
} else { |
|
130
|
13 |
|
$request = clone $this->requestPrototype; |
|
131
|
13 |
|
$request->setUrl($this->url); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
13 |
|
foreach (array_keys($this->getProject()->getProperties()) as $propName) { |
|
135
|
13 |
|
if (0 === strpos($propName, 'phing.http.')) { |
|
136
|
2 |
|
$request->setConfig(substr($propName, 11), $this->getProject()->getProperty($propName)); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// set the authentication data |
|
141
|
13 |
|
if (!empty($this->authUser)) { |
|
142
|
2 |
|
$request->setAuth( |
|
143
|
2 |
|
$this->authUser, |
|
144
|
2 |
|
$this->authPassword, |
|
145
|
2 |
|
$this->authScheme |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
13 |
|
foreach ($this->configData as $config) { |
|
150
|
4 |
|
$request->setConfig($config->getName(), $config->getValue()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
13 |
|
foreach ($this->headers as $header) { |
|
154
|
2 |
|
$request->setHeader($header->getName(), $header->getValue()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
13 |
|
return $request; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Processes the server's response |
|
162
|
|
|
* |
|
163
|
|
|
* @param HTTP_Request2_Response $response |
|
164
|
|
|
* @return void |
|
165
|
|
|
* @throws BuildException |
|
166
|
|
|
*/ |
|
167
|
|
|
abstract protected function processResponse(HTTP_Request2_Response $response); |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Makes a HTTP request and processes its response |
|
171
|
|
|
* |
|
172
|
|
|
* @throws BuildException |
|
173
|
|
|
*/ |
|
174
|
16 |
|
public function main() |
|
175
|
|
|
{ |
|
176
|
16 |
|
if (!isset($this->url)) { |
|
177
|
2 |
|
throw new BuildException("Required attribute 'url' is missing"); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
try { |
|
181
|
14 |
|
$this->processResponse($this->createRequest()->send()); |
|
182
|
7 |
|
} catch (HTTP_Request2_MessageException $e) { |
|
183
|
|
|
throw new BuildException($e); |
|
184
|
|
|
} |
|
185
|
7 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Creates an additional header for this task |
|
189
|
|
|
* |
|
190
|
|
|
* @return Parameter The created header |
|
191
|
|
|
*/ |
|
192
|
2 |
|
public function createHeader() |
|
193
|
|
|
{ |
|
194
|
2 |
|
$num = array_push($this->headers, new Parameter()); |
|
195
|
|
|
|
|
196
|
2 |
|
return $this->headers[$num - 1]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Creates a config parameter for this task |
|
201
|
|
|
* |
|
202
|
|
|
* @return Parameter The created config parameter |
|
203
|
|
|
*/ |
|
204
|
4 |
|
public function createConfig() |
|
205
|
|
|
{ |
|
206
|
4 |
|
$num = array_push($this->configData, new Parameter()); |
|
207
|
|
|
|
|
208
|
4 |
|
return $this->configData[$num - 1]; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Sets the authentication user name |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $user |
|
215
|
|
|
*/ |
|
216
|
2 |
|
public function setAuthUser($user) |
|
217
|
|
|
{ |
|
218
|
2 |
|
$this->authUser = $user; |
|
219
|
2 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Sets the authentication password |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $password |
|
225
|
|
|
*/ |
|
226
|
2 |
|
public function setAuthPassword($password) |
|
227
|
|
|
{ |
|
228
|
2 |
|
$this->authPassword = $password; |
|
229
|
2 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Sets the authentication scheme |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $scheme |
|
235
|
|
|
*/ |
|
236
|
2 |
|
public function setAuthScheme($scheme) |
|
237
|
|
|
{ |
|
238
|
2 |
|
$this->authScheme = $scheme; |
|
239
|
2 |
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|