See also: lithium\action\Dispatcher
See also: lithium\action\Controller
See also: lithium\net\http\Router
See also: lithium\net\http\Route
See also: lithium\action\Request::__get()
Inheritance: extends lithium\net\http\Request
 protected function _init()
 {
     $safari = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.48.3 ';
     $safari .= '(KHTML, like Gecko) Version/5.1 Safari/534.48.3';
     parent::_init();
     $this->_env = array('FCGI_ROLE' => 'RESPONDER', 'PATH_INFO' => '', 'PATH_TRANSLATED' => '/lithium/app/webroot/index.php', 'QUERY_STRING' => '', 'REQUEST_METHOD' => 'GET', 'CONTENT_TYPE' => '', 'CONTENT_LENGTH' => '', 'SCRIPT_NAME' => '/index.php', 'SCRIPT_FILENAME' => '/lithium/app/webroot/index.php', 'REQUEST_URI' => '/', 'DOCUMENT_URI' => '/index.php', 'DOCUMENT_ROOT' => '/lithium/app/webroot', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'REMOTE_ADDR' => '127.0.0.1', 'REMOTE_PORT' => '52987', 'SERVER_ADDR' => '127.0.0.1', 'SERVER_PORT' => '80', 'SERVER_NAME' => 'sandbox.local', 'HTTP_HOST' => 'sandbox.local', 'HTTP_USER_AGENT' => $safari, 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_LANGUAGE' => 'en-us', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', 'HTTP_CONNECTION' => 'keep-alive', 'PHP_SELF' => '/index.php');
 }
Example #2
0
 /**
  * Tests that `action\Request` correctly inherits the functionality of the `to()` method
  * inherited from `lithium\net\http\Request`.
  */
 public function testConvertToUrl()
 {
     $request = new Request(array('env' => array('HTTP_HOST' => 'foo.com', 'HTTPS' => 'on'), 'base' => '/the/base/path', 'url' => '/the/url', 'query' => array('some' => 'query', 'parameter' => 'values')));
     $expected = 'https://foo.com/the/base/path/the/url?some=query&parameter=values';
     $this->assertEqual($expected, $request->to('url'));
 }
 protected function _init()
 {
     parent::_init();
     $this->_env = array('PLATFORM' => 'CGI', 'SCRIPT_FILENAME' => false, 'DOCUMENT_ROOT' => false, 'SCRIPT_URL' => '/lithium/app/webroot/index.php');
 }
 public function testRequestTypeFromHeader()
 {
     $request = new Request(array('env' => array('Content-type' => 'json')));
     $expected = 'json';
     $result = $request->type();
     $this->assertEqual($expected, $result);
 }
Example #5
0
 /**
  * Initialize options for `Router::match()`.
  *
  * @param string|array $url Options to match to a URL. Optionally, this can be a string
  *        containing a manually generated URL.
  * @param \lithium\action\Request $context
  * @param array $options Options for the generation of the matched URL.
  * @return array The initialized options.
  */
 protected static function _matchOptions(&$url, $context, $options)
 {
     $defaults = array('scheme' => null, 'host' => null, 'absolute' => false, 'base' => '');
     if ($context) {
         $defaults = array('base' => $context->env('base'), 'host' => $context->host, 'scheme' => $context->scheme . ($context->scheme ? '://' : '//')) + $defaults;
     }
     $options += array('scope' => static::scope());
     $vars = array();
     $scope = $options['scope'];
     if (is_array($scope)) {
         list($tmp, $vars) = each($scope);
         if (!is_array($vars)) {
             $vars = $scope;
             $scope = static::scope();
         } else {
             $scope = $tmp;
         }
     }
     if ($config = static::attached($scope, $vars)) {
         if (is_array($url)) {
             unset($url['library']);
         }
         $config['host'] = $config['host'] ?: $defaults['host'];
         if ($config['scheme'] === false) {
             $config['scheme'] = '//';
         } else {
             $config['scheme'] .= $config['scheme'] ? '://' : $defaults['scheme'];
         }
         $config['scheme'] = $config['scheme'] ?: 'http://';
         $base = isset($config['base']) ? '/' . $config['base'] : $defaults['base'];
         $base = $base . ($config['prefix'] ? '/' . $config['prefix'] : '');
         $config['base'] = rtrim($config['absolute'] ? '/' . trim($base, '/') : $base, '/');
         $defaults = $config + $defaults;
     }
     return $options + $defaults;
 }
 protected function _init()
 {
     parent::_init();
     $this->_env = array('PLATFORM' => 'IIS', 'SCRIPT_NAME' => '\\index.php', 'SCRIPT_FILENAME' => false, 'DOCUMENT_ROOT' => false, 'PATH_TRANSLATED' => '\\lithium\\app\\webroot\\index.php', 'HTTP_PC_REMOTE_ADDR' => '123.456.789.000');
 }
Example #7
0
 public function testRequestWithEnvVariables()
 {
     $request = new Request(array('env' => array('DOCUMENT_ROOT' => $this->_docroot, 'HTTP_HOST' => 'foo.com', 'HTTPS' => 'on', 'SERVER_PROTOCOL' => 'HTTP/1.0', 'REQUEST_URI' => '/lithium/app/hello/world?page=1', 'PHP_SELF' => '/lithium/app/index.php')));
     $this->assertIdentical('foo.com', $request->host);
     $this->assertIdentical('https', $request->scheme);
     $this->assertIdentical('HTTP/1.0', $request->protocol);
     $this->assertIdentical('1.0', $request->version);
     $this->assertIdentical('/hello/world', $request->url);
     $this->assertIdentical('/lithium/app', $request->env('base'));
 }
Example #8
0
 /**
  * Try to discover base url from `$_SERVER` (with `Request`).
  *
  * @see li3_mailer\net\mail\Message::_init()
  * @see lithium\action\Request
  * @return string Base url if found, `null` otherwise.
  */
 protected static function _discoverURL()
 {
     $request = new Request();
     if ($host = $request->env('HTTP_HOST')) {
         $scheme = $request->env('HTTPS') ? 'https://' : 'http://';
         $base = $request->env('base');
         return $scheme . $host . $base;
     }
     return null;
 }
Example #9
0
 /**
  * Assists `Media::negotiate()` in processing the negotiation conditions of a content type, by
  * iterating through the conditions and checking each one against the `Request` object.
  *
  * @see lithium\net\http\Media::negotiate()
  * @see lithium\net\http\Media::type()
  * @see lithium\action\Request
  * @param \lithium\action\Request $request The request to be checked against a
  *        set of conditions (if applicable).
  * @param array $config Represents a content type configuration, which is an array containing 3
  *              keys:
  *              - `'name'` _string_: The type name, i.e. `'html'` or `'json'`.
  *              - `'content'` _mixed_: One or more content types that the configuration
  *                represents, i.e. `'text/html'`, `'application/xhtml+xml'` or
  *                `'application/json'`, or an array containing multiple content types.
  *              - `'options'` _array_: An array containing rendering information, and an
  *                optional `'conditions'` key, which contains an array of matching parameters.
  *                For more details on these matching parameters, see `Media::type()`.
  * @return boolean Returns `true` if the information in `$request` matches the type
  *         configuration in `$config`, otherwise false.
  */
 public static function match($request, array $config)
 {
     if (!isset($config['options']['conditions'])) {
         return true;
     }
     $conditions = $config['options']['conditions'];
     foreach ($conditions as $key => $value) {
         switch (true) {
             case $key === 'type':
                 if ($value !== ($request->type === $config['name'])) {
                     return false;
                 }
                 break;
             case strpos($key, ':'):
                 if ($request->get($key) !== $value) {
                     return false;
                 }
                 break;
             case $request->is($key) !== $value:
                 return false;
                 break;
         }
     }
     return true;
 }
Example #10
0
 /**
  * Handler for HTTP Digest Authentication
  *
  * @param \lithium\action\Request $request
  * @return boolean|array
  */
 protected function _digest($request)
 {
     $username = $password = null;
     $auth = $this->_classes['auth'];
     $data = $auth::decode($request->env('PHP_AUTH_DIGEST'));
     $data['realm'] = $this->_config['realm'];
     $data['method'] = $request->method;
     $users = $this->_config['users'];
     if (!empty($data['username']) && !empty($users[$data['username']])) {
         $username = $data['username'];
         $password = $users[$data['username']];
     }
     $encoded = $auth::encode($username, $password, $data);
     if ($encoded['response'] !== $data['response']) {
         $nonce = uniqid();
         $opaque = md5($data['realm']);
         $message = "WWW-Authenticate: Digest realm=\"{$data['realm']}\",qop=\"auth\",";
         $message .= "nonce=\"{$nonce}\",opaque=\"{$opaque}\"";
         $this->_writeHeader($message);
         return false;
     }
     return compact('username', 'password');
 }
Example #11
0
 public function testParsingAcceptHeader()
 {
     $chrome = array('application/xml', 'application/xhtml+xml', 'text/html;q=0.9', 'text/plain;q=0.8', 'image/png', '*/*;q=0.5');
     $firefox = array('text/html', 'application/xhtml+xml', 'application/xml;q=0.9', '*/*;q=0.8');
     $safari = array('application/xml', 'application/xhtml+xml', 'text/html;q=0.9', 'text/plain;q=0.8', 'image/png', '*/*;q=0.5');
     $opera = array('text/html', 'application/xml;q=0.9', 'application/xhtml+xml', 'image/png', 'image/jpeg', 'image/gif', 'image/x-xbitmap', '*/*;q=0.1');
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $chrome))));
     $this->assertEqual('html', $request->accepts());
     $this->assertTrue(array_search('text/plain', $request->accepts(true)), 4);
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $safari))));
     $this->assertEqual('html', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $firefox))));
     $this->assertEqual('html', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $opera))));
     $this->assertEqual('html', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $chrome))));
     $request->params['type'] = 'txt';
     $result = $request->accepts(true);
     $this->assertEqual('text/plain', $result[0]);
 }
Example #12
0
 protected function _base()
 {
     $request = new Request();
     return $request->env('HTTP_HOST') ?: 'li3_mailer.generated';
 }
Example #13
0
 /**
  * Detects preferred locales from an action request by looking at the
  * `'Accept-Language'` header as described by RFC 2616, section 14.4.
  *
  * @link http://www.ietf.org/rfc/rfc2616.txt
  * @param \lithium\action\Request $request
  * @return array Preferred locales in their canonical form (i.e. `'fr_CA'`).
  */
 protected static function _preferredAction($request)
 {
     $result = array();
     $regex = "/^\\s*(?P<locale>\\w\\w(?:[-]\\w\\w)?)(?:;q=(?P<quality>(0|1|0\\.\\d+)))?\\s*\$/";
     foreach (explode(',', $request->env('HTTP_ACCEPT_LANGUAGE')) as $part) {
         if (preg_match($regex, $part, $matches)) {
             $locale = static::canonicalize($matches['locale']);
             $quality = isset($matches['quality']) ? $matches['quality'] : 1;
             $result[$quality][] = $locale;
         }
     }
     krsort($result);
     return array_reduce($result, function ($carry, $item) {
         return array_merge($carry, array_values($item));
     }, array());
 }