/** * Perform request using Microsoft_Http_Client channel * * @param string $path Path * @param string $queryString Query string * @param string $httpVerb HTTP verb the request will use * @param array $headers x-ms headers to add * @param boolean $forTableStorage Is the request for table storage? * @param mixed $rawData Optional RAW HTTP data to be sent over the wire * @param string $resourceType Resource type * @param string $requiredPermission Required permission * @return Microsoft_Http_Response */ protected function _performRequest($path = '/', $queryString = '', $httpVerb = Microsoft_Http_Client::GET, $headers = array(), $forTableStorage = false, $rawData = null, $resourceType = Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ) { // Clean path if (strpos($path, '/') !== 0) { $path = '/' . $path; } // Clean headers if (is_null($headers)) { $headers = array(); } // Ensure cUrl will also work correctly: // - disable Content-Type if required // - disable Expect: 100 Continue if (!isset($headers["Content-Type"])) { $headers["Content-Type"] = ''; } $headers["Expect"] = ''; // Add version header $headers['x-ms-version'] = $this->_apiVersion; // URL encoding $path = self::urlencode($path); $queryString = self::urlencode($queryString); // Generate URL and sign request $requestUrl = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission); $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission, $rawData); // Prepare request $this->_httpClientChannel->resetParameters(true); $this->_httpClientChannel->setUri($requestUrl); $this->_httpClientChannel->setHeaders($requestHeaders); $this->_httpClientChannel->setRawData($rawData); // Execute request $response = $this->_retryPolicy->execute(array($this->_httpClientChannel, 'request'), array($httpVerb)); return $response; }
/** * Perform request using Microsoft_Http_Client channel * * @param string $path Path * @param array $query Query parameters * @param string $httpVerb HTTP verb the request will use * @param array $headers x-ms headers to add * @param mixed $rawData Optional RAW HTTP data to be sent over the wire * @return Microsoft_Http_Response */ protected function _performRequest($path = '/', $query = array(), $httpVerb = Microsoft_Http_Client::GET, $headers = array(), $rawData = null) { // Clean path if (strpos($path, '/') !== 0) { $path = '/' . $path; } // Clean headers if (is_null($headers)) { $headers = array(); } // Ensure cUrl will also work correctly: // - disable Content-Type if required // - disable Expect: 100 Continue if (!isset($headers["Content-Type"])) { $headers["Content-Type"] = ''; } //$headers["Expect"] = ''; // Add version header $headers['x-ms-version'] = $this->_apiVersion; // Generate URL and sign request $requestUrl = $this->getBaseUrl() . rawurlencode($path); $requestHeaders = $headers; if (count($query) > 0) { $queryString = ''; foreach ($query as $key => $value) { $queryString .= ($queryString ? '&' : '?') . rawurlencode($key) . '=' . rawurlencode($value); } $requestUrl .= $queryString; } // Prepare request $this->_httpClientChannel->resetParameters(true); $this->_httpClientChannel->setUri($requestUrl); $this->_httpClientChannel->setHeaders($requestHeaders); $this->_httpClientChannel->setRawData($rawData); // Execute request $response = $this->_retryPolicy->execute(array($this->_httpClientChannel, 'request'), array($httpVerb)); // Store request id $this->_lastRequestId = $response->getHeader('x-ms-request-id'); return $response; }
/** * Send request to the proxy server * * @param string $method * @param Microsoft_Uri_Http $uri * @param string $http_ver * @param array $headers * @param string $body * @return string Request as string */ public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '') { // If no proxy is set, fall back to default Socket adapter if (!$this->config['proxy_host']) { return parent::write($method, $uri, $http_ver, $headers, $body); } // Make sure we're properly connected if (!$this->socket) { require_once 'Microsoft/Http/Client/Adapter/Exception.php'; throw new Microsoft_Http_Client_Adapter_Exception("Trying to write but we are not connected"); } $host = $this->config['proxy_host']; $port = $this->config['proxy_port']; if ($this->connected_to[0] != "tcp://{$host}" || $this->connected_to[1] != $port) { require_once 'Microsoft/Http/Client/Adapter/Exception.php'; throw new Microsoft_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong proxy server"); } // Add Proxy-Authorization header if ($this->config['proxy_user'] && !isset($headers['proxy-authorization'])) { $headers['proxy-authorization'] = Microsoft_Http_Client::encodeAuthHeader($this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']); } // if we are proxying HTTPS, preform CONNECT handshake with the proxy if ($uri->getScheme() == 'https' && !$this->negotiated) { $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers); $this->negotiated = true; } // Save request method for later $this->method = $method; // Build request headers if ($this->negotiated) { $path = $uri->getPath(); if ($uri->getQuery()) { $path .= '?' . $uri->getQuery(); } $request = "{$method} {$path} HTTP/{$http_ver}\r\n"; } else { $request = "{$method} {$uri} HTTP/{$http_ver}\r\n"; } // Add all headers to the request string foreach ($headers as $k => $v) { if (is_string($k)) { $v = "{$k}: {$v}"; } $request .= "{$v}\r\n"; } // Add the request body $request .= "\r\n" . $body; // Send the request if (!@fwrite($this->socket, $request)) { require_once 'Microsoft/Http/Client/Adapter/Exception.php'; throw new Microsoft_Http_Client_Adapter_Exception("Error writing request to proxy server"); } return $request; }
/** * Attempt to detect the MIME type of a file using available extensions * * This method will try to detect the MIME type of a file. If the fileinfo * extension is available, it will be used. If not, the mime_magic * extension which is deprected but is still available in many PHP setups * will be tried. * * If neither extension is available, the default application/octet-stream * MIME type will be returned * * @param string $file File path * @return string MIME type */ protected function _detectFileMimeType($file) { $type = null; // First try with fileinfo functions if (function_exists('finfo_open')) { if (self::$_fileInfoDb === null) { self::$_fileInfoDb = @finfo_open(FILEINFO_MIME); } if (self::$_fileInfoDb) { $type = finfo_file(self::$_fileInfoDb, $file); } } elseif (function_exists('mime_content_type')) { $type = mime_content_type($file); } // Fallback to the default application/octet-stream if (!$type) { $type = 'application/octet-stream'; } return $type; }