/** * @param URL $u * @return blaze\lang\String */ public function toExternalForm(URL $u) { $str = $u->getScheme() . '://'; if ($u->getUser() != null) { $str .= $u->getUser(); if ($u->getPassword() != null) { $str .= ':' . $u->getPassword(); } $str .= '@'; } $str .= $u->getHost(); if ($u->getPort() != -1) { $str .= ':' . $u->getPort(); } if ($u->getPath() != null) { $str .= '/' . $u->getPath(); } if ($u->getQuery() != null) { $str .= '?' . $u->getQuery(); } if ($u->getFragment() != null) { $str .= '#' . $u->getFragment(); } return new \blaze\lang\String($str); }
/** * Acquire a handler instance * * @param string key * @return remote.protocol.ProtocolHandler * @throws remote.protocol.UnknownProtocolException */ public function acquire($key, $initialize = FALSE) { $url = new URL($key); $key = new URL($url->getScheme() . '://' . $url->getHost()); if ($this->pool->containsKey($key)) { $instance = $this->pool->get($key); } else { sscanf($url->getScheme(), '%[^+]+%s', $type, $option); $class = HandlerFactory::handlerFor($type); $instance = $this->pool($key, $class->newInstance($option)); } // Add logger if (NULL !== ($cat = $url->getParam('log'))) { $instance->setTrace(Logger::getInstance()->getCategory($cat)); } // Set timeout, defaults to 60 seconds $instance->setTimeout($url->getParam('timeout', 60)); $initialize && $instance->initialize($url); return $instance; }
/** * Creates a new request object * * @param string method * @param peer.URL url * @return scriptlet.HttpScriptletRequest */ protected function newRequest($method, URL $url) { $q = $url->getQuery(''); $req = new HttpScriptletRequest(); $req->method = $method; $req->env['SERVER_PROTOCOL'] = 'HTTP/1.1'; $req->env['REQUEST_URI'] = $url->getPath('/') . ($q ? '?' . $q : ''); $req->env['QUERY_STRING'] = $q; $req->env['HTTP_HOST'] = $url->getHost(); if ('https' === $url->getScheme()) { $req->env['HTTPS'] = 'on'; } $req->setHeaders(array()); $req->setParams($url->getParams()); return $req; }
public function doCreate() { $req = $this->newRequest('GET', new URL('http://localhost/')); $res = new HttpScriptletResponse(); $s = newinstance('scriptlet.xml.XMLScriptlet', array(), '{ public function needsSession($request) { return TRUE; } }'); $s->service($req, $res); $this->assertEquals(HttpConstants::STATUS_FOUND, $res->statusCode); // Check URL from Location: header contains the session ID with($redirect = new URL(substr($res->headers[0], strlen('Location: ')))); $this->assertEquals('http', $redirect->getScheme()); $this->assertEquals('localhost', $redirect->getHost()); $this->assertEquals(sprintf('/xml/psessionid=%s/static', session_id()), $redirect->getPath()); $this->assertEquals(array(), $redirect->getParams(), $redirect->getURL()); }
/** * Get transport implementation for a specific URL * * @param peer.URL url * @return peer.http.HttpTransport * @throws lang.IllegalArgumentException in case the scheme is not supported */ public static function transportFor(URL $url) { sscanf($url->getScheme(), '%[^+]+%s', $scheme, $arg); if (!isset(self::$transports[$scheme])) { throw new IllegalArgumentException('Scheme "' . $scheme . '" unsupported'); } return self::$transports[$scheme]->newInstance($url, $arg); }
public function handleSessionInitializationError() { $req = $this->newRequest('GET', new URL('http://localhost/?psessionid=MALFORMED')); $res = new HttpScriptletResponse(); $s = newinstance('scriptlet.HttpScriptlet', array(), '{ public function needsSession($request) { return TRUE; } public function handleSessionInitialization($request) { if (!preg_match("/^a-f0-9$/", $request->getSessionId())) { throw new IllegalArgumentException("Invalid characters in session id"); } parent::handleSessionInitialization($request); } public function handleSessionInitializationError($request, $response) { $request->getURL()->addParam("relogin", 1); return $request->session->initialize(NULL); } }'); $s->service($req, $res); $this->assertEquals(HttpConstants::STATUS_FOUND, $res->statusCode); // Check URL from Location: header contains the session ID with($redirect = new URL(substr($res->headers[0], strlen('Location: ')))); $this->assertEquals('http', $redirect->getScheme()); $this->assertEquals('localhost', $redirect->getHost()); $this->assertEquals('/', $redirect->getPath()); $this->assertEquals(session_id(), $redirect->getParam('psessionid')); $this->assertEquals('1', $redirect->getParam('relogin')); }
/** * Navigate to a given URL * * @param string target * @param string params * @param string method * @throws unittest.AssertionFailedError */ public function navigateTo($target, $params = NULL, $method = HttpConstants::GET) { if (strstr($target, '://')) { $url = new URL($target); $this->conn = $this->getConnection(sprintf('%s://%s%s/', $url->getScheme(), $url->getHost(), -1 === $url->getPort(-1) ? '' : ':' . $url->getPort())); $params ? $url->setParams($params) : ''; $this->beginAt($url->getPath(), $url->getParams(), $method); } else { if ('' !== $target && '/' === $target[0]) { $this->beginAt($target, $params, $method); } else { $base = $this->getBase(); $this->beginAt(substr($base, 0, strrpos($base, '/')) . '/' . $target, $params, $method); } } }
function my_has_URL_invalid_content($bookmark_url) { return URL::getScheme($bookmark_url) === 'javascript'; }
/** * Given an associative array of setting constant names to tests, ensures * that each setting's value passes the appropriate validation test. * If a setting is defined as a value that does not pass the selected test, * a SettingException is thrown. The available tests are expressed as * strings, and work as follows: * * boolean, bool: Validates that the value is a boolean literal. * * string, str: Validates that the value is a string literal. * * integer, int: Tests the value against PHP's FILTER_VALIDATE_INT logic. * * num, number, numeric: Validates that the value is numeric according to * PHP's is_numeric() logic. * * ratio: Validates that the value is a number between 0 and 1 (inclusive). * * file: Validates that the value is a path to a real file or a link to a * real file. * * dir, directory: Validates that the value is a path to a real directory, * and that it does not end in a directory separator. * * writable: Validates that the value is a path to a file to which the * effective user has the ability to write. Note that this test will * succeed if the file does not exist, provided the effective user has the * ability to create new files in the containing directory. * * email: Validates that the value appears to be an email address, or * multiple email addresses in a comma-delimited list. * * executable: Validates that the value is a valid file system path with * the executable bit set. * * url: Tests whether the value is a valid URL. * * In addition, it is also possible to pass a Perl-formatted regular * expression in the same format that the PHP's PCRE functions accept; in * this case, the value will be tested for a match against the regular * expression. * * A setting may be defined as optional by using the prefix '?' on any * test (including regular expressions). When this flag is present and the * corresponding constant has not been defined, it will be automatically * defined as null. * * @param array $tests */ public static function testSettingTypes(array $tests) { foreach ($tests as $setting => $test) { if (array_key_exists($setting, self::$_validatedSettings)) { /* These are constants so there's no point in checking them more than once. */ continue; } if ($test[0] == '?') { $optional = true; $test = substr($test, 1); } else { $optional = false; } if (defined($setting)) { $settingVal = constant($setting); } elseif ($optional) { $settingVal = null; } else { throw new SettingException('Required setting ' . $setting . ' is not defined.'); } if ($optional && $settingVal === null) { continue; } if (substr($test, 0, 1) == '/') { if (!preg_match($test, $settingVal)) { throw new SettingException('Setting ' . $setting . ' failed the required ' . 'regular expression match.'); } continue; } $exceptionMessage = null; switch ($test) { case 'boolean': case 'bool': if (!is_bool($settingVal)) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'a boolean value.'; } break; case 'string': case 'str': if (!is_string($settingVal)) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'a string value.'; } break; case 'integer': case 'int': if (filter_var($settingVal, FILTER_VALIDATE_INT) === false) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'an integer value.'; } break; case 'num': case 'number': case 'numeric': if (!is_numeric($settingVal)) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'a numeric value.'; } break; case 'ratio': if (!is_numeric($settingVal) || $settingVal < 0 || $settingVal > 1) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'a ratio.'; } break; case 'file': if (!is_file($settingVal) && !is_link($settingVal)) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'a path to a real file.'; } break; case 'dir': case 'directory': // Don't allow trailing slashes if (substr($settingVal, -1) == DIRECTORY_SEPARATOR) { $exceptionMessage = 'Setting ' . $setting . ' must ' . 'not contain a trailing slash.'; } elseif (!is_dir($settingVal)) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'a real directory.'; } break; case 'writable': if (!strlen($settingVal) || !self::testWritable($settingVal)) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'a writable file.'; } break; case 'email': // Allow comma-delimited instances of multiple addresses $addresses = explode(',', $settingVal); foreach ($addresses as $address) { if (!filter_var($address, FILTER_VALIDATE_EMAIL)) { $exceptionMessage = 'Setting ' . $setting . ' must be either a ' . 'single email address, or a comma-delimited ' . 'list of email addresses.'; break; } } break; case 'executable': if (!is_executable($settingVal)) { $exceptionMessage = 'Setting ' . $setting . ' must be ' . 'an executable file.'; } break; case 'url': try { $url = new URL($settingVal); // Require the presence of a scheme if (strpos($settingVal, $url->getScheme() . '://') !== 0) { $exceptionMessage = 'The value of setting ' . $setting . ' is missing a ' . 'scheme.'; } } catch (URLException $e) { $exceptionMessage = 'Setting ' . $setting . ' must ' . 'be a valid URL.'; } break; default: throw new SettingException('Unrecognized setting test "' . $test . '".'); } if ($exceptionMessage) { throw new SettingException($exceptionMessage); } self::$_validatedSettings[$setting] = null; } }
/** * Parse DSN * * @param string dsn * @return bool success */ protected function _parsedsn($dsn) { if (NULL === $dsn) { return TRUE; } $u = new URL($dsn); if (!$u->getHost()) { throw new IllegalArgumentException('DSN parsing failed ["' . $dsn . '"]'); } // Scheme switch (strtoupper($u->getScheme())) { case 'ESMTP': $this->ext = TRUE; break; case 'SMTP': $this->ext = FALSE; break; default: throw new IllegalArgumentException('Scheme "' . $u->getScheme() . '" not supported'); } // Copy host and port $this->host = $u->getHost(); $this->port = $u->getPort() ? $u->getPort() : 25; // User & password if ($u->getUser()) { $this->auth = $u->getParam('auth', SMTP_AUTH_PLAIN); $this->user = $u->getUser(); $this->pass = $u->getPassword(); } }