From 07729c42ec08eeb30e00956f3299ab7b754b0cc5 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Wed, 19 Jul 2017 10:19:07 -0700 Subject: [PATCH 1/4] Make ParseFile use the current HttpClient for downlaod --- src/Parse/ParseFile.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Parse/ParseFile.php b/src/Parse/ParseFile.php index 41c3c9ea..bd0fd830 100755 --- a/src/Parse/ParseFile.php +++ b/src/Parse/ParseFile.php @@ -242,6 +242,29 @@ private function upload($useMasterKey = false) */ private function download() { + ////// NEW FASHION + + $httpClient = ParseClient::getHttpClient(); + $httpClient->setup(); + $response = $httpClient->send($this->url); + if ($httpClient->getErrorCode()) { + throw new ParseException($httpClient->getErrorMessage(), $httpClient->getErrorCode()); + } + $httpStatus = $httpClient->getResponseStatusCode(); + if($httpStatus > 399) { + throw new ParseException('Download failed, file may have been deleted.', $httpStatus); + } + $this->mimeType = $httpClient->getResponseContentType(); + $this->data = $response; + + return $response; + + + + + ///// OLD FASHIONED + + /* $rest = curl_init(); curl_setopt($rest, CURLOPT_URL, $this->url); curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1); @@ -259,6 +282,7 @@ private function download() curl_close($rest); return $response; + */ } /** From 91ee511d695ae67f10a9e5ad550b9bd2b8f973eb Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Wed, 19 Jul 2017 11:20:12 -0700 Subject: [PATCH 2/4] Update so stream client properly clears errors from request to request --- src/Parse/HttpClients/ParseStream.php | 3 +++ src/Parse/HttpClients/ParseStreamHttpClient.php | 13 ++++++------- src/Parse/ParseFile.php | 2 +- tests/Parse/ParseFileTest.php | 14 +++++++++++++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Parse/HttpClients/ParseStream.php b/src/Parse/HttpClients/ParseStream.php index 1d4faa0f..f9ce657d 100644 --- a/src/Parse/HttpClients/ParseStream.php +++ b/src/Parse/HttpClients/ParseStream.php @@ -62,6 +62,9 @@ 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 bd0fd830..a4a2c6b3 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() { diff --git a/tests/Parse/ParseFileTest.php b/tests/Parse/ParseFileTest.php index 58174c09..7c7118ee 100644 --- a/tests/Parse/ParseFileTest.php +++ b/tests/Parse/ParseFileTest.php @@ -2,6 +2,8 @@ namespace Parse\Test; +use Parse\HttpClients\ParseStreamHttpClient; +use Parse\ParseClient; use Parse\ParseException; use Parse\ParseFile; use Parse\ParseObject; @@ -93,9 +95,19 @@ 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(); + } /** From 77e7a24c1d0e51dc475009b7ec75cefe0101e15f Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Wed, 19 Jul 2017 11:23:58 -0700 Subject: [PATCH 3/4] Removed commented code and lint --- src/Parse/HttpClients/ParseStream.php | 1 - src/Parse/ParseFile.php | 29 +-------------------------- tests/Parse/ParseFileTest.php | 3 +-- 3 files changed, 2 insertions(+), 31 deletions(-) diff --git a/src/Parse/HttpClients/ParseStream.php b/src/Parse/HttpClients/ParseStream.php index f9ce657d..dba16607 100644 --- a/src/Parse/HttpClients/ParseStream.php +++ b/src/Parse/HttpClients/ParseStream.php @@ -64,7 +64,6 @@ public function get($url) $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/ParseFile.php b/src/Parse/ParseFile.php index a4a2c6b3..de9a7678 100755 --- a/src/Parse/ParseFile.php +++ b/src/Parse/ParseFile.php @@ -242,8 +242,6 @@ private function upload($useMasterKey = false) */ private function download() { - ////// NEW FASHION - $httpClient = ParseClient::getHttpClient(); $httpClient->setup(); $response = $httpClient->send($this->url); @@ -251,38 +249,13 @@ private function download() throw new ParseException($httpClient->getErrorMessage(), $httpClient->getErrorCode()); } $httpStatus = $httpClient->getResponseStatusCode(); - if($httpStatus > 399) { - throw new ParseException('Download failed, file may have been deleted.', $httpStatus); - } - $this->mimeType = $httpClient->getResponseContentType(); - $this->data = $response; - - return $response; - - - - - ///// OLD FASHIONED - - /* - $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)); - } - $httpStatus = curl_getinfo($rest, CURLINFO_HTTP_CODE); 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 7c7118ee..f1fe56f7 100644 --- a/tests/Parse/ParseFileTest.php +++ b/tests/Parse/ParseFileTest.php @@ -97,7 +97,7 @@ public function testParseFileDownloadBadURL() { global $USE_CLIENT_STREAM; - if(!isset($USE_CLIENT_STREAM)) { + if (!isset($USE_CLIENT_STREAM)) { // curl exception expectation $this->setExpectedException('\Parse\ParseException', '', 6); } else { @@ -107,7 +107,6 @@ public function testParseFileDownloadBadURL() $file = ParseFile::_createFromServer('file.txt', 'http://404.example.com'); $file->getData(); - } /** From 38ce6f12c3b7c3d92fd40951b42bce05a7363422 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Wed, 19 Jul 2017 11:58:04 -0700 Subject: [PATCH 4/4] Removed unnecessary uses --- tests/Parse/ParseFileTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Parse/ParseFileTest.php b/tests/Parse/ParseFileTest.php index f1fe56f7..3bdf44ca 100644 --- a/tests/Parse/ParseFileTest.php +++ b/tests/Parse/ParseFileTest.php @@ -2,9 +2,6 @@ namespace Parse\Test; -use Parse\HttpClients\ParseStreamHttpClient; -use Parse\ParseClient; -use Parse\ParseException; use Parse\ParseFile; use Parse\ParseObject; use Parse\ParseQuery;