From 27970d5667ef069d5a3e9d21a8596f00f5605933 Mon Sep 17 00:00:00 2001 From: Marco Palladino Date: Tue, 5 Jun 2012 16:46:59 +0200 Subject: [PATCH] fixes --- .gitignore | 1 + main/mashape/MashapeClient.php | 0 .../mashape/exceptions/ExceptionConstants.php | 0 .../exceptions/MashapeClientException.php | 0 main/mashape/http/AuthUtil.php | 8 +++- main/mashape/http/Chunked.php | 46 +++++++++++++++++++ main/mashape/http/HttpClient.php | 20 +++++--- main/mashape/http/HttpMethod.php | 0 main/mashape/http/UrlUtils.php | 5 +- main/mashape/json/Json.php | 0 main/mashape/json/Services_JSON.php | 0 11 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 .gitignore mode change 100644 => 100755 main/mashape/MashapeClient.php mode change 100644 => 100755 main/mashape/exceptions/ExceptionConstants.php mode change 100644 => 100755 main/mashape/exceptions/MashapeClientException.php mode change 100644 => 100755 main/mashape/http/AuthUtil.php create mode 100755 main/mashape/http/Chunked.php mode change 100644 => 100755 main/mashape/http/HttpClient.php mode change 100644 => 100755 main/mashape/http/HttpMethod.php mode change 100644 => 100755 main/mashape/http/UrlUtils.php mode change 100644 => 100755 main/mashape/json/Json.php mode change 100644 => 100755 main/mashape/json/Services_JSON.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/main/mashape/MashapeClient.php b/main/mashape/MashapeClient.php old mode 100644 new mode 100755 diff --git a/main/mashape/exceptions/ExceptionConstants.php b/main/mashape/exceptions/ExceptionConstants.php old mode 100644 new mode 100755 diff --git a/main/mashape/exceptions/MashapeClientException.php b/main/mashape/exceptions/MashapeClientException.php old mode 100644 new mode 100755 diff --git a/main/mashape/http/AuthUtil.php b/main/mashape/http/AuthUtil.php old mode 100644 new mode 100755 index 112a761..3b6a928 --- a/main/mashape/http/AuthUtil.php +++ b/main/mashape/http/AuthUtil.php @@ -27,8 +27,12 @@ class AuthUtil { public static function generateAuthenticationHeader($publicKey, $privateKey) { - $hash = hash_hmac("sha1", $publicKey, $privateKey); - $header = "Proxy-Authorization: " . base64_encode($publicKey . ":" . $hash) . "\r\n"; + $header = ""; + if (!($publicKey == null || $privateKey == null)) { + $uuid = uniqid(); + $hash = hash_hmac("sha1", $uuid, $privateKey); + $header = "X-Mashape-Authorization: " . base64_encode($publicKey . ":" . $hash . $uuid) . "\r\n"; + } return $header; } diff --git a/main/mashape/http/Chunked.php b/main/mashape/http/Chunked.php new file mode 100755 index 0000000..24df1c7 --- /dev/null +++ b/main/mashape/http/Chunked.php @@ -0,0 +1,46 @@ + diff --git a/main/mashape/http/HttpClient.php b/main/mashape/http/HttpClient.php old mode 100644 new mode 100755 index c909042..e8dde94 --- a/main/mashape/http/HttpClient.php +++ b/main/mashape/http/HttpClient.php @@ -25,6 +25,7 @@ */ require_once(dirname(__FILE__) . "/../json/Json.php"); +require_once(dirname(__FILE__) . "/Chunked.php"); require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php"); require_once(dirname(__FILE__) . "/HttpMethod.php"); require_once(dirname(__FILE__) . "/UrlUtils.php"); @@ -32,7 +33,7 @@ require_once(dirname(__FILE__) . "/AuthUtil.php"); class HttpClient { - public static function doRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey) { + public static function doRequest($httpMethod, $url, $parameters, $publicKey, $privateKey, $encodeJson = true) { if (!($httpMethod == HttpMethod::DELETE || $httpMethod == HttpMethod::GET || $httpMethod == HttpMethod::POST || $httpMethod == HttpMethod::PUT)) { @@ -41,25 +42,32 @@ class HttpClient { UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false); - $response = self::execRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey); + $response = self::execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey); + if (!$encodeJson) { + return $response; + } + $jsonResponse = json_decode($response); if (empty($jsonResponse)) { - throw new MashapeClientException(sprintf(EXCEPTION_JSONDECODE_REQUEST, $response), EXCEPTION_SYSTEM_ERROR_CODE); + // It may be a chunked response + $jsonResponse = json_decode(http_chunked_decode($response)); + if (empty($jsonResponse)) { + throw new MashapeClientException(sprintf(EXCEPTION_JSONDECODE_REQUEST, $response), EXCEPTION_SYSTEM_ERROR_CODE); + } } return $jsonResponse; - } - private static function execRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey) { + private static function execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey) { $data = null; if ($httpMethod != HttpMethod::GET) { $url = self::removeQueryString($url); $data = http_build_query($parameters); } - $headers = ($mashapeAuthentication) ? AuthUtil::generateAuthenticationHeader($publicKey, $privateKey) : ""; + $headers = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey); $headers .= UrlUtils::generateClientHeaders(); $opts = array('http' => diff --git a/main/mashape/http/HttpMethod.php b/main/mashape/http/HttpMethod.php old mode 100644 new mode 100755 diff --git a/main/mashape/http/UrlUtils.php b/main/mashape/http/UrlUtils.php old mode 100644 new mode 100755 index 653aaa2..d30946c --- a/main/mashape/http/UrlUtils.php +++ b/main/mashape/http/UrlUtils.php @@ -24,6 +24,9 @@ * */ +define("CLIENT_LIBRARY_LANGUAGE", "PHP"); +define("CLIENT_LIBRARY_VERSION", "V04"); + define("PLACEHOLDER_REGEX", "/\{([\w\.]+)\}/"); class UrlUtils { @@ -91,7 +94,7 @@ class UrlUtils { } public static function generateClientHeaders() { - $headers = "User-Agent: mashape-php/1.0: " . "\r\n"; + $headers = "X-Mashape-Language: " . CLIENT_LIBRARY_LANGUAGE . "\r\n" . "X-Mashape-Version: " . CLIENT_LIBRARY_VERSION . "\r\n"; return $headers; } diff --git a/main/mashape/json/Json.php b/main/mashape/json/Json.php old mode 100644 new mode 100755 diff --git a/main/mashape/json/Services_JSON.php b/main/mashape/json/Services_JSON.php old mode 100644 new mode 100755