Changeset 1496

Show
Ignore:
Timestamp:
04/04/08 22:57:25 (5 months ago)
Author:
mikey
Message:

allow file urls

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/peer/stubURL.php

    r1485 r1496  
    3737    { 
    3838        $this->url = parse_url($url); 
    39         if (isset($this->url['host']) == true) { 
     39        if (isset($this->url['host']) === true) { 
    4040            $this->url['host'] = strtolower($this->url['host']); 
    4141        } 
     
    4444        // will lead to an unset $this->url['pass'] which is wrong 
    4545        // due to RFC1738 3.1, it has to be an empty string 
    46         if (isset($this->url['user']) == true && isset($this->url['pass']) == false && $this->get(true) != $url) { 
     46        if (isset($this->url['user']) === true && isset($this->url['pass']) === false && $this->get(true) !== $url) { 
    4747            $this->url['pass'] = ''; 
    4848        } 
    4949         
    50         if ($this->hasQuery() == true) { 
     50        if ($this->hasQuery() === true) { 
    5151            parse_str($this->url['query'], $this->params); 
    5252        } 
     
    6262    public static function fromString($urlString) 
    6363    { 
    64         if (strlen($urlString) == 0) { 
     64        if (strlen($urlString) === 0) { 
    6565            return null; 
    6666        } 
    6767         
    6868        $url = new self($urlString); 
    69         if ($url->isValid() == false) { 
     69        if ($url->isValid() === false) { 
    7070            throw new stubMalformedURLException('The URL ' . $urlString . ' is not a valid URL.'); 
    7171        } 
     
    8181    public function isValid() 
    8282    { 
    83         if (strlen($this->get()) == 0) { 
     83        if (strlen($this->get()) === 0) { 
    8484            return false; 
    8585        } 
    8686         
    87         if (isset($this->url['scheme']) == false) { 
     87        if (isset($this->url['scheme']) === false) { 
    8888            return false; 
    8989        } 
    9090         
    91         if (isset($this->url['user']) == true) { 
     91        if (isset($this->url['user']) === true) { 
    9292            if (ereg('([@:/])', $this->url['user']) == true) { 
    9393                return false; 
    9494            } 
    9595             
    96             if (isset($this->url['pass']) == true && ereg('([@:/])', $this->url['pass']) == true) { 
     96            if (isset($this->url['pass']) === true && ereg('([@:/])', $this->url['pass']) == true) { 
    9797                return false; 
    9898            } 
     
    100100         
    101101        // if host is set and seems to comply with host character rules or host is localhost syntax is ok 
    102         if (isset($this->url['host']) == true && (eregi('([a-z0-9-]*)\.([a-z]{2,4})', $this->url['host']) == true || eregi('([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})', $this->url['host']) == true || 'localhost' == $this->url['host'])) { 
     102        if (isset($this->url['host']) === true && (eregi('([a-z0-9-]*)\.([a-z]{2,4})', $this->url['host']) == true || eregi('([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})\.([0-9-]{1,3})', $this->url['host']) == true || 'localhost' == $this->url['host'])) { 
     103            return true; 
     104        } elseif (isset($this->url['host']) === false || strlen($this->url['host']) === 0) { 
    103105            return true; 
    104106        } 
     
    133135        } 
    134136         
    135         if (checkdnsrr($this->url['host'], 'ANY') == true || checkdnsrr($this->url['host'], 'MX') == true) { 
     137        if (checkdnsrr($this->url['host'], 'ANY') === true || checkdnsrr($this->url['host'], 'MX') === true) { 
    136138            return true; 
    137139        } 
     
    150152        $url  = ''; 
    151153        $user = ''; 
    152         if (isset($this->url['user']) == true) { 
     154        if (isset($this->url['user']) === true) { 
    153155            $user = $this->url['user']; 
    154             if (isset($this->url['pass']) == true) { 
     156            if (isset($this->url['pass']) === true) { 
    155157                $user .= ':' . $this->url['pass']; 
    156158            } 
     
    159161        } 
    160162         
    161         if (true == $port && isset($this->url['port']) == true) { 
     163        if (true == $port && isset($this->url['port']) === true) { 
    162164            $port =  ':' . $this->url['port']; 
    163165        } else { 
     
    165167        } 
    166168         
    167         if (isset($this->url['scheme']) == true && isset($this->url['host']) == true) { 
    168             $url = $this->url['scheme'] . '://' . $user . $this->url['host'] . $port; 
    169             if (isset($this->url['path']) == true) { 
     169        if (isset($this->url['scheme']) === true) { 
     170            $url = $this->url['scheme'] . '://'; 
     171            if (isset($this->url['host']) === true) { 
     172                $url .= $user . $this->url['host'] . $port; 
     173            } 
     174             
     175            if (isset($this->url['path']) === true) { 
    170176                $url .= $this->url['path']; 
    171177            } 
    172178        } 
    173179         
    174         if ($this->hasQuery() == true) { 
     180        if ($this->hasQuery() === true) { 
    175181            $url .= '?' . $this->url['query']; 
    176182        } 
     
    190196    public function getScheme() 
    191197    { 
    192         if (isset($this->url['scheme']) == true) { 
     198        if (isset($this->url['scheme']) === true) { 
    193199            return $this->url['scheme']; 
    194200        } 
     
    205211    public function getUser($defaultUser = null) 
    206212    { 
    207         if (isset($this->url['user']) == true) { 
     213        if (isset($this->url['user']) === true) { 
    208214            return $this->url['user']; 
    209215        } 
     
    220226    public function getPassword($defaultPassword = null) 
    221227    { 
    222         if (isset($this->url['pass']) == true) { 
     228        if (isset($this->url['pass']) === true) { 
    223229            return $this->url['pass']; 
    224230        } 
     
    235241    public function getHost($defaultHost = null) 
    236242    { 
    237         if (isset($this->url['host']) == true) { 
     243        if (isset($this->url['host']) === true) { 
    238244            return $this->url['host']; 
    239245        } 
     
    250256    public function getPort($defaultPort = null) 
    251257    { 
    252         if (isset($this->url['port']) == true) { 
     258        if (isset($this->url['port']) === true) { 
    253259            return $this->url['port']; 
    254260        } 
     
    274280    public function getPath() 
    275281    { 
    276         if (isset($this->url['path']) == false) { 
     282        if (isset($this->url['path']) === false) { 
    277283            return null; 
    278284        } 
    279285         
    280         if ($this->hasQuery() == true) { 
     286        if ($this->hasQuery() === true) { 
    281287            return $this->url['path'] . '?' . $this->url['query']; 
    282288        } 
     
    292298    public function hasQuery() 
    293299    { 
    294         return (isset($this->url['query']) == true && strlen($this->url['query']) > 0); 
     300        return (isset($this->url['query']) === true && strlen($this->url['query']) > 0); 
    295301    } 
    296302 
     
    304310    public function getParam($name, $defaultValue = null) 
    305311    { 
    306         if (isset($this->params[$name]) == false) { 
     312        if (isset($this->params[$name]) === false) { 
    307313            return $defaultValue; 
    308314        } 
  • trunk/src/test/php/net/stubbles/peer/stubURLTestCase.php

    r1485 r1496  
    214214 
    215215    /** 
     216     * assure that values are returned the expected way 
     217     * 
     218     * @test 
     219     */ 
     220    public function valueIPNoPath() 
     221    { 
     222        $url = stubURL::fromString('http://127.0.0.1'); 
     223        $this->assertTrue($url->isValid()); 
     224        $this->assertEquals('http://127.0.0.1', $url->get()); 
     225        $this->assertEquals('http', $url->getScheme()); 
     226        $this->assertNull($url->getUser()); 
     227        $this->assertEquals('foo', $url->getUser('foo')); 
     228        $this->assertNull($url->getPassword()); 
     229        $this->assertEquals('foo', $url->getPassword('foo')); 
     230        $this->assertEquals('127.0.0.1', $url->getHost()); 
     231        $this->assertNull($url->getPort()); 
     232        $this->assertEquals(313, $url->getPort(313)); 
     233        $this->assertNull($url->getPath()); 
     234        $this->assertFalse($url->hasQuery()); 
     235        $this->assertTrue($url->checkDNS()); 
     236    } 
     237 
     238    /** 
     239     * assure that values are returned the expected way 
     240     * 
     241     * @test 
     242     */ 
     243    public function fileURL() 
     244    { 
     245        $url = stubURL::fromString('file:///home'); 
     246        $this->assertTrue($url->isValid()); 
     247        $this->assertEquals('file:///home', $url->get()); 
     248        $this->assertEquals('file', $url->getScheme()); 
     249        $this->assertNull($url->getUser()); 
     250        $this->assertEquals('foo', $url->getUser('foo')); 
     251        $this->assertNull($url->getPassword()); 
     252        $this->assertEquals('foo', $url->getPassword('foo')); 
     253        $this->assertNull($url->getHost()); 
     254        $this->assertEquals('127.0.0.1', $url->getHost('127.0.0.1')); 
     255        $this->assertNull($url->getPort()); 
     256        $this->assertEquals(313, $url->getPort(313)); 
     257        $this->assertEquals('/home', $url->getPath()); 
     258        $this->assertFalse($url->hasQuery()); 
     259        $this->assertFalse($url->checkDNS()); 
     260    } 
     261 
     262    /** 
    216263     * assure that wrong values trigger an exception 
    217264     *