HTTP.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * [Request_Client_External] HTTP driver performs external requests using the
  4. * php-http extension. To use this driver, ensure the following is completed
  5. * before executing an external request- ideally in the application bootstrap.
  6. *
  7. * @example
  8. *
  9. * // In application bootstrap
  10. * Request_Client_External::$client = 'Request_Client_HTTP';
  11. *
  12. * @package Kohana
  13. * @category Base
  14. * @author Kohana Team
  15. * @copyright (c) Kohana Team
  16. * @license https://koseven.ga/LICENSE.md
  17. * @uses [PECL HTTP](http://php.net/manual/en/book.http.php)
  18. */
  19. class Kohana_Request_Client_HTTP extends Request_Client_External {
  20. /**
  21. * Creates a new `Request_Client` object,
  22. * allows for dependency injection.
  23. *
  24. * @param array $params Params
  25. * @throws Request_Exception
  26. */
  27. public function __construct(array $params = [])
  28. {
  29. // Check that PECL HTTP supports requests
  30. if (!function_exists('http_support')) {
  31. throw new Request_Exception('Undefined function http_support()');
  32. }
  33. if (! http_support(HTTP_SUPPORT_REQUESTS))
  34. {
  35. throw new Request_Exception('Need HTTP request support!');
  36. }
  37. // Carry on
  38. parent::__construct($params);
  39. }
  40. /**
  41. * @var array curl options
  42. * @link http://www.php.net/manual/function.curl-setopt
  43. */
  44. protected $_options = [];
  45. /**
  46. * Sends the HTTP message [Request] to a remote server and processes
  47. * the response.
  48. *
  49. * @param Request $request request to send
  50. * @param Response $request response to send
  51. * @return Response
  52. */
  53. public function _send_message(Request $request, Response $response)
  54. {
  55. $http_method_mapping = [
  56. HTTP_Request::GET => HTTPRequest::METH_GET,
  57. HTTP_Request::HEAD => HTTPRequest::METH_HEAD,
  58. HTTP_Request::POST => HTTPRequest::METH_POST,
  59. HTTP_Request::PUT => HTTPRequest::METH_PUT,
  60. HTTP_Request::DELETE => HTTPRequest::METH_DELETE,
  61. HTTP_Request::OPTIONS => HTTPRequest::METH_OPTIONS,
  62. HTTP_Request::TRACE => HTTPRequest::METH_TRACE,
  63. HTTP_Request::CONNECT => HTTPRequest::METH_CONNECT,
  64. ];
  65. // Create an http request object
  66. $http_request = new HTTPRequest($request->uri(), $http_method_mapping[$request->method()]);
  67. if ($this->_options)
  68. {
  69. // Set custom options
  70. $http_request->setOptions($this->_options);
  71. }
  72. // Set headers
  73. $http_request->setHeaders($request->headers()->getArrayCopy());
  74. // Set cookies
  75. $http_request->setCookies($request->cookie());
  76. // Set query data (?foo=bar&bar=foo)
  77. $http_request->setQueryData($request->query());
  78. // Set the body
  79. if ($request->method() == HTTP_Request::PUT)
  80. {
  81. $http_request->addPutData($request->body());
  82. }
  83. else
  84. {
  85. $http_request->setBody($request->body());
  86. }
  87. try
  88. {
  89. $http_request->send();
  90. }
  91. catch (HTTPRequestException $e)
  92. {
  93. throw new Request_Exception($e->getMessage());
  94. }
  95. catch (HTTPMalformedHeaderException $e)
  96. {
  97. throw new Request_Exception($e->getMessage());
  98. }
  99. catch (HTTPEncodingException $e)
  100. {
  101. throw new Request_Exception($e->getMessage());
  102. }
  103. // Build the response
  104. $response->status($http_request->getResponseCode())
  105. ->headers($http_request->getResponseHeader())
  106. ->cookie($http_request->getResponseCookies())
  107. ->body($http_request->getResponseBody());
  108. return $response;
  109. }
  110. }