Drupal investigation

Client.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * This file is part of the Goutte package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Goutte;
  11. use GuzzleHttp\Client as GuzzleClient;
  12. use GuzzleHttp\ClientInterface as GuzzleClientInterface;
  13. use GuzzleHttp\Cookie\CookieJar;
  14. use GuzzleHttp\Exception\RequestException;
  15. use Psr\Http\Message\ResponseInterface;
  16. use Symfony\Component\BrowserKit\Client as BaseClient;
  17. use Symfony\Component\BrowserKit\Request;
  18. use Symfony\Component\BrowserKit\Response;
  19. /**
  20. * Client.
  21. *
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. * @author Michael Dowling <michael@guzzlephp.org>
  24. * @author Charles Sarrazin <charles@sarraz.in>
  25. */
  26. class Client extends BaseClient
  27. {
  28. protected $client;
  29. private $headers = array();
  30. private $auth = null;
  31. public function setClient(GuzzleClientInterface $client)
  32. {
  33. $this->client = $client;
  34. return $this;
  35. }
  36. public function getClient()
  37. {
  38. if (!$this->client) {
  39. $this->client = new GuzzleClient(array('allow_redirects' => false, 'cookies' => true));
  40. }
  41. return $this->client;
  42. }
  43. public function setHeader($name, $value)
  44. {
  45. $this->headers[strtolower($name)] = $value;
  46. return $this;
  47. }
  48. public function removeHeader($name)
  49. {
  50. unset($this->headers[strtolower($name)]);
  51. }
  52. public function resetHeaders()
  53. {
  54. $this->headers = array();
  55. return $this;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function restart()
  61. {
  62. parent::restart();
  63. $this->resetAuth()
  64. ->resetHeaders();
  65. }
  66. public function setAuth($user, $password = '', $type = 'basic')
  67. {
  68. $this->auth = array($user, $password, $type);
  69. return $this;
  70. }
  71. public function resetAuth()
  72. {
  73. $this->auth = null;
  74. return $this;
  75. }
  76. /**
  77. * @param Request $request
  78. *
  79. * @return Response
  80. */
  81. protected function doRequest($request)
  82. {
  83. $headers = array();
  84. foreach ($request->getServer() as $key => $val) {
  85. $key = strtolower(str_replace('_', '-', $key));
  86. $contentHeaders = array('content-length' => true, 'content-md5' => true, 'content-type' => true);
  87. if (0 === strpos($key, 'http-')) {
  88. $headers[substr($key, 5)] = $val;
  89. }
  90. // CONTENT_* are not prefixed with HTTP_
  91. elseif (isset($contentHeaders[$key])) {
  92. $headers[$key] = $val;
  93. }
  94. }
  95. $cookies = CookieJar::fromArray(
  96. $this->getCookieJar()->allRawValues($request->getUri()),
  97. parse_url($request->getUri(), PHP_URL_HOST)
  98. );
  99. $requestOptions = array(
  100. 'cookies' => $cookies,
  101. 'allow_redirects' => false,
  102. 'auth' => $this->auth,
  103. );
  104. if (!in_array($request->getMethod(), array('GET', 'HEAD'))) {
  105. if (null !== $content = $request->getContent()) {
  106. $requestOptions['body'] = $content;
  107. } else {
  108. if ($files = $request->getFiles()) {
  109. $requestOptions['multipart'] = [];
  110. $this->addPostFields($request->getParameters(), $requestOptions['multipart']);
  111. $this->addPostFiles($files, $requestOptions['multipart']);
  112. } else {
  113. $requestOptions['form_params'] = $request->getParameters();
  114. }
  115. }
  116. }
  117. if (!empty($headers)) {
  118. $requestOptions['headers'] = $headers;
  119. }
  120. $method = $request->getMethod();
  121. $uri = $request->getUri();
  122. foreach ($this->headers as $name => $value) {
  123. $requestOptions['headers'][$name] = $value;
  124. }
  125. // Let BrowserKit handle redirects
  126. try {
  127. $response = $this->getClient()->request($method, $uri, $requestOptions);
  128. } catch (RequestException $e) {
  129. $response = $e->getResponse();
  130. if (null === $response) {
  131. throw $e;
  132. }
  133. }
  134. return $this->createResponse($response);
  135. }
  136. protected function addPostFiles(array $files, array &$multipart, $arrayName = '')
  137. {
  138. if (empty($files)) {
  139. return;
  140. }
  141. foreach ($files as $name => $info) {
  142. if (!empty($arrayName)) {
  143. $name = $arrayName.'['.$name.']';
  144. }
  145. $file = [
  146. 'name' => $name,
  147. ];
  148. if (is_array($info)) {
  149. if (isset($info['tmp_name'])) {
  150. if ('' !== $info['tmp_name']) {
  151. $file['contents'] = fopen($info['tmp_name'], 'r');
  152. if (isset($info['name'])) {
  153. $file['filename'] = $info['name'];
  154. }
  155. } else {
  156. continue;
  157. }
  158. } else {
  159. $this->addPostFiles($info, $multipart, $name);
  160. continue;
  161. }
  162. } else {
  163. $file['contents'] = fopen($info, 'r');
  164. }
  165. $multipart[] = $file;
  166. }
  167. }
  168. public function addPostFields(array $formParams, array &$multipart, $arrayName = '')
  169. {
  170. foreach ($formParams as $name => $value) {
  171. if (!empty($arrayName)) {
  172. $name = $arrayName.'['.$name.']';
  173. }
  174. if (is_array($value)) {
  175. $this->addPostFields($value, $multipart, $name);
  176. } else {
  177. $multipart[] = [
  178. 'name' => $name,
  179. 'contents' => $value,
  180. ];
  181. }
  182. }
  183. }
  184. protected function createResponse(ResponseInterface $response)
  185. {
  186. return new Response((string) $response->getBody(), $response->getStatusCode(), $response->getHeaders());
  187. }
  188. }