Skip to content

Commit fef34b6

Browse files
author
Jippe Holwerda
committed
Using http status code to determine successful API connection instead of error text.
1 parent 14a6ad7 commit fef34b6

4 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/class-tiny-compress-curl.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ protected function shrink($input) {
5656
}
5757

5858
$header_size = curl_getinfo($request, CURLINFO_HEADER_SIZE);
59+
$status_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
5960
$headers = self::parse_headers(substr($response, 0, $header_size));
6061
curl_close($request);
6162

62-
return array(self::decode(substr($response, $header_size)), $headers);
63+
return array(self::decode(substr($response, $header_size)), $headers, $status_code);
6364
}
6465

6566
protected function output_options($url) {

src/class-tiny-compress-fopen.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ protected function shrink($input) {
4141
$context = stream_context_create($this->shrink_options($input));
4242
$request = @fopen(Tiny_Config::URL, 'r', false, $context);
4343

44+
$status_code = null;
45+
if ($http_response_header && count($http_response_header) > 0) {
46+
$http_code_values = explode(' ', $http_response_header[0]);
47+
if (count($http_code_values) > 1) {
48+
$status_code = intval($http_code_values[1]);
49+
}
50+
}
51+
4452
if (!$request) {
53+
$headers = self::parse_headers($http_response_header);
4554
return array(array(
4655
'error' => 'FopenError',
47-
'message' => 'Could not compress, enable cURL for detailed error'
48-
), null
56+
'message' => 'Could not compress, enable cURL for detailed error',
57+
), $headers, $status_code
4958
);
5059
}
5160

@@ -54,7 +63,7 @@ protected function shrink($input) {
5463
$headers = self::parse_headers($meta_data['wrapper_data']);
5564
fclose($request);
5665

57-
return array(self::decode($response), $headers);
66+
return array(self::decode($response), $headers, $status_code);
5867
}
5968

6069
protected function output_options() {
@@ -82,8 +91,4 @@ protected function output($url) {
8291

8392
return $response;
8493
}
85-
86-
public function get_status(&$details) {
87-
return null;
88-
}
8994
}

src/class-tiny-compress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ abstract protected function shrink($input);
4444
abstract protected function output($url);
4545

4646
public function get_status(&$details) {
47-
list($details, $headers) = $this->shrink(null);
47+
list($details, $headers, $status_code) = $this->shrink(null);
4848

4949
$this->call_after_compress_callback($details, $headers);
50-
if (!isset($details['error']) || $details["error"] == 'InputMissing' || $details["error"] == 'TooManyRequests') {
50+
if ($status_code >= 400 && $status_code < 500 && $status_code != 401) {
5151
return true;
5252
} else {
5353
return false;

test/integration/CompressIntegrationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,4 @@ public function testIncorrectJsonButton() {
8080
self::$driver->wait(2)->until(WebDriverExpectedCondition::textToBePresentInElement(
8181
WebDriverBy::cssSelector('td.tiny-compress-images'), 'JSON: Syntax error [4]'));
8282
}
83-
8483
}

0 commit comments

Comments
 (0)