HttpRequestTask.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /*
  3. * $Id: HttpRequestTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. /**
  23. * A HTTP request task.
  24. * Making an HTTP request and try to match the response against an provided
  25. * regular expression.
  26. *
  27. * @package phing.tasks.ext
  28. * @author Benjamin Schultz <bschultz@proqrent.de>
  29. * @version $Id: HttpRequestTask.php 905 2010-10-05 16:28:03Z mrook $
  30. * @since 2.4.1
  31. */
  32. class HttpRequestTask extends Task
  33. {
  34. /**
  35. * Holds the request URL
  36. *
  37. * @var string
  38. */
  39. protected $_url = null;
  40. /**
  41. * Holds the regular expression that should match the response
  42. *
  43. * @var string
  44. */
  45. protected $_responseRegex = '';
  46. /**
  47. * Whether to enable detailed logging
  48. *
  49. * @var boolean
  50. */
  51. protected $_verbose = false;
  52. /**
  53. * Holds additional header data
  54. *
  55. * @var array<Parameter>
  56. */
  57. protected $_headers = array();
  58. /**
  59. * Holds additional config data for HTTP_Request2
  60. *
  61. * @var array<Parameter>
  62. */
  63. protected $_configData = array();
  64. /**
  65. * Holds the authentication user name
  66. *
  67. * @var string
  68. */
  69. protected $_authUser = null;
  70. /**
  71. * Holds the authentication password
  72. *
  73. * @var string
  74. */
  75. protected $_authPassword = '';
  76. /**
  77. * Holds the authentication scheme
  78. *
  79. * @var string
  80. */
  81. protected $_authScheme;
  82. /**
  83. * Holds the events that will be logged
  84. *
  85. * @var array<string>
  86. */
  87. protected $_observerEvents = array(
  88. 'connect',
  89. 'sentHeaders',
  90. 'sentBodyPart',
  91. 'receivedHeaders',
  92. 'receivedBody',
  93. 'disconnect',
  94. );
  95. /**
  96. * Sets the request URL
  97. *
  98. * @param string $url
  99. */
  100. public function setUrl($url)
  101. {
  102. $this->_url = $url;
  103. }
  104. /**
  105. * Sets the response regex
  106. *
  107. * @param string $regex
  108. */
  109. public function setResponseRegex($regex)
  110. {
  111. $this->_responseRegex = $regex;
  112. }
  113. /**
  114. * Sets the authentication user name
  115. *
  116. * @param string $user
  117. */
  118. public function setAuthUser($user)
  119. {
  120. $this->_authUser = $user;
  121. }
  122. /**
  123. * Sets the authentication password
  124. *
  125. * @param string $password
  126. */
  127. public function setAuthPassword($password)
  128. {
  129. $this->_authPassword = $password;
  130. }
  131. /**
  132. * Sets the authentication scheme
  133. *
  134. * @param string $scheme
  135. */
  136. public function setAuthScheme($scheme)
  137. {
  138. $this->_authScheme = $scheme;
  139. }
  140. /**
  141. * Sets whether to enable detailed logging
  142. *
  143. * @param boolean $verbose
  144. */
  145. public function setVerbose($verbose)
  146. {
  147. $this->_verbose = StringHelper::booleanValue($verbose);
  148. }
  149. /**
  150. * Sets a list of observer events that will be logged
  151. * if verbose output is enabled.
  152. *
  153. * @param string $observerEvents List of observer events
  154. *
  155. * @return void
  156. */
  157. public function setObserverEvents($observerEvents)
  158. {
  159. $this->_observerEvents = array();
  160. $token = ' ,;';
  161. $ext = strtok($observerEvents, $token);
  162. while ($ext !== false) {
  163. $this->_observerEvents[] = $ext;
  164. $ext = strtok($token);
  165. }
  166. }
  167. /**
  168. * Creates an additional header for this task
  169. *
  170. * @return Parameter The created header
  171. */
  172. public function createHeader()
  173. {
  174. $num = array_push($this->_headers, new Parameter());
  175. return $this->_headers[$num-1];
  176. }
  177. /**
  178. * Creates a config parameter for this task
  179. *
  180. * @return Parameter The created parameter
  181. */
  182. public function createConfig()
  183. {
  184. $num = array_push($this->_configData, new Parameter());
  185. return $this->_configData[$num-1];
  186. }
  187. /**
  188. * Load the necessary environment for running this task.
  189. *
  190. * @throws BuildException
  191. */
  192. public function init()
  193. {
  194. @include_once 'HTTP/Request2.php';
  195. if (! class_exists('HTTP_Request2')) {
  196. throw new BuildException(
  197. 'HttpRequestTask depends on HTTP_Request2 being installed '
  198. . 'and on include_path.',
  199. $this->getLocation()
  200. );
  201. }
  202. $this->_authScheme = HTTP_Request2::AUTH_BASIC;
  203. // Other dependencies that should only be loaded
  204. // when class is actually used
  205. require_once 'HTTP/Request2/Observer/Log.php';
  206. }
  207. /**
  208. * Make the http request
  209. */
  210. public function main()
  211. {
  212. if (!isset($this->_url)) {
  213. throw new BuildException("Missing attribute 'url' set");
  214. }
  215. $request = new HTTP_Request2($this->_url);
  216. // set the authentication data
  217. if (!empty($this->_authUser)) {
  218. $request->setAuth(
  219. $this->_authUser,
  220. $this->_authPassword,
  221. $this->_authScheme
  222. );
  223. }
  224. foreach ($this->_configData as $config) {
  225. $request->setConfig($config->getName(), $config->getValue());
  226. }
  227. foreach ($this->_headers as $header) {
  228. $request->setHeader($header->getName(), $header->getValue());
  229. }
  230. if ($this->_verbose) {
  231. $observer = new HTTP_Request2_Observer_Log();
  232. // set the events we want to log
  233. $observer->events = $this->_observerEvents;
  234. $request->attach($observer);
  235. }
  236. $response = $request->send();
  237. if ($this->_responseRegex !== '') {
  238. $matches = array();
  239. preg_match($this->_responseRegex, $response->getBody(), $matches);
  240. if (count($matches) === 0) {
  241. throw new BuildException(
  242. 'The received response body did not match the '
  243. . 'given regular expression'
  244. );
  245. } else {
  246. $this->log('The response body matched the provided regex.');
  247. }
  248. }
  249. }
  250. }