PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/request/client/stream.php

https://bitbucket.org/herson091/kohana1
PHP | 109 lines | 53 code | 21 blank | 35 comment | 4 complexity | 0ebe1d517b57d6087f38e0cbb81d63fe MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * [Request_Client_External] Stream driver performs external requests using php
  4. * sockets. 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_Stream';
  11. *
  12. * @package Kohana
  13. * @category Base
  14. * @author Kohana Team
  15. * @copyright (c) 2008-2012 Kohana Team
  16. * @license http://kohanaframework.org/license
  17. * @uses [PHP Streams](http://php.net/manual/en/book.stream.php)
  18. */
  19. class Kohana_Request_Client_Stream extends Request_Client_External {
  20. /**
  21. * Sends the HTTP message [Request] to a remote server and processes
  22. * the response.
  23. *
  24. * @param Request $request request to send
  25. * @return Response
  26. * @uses [PHP cURL](http://php.net/manual/en/book.curl.php)
  27. */
  28. public function _send_message(Request $request)
  29. {
  30. // Calculate stream mode
  31. $mode = ($request->method() === HTTP_Request::GET) ? 'r' : 'r+';
  32. // Process cookies
  33. if ($cookies = $request->cookie())
  34. {
  35. $request->headers('cookie', http_build_query($cookies, NULL, '; '));
  36. }
  37. // Get the message body
  38. $body = $request->body();
  39. if (is_resource($body))
  40. {
  41. $body = stream_get_contents($body);
  42. }
  43. // Set the content length
  44. $request->headers('content-length', (string) strlen($body));
  45. list($protocol) = explode('/', $request->protocol());
  46. // Create the context
  47. $options = array(
  48. strtolower($protocol) => array(
  49. 'method' => $request->method(),
  50. 'header' => (string) $request->headers(),
  51. 'content' => $body
  52. )
  53. );
  54. // Create the context stream
  55. $context = stream_context_create($options);
  56. stream_context_set_option($context, $this->_options);
  57. $uri = $request->uri();
  58. if ($query = $request->query())
  59. {
  60. $uri .= '?'.http_build_query($query, NULL, '&');
  61. }
  62. $stream = fopen($uri, $mode, FALSE, $context);
  63. $meta_data = stream_get_meta_data($stream);
  64. // Get the HTTP response code
  65. $http_response = array_shift($meta_data['wrapper_data']);
  66. if (preg_match_all('/(\w+\/\d\.\d) (\d{3})/', $http_response, $matches) !== FALSE)
  67. {
  68. $protocol = $matches[1][0];
  69. $status = (int) $matches[2][0];
  70. }
  71. else
  72. {
  73. $protocol = NULL;
  74. $status = NULL;
  75. }
  76. // Create a response
  77. $response = $request->create_response();
  78. $response_header = $response->headers();
  79. // Process headers
  80. array_map(array($response_header, 'parse_header_string'), array(), $meta_data['wrapper_data']);
  81. $response->status($status)
  82. ->protocol($protocol)
  83. ->body(stream_get_contents($stream));
  84. // Close the stream after use
  85. fclose($stream);
  86. return $response;
  87. }
  88. } // End Kohana_Request_Client_Stream