public static function instance(&$uri = TRUE) { if (!Request::$instance) { if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { Request::$protocol = 'https'; } if (isset($_SERVER['REQUEST_METHOD'])) { Request::$method = $_SERVER['REQUEST_METHOD']; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { Request::$is_ajax = TRUE; } Request::$client_ip = self::get_client_ip(); if (Request::$method !== 'GET' and Request::$method !== 'POST') { parse_str(file_get_contents('php://input'), $_POST); } if (isset($_SERVER['HTTP_USER_AGENT'])) { Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_REFERER'])) { Request::$referrer = $_SERVER['HTTP_REFERER']; } if ($uri === TRUE) { $uri = Request::detect_uri(); } $uri = preg_replace('#//+#', '/', $uri); $uri = preg_replace('#\\.[\\s./]*/#', '', $uri); Request::$instance = new Request($uri); } return Request::$instance; }
public function execute(Request $request) { if ($this->callback_depth() > $this->max_callback_depth()) { throw new Request_Client_Recursion_Exception("Could not execute request to :uri - too many recursions after :depth requests", array(":uri" => $request->uri(), ":depth" => $this->callback_depth() - 1)); } $orig_response = $response = Response::factory(array("_protocol" => $request->protocol())); if (($cache = $this->cache()) instanceof HTTP_Cache) { return $cache->execute($this, $request, $response); } $response = $this->execute_request($request, $response); foreach ($this->header_callbacks() as $header => $callback) { if ($response->headers($header)) { $cb_result = call_user_func($callback, $request, $response, $this); if ($cb_result instanceof Request) { $this->assign_client_properties($cb_result->client()); $cb_result->client()->callback_depth($this->callback_depth() + 1); $response = $cb_result->execute(); } elseif ($cb_result instanceof Response) { $response = $cb_result; } if ($response !== $orig_response) { break; } } } return $response; }
/** * Main request singleton instance. If no URI is provided, the URI will * be automatically detected using PATH_INFO, REQUEST_URI, or PHP_SELF. * * $request = Request::instance(); * * @param string URI of the request * @return Request */ public static function instance($uri = TRUE) { if (!Request::$instance) { if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method Request::$method = $_SERVER['REQUEST_METHOD']; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure Request::$protocol = 'https'; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { // This request is an AJAX request Request::$is_ajax = TRUE; } if (isset($_SERVER['HTTP_REFERER'])) { // There is a referrer for this request Request::$referrer = $_SERVER['HTTP_REFERER']; } if (isset($_SERVER['HTTP_USER_AGENT'])) { // Set the client user agent Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { // The remote IP address Request::$client_ip = $_SERVER['REMOTE_ADDR']; } if (Request::$method !== 'GET' and Request::$method !== 'POST') { // Methods besides GET and POST do not properly parse the form-encoded // query string into the $_POST array, so we overload it manually. parse_str(file_get_contents('php://input'), $_POST); } if ($uri === TRUE) { // If the route wasn't passed, just generate the route from $_POST or $_GET values $uri = array_merge(JRequest::get('get'), JRequest::get('post')); } // Set the default request client $client = JFactory::getApplication()->isAdmin() ? 'admin' : 'site'; // Create the instance singleton Request::$instance = Request::$current = new Request($uri, $client); } return Request::$instance; }
/** * Sends the HTTP message [Request] to a remote server and processes * the response. * * @param Request request to send * @return Response * @uses [PHP cURL](http://php.net/manual/en/book.curl.php) */ public function _send_message(Request $request) { // 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, '; ')); } // Get the message body $body = $request->body(); if (is_resource($body)) { $body = stream_get_contents($body); } // Set the content length $request->headers('content-length', (string) strlen($body)); list($protocol) = explode('/', $request->protocol()); // Create the context $options = array(strtolower($protocol) => array('method' => $request->method(), 'header' => (string) $request->headers(), 'content' => $body)); // Create the context stream $context = stream_context_create($options); stream_context_set_option($context, $this->_options); $uri = $request->uri(); if ($query = $request->query()) { $uri .= '?' . http_build_query($query, NULL, '&'); } $stream = fopen($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; } // Create a response $response = $request->create_response(); $response_header = $response->headers(); // Process headers array_map(array($response_header, 'parse_header_string'), array(), $meta_data['wrapper_data']); $response->status($status)->protocol($protocol)->body(stream_get_contents($stream)); // Close the stream after use fclose($stream); return $response; }
function setOptions(array $options) { if (isset($options['windowed_urls'])) { HTML::$windowed_urls = $options['windowed_urls']; } if (isset($options['charset'])) { Kohana::$charset = $options['charset']; } if (isset($options['protocol'])) { Request::$protocol = $options['protocol']; } if (isset($options['index_file'])) { Kohana::$index_file = $options['index_file']; } if (isset($options['HTTP_HOST'])) { $_SERVER['HTTP_HOST'] = $options['HTTP_HOST']; } }
/** * Changes certain aspects of the enviroment * * @param array $vars * @return boolean */ function setEnviroment(array $vars) { if (empty($vars)) { return FALSE; } if (isset($vars['base_url'])) { Kohana::$base_url = $vars['base_url']; } if (isset($vars['protocol'])) { Request::$protocol = $vars['protocol']; } if (isset($vars['index_file'])) { Kohana::$index_file = $vars['index_file']; } if (isset($vars['HTTP_HOST'])) { $_SERVER['HTTP_HOST'] = $vars['HTTP_HOST']; } if (isset($vars['_GET'])) { $_GET = $vars['_GET']; } return TRUE; }
/** * Creates a new request object for the given URI. New requests should be * created using the [Request::instance] or [Request::factory] methods. * * $request = Request::factory($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 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 static function factory($uri = TRUE, HTTP_Cache $cache = NULL, $injected_routes = array()) { // If this is the initial request if (!Request::$initial) { if (Kohana::$is_cli) { // Default protocol for command line is cli:// $protocol = 'cli'; // Get the command line options $options = CLI::options('uri', 'method', 'get', 'post', 'referrer'); if (isset($options['uri'])) { // Use the specified URI $uri = $options['uri']; } elseif ($uri === TRUE) { $uri = ''; } if (isset($options['method'])) { // Use the specified method $method = strtoupper($options['method']); } else { // Default to GET requests $method = HTTP_Request::GET; } if (isset($options['get'])) { // Overload the global GET data parse_str($options['get'], $_GET); } if (isset($options['post'])) { // Overload the global POST data parse_str($options['post'], $_POST); } if (isset($options['referrer'])) { $referrer = $options['referrer']; } } else { if (isset($_SERVER['SERVER_PROTOCOL'])) { $protocol = $_SERVER['SERVER_PROTOCOL']; } else { $protocol = HTTP::$protocol; } if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method $method = $_SERVER['REQUEST_METHOD']; } else { // Default to GET requests $method = HTTP_Request::GET; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure $secure = TRUE; } if (isset($_SERVER['HTTP_REFERER'])) { // There is a referrer for this request $referrer = $_SERVER['HTTP_REFERER']; } if (isset($_SERVER['HTTP_USER_AGENT'])) { // Browser type Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { // Typically used to denote AJAX requests $requested_with = $_SERVER['HTTP_X_REQUESTED_WITH']; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) { // Use the forwarded IP address, typically set when the // client is using a proxy server. // Format: "X-Forwarded-For: client1, proxy1, proxy2" $client_ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); Request::$client_ip = array_shift($client_ips); unset($client_ips); } elseif (isset($_SERVER['HTTP_CLIENT_IP']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) { // Use the forwarded IP address, typically set when the // client is using a proxy server. $client_ips = explode(',', $_SERVER['HTTP_CLIENT_IP']); Request::$client_ip = array_shift($client_ips); unset($client_ips); } elseif (isset($_SERVER['REMOTE_ADDR'])) { // The remote IP address Request::$client_ip = $_SERVER['REMOTE_ADDR']; } if ($method !== HTTP_Request::GET) { // Ensure the raw body is saved for future use $body = file_get_contents('php://input'); } if ($uri === TRUE) { // Attempt to guess the proper URI $uri = Request::detect_uri(); } } // Create the instance singleton Request::$initial = $request = new Request($uri, $cache); // Store global GET and POST data in the initial request only $request->protocol($protocol)->query($_GET)->post($_POST); if (isset($secure)) { // Set the request security $request->secure($secure); } if (isset($method)) { // Set the request method $request->method($method); } if (isset($referrer)) { // Set the referrer $request->referrer($referrer); } if (isset($requested_with)) { // Apply the requested with variable $request->requested_with($requested_with); } if (isset($body)) { // Set the request body (probably a PUT type) $request->body($body); } } else { $request = new Request($uri, $cache, $injected_routes); } return $request; }
/** * Main request singleton instance. If no URI is provided, the URI will * be automatically detected using PATH_INFO, REQUEST_URI, or PHP_SELF. * * $request = Request::instance(); * * @param string URI of the request * @return Request */ public static function instance(&$uri = TRUE) { if (!Request::$instance) { if (Kohana::$is_cli) { // Default protocol for command line is cli:// Request::$protocol = 'cli'; // Get the command line options $options = CLI::options('uri', 'method', 'get', 'post'); if (isset($options['uri'])) { // Use the specified URI $uri = $options['uri']; } if (isset($options['method'])) { // Use the specified method Request::$method = strtoupper($options['method']); } if (isset($options['get'])) { // Overload the global GET data parse_str($options['get'], $_GET); } if (isset($options['post'])) { // Overload the global POST data parse_str($options['post'], $_POST); } } else { if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method Request::$method = $_SERVER['REQUEST_METHOD']; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure Request::$protocol = 'https'; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { // This request is an AJAX request Request::$is_ajax = TRUE; } if (isset($_SERVER['HTTP_REFERER'])) { // There is a referrer for this request Request::$referrer = $_SERVER['HTTP_REFERER']; } if (isset($_SERVER['HTTP_USER_AGENT'])) { // Set the client user agent Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { // The remote IP address Request::$client_ip = $_SERVER['REMOTE_ADDR']; } if (Request::$method !== 'GET' and Request::$method !== 'POST') { // Methods besides GET and POST do not properly parse the form-encoded // query string into the $_POST array, so we overload it manually. parse_str(file_get_contents('php://input'), $_POST); } if ($uri === TRUE) { if (!empty($_SERVER['PATH_INFO'])) { // PATH_INFO does not contain the docroot or index $uri = $_SERVER['PATH_INFO']; } else { // REQUEST_URI and PHP_SELF include the docroot and index if (isset($_SERVER['REQUEST_URI'])) { // REQUEST_URI includes the query string, remove it $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); } elseif (isset($_SERVER['PHP_SELF'])) { $uri = $_SERVER['PHP_SELF']; } elseif (isset($_SERVER['REDIRECT_URL'])) { $uri = $_SERVER['REDIRECT_URL']; } else { // If you ever see this error, please report an issue at and include a dump of $_SERVER // http://dev.kohanaphp.com/projects/kohana3/issues throw new Kohana_Exception('Unable to detect the URI using PATH_INFO, REQUEST_URI, or PHP_SELF'); } // Get the path from the base URL, including the index file $base_url = parse_url(Kohana::$base_url, PHP_URL_PATH); if (strpos($uri, $base_url) === 0) { // Remove the base URL from the URI $uri = substr($uri, strlen($base_url)); } if (Kohana::$index_file and strpos($uri, Kohana::$index_file) === 0) { // Remove the index file from the URI $uri = substr($uri, strlen(Kohana::$index_file)); } } } } // Reduce multiple slashes to a single slash $uri = preg_replace('#//+#', '/', $uri); // Remove all dot-paths from the URI, they are not valid $uri = preg_replace('#\\.[\\s./]*/#', '', $uri); // Create the instance singleton Request::$instance = Request::$current = new Request($uri); // Add the default Content-Type header Request::$instance->headers['Content-Type'] = 'text/html; charset=' . Kohana::$charset; } return Request::$instance; }
/** * 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; }
/** * Send all of the response headers to the browser. * * The developer may set response headers using the "header" method. All of * the headers set by the developer will be automatically sent to the browser * when the response is sent via the "send" method. There is no need to call * this method before calling the "send" method. * * The protocol and status header will be set automatically, as well as the * content-type and charset, unless those headers have been set explicitly. * The content-type charset used will be the application encoding. * * @return void */ public function send_headers() { if (!isset($this->headers['Content-Type'])) { $encoding = Config::$items['application']['encoding']; $this->header('Content-Type', "text/html; charset={$encoding}"); } header(Request::protocol() . ' ' . $this->status . ' ' . static::$statuses[$this->status]); foreach ($this->headers as $name => $value) { header($name . ': ' . $value, true); } }
public static function factory($uri = TRUE, Cache $cache = NULL) { // If this is the initial request if (!Request::$initial) { if (Kohana::$is_cli) { // Default protocol for command line is cli:// $protocol = 'cli'; // Get the command line options $options = CLI::options('uri', 'method', 'get', 'post', 'referrer'); if (isset($options['uri'])) { // Use the specified URI $uri = $options['uri']; } if (isset($options['method'])) { // Use the specified method $method = strtoupper($options['method']); } else { $method = 'GET'; } if (isset($options['get'])) { // Overload the global GET data parse_str($options['get'], $_GET); } if (isset($options['post'])) { // Overload the global POST data parse_str($options['post'], $_POST); } if (isset($options['referrer'])) { $referrer = $options['referrer']; } else { $referrer = NULL; } } else { if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method $method = $_SERVER['REQUEST_METHOD']; } else { // Default to GET $method = Http_Request::GET; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure $protocol = 'https'; } else { $protocol = 'http'; } if (isset($_SERVER['HTTP_REFERER'])) { // There is a referrer for this request $referrer = $_SERVER['HTTP_REFERER']; } else { $referrer = NULL; } if (isset($_SERVER['HTTP_USER_AGENT'])) { // Set the client user agent Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { $requested_with = $_SERVER['HTTP_X_REQUESTED_WITH']; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { // The remote IP address Request::$client_ip = $_SERVER['REMOTE_ADDR']; } if ($method !== 'GET') { // Ensure the raw body is saved for future use $body = file_get_contents('php://input'); } if ($uri === TRUE) { $uri = Request::detect_uri(); } } // Create the instance singleton $request = new Request($uri, $cache); $request->protocol($protocol)->method($method)->referrer($referrer); // Apply the requested with variable isset($requested_with) and $request->requested_with($requested_with); // If there is a body, set it to the model isset($body) and $request->body($body); } else { $request = new Request($uri, $cache); } // Create the initial request if it does not exist if (!Request::$initial) { Request::$initial = $request; $request->query($_GET)->post($_POST); } return $request; }
/** * @param string $uri * @param array $client_params * @param bool $allow_external * @param array $injected_routes * @internal param \HTTP_Cache $cache * @return \HAPI_Request|\Request|void */ public static function factory($uri = '', $client_params = array(), $allow_external = TRUE, $injected_routes = array()) { // If this is the initial request if (!Request::$initial) { if (isset($_SERVER['SERVER_PROTOCOL'])) { $protocol = $_SERVER['SERVER_PROTOCOL']; } else { $protocol = HTTP::$protocol; } if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method $method = $_SERVER['REQUEST_METHOD']; } else { // Default to GET requests $method = HTTP_Request::GET; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure $secure = TRUE; } if (isset($_SERVER['HTTP_REFERER'])) { // There is a referrer for this request $referrer = $_SERVER['HTTP_REFERER']; } if (isset($_SERVER['HTTP_USER_AGENT'])) { // Browser type Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { // Typically used to denote AJAX requests $requested_with = $_SERVER['HTTP_X_REQUESTED_WITH']; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) { // Use the forwarded IP address, typically set when the // client is using a proxy server. // Format: "X-Forwarded-For: client1, proxy1, proxy2" $client_ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); Request::$client_ip = array_shift($client_ips); unset($client_ips); } elseif (isset($_SERVER['HTTP_CLIENT_IP']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) { // Use the forwarded IP address, typically set when the // client is using a proxy server. $client_ips = explode(',', $_SERVER['HTTP_CLIENT_IP']); Request::$client_ip = array_shift($client_ips); unset($client_ips); } elseif (isset($_SERVER['REMOTE_ADDR'])) { // The remote IP address Request::$client_ip = $_SERVER['REMOTE_ADDR']; } if ($method !== HTTP_Request::GET) { // Ensure the raw body is saved for future use $body = file_get_contents('php://input'); } $cookies = array(); if ($cookie_keys = array_keys($_COOKIE)) { foreach ($cookie_keys as $key) { $cookies[$key] = Cookie::get($key); } } // Create the instance singleton Request::$initial = $request = new Request($uri, $client_params, $allow_external, $injected_routes); // Store global GET and POST data in the initial request only $request->protocol($protocol)->query($_GET)->post($_POST); if (isset($secure)) { // Set the request security $request->secure($secure); } if (isset($method)) { // Set the request method $request->method($method); } if (isset($referrer)) { // Set the referrer $request->referrer($referrer); } if (isset($requested_with)) { // Apply the requested with variable $request->requested_with($requested_with); } if (isset($body)) { // Set the request body (probably a PUT type) $request->body($body); } if (isset($cookies)) { $request->cookie($cookies); } } else { $request = new HAPI_Request($uri, $client_params, $allow_external, $injected_routes); } return $request; }
/** * Установка заголовка 503 - Сервис недоступен */ public static function h503() { $header_503 = Request::protocol() . " 503 Service Temporarily Unavailable"; @header($header_503); @header('Retry-After: 3600'); }
/** * Main request singleton instance. If no URI is provided, the URI will * be automatically detected using PATH_INFO, REQUEST_URI, or PHP_SELF. * * @param string URI of the request * @return Request */ public static function instance(&$uri = FALSE) { if (Request::$instance === NULL) { if (Kohana::$is_cli) { // Default protocol for command line is cli:// Request::$protocol = 'cli'; // Get the command line options $options = cli::options('uri', 'method', 'get', 'post'); if (isset($options['uri'])) { // Use the specified URI $uri = $options['uri']; } if (isset($options['method'])) { // Use the specified method Request::$method = strtoupper($options['method']); } if (isset($options['get'])) { // Overload the global GET data parse_str($options['get'], $_GET); } if (isset($options['post'])) { // Overload the global POST data parse_str($options['post'], $_POST); } } else { if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method Request::$method = $_SERVER['REQUEST_METHOD']; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure Request::$protocol = 'https'; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { // This request is an AJAX request Request::$is_ajax = TRUE; } if (isset($_SERVER['HTTP_REFERER'])) { // There is a referrer for this request Request::$referrer = $_SERVER['HTTP_REFERER']; } if (Request::$method !== 'GET' and Request::$method !== 'POST') { // Methods besides GET and POST do not properly parse the form-encoded // query string into the $_POST array, so we overload it manually. parse_str(file_get_contents('php://input'), $_POST); } if ($uri === FALSE) { if (isset($_SERVER['PATH_INFO'])) { // PATH_INFO is most realiable way to handle routing, as it // does not include the document root or index file $uri = $_SERVER['PATH_INFO']; } else { // REQUEST_URI and PHP_SELF both provide the full path, // including the document root and index file if (isset($_SERVER['REQUEST_URI'])) { $uri = $_SERVER['REQUEST_URI']; } elseif (isset($_SERVER['PHP_SELF'])) { $uri = $_SERVER['PHP_SELF']; } if (isset($_SERVER['SCRIPT_NAME']) and strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) { // Remove the document root and index file from the URI $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); } } } } // Create the instance singleton Request::$instance = new Request($uri); } return Request::$instance; }
/** * Send all of the response headers to the browser. * * @return void */ public function send_headers() { // If the server is using FastCGI, we need to send a slightly different // protocol and status header than we normally would. Otherwise it will // not call any custom scripts setup to handle 404 responses. // // The status header will contain both the code and the status message, // such as "OK" or "Not Found". For typical servers, the HTTP protocol // will also be included with the status. if (isset($_SERVER['FCGI_SERVER_VERSION'])) { header('Status: ' . $this->status . ' ' . $this->message()); } else { header(Request::protocol() . ' ' . $this->status . ' ' . $this->message()); } // If the content type was not set by the developer, we will set the // header to a default value that indicates to the browser that the // response is HTML and that it uses the default encoding. if (!isset($this->headers['content-type'])) { $encoding = Config::get('application.encoding'); $this->header('content-type', 'text/html; charset=' . $encoding); } // Once the framework controlled headers have been sentm, we can // simply iterate over the developer's headers and send each one // back to the browser for the response. foreach ($this->headers as $name => $value) { header("{$name}: {$value}", true); } }
public static function factory($uri = TRUE, $client_params = array(), $allow_external = TRUE, $injected_routes = array()) { if (!Request::$initial) { $protocol = HTTP::$protocol; if (isset($_SERVER['REQUEST_METHOD'])) { $method = $_SERVER['REQUEST_METHOD']; } else { $method = HTTP_Request::GET; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN) or isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) { $secure = TRUE; } if (isset($_SERVER['HTTP_REFERER'])) { $referrer = $_SERVER['HTTP_REFERER']; } if (isset($_SERVER['HTTP_USER_AGENT'])) { Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { $requested_with = $_SERVER['HTTP_X_REQUESTED_WITH']; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) { $client_ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); Request::$client_ip = array_shift($client_ips); unset($client_ips); } elseif (isset($_SERVER['HTTP_CLIENT_IP']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) { $client_ips = explode(',', $_SERVER['HTTP_CLIENT_IP']); Request::$client_ip = array_shift($client_ips); unset($client_ips); } elseif (isset($_SERVER['REMOTE_ADDR'])) { // The remote IP address Request::$client_ip = $_SERVER['REMOTE_ADDR']; } if ($method !== HTTP_Request::GET) { // Ensure the raw body is saved for future use $body = file_get_contents('php://input'); } if ($uri === TRUE) { // Attempt to guess the proper URI $uri = Request::detect_uri(); } $cookies = array(); if ($cookie_keys = array_keys($_COOKIE)) { foreach ($cookie_keys as $key) { $cookies[$key] = Cookie::get($key); } } // Create the instance singleton Request::$initial = $request = new Request($uri, $client_params, $allow_external, $injected_routes); // Store global GET and POST data in the initial request only $request->protocol($protocol)->query($_GET)->post($_POST); if (isset($secure)) { // Set the request security $request->secure($secure); } if (isset($method)) { // Set the request method $request->method($method); } if (isset($referrer)) { // Set the referrer $request->referrer($referrer); } if (isset($requested_with)) { // Apply the requested with variable $request->requested_with($requested_with); } if (isset($body)) { // Set the request body (probably a PUT type) $request->body($body); } if (isset($cookies)) { $request->cookie($cookies); } } else { $request = new Request($uri, $client_params, $allow_external, $injected_routes); } return $request; }
/** * Main request singleton instance. If no URI is provided, the URI will * be automatically detected. * * $request = Request::instance(); * * @param string URI of the request * @return Request * @uses Request::detect_uri */ public static function instance( & $uri = TRUE) { if ( ! Request::$instance) { if (Kohana::$is_cli) { // Default protocol for command line is cli:// Request::$protocol = 'cli'; // Get the command line options $options = CLI::options('uri', 'method', 'get', 'post'); if (isset($options['uri'])) { // Use the specified URI $uri = $options['uri']; } if (isset($options['method'])) { // Use the specified method Request::$method = strtoupper($options['method']); } if (isset($options['get'])) { // Overload the global GET data parse_str($options['get'], $_GET); } if (isset($options['post'])) { // Overload the global POST data parse_str($options['post'], $_POST); } } else { if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method Request::$method = $_SERVER['REQUEST_METHOD']; } if ( ! empty($_SERVER['HTTPS']) AND filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure Request::$protocol = 'https'; } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { // This request is an AJAX request Request::$is_ajax = TRUE; } if (isset($_SERVER['HTTP_REFERER'])) { // There is a referrer for this request Request::$referrer = $_SERVER['HTTP_REFERER']; } if (isset($_SERVER['HTTP_USER_AGENT'])) { // Set the client user agent Request::$user_agent = $_SERVER['HTTP_USER_AGENT']; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { // Use the forwarded IP address, typically set when the // client is using a proxy server. Request::$client_ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { // The remote IP address Request::$client_ip = $_SERVER['REMOTE_ADDR']; } if (Request::$method !== 'GET' AND Request::$method !== 'POST') { // Methods besides GET and POST do not properly parse the form-encoded // query string into the $_POST array, so we overload it manually. parse_str(file_get_contents('php://input'), $_POST); } if ($uri === TRUE) { $uri = Request::detect_uri(); } } // Reduce multiple slashes to a single slash $uri = preg_replace('#//+#', '/', $uri); // Remove all dot-paths from the URI, they are not valid $uri = preg_replace('#\.[\s./]*/#', '', $uri); // Create the instance singleton Request::$instance = Request::$current = new Request($uri); // Add the default Content-Type header Request::$instance->headers['Content-Type'] = 'text/html; charset='.Kohana::$charset; } return Request::$instance; }
/** * Processes the request, executing the controller action that handles this * request, determined by the [Route]. * * 1. Before the controller action is called, the [Controller::before] method * will be called. * 2. Next the controller action will be called. * 3. After the controller action is called, the [Controller::after] method * will be called. * * By default, the output from the controller is captured and returned, and * no headers are sent. * * $request->execute(); * * @param Request $request * @param Response $response * @return Response * @throws Kohana_Exception * @uses [Kohana::$profiling] * @uses [Profiler] */ public function execute(Request $request) { // Prevent too much recursion of header callback requests if ($this->callback_depth() > $this->max_callback_depth()) { throw new Request_Client_Recursion_Exception("Could not execute request to :uri - too many recursions after :depth requests", array(':uri' => $request->uri(), ':depth' => $this->callback_depth() - 1)); } // Execute the request and pass the currently used protocol $orig_response = $response = Response::factory(array('_protocol' => $request->protocol())); if (($cache = $this->cache()) instanceof HTTP_Cache) { return $cache->execute($this, $request, $response); } $response = $this->execute_request($request, $response); // Execute response callbacks foreach ($this->header_callbacks() as $header => $callback) { if ($response->headers($header)) { $cb_result = call_user_func($callback, $request, $response, $this); if ($cb_result instanceof Request) { // If the callback returns a request, automatically assign client params $this->assign_client_properties($cb_result->client()); $cb_result->client()->callback_depth($this->callback_depth() + 1); // Execute the request $response = $cb_result->execute(); } elseif ($cb_result instanceof Response) { // Assign the returned response $response = $cb_result; } // If the callback has created a new response, do not process any further if ($response !== $orig_response) { break; } } } return $response; }
/** * Main request singleton instance. If no URI is provided, the URI will * be automatically detected using PATH_INFO, REQUEST_URI, or PHP_SELF. * * @param string URI of the request * @return Request */ public static function instance($uri = TRUE) { static $instance; if ($instance === NULL) { if (Ko::$is_cli) { // Default protocol for command line is cli:// self::$protocol = 'cli'; // Get the command line options $options = CLI::options('uri', 'method', 'get', 'post'); if (isset($options['uri'])) { // Use the specified URI $uri = $options['uri']; } if (isset($options['method'])) { // Use the specified method self::$method = strtoupper($options['method']); } if (isset($options['get'])) { // Overload the global GET data parse_str($options['get'], $_GET); } if (isset($options['post'])) { // Overload the global POST data parse_str($options['post'], $_POST); } } else { if (isset($_SERVER['REQUEST_METHOD'])) { // Use the server request method self::$method = $_SERVER['REQUEST_METHOD']; } if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { // This request is secure self::$protocol = 'https'; } if (self::$method !== 'GET' && self::$method !== 'POST') { // Methods besides GET and POST do not properly parse the form-encoded // query string into the $_POST array, so we overload it manually. parse_str(file_get_contents('php://input'), $_POST); } if ($uri === TRUE) { if (isset($_SERVER['PATH_INFO'])) { $uri = $_SERVER['PATH_INFO']; } else { if (isset($_SERVER['REQUEST_URI'])) { $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); } elseif (isset($_SERVER['PHP_SELF'])) { $uri = $_SERVER['PHP_SELF']; } else { throw new KoException('Unable to detect the URI using PATH_INFO, REQUEST_URI, or PHP_SELF'); } // Get the path from the base URL, including the index file $base_url = parse_url(Ko::$base_url, PHP_URL_PATH); if (strpos($uri, $base_url) === 0) { $uri = substr($uri, strlen($base_url)); } if (Ko::$index_file && strpos($uri, Ko::$index_file) === 0) { $uri = substr($uri, strlen(Ko::$index_file)); } } } } // Reduce multiple slashes to a single slash $uri = preg_replace('#//+#', '/', $uri); // Remove all dot-paths from the URI, they are not valid $uri = preg_replace('#\\.[\\s./]*/#', '', $uri); // Create the instance singleton $instance = new self($uri); // Add the Content-Type header $instance->headers['Content-Type'] = 'text/html; charset=' . Ko::$charset; } return $instance; }