Exemplo n.º 1
0
 function testBasicParsing()
 {
     $url = new SimpleUrl("https://www.lastcraft.com/test/");
     $this->assertEqual($url->getScheme(), "https");
     $this->assertEqual($url->getHost(), "www.lastcraft.com");
     $this->assertEqual($url->getPath(), "/test/");
 }
Exemplo n.º 2
0
 /**
  *    Finds the name of the realm by comparing URLs.
  *    @param SimpleUrl $url        URL to test.
  *    @return SimpleRealm          Name of realm.
  *    @access protected
  */
 protected function findRealmFromUrl($url)
 {
     if (!isset($this->realms[$url->getHost()])) {
         return false;
     }
     foreach ($this->realms[$url->getHost()] as $name => $realm) {
         if ($realm->isWithin($url)) {
             return $realm;
         }
     }
     return false;
 }
Exemplo n.º 3
0
 function assertUrl($raw, $parts, $params = false, $coords = false)
 {
     if (!is_array($params)) {
         $params = array();
     }
     $url = new SimpleUrl($raw);
     $this->assertIdentical($url->getScheme(), $parts[0], "[{$raw}] scheme -> %s");
     $this->assertIdentical($url->getUsername(), $parts[1], "[{$raw}] username -> %s");
     $this->assertIdentical($url->getPassword(), $parts[2], "[{$raw}] password -> %s");
     $this->assertIdentical($url->getHost(), $parts[3], "[{$raw}] host -> %s");
     $this->assertIdentical($url->getPort(), $parts[4], "[{$raw}] port -> %s");
     $this->assertIdentical($url->getPath(), $parts[5], "[{$raw}] path -> %s");
     $this->assertIdentical($url->getTld(), $parts[6], "[{$raw}] tld -> %s");
     $this->assertIdentical($url->getEncodedRequest(), $parts[7], "[{$raw}] encoded -> %s");
     $this->assertIdentical($url->getFragment(), $parts[8], "[{$raw}] fragment -> %s");
     if ($coords) {
         $this->assertIdentical($url->getX(), $coords[0], "[{$raw}] x -> %s");
         $this->assertIdentical($url->getY(), $coords[1], "[{$raw}] y -> %s");
     }
 }
Exemplo n.º 4
0
 /**
  *    Test to see if link is an absolute one.
  *    @param string $url     Url to test.
  *    @return boolean        True if absolute.
  *    @access protected
  */
 function _linkIsAbsolute($url)
 {
     $parsed = new SimpleUrl($url);
     return (bool) ($parsed->getScheme() && $parsed->getHost());
 }
Exemplo n.º 5
0
 /**
  *    Extracts new cookies into the cookie jar.
  *    @param SimpleUrl $url     Target to fetch as url object.
  *    @param array $cookies     New cookies.
  *    @access private
  */
 function _addCookies($url, $cookies)
 {
     foreach ($cookies as $cookie) {
         if ($url->getHost()) {
             $cookie->setHost($url->getHost());
         }
         $this->_cookie_jar->setCookie($cookie);
     }
 }
Exemplo n.º 6
0
 function assertUrl($raw, $parts, $params = false)
 {
     if (!is_array($params)) {
         $params = array();
     }
     $url = new SimpleUrl($raw);
     $this->assertIdentical($url->getScheme(), $parts[0], "[{$raw}] scheme->%s");
     $this->assertIdentical($url->getUsername(), $parts[1], "[{$raw}] username->%s");
     $this->assertIdentical($url->getPassword(), $parts[2], "[{$raw}] password->%s");
     $this->assertIdentical($url->getHost(), $parts[3], "[{$raw}] host->%s");
     $this->assertIdentical($url->getPort(), $parts[4], "[{$raw}] port->%s");
     $this->assertIdentical($url->getPath(), $parts[5], "[{$raw}] path->%s");
     $this->assertIdentical($url->getTld(), $parts[6], "[{$raw}] tld->%s");
     $this->assertIdentical($url->getEncodedRequest(), $parts[7], "[{$raw}] encoded->%s");
     $query = new SimpleQueryString();
     foreach ($params as $key => $value) {
         $query->add($key, $value);
     }
     $this->assertIdentical($url->getRequest(), $query, "[{$raw}] request->%s");
     $this->assertIdentical($url->getFragment(), $parts[8], "[{$raw}] fragment->%s");
 }
Exemplo n.º 7
0
 /**
  *    Uses a URL to sift relevant cookies by host and
  *    path. Results are list of strings of form "name=value".
  *    @param SimpleUrl $url       Url to select by.
  *    @return array               Valid name and value pairs.
  *    @access public
  */
 function selectAsPairs($url)
 {
     $pairs = array();
     foreach ($this->cookies as $cookie) {
         if ($this->isMatch($cookie, $url->getHost(), $url->getPath(), $cookie->getName())) {
             $pairs[] = $cookie->getName() . '=' . $cookie->getValue();
         }
     }
     return $pairs;
 }
Exemplo n.º 8
0
 /**
  *    Replaces unknown sections to turn a relative
  *    URL into an absolute one. The base URL can
  *    be either a string or a SimpleUrl object.
  *    @param string/SimpleUrl $base       Base URL.
  *    @access public
  */
 function makeAbsolute($base)
 {
     if (!is_object($base)) {
         $base = new SimpleUrl($base);
     }
     $scheme = $this->getScheme() ? $this->getScheme() : $base->getScheme();
     $host = $this->getHost() ? $this->getHost() : $base->getHost();
     if (substr($this->_path, 0, 1) == "/") {
         $path = $this->normalisePath($this->_path);
     } else {
         $path = $this->normalisePath($base->getBasePath() . $this->_path);
     }
     $identity = '';
     if ($this->_username && $this->_password) {
         $identity = $this->_username . ':' . $this->_password . '@';
     }
     $encoded = $this->getEncodedRequest();
     $fragment = $this->getFragment() ? '#' . $this->getFragment() : '';
     return new SimpleUrl("{$scheme}://{$identity}{$host}{$path}{$encoded}{$fragment}");
 }
Exemplo n.º 9
0
 function testDOSDirnameAfterFile()
 {
     $url = new SimpleUrl('file://C:\\config.sys');
     $this->assertEqual($url->getScheme(), 'file');
     $this->assertIdentical($url->getHost(), false);
     $this->assertEqual($url->getPath(), '/C:/config.sys');
 }
Exemplo n.º 10
0
 /**
  *    Replaces unknown sections to turn a relative
  *    URL into an absolute one. The base URL can
  *    be either a string or a SimpleUrl object.
  *    @param string/SimpleUrl $base       Base URL.
  *    @access public
  */
 function makeAbsolute($base)
 {
     if (!is_object($base)) {
         $base = new SimpleUrl($base);
     }
     $scheme = $this->getScheme() ? $this->getScheme() : $base->getScheme();
     $host = $this->getHost();
     $port = $this->getPort() ? ':' . $this->getPort() : '';
     $path = $this->normalisePath($this->_path);
     if (!$host) {
         $host = $base->getHost();
         $port = $base->getPort() ? ':' . $base->getPort() : '';
         if ($this->_isRelativePath($this->_path)) {
             $path = $this->normalisePath($base->getBasePath() . $this->_path);
         }
     }
     $identity = $this->_getIdentity() ? $this->_getIdentity() . '@' : '';
     $encoded = $this->getEncodedRequest();
     $fragment = $this->getFragment() ? '#' . $this->getFragment() : '';
     $coords = $this->_x !== false ? '?' . $this->_x . ',' . $this->_y : '';
     return new SimpleUrl("{$scheme}://{$identity}{$host}{$port}{$path}{$encoded}{$fragment}{$coords}");
 }
Exemplo n.º 11
0
 /**
  *    Replaces unknown sections to turn a relative
  *    URL into an absolute one. The base URL can
  *    be either a string or a SimpleUrl object.
  *    @param string/SimpleUrl $base       Base URL.
  *    @access public
  */
 function makeAbsolute($base)
 {
     if (!is_object($base)) {
         $base = new SimpleUrl($base);
     }
     if ($this->getHost()) {
         $scheme = $this->getScheme();
         $host = $this->getHost();
         $port = $this->getPort() ? ':' . $this->getPort() : '';
         $identity = $this->getIdentity() ? $this->getIdentity() . '@' : '';
         if (!$identity) {
             $identity = $base->getIdentity() ? $base->getIdentity() . '@' : '';
         }
     } else {
         $scheme = $base->getScheme();
         $host = $base->getHost();
         $port = $base->getPort() ? ':' . $base->getPort() : '';
         $identity = $base->getIdentity() ? $base->getIdentity() . '@' : '';
     }
     $path = $this->normalisePath($this->extractAbsolutePath($base));
     $encoded = $this->getEncodedRequest();
     $fragment = $this->getFragment() ? '#' . $this->getFragment() : '';
     $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY();
     return new SimpleUrl("{$scheme}://{$identity}{$host}{$port}{$path}{$encoded}{$fragment}{$coords}");
 }
Exemplo n.º 12
0
 /**
  *    Replaces unknown sections to turn a relative
  *    URL into an absolute one.
  *    @param string $base            Base URL.
  *    @access public
  */
 function makeAbsolute($base) {
     $base_url = new SimpleUrl($base);
     if (!$this->getScheme()) {
         $this->_scheme = $base_url->getScheme();
     }
     if (!$this->getHost()) {
         $this->_host = $base_url->getHost();
     }
     if (substr($this->getPath(), 0, 1) != "/") {
         $this->_path = $base_url->getBasePath() . $this->getPath();
     }
     $this->_path = $this->normalisePath($this->_path);
 }
Exemplo n.º 13
0
 /**
  *    Fetches a URL performing the standard tests.
  *    @param $url        Target to fetch as string.
  *    @param $request    Test version of SimpleHttpRequest.
  *    @return            Content of page.
  *    @access public
  */
 function fetchUrl($url, $request = false)
 {
     if (!is_object($request)) {
         $request = new SimpleHttpRequest($url);
     }
     foreach ($this->_cookie_jar->getValidCookies() as $cookie) {
         $request->setCookie($cookie);
     }
     $this->_response = $request->fetch();
     $this->_checkExpectations($url, $this->_response);
     foreach ($this->_response->getNewCookies() as $cookie) {
         $parsed_url = new SimpleUrl($url);
         if ($parsed_url->getHost()) {
             $cookie->setHost($parsed_url->getHost());
         }
         $this->_cookie_jar->setCookie($cookie);
     }
     return $this->_response->getContent();
 }
Exemplo n.º 14
0
 /**
  *    Retries a request after setting the authentication
  *    for the current realm.
  *    @param string $username    Username for realm.
  *    @param string $password    Password for realm.
  *    @return boolean            True if successful fetch. Note
  *                               that authentication may still have
  *                               failed.
  *    @access public
  */
 function authenticate($username, $password)
 {
     if (!$this->_history->getUrl()) {
         return false;
     }
     if (!$this->_headers || !$this->_headers->getRealm()) {
         return false;
     }
     $url = new SimpleUrl($this->_history->getUrl());
     $this->_user_agent->setIdentity($url->getHost(), $this->_headers->getRealm(), $username, $password);
     return $this->retry();
 }
Exemplo n.º 15
0
 /**
  *    Writes new cookies to the cookie jar.
  *    @param SimpleCookieJar $jar   Jar to write to.
  *    @param SimpleUrl $url         Host and path to write under.
  *    @access public
  */
 function writeCookiesToJar(&$jar, $url)
 {
     foreach ($this->_cookies as $cookie) {
         $jar->setCookie($cookie->getName(), $cookie->getValue(), $url->getHost(), $cookie->getPath(), $cookie->getExpiry());
     }
 }
Exemplo n.º 16
0
 /**
  *    Replaces unknown sections to turn a relative
  *    URL into an absolute one. The base URL can
  *    be either a string or a SimpleUrl object.
  *    @param string/SimpleUrl $base       Base URL.
  *    @access public
  */
 function makeAbsolute($base)
 {
     if (!is_object($base)) {
         $base = new SimpleUrl($base);
     }
     $scheme = $this->getScheme() ? $this->getScheme() : $base->getScheme();
     $host = $this->getHost() ? $this->getHost() : $base->getHost();
     $port = $this->_extractAbsolutePort($base);
     $path = $this->normalisePath($this->_extractAbsolutePath($base));
     $identity = $this->_getIdentity() ? $this->_getIdentity() . '@' : '';
     $encoded = $this->getEncodedRequest();
     $fragment = $this->getFragment() ? '#' . $this->getFragment() : '';
     return new SimpleUrl("{$scheme}://{$identity}{$host}{$port}{$path}{$encoded}{$fragment}");
 }
Exemplo n.º 17
0
 /**
  *    Reads the current cookies within the base URL.
  *    @param string $name     Key of cookie to find.
  *    @param SimpleUrl $base  Base URL to search from.
  *    @return string/boolean  Null if there is no base URL, false
  *                            if the cookie is not set.
  *    @access public
  */
 function getBaseCookieValue($name, $base)
 {
     if (!$base) {
         return null;
     }
     return $this->getCookieValue($base->getHost(), $base->getPath(), $name);
 }
Exemplo n.º 18
0
 /**
  * @param SimpleUrl
  * @param SimpleEncoding
  * @return k_ActingAsSimpleHttpResponse
  */
 protected function fetch(SimpleUrl $url, SimpleEncoding $parameters)
 {
     // extract primitives from SimpleTest abstractions
     $url_path = $url->getPath();
     $url_query = array();
     parse_str(str_replace('?', '', $url->getEncodedRequest()), $url_query);
     $method = $parameters->getMethod();
     $data = array();
     foreach ($parameters->getAll() as $pair) {
         $data[$pair->getKey()] = $pair->getValue();
     }
     if (!in_array($url->getHost(), array("", $this->servername))) {
         return new k_ActingAsSimpleHttpResponse($url, $data, array("HTTP/1.1 502"), "External URL requested: " . $url->asString(), $method);
     }
     // set up a mocked environment
     $server = array('SERVER_NAME' => $this->servername, 'REQUEST_METHOD' => $method, 'REQUEST_URI' => $url_path);
     $headers = new k_VirtualHeaders();
     $this->authenticator->addHeaders($headers, $url);
     foreach ($this->additional_headers as $line) {
         $headers->addHeaderLine($line);
     }
     $socket_buffer = new k_VirtualSocketBuffer();
     $parameters->writeHeadersTo($socket_buffer);
     foreach ($socket_buffer->getLines() as $line) {
         $headers->addHeaderLine($line);
     }
     $superglobals = new k_adapter_MockGlobalsAccess($url_query, $data, $server, $headers->headers());
     $response = k()->setContext(new k_HttpRequest("", null, $this->identity_loader, $this->language_loader, $this->translator_loader, $superglobals, $this->cookie_access, $this->session_access))->setComponentCreator($this->components)->setCharsetStrategy($this->charset_strategy)->run($this->root_class_name);
     $output = new k_SimpleOutputAccess();
     $response->out($output);
     return $output->toSimpleHttpResponse($url, $data, $method);
 }