Skip to content

Commit cbb6301

Browse files
author
Benjamin Wilson Friedman
authored
ParseFile use HttpClient for download (#341)
* Make ParseFile use the current HttpClient for downlaod * Update so stream client properly clears errors from request to request * Removed commented code and lint * Removed unnecessary uses
1 parent 7ff2404 commit cbb6301

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

src/Parse/HttpClients/ParseStream.php

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public function get($url)
6262
try {
6363
// get our response
6464
$response = file_get_contents($url, false, $this->stream);
65+
$this->errorMessage = null;
66+
$this->errorCode = null;
6567
} catch (\Exception $e) {
6668
// set our error message/code and return false
6769
$this->errorMessage = $e->getMessage();

src/Parse/HttpClients/ParseStreamHttpClient.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ public function setup()
169169
$this->options['ssl'] = array(
170170
'verify_peer' => true,
171171
'verify_peer_name' => true,
172-
'allow_self_signed' => true, // All root certificates are self-signed
173-
'follow_location' => 1
172+
'allow_self_signed' => true // All root certificates are self-signed
174173
);
175174
}
176175

@@ -233,11 +232,11 @@ public function send($url, $method = 'GET', $data = array())
233232
// get our response headers
234233
$rawHeaders = $this->parseStream->getResponseHeaders();
235234

236-
if ($response === false || !$rawHeaders) {
237-
// set an error and code
238-
$this->streamErrorMessage = $this->parseStream->getErrorMessage();
239-
$this->streamErrorCode = $this->parseStream->getErrorCode();
240-
} else {
235+
// set any error and code
236+
$this->streamErrorMessage = $this->parseStream->getErrorMessage();
237+
$this->streamErrorCode = $this->parseStream->getErrorCode();
238+
239+
if ($response !== false && $rawHeaders) {
241240
// set our response headers
242241
$this->responseHeaders = self::formatHeaders($rawHeaders);
243242

src/Parse/ParseFile.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static function _createFromServer($name, $url)
181181
/**
182182
* Encode to associative array representation.
183183
*
184-
* @return string
184+
* @return array
185185
*/
186186
public function _encode()
187187
{
@@ -242,21 +242,18 @@ private function upload($useMasterKey = false)
242242
*/
243243
private function download()
244244
{
245-
$rest = curl_init();
246-
curl_setopt($rest, CURLOPT_URL, $this->url);
247-
curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
248-
curl_setopt($rest, CURLOPT_BINARYTRANSFER, 1);
249-
$response = curl_exec($rest);
250-
if (curl_errno($rest)) {
251-
throw new ParseException(curl_error($rest), curl_errno($rest));
245+
$httpClient = ParseClient::getHttpClient();
246+
$httpClient->setup();
247+
$response = $httpClient->send($this->url);
248+
if ($httpClient->getErrorCode()) {
249+
throw new ParseException($httpClient->getErrorMessage(), $httpClient->getErrorCode());
252250
}
253-
$httpStatus = curl_getinfo($rest, CURLINFO_HTTP_CODE);
251+
$httpStatus = $httpClient->getResponseStatusCode();
254252
if ($httpStatus > 399) {
255253
throw new ParseException('Download failed, file may have been deleted.', $httpStatus);
256254
}
257-
$this->mimeType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE);
255+
$this->mimeType = $httpClient->getResponseContentType();
258256
$this->data = $response;
259-
curl_close($rest);
260257

261258
return $response;
262259
}

tests/Parse/ParseFileTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Parse\Test;
44

5-
use Parse\ParseException;
65
use Parse\ParseFile;
76
use Parse\ParseObject;
87
use Parse\ParseQuery;
@@ -93,7 +92,16 @@ public function testParsefileDeleteUnsaved()
9392
*/
9493
public function testParseFileDownloadBadURL()
9594
{
96-
$this->setExpectedException('\Parse\ParseException', '', 6);
95+
global $USE_CLIENT_STREAM;
96+
97+
if (!isset($USE_CLIENT_STREAM)) {
98+
// curl exception expectation
99+
$this->setExpectedException('\Parse\ParseException', '', 6);
100+
} else {
101+
// stream exception expectation
102+
$this->setExpectedException('\Parse\ParseException', '', 2);
103+
}
104+
97105
$file = ParseFile::_createFromServer('file.txt', 'http://404.example.com');
98106
$file->getData();
99107
}

0 commit comments

Comments
 (0)