示例#1
0
 /**
  * Creates a new `Request_Client` object,
  * allows for dependency injection.
  *
  * @param   array    $params Params
  * @throws  Request_Exception
  */
 public function __construct(array $params = array())
 {
     // Check that PECL HTTP supports requests
     if (!http_support(HTTP_SUPPORT_REQUESTS)) {
         throw new Request_Exception('Need HTTP request support!');
     }
     // Carry on
     parent::__construct($params);
 }
示例#2
0
 /**
  * Test for Request_Client_External::options() to ensure options
  * can be set to the external client (for cURL and PECL_HTTP)
  *
  * @dataProvider provider_options_set_to_external_client
  * 
  * @param   array    settings 
  * @param   array    expected 
  * @return void
  */
 public function test_options_set_to_external_client($settings, $expected)
 {
     $request_client = Request_Client_External::factory(array(), 'Request_Client_Curl');
     // Test for empty array
     $this->assertSame(array(), $request_client->options());
     // Test that set works as expected
     $this->assertSame($request_client->options($settings), $request_client);
     // Test that each setting is present and returned
     foreach ($expected as $key => $value) {
         $this->assertSame($request_client->options($key), $value);
     }
 }
 /**
  * Creates a new request object for the given URI. New requests should be
  * Created using the [Request::factory] method.
  *
  *     $request = new Request($uri);
  *
  * If $cache parameter is set, the response for the request will attempt to
  * be retrieved from the cache.
  *
  * @param   string  $uri              URI of the request
  * @param   array   $client_params    Array of params to pass to the request client
  * @param   bool    $allow_external   Allow external requests? (deprecated in 3.3)
  * @param   array   $injected_routes  An array of routes to use, for testing
  * @return  void
  * @throws  Request_Exception
  * @uses    Route::all
  * @uses    Route::matches
  */
 public function __construct($uri, $client_params = array(), $allow_external = TRUE, $injected_routes = array())
 {
     $client_params = is_array($client_params) ? $client_params : array();
     // Initialise the header
     $this->_header = new HTTP_Header(array());
     // Assign injected routes
     $this->_routes = $injected_routes;
     // Cleanse query parameters from URI (faster that parse_url())
     $split_uri = explode('?', $uri);
     $uri = array_shift($split_uri);
     // Initial request has global $_GET already applied
     if (Request::$initial !== NULL) {
         if ($split_uri) {
             parse_str($split_uri[0], $this->_get);
         }
     }
     // Detect protocol (if present)
     // $allow_external = FALSE prevents the default index.php from
     // being able to proxy external pages.
     if (!$allow_external or strpos($uri, '://') === FALSE) {
         // Remove trailing slashes from the URI
         $this->_uri = trim($uri, '/');
         // Apply the client
         $this->_client = new Request_Client_Internal($client_params);
     } else {
         // Create a route
         $this->_route = new Route($uri);
         // Store the URI
         $this->_uri = $uri;
         // Set the security setting if required
         if (strpos($uri, 'https://') === 0) {
             $this->secure(TRUE);
         }
         // Set external state
         $this->_external = TRUE;
         // Setup the client
         $this->_client = Request_Client_External::factory($client_params);
     }
 }
 /**
  * Tests the [Request_Client_External::factory()] method
  * 
  * @dataProvider provider_factory
  *
  * @param   array   $params  params 
  * @param   string  $client  client 
  * @param   Request_Client_External $expected expected 
  * @return  void
  */
 public function test_factory($params, $client, $expected)
 {
     $this->assertInstanceOf($expected, Request_Client_External::factory($params, $client));
 }
示例#5
0
 /**
  * Creates a new request object for the given URI. New requests should be
  * created using the [Request::instance] or [Request::factory] methods.
  *
  *     $request = new Request($uri);
  *
  * If $cache parameter is set, the response for the request will attempt to
  * be retrieved from the cache.
  *
  * @param   string  $uri URI of the request
  * @param   HTTP_Cache   $cache
  * @param   array   $injected_routes an array of routes to use, for testing
  * @return  void
  * @throws  Request_Exception
  * @uses    Route::all
  * @uses    Route::matches
  */
 public function __construct($uri, HTTP_Cache $cache = NULL, $injected_routes = array())
 {
     // Initialise the header
     $this->_header = new HTTP_Header(array());
     // Assign injected routes
     $this->_injected_routes = $injected_routes;
     // Cleanse query parameters from URI (faster that parse_url())
     $split_uri = explode('?', $uri);
     $uri = array_shift($split_uri);
     // Initial request has global $_GET already applied
     if (Request::$initial !== NULL) {
         if ($split_uri) {
             parse_str($split_uri[0], $this->_get);
         }
     }
     // Detect protocol (if present)
     // Always default to an internal request if we don't have an initial.
     // This prevents the default index.php from being able to proxy
     // external pages.
     if (Request::$initial === NULL or strpos($uri, '://') === FALSE) {
         // Remove trailing slashes from the URI
         $uri = trim($uri, '/');
         $processed_uri = Request::process_uri($uri, $this->_injected_routes);
         // Return here rather than throw exception. This will allow
         // use of Request object even with unmatched route
         if ($processed_uri === NULL) {
             $this->_uri = $uri;
             return;
         }
         // Store the URI
         $this->_uri = $uri;
         // Store the matching route
         $this->_route = $processed_uri['route'];
         $params = $processed_uri['params'];
         // Is this route external?
         $this->_external = $this->_route->is_external();
         if (isset($params['directory'])) {
             // Controllers are in a sub-directory
             $this->_directory = $params['directory'];
         }
         // Store the controller
         $this->_controller = $params['controller'];
         if (isset($params['action'])) {
             // Store the action
             $this->_action = $params['action'];
         } else {
             // Use the default action
             $this->_action = Route::$default_action;
         }
         // These are accessible as public vars and can be overloaded
         unset($params['controller'], $params['action'], $params['directory']);
         // Params cannot be changed once matched
         $this->_params = $params;
         // Apply the client
         $this->_client = new Request_Client_Internal(array('cache' => $cache));
     } else {
         // Create a route
         $this->_route = new Route($uri);
         // Store the URI
         $this->_uri = $uri;
         // Set the security setting if required
         if (strpos($uri, 'https://') === 0) {
             $this->secure(TRUE);
         }
         // Set external state
         $this->_external = TRUE;
         // Setup the client
         $this->_client = Request_Client_External::factory(array('cache' => $cache));
     }
 }
示例#6
0
 /**
  * Execute the request using PHP stream. (not recommended)
  *
  * @param   Request   $request  Request to execute
  * @return  Response
  */
 protected function _native_execute(Request $request)
 {
     // Reset the headers
     Request_Client_External::$_processed_headers = array();
     // Calculate stream mode
     $mode = $request->method() === HTTP_Request::GET ? 'r' : 'r+';
     // Process cookies
     if ($cookies = $request->cookie()) {
         $request->headers('cookie', http_build_query($cookies, NULL, '; '));
     }
     // Create the context
     $options = array($request->protocol() => array('method' => $request->method(), 'header' => (string) $request->headers(), 'content' => $request->body(), 'user-agent' => 'Kohana Framework ' . Kohana::VERSION . ' (' . Kohana::CODENAME . ')'));
     // Create the context stream
     $context = stream_context_create($options);
     stream_context_set_option($context, $this->_options);
     $stream = fopen($request->uri(), $mode, FALSE, $context);
     $meta_data = stream_get_meta_data($stream);
     // Get the HTTP response code
     $http_response = array_shift($meta_data['wrapper_data']);
     if (preg_match_all('/(\\w+\\/\\d\\.\\d) (\\d{3})/', $http_response, $matches) !== FALSE) {
         $protocol = $matches[1][0];
         $status = (int) $matches[2][0];
     } else {
         $protocol = NULL;
         $status = NULL;
     }
     // Process headers
     array_map(array('Request_Client_External', '_parse_headers'), array(), $meta_data['wrapper_data']);
     // Create a response
     $response = $request->create_response();
     $response->status($status)->protocol($protocol)->headers(Request_Client_External::$_processed_headers)->body(stream_get_contents($stream));
     // Close the stream after use
     fclose($stream);
     return $response;
 }