Exemple #1
0
 /**
  * Sets the service url from which the OData URL is parsed
  *
  * @param string $serviceUri The service url, absolute or relative.
  * 
  * @return void
  * 
  * @throws ODataException If the base uri in the configuration is malformed.
  */
 public function setServiceUri($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) {
             throw ODataException::createInternalServerError(Messages::hostMalFormedBaseUriInConfig());
         }
         $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())) {
             throw ODataException::createInternalServerError(Messages::hostMalFormedBaseUriInConfig(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) {
                 throw ODataException::createBadRequestError(Messages::hostRequestUriIsNotBasedOnRelativeUriInConfig($this->_absoluteRequestUriAsString, $serviceUri));
             }
             while ($j >= 0 && $requestUriSegments[$i] === $segments[$j]) {
                 $i--;
                 $j--;
             }
             if ($j != -1) {
                 throw ODataException::createBadRequestError(Messages::hostRequestUriIsNotBasedOnRelativeUriInConfig($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;
     }
 }
Exemple #2
0
 public function testIsBaseOf()
 {
     $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)));
 }