Ejemplo n.º 1
0
 public function testNormalizedProtocol()
 {
     $url = new CUrl("http://www.example.com/");
     $this->assertTrue($url->normalizedProtocol()->equals("http"));
     $url = new CUrl("HTTPS://WWW.EXAMPLE.COM/");
     $this->assertTrue($url->normalizedProtocol()->equals("https"));
     $url = new CUrl("FTP://WWW.EXAMPLE.COM/");
     $this->assertTrue($url->normalizedProtocol()->equals("ftp"));
 }
Ejemplo n.º 2
0
 /**
  * Creates a request to be sent over the Internet.
  *
  * For the request to be sent over HTTPS instead of regular HTTP, the input URL should explicitly indicate "https"
  * as the communication protocol to be used. The same applies to FTP/FTPS.
  *
  * When making an `HTTP_POST` request, at least one POST field should be specified with `addPostField` method
  * before sending the request. Likewise, if the request type is `HTTP_UPLOAD`, `HTTP_PUT`, or `FTP_UPLOAD`, the
  * outgoing file's path or outgoing data needs to be provided before sending the request, using either
  * `setUploadFile` or `setUploadData` method.
  *
  * The number of the destination port can be specified in the URL or with `setPort` method.
  *
  * @param  string $url The URL to which the request is to be sent. If no protocol is indicated in the URL, the
  * HTTP protocol is assumed.
  * @param  enum $type **OPTIONAL. Default is** `HTTP_GET`. The type of the request (see [Summary](#summary)).
  * @param  string $downloadDestinationFp **OPTIONAL.** For `HTTP_DOWNLOAD`, `FTP_DOWNLOAD`, and `ANY_DOWNLOAD`
  * request types, this is the file path to which the downloaded data should be saved.
  */
 public function __construct($url, $type = self::HTTP_GET, $downloadDestinationFp = null)
 {
     assert('is_cstring($url) && is_enum($type) && ' . '(!isset($downloadDestinationFp) || is_cstring($downloadDestinationFp))', vs(isset($this), get_defined_vars()));
     assert('CUrl::isValid($url, true)', vs(isset($this), get_defined_vars()));
     assert('!(($type == self::HTTP_DOWNLOAD || $type == self::FTP_DOWNLOAD || ' . '$type == self::ANY_DOWNLOAD) && !isset($downloadDestinationFp))', vs(isset($this), get_defined_vars()));
     // Find out the basic protocol from the request type.
     switch ($type) {
         case self::HTTP_GET:
         case self::HTTP_DOWNLOAD:
         case self::HTTP_POST:
         case self::HTTP_UPLOAD:
         case self::HTTP_PUT:
         case self::HTTP_DELETE:
         case self::HTTP_HEAD:
             $this->m_requestBasicProtocol = "http";
             break;
         case self::FTP_LIST:
         case self::FTP_DOWNLOAD:
         case self::FTP_UPLOAD:
             $this->m_requestBasicProtocol = "ftp";
             break;
         case self::ANY_DOWNLOAD:
             // Look into the URL.
             $objUrl = new CUrl(CUrl::ensureProtocol($url));
             $protocolFromUrl = $objUrl->normalizedProtocol();
             $this->m_requestBasicProtocol = self::basicProtocol($protocolFromUrl);
             break;
         default:
             assert('false', vs(isset($this), get_defined_vars()));
             break;
     }
     // If the URL does not indicate any protocol, which prevents it from being fully qualified, make the URL
     // indicate the protocol that was derived from the request type.
     $url = CUrl::ensureProtocol($url, $this->m_requestBasicProtocol);
     if (CDebug::isDebugModeOn()) {
         $objUrl = new CUrl($url);
         assert('$objUrl->hasProtocol()', vs(isset($this), get_defined_vars()));
         $basicProtocolFromUrl = self::basicProtocol($objUrl->normalizedProtocol());
         assert('CString::equals($basicProtocolFromUrl, $this->m_requestBasicProtocol)', vs(isset($this), get_defined_vars()));
     }
     $this->m_url = $url;
     $this->m_type = $type;
     $this->m_downloadDestinationFp = CFilePath::frameworkPath($downloadDestinationFp);
     $this->m_requestSummary = CMap::make();
     $this->m_responseHeaders = CArray::make();
     $this->m_responseHeadersLcKeys = CMap::make();
     $this->m_curl = curl_init();
     if (!is_resource($this->m_curl) || !CString::isEmpty(curl_error($this->m_curl))) {
         $this->m_hasError = true;
         $curlError = !is_resource($this->m_curl) ? "" : curl_error($this->m_curl);
         $this->m_errorMessage = CString::isEmpty($curlError) ? "cURL initialization failed." : $curlError;
         $this->finalize();
         return;
     }
 }