diff --git a/src/Parse/HttpClients/ParseStream.php b/src/Parse/HttpClients/ParseStream.php index 1d4faa0f..dba16607 100644 --- a/src/Parse/HttpClients/ParseStream.php +++ b/src/Parse/HttpClients/ParseStream.php @@ -62,6 +62,8 @@ public function get($url) try { // get our response $response = file_get_contents($url, false, $this->stream); + $this->errorMessage = null; + $this->errorCode = null; } catch (\Exception $e) { // set our error message/code and return false $this->errorMessage = $e->getMessage(); diff --git a/src/Parse/HttpClients/ParseStreamHttpClient.php b/src/Parse/HttpClients/ParseStreamHttpClient.php index 8ac30fdf..4c9b29aa 100644 --- a/src/Parse/HttpClients/ParseStreamHttpClient.php +++ b/src/Parse/HttpClients/ParseStreamHttpClient.php @@ -169,8 +169,7 @@ public function setup() $this->options['ssl'] = array( 'verify_peer' => true, 'verify_peer_name' => true, - 'allow_self_signed' => true, // All root certificates are self-signed - 'follow_location' => 1 + 'allow_self_signed' => true // All root certificates are self-signed ); } @@ -233,11 +232,11 @@ public function send($url, $method = 'GET', $data = array()) // get our response headers $rawHeaders = $this->parseStream->getResponseHeaders(); - if ($response === false || !$rawHeaders) { - // set an error and code - $this->streamErrorMessage = $this->parseStream->getErrorMessage(); - $this->streamErrorCode = $this->parseStream->getErrorCode(); - } else { + // set any error and code + $this->streamErrorMessage = $this->parseStream->getErrorMessage(); + $this->streamErrorCode = $this->parseStream->getErrorCode(); + + if ($response !== false && $rawHeaders) { // set our response headers $this->responseHeaders = self::formatHeaders($rawHeaders); diff --git a/src/Parse/ParseFile.php b/src/Parse/ParseFile.php index 41c3c9ea..de9a7678 100755 --- a/src/Parse/ParseFile.php +++ b/src/Parse/ParseFile.php @@ -181,7 +181,7 @@ public static function _createFromServer($name, $url) /** * Encode to associative array representation. * - * @return string + * @return array */ public function _encode() { @@ -242,21 +242,18 @@ private function upload($useMasterKey = false) */ private function download() { - $rest = curl_init(); - curl_setopt($rest, CURLOPT_URL, $this->url); - curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($rest, CURLOPT_BINARYTRANSFER, 1); - $response = curl_exec($rest); - if (curl_errno($rest)) { - throw new ParseException(curl_error($rest), curl_errno($rest)); + $httpClient = ParseClient::getHttpClient(); + $httpClient->setup(); + $response = $httpClient->send($this->url); + if ($httpClient->getErrorCode()) { + throw new ParseException($httpClient->getErrorMessage(), $httpClient->getErrorCode()); } - $httpStatus = curl_getinfo($rest, CURLINFO_HTTP_CODE); + $httpStatus = $httpClient->getResponseStatusCode(); if ($httpStatus > 399) { throw new ParseException('Download failed, file may have been deleted.', $httpStatus); } - $this->mimeType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE); + $this->mimeType = $httpClient->getResponseContentType(); $this->data = $response; - curl_close($rest); return $response; } diff --git a/tests/Parse/ParseFileTest.php b/tests/Parse/ParseFileTest.php index 58174c09..3bdf44ca 100644 --- a/tests/Parse/ParseFileTest.php +++ b/tests/Parse/ParseFileTest.php @@ -2,7 +2,6 @@ namespace Parse\Test; -use Parse\ParseException; use Parse\ParseFile; use Parse\ParseObject; use Parse\ParseQuery; @@ -93,7 +92,16 @@ public function testParsefileDeleteUnsaved() */ public function testParseFileDownloadBadURL() { - $this->setExpectedException('\Parse\ParseException', '', 6); + global $USE_CLIENT_STREAM; + + if (!isset($USE_CLIENT_STREAM)) { + // curl exception expectation + $this->setExpectedException('\Parse\ParseException', '', 6); + } else { + // stream exception expectation + $this->setExpectedException('\Parse\ParseException', '', 2); + } + $file = ParseFile::_createFromServer('file.txt', 'http://404.example.com'); $file->getData(); }