コード例 #1
0
ファイル: UrlTest.php プロジェクト: i-geo/odataphpprod
 public function testUrl()
 {
     try {
         $urlStr = "http://localhost/NorthwindService.svc/Customers('ALFKI')/Orders?\$filter=OrderID eq 123";
         $url = new Url($urlStr);
         $this->assertEquals($url->getScheme(), 'http');
         $this->assertEquals($url->getPort(), 80);
         $this->assertEquals($url->getHost(), 'localhost');
         $this->assertEquals($url->getPath(), "/NorthwindService.svc/Customers('ALFKI')/Orders");
         $this->assertEquals($url->getQuery(), '$filter=OrderID eq 123');
         $this->assertTrue($url->isAbsolute());
         $this->assertFalse($url->isRelative());
         //This is valid
         $urlStr = "http://localhost///NorthwindService.svc/Customers('ALFKI')/Orders//?\$filter=OrderID eq 123";
         $url = new Url($urlStr);
         $segments = $url->getSegments();
         $this->assertEquals(count($segments), 3);
         $this->assertEquals($segments[0], 'NorthwindService.svc');
         $this->assertEquals($segments[1], "Customers('ALFKI')");
         $this->assertEquals($segments[2], "Orders");
         //This is valid
         $urlStr = "http://localhost/NorthwindService.svc/@/./!/Customers('ALFKI')/Orders?\$filter=OrderID eq 123";
         new Url($urlStr);
         $exceptionThrown = false;
         try {
             $urlStr = "http://localhost/NorthwindService.svc//Customers('ALFKI')/Orders?\$filter=OrderID eq 123";
             new Url($urlStr);
         } catch (UrlFormatException $exception) {
             $exceptionThrown = true;
         }
         if (!$exceptionThrown) {
             $this->fail('An expected UrlFormatException has not been raised');
         }
         $urlStr1 = "http://localhost/NorthwindService.svc";
         $urlStr2 = "http://localhost/NorthwindService.svc/Customers('ALFKI')/Orders";
         $url1 = new Url($urlStr1);
         $this->assertTrue($url1->isBaseOf(new Url($urlStr2)));
         $urlStr1 = "http://localhost/NorthwindService.svc/Customers('ALFKI')/Orders/Order_Details";
         $urlStr2 = "http://localhost/NorthwindService.svc/Customers('ALFKI')/Orders";
         $url1 = new Url($urlStr1);
         $this->assertFalse($url1->isBaseOf(new Url($urlStr2)));
         $urlStr1 = "http://localhost/NorthwindService";
         $urlStr2 = "http://localhost/NorthwindService.svc/Customers('ALFKI')/Orders";
         $url1 = new Url($urlStr1);
         $this->assertFalse($url1->isBaseOf(new Url($urlStr2)));
         $urlStr1 = "http://localhost/NorthwindService.svc";
         $urlStr2 = "https://localhost/NorthwindService.svc/Customers('ALFKI')/Orders";
         $url1 = new Url($urlStr1);
         $this->assertFalse($url1->isBaseOf(new Url($urlStr2)));
         $urlStr1 = "http://localhost:80/NorthwindService.svc";
         $urlStr2 = "http://localhost/NorthwindService.svc/Customers('ALFKI')/Orders";
         $url1 = new Url($urlStr1);
         $this->assertTrue($url1->isBaseOf(new Url($urlStr2)));
         $urlStr1 = "https://localhost:443/NorthwindService.svc";
         $urlStr2 = "https://localhost/NorthwindService.svc/Customers('ALFKI')/Orders";
         $url1 = new Url($urlStr1);
         $this->assertTrue($url1->isBaseOf(new Url($urlStr2)));
         $urlStr1 = "http://msn.com/NorthwindService.svc";
         $urlStr2 = "http://localhost/NorthwindService.svc/Customers('ALFKI')/Orders";
         $url1 = new Url($urlStr1);
         $this->assertFalse($url1->isBaseOf(new Url($urlStr2)));
     } catch (\Exception $exception) {
         $this->fail('An expected Exception has not been raised' . $exception->getMessage());
     }
 }
コード例 #2
0
 /**
  * Sets the absolute service url from configuration file.
  * Note: This is an one time called internal method invoked 
  * from Dispathcer.
  *   
  * @param string $serviceUri The service url.
  * 
  * @return void
  * 
  * @throws ODataException If the base uri in the configuration is malformed.
  */
 public function setAbsoluteServiceUri($serviceUri)
 {
     if (is_null($this->_absoluteServiceUri)) {
         $isAbsoluteServiceUri = strpos($serviceUri, 'http://') === 0 || strpos($serviceUri, 'https://') === 0;
         try {
             $this->_absoluteServiceUri = new Url($serviceUri, $isAbsoluteServiceUri);
         } catch (UrlFormatException $exception) {
             ODataException::createInternalServerError(Messages::dataServiceHostMalFormedBaseUriInConfig());
         }
         $segments = $this->_absoluteServiceUri->getSegments();
         $lastSegment = $segments[count($segments) - 1];
         $endsWithSvc = substr_compare($lastSegment, '.svc', -strlen('.svc'), strlen('.svc')) === 0;
         if (!$endsWithSvc || !is_null($this->_absoluteServiceUri->getQuery()) || !is_null($this->_absoluteServiceUri->getFragment())) {
             ODataException::createInternalServerError(Messages::dataServiceHostMalFormedBaseUriInConfig(true));
         }
         if (!$isAbsoluteServiceUri) {
             $requestUriSegments = $this->_absoluteRequestUri->getSegments();
             $i = count($requestUriSegments) - 1;
             // Find index of segment in the request uri that end with .svc
             // There will be always a .svc segment in the request uri otherwise
             // uri redirection will not happen.
             for (; $i >= 0; $i--) {
                 $endsWithSvc = substr_compare($requestUriSegments[$i], '.svc', -strlen('.svc'), strlen('.svc')) === 0;
                 if ($endsWithSvc) {
                     break;
                 }
             }
             $j = count($segments) - 1;
             $k = $i;
             if ($j > $i) {
                 ODataException::createBadRequestError(Messages::dataServiceHostRequestUriIsNotBasedOnRelativeUriInConfig($this->_absoluteRequestUriAsString, $serviceUri));
             }
             while ($j >= 0 && $requestUriSegments[$i] === $segments[$j]) {
                 $i--;
                 $j--;
             }
             if ($j != -1) {
                 ODataException::createBadRequestError(Messages::dataServiceHostRequestUriIsNotBasedOnRelativeUriInConfig($this->_absoluteRequestUriAsString, $serviceUri));
             }
             $serviceUri = $this->_absoluteRequestUri->getScheme() . '://' . $this->_absoluteRequestUri->getHost() . ':' . $this->_absoluteRequestUri->getPort();
             for ($l = 0; $l <= $k; $l++) {
                 $serviceUri .= '/' . $requestUriSegments[$l];
             }
             $this->_absoluteServiceUri = new Url($serviceUri);
         }
         $this->_absoluteServiceUriAsString = $serviceUri;
     }
 }