/lib/Elastica/Transport/Http.php

https://github.com/darkyoung/Elastica · PHP · 149 lines · 79 code · 29 blank · 41 comment · 17 complexity · 05caafe516120de35e64627c236359f9 MD5 · raw file

  1. <?php
  2. /**
  3. * Elastica Http Transport object
  4. *
  5. * @category Xodoa
  6. * @package Elastica
  7. * @author Nicolas Ruflin <spam@ruflin.com>
  8. */
  9. class Elastica_Transport_Http extends Elastica_Transport_Abstract
  10. {
  11. /**
  12. * Http scheme
  13. *
  14. * @var string Http scheme
  15. */
  16. protected $_scheme = 'http';
  17. /**
  18. * Curl resource to reuse
  19. *
  20. * @var resource Curl resource to reuse
  21. */
  22. protected static $_connection = null;
  23. /**
  24. * Makes calls to the elasticsearch server
  25. *
  26. * All calls that are made to the server are done through this function
  27. *
  28. * @param array $params Host, Port, ...
  29. * @return Elastica_Response Response object
  30. */
  31. public function exec(array $params)
  32. {
  33. $request = $this->getRequest();
  34. $conn = $this->_getConnection($request->getConfig('persistent'));
  35. // If url is set, url is taken. Otherwise port, host and path
  36. if (!empty($params['url'])) {
  37. $baseUri = $params['url'];
  38. } else {
  39. if (!isset($params['host']) || !isset($params['port'])) {
  40. throw new Elastica_Exception_Invalid('host and port have to be set');
  41. }
  42. $path = isset($params['path']) ? $params['path'] : '';
  43. $baseUri = $this->_scheme . '://' . $params['host'] . ':' . $params['port'] . '/' . $path;
  44. }
  45. $baseUri .= $request->getPath();
  46. $query = $request->getQuery();
  47. if (!empty($query)) {
  48. $baseUri .= '?' . http_build_query($query);
  49. }
  50. curl_setopt($conn, CURLOPT_URL, $baseUri);
  51. curl_setopt($conn, CURLOPT_TIMEOUT, $request->getConfig('timeout'));
  52. curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $request->getMethod());
  53. curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
  54. $this->_setupCurl($conn);
  55. $headersConfig = $request->getConfig('headers');
  56. if (!empty($headersConfig)) {
  57. $headers = array();
  58. while (list($header, $headerValue) = each($headersConfig)) {
  59. array_push($headers, $header . ': ' . $headerValue);
  60. }
  61. curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
  62. }
  63. // TODO: REFACTOR
  64. $data = $request->getData();
  65. if (isset($data) && !empty($data)) {
  66. if (is_array($data)) {
  67. $content = json_encode($data);
  68. } else {
  69. $content = $data;
  70. }
  71. // Escaping of / not necessary. Causes problems in base64 encoding of files
  72. $content = str_replace('\/', '/', $content);
  73. curl_setopt($conn, CURLOPT_POSTFIELDS, $content);
  74. }
  75. $start = microtime(true);
  76. // cURL opt returntransfer leaks memory, therefore OB instead.
  77. ob_start();
  78. curl_exec($conn);
  79. $responseString = ob_get_clean();
  80. $end = microtime(true);
  81. // Checks if error exists
  82. $errorNumber = curl_errno($conn);
  83. $response = new Elastica_Response($responseString);
  84. if (defined('DEBUG') && DEBUG) {
  85. $response->setQueryTime($end - $start);
  86. $response->setTransferInfo(curl_getinfo($conn));
  87. }
  88. if ($response->hasError()) {
  89. throw new Elastica_Exception_Response($response);
  90. }
  91. if ($errorNumber > 0) {
  92. throw new Elastica_Exception_Client($errorNumber, $request, $response);
  93. }
  94. return $response;
  95. }
  96. /**
  97. * Called to add additional curl params
  98. *
  99. * @param resource $connection Curl connection
  100. */
  101. protected function _setupCurl($connection)
  102. {
  103. foreach ($this->_request->getClient()->getConfig('curl') as $key => $param) {
  104. curl_setopt($connection, $key, $param);
  105. }
  106. }
  107. /**
  108. * Return Curl ressource
  109. *
  110. * @param bool $persistent False if not persistent connection
  111. * @return resource Connection resource
  112. */
  113. protected function _getConnection($persistent = true)
  114. {
  115. if (!$persistent || !self::$_connection) {
  116. self::$_connection = curl_init();
  117. }
  118. return self::$_connection;
  119. }
  120. }