Exemplo n.º 1
0
 /**
  * Validates the whole URL.
  *
  * @param $url     string
  * @param $options Options
  *
  * @return string
  */
 public static function validateUrl($url, Options $options)
 {
     if (trim($url) == '') {
         throw new InvalidURLException('Provided URL "' . $url . '" cannot be empty');
     }
     //Split URL into parts first
     $parts = parse_url($url);
     if (empty($parts)) {
         throw new InvalidURLException('Error parsing URL "' . $url . '"');
     }
     if (!array_key_exists('host', $parts)) {
         throw new InvalidURLException('Provided URL "' . $url . '" doesn\'t contain a hostname');
     }
     //If credentials are passed in, but we don't want them, raise an exception
     if (!$options->getSendCredentials() && (array_key_exists('user', $parts) || array_key_exists('pass', $parts))) {
         throw new InvalidURLException('Credentials passed in but "sendCredentials" is set to false');
     }
     //First, validate the scheme
     if (array_key_exists('scheme', $parts)) {
         $parts['scheme'] = self::validateScheme($parts['scheme'], $options);
     } else {
         //Default to http
         $parts['scheme'] = 'http';
     }
     //Validate the port
     if (array_key_exists('port', $parts)) {
         $parts['port'] = self::validatePort($parts['port'], $options);
     }
     //Validate the host
     $host = self::validateHost($parts['host'], $options);
     if ($options->getPinDns()) {
         //Since we're pinning DNS, we replace the host in the URL
         //with an IP, then get cURL to send the Host header
         $parts['host'] = $host['ips'][0];
     } else {
         //Not pinning DNS, so just use the host
         $parts['host'] = $host['host'];
     }
     //Rebuild the URL
     $url = self::buildUrl($parts);
     return array('url' => $url, 'host' => $host['host'], 'ips' => $host['ips']);
 }