public function testType() { $request = new Request(); $this->assertEqual('html', $request->type()); $request = new Request(array('env' => array('CONTENT_TYPE' => 'application/json; charset=UTF-8', 'REQUEST_METHOD' => 'POST'))); $this->assertEqual('application/json; charset=UTF-8', $request->env('CONTENT_TYPE')); $this->assertEqual('json', $request->type()); }
/** * 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; }
public function testServerHttpBase() { $_SERVER['HTTP_HOST'] = 'sub.lithium.local'; $request = new Request(); $expected = '.lithium.local'; $result = $request->env('HTTP_BASE'); $this->assertEqual($expected, $result); }
/** * 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; }
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')); }
/** * 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'); }
protected function _base() { $request = new Request(); return $request->env('HTTP_HOST') ?: 'li3_mailer.generated'; }
/** * 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()); }