diff options
author | emkael <emkael@tlen.pl> | 2018-04-26 01:00:12 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2018-04-26 01:00:12 +0200 |
commit | 4b8a9a5189a625bf99fedec7fd31f6e146410a14 (patch) | |
tree | a29759d5f0f0c29f07c89c59f6b5cf38407d1470 /lib/facebook-graph-sdk/src/Facebook/HttpClients | |
parent | ab82beef33f4c196383a81213dcdff4ebe9447ae (diff) |
Update FB API library
Diffstat (limited to 'lib/facebook-graph-sdk/src/Facebook/HttpClients')
7 files changed, 113 insertions, 61 deletions
diff --git a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurl.php b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurl.php index e5d124a..28e4ba5 100644 --- a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurl.php +++ b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurl.php @@ -1,6 +1,6 @@ <?php /** - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source code or binary diff --git a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php index 955ac06..9516cc8 100644 --- a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php +++ b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php @@ -1,6 +1,6 @@ <?php /** - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source code or binary @@ -54,16 +54,6 @@ class FacebookCurlHttpClient implements FacebookHttpClientInterface protected $facebookCurl; /** - * @const Curl Version which is unaffected by the proxy header length error. - */ - const CURL_PROXY_QUIRK_VER = 0x071E00; - - /** - * @const "Connection Established" header text - */ - const CONNECTION_ESTABLISHED = "HTTP/1.0 200 Connection established\r\n\r\n"; - - /** * @param FacebookCurl|null Procedural curl as object */ public function __construct(FacebookCurl $facebookCurl = null) @@ -108,7 +98,7 @@ class FacebookCurlHttpClient implements FacebookHttpClientInterface CURLOPT_URL => $url, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => $timeOut, - CURLOPT_RETURNTRANSFER => true, // Follow 301 redirects + CURLOPT_RETURNTRANSFER => true, // Return response as string CURLOPT_HEADER => true, // Enable header processing CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_SSL_VERIFYPEER => true, @@ -164,47 +154,10 @@ class FacebookCurlHttpClient implements FacebookHttpClientInterface */ public function extractResponseHeadersAndBody() { - $headerSize = $this->getHeaderSize(); - - $rawHeaders = mb_substr($this->rawResponse, 0, $headerSize); - $rawBody = mb_substr($this->rawResponse, $headerSize); + $parts = explode("\r\n\r\n", $this->rawResponse); + $rawBody = array_pop($parts); + $rawHeaders = implode("\r\n\r\n", $parts); return [trim($rawHeaders), trim($rawBody)]; } - - /** - * Return proper header size - * - * @return integer - */ - private function getHeaderSize() - { - $headerSize = $this->facebookCurl->getinfo(CURLINFO_HEADER_SIZE); - // This corrects a Curl bug where header size does not account - // for additional Proxy headers. - if ($this->needsCurlProxyFix()) { - // Additional way to calculate the request body size. - if (preg_match('/Content-Length: (\d+)/', $this->rawResponse, $m)) { - $headerSize = mb_strlen($this->rawResponse) - $m[1]; - } elseif (stripos($this->rawResponse, self::CONNECTION_ESTABLISHED) !== false) { - $headerSize += mb_strlen(self::CONNECTION_ESTABLISHED); - } - } - - return $headerSize; - } - - /** - * Detect versions of Curl which report incorrect header lengths when - * using Proxies. - * - * @return boolean - */ - private function needsCurlProxyFix() - { - $ver = $this->facebookCurl->version(); - $version = $ver['version_number']; - - return $version < self::CURL_PROXY_QUIRK_VER; - } } diff --git a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookGuzzleHttpClient.php b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookGuzzleHttpClient.php index 6f2a1c6..8feb7cb 100644 --- a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookGuzzleHttpClient.php +++ b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookGuzzleHttpClient.php @@ -1,6 +1,6 @@ <?php /** - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source code or binary diff --git a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookHttpClientInterface.php b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookHttpClientInterface.php index 0029bc0..1fbf953 100644 --- a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookHttpClientInterface.php +++ b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookHttpClientInterface.php @@ -1,6 +1,6 @@ <?php /** - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source code or binary diff --git a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStream.php b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStream.php index 95aa22e..3f39988 100644 --- a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStream.php +++ b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStream.php @@ -1,6 +1,6 @@ <?php /** - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source code or binary @@ -41,7 +41,7 @@ class FacebookStream /** * @var array Response headers from the stream wrapper */ - protected $responseHeaders; + protected $responseHeaders = []; /** * Make a new context stream reference instance @@ -56,7 +56,7 @@ class FacebookStream /** * The response headers from the stream wrapper * - * @return array|null + * @return array */ public function getResponseHeaders() { @@ -73,7 +73,7 @@ class FacebookStream public function fileGetContents($url) { $rawResponse = file_get_contents($url, false, $this->stream); - $this->responseHeaders = $http_response_header; + $this->responseHeaders = $http_response_header ?: []; return $rawResponse; } diff --git a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStreamHttpClient.php b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStreamHttpClient.php index b157514..1cdfd53 100644 --- a/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStreamHttpClient.php +++ b/lib/facebook-graph-sdk/src/Facebook/HttpClients/FacebookStreamHttpClient.php @@ -1,6 +1,6 @@ <?php /** - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source code or binary @@ -66,7 +66,7 @@ class FacebookStreamHttpClient implements FacebookHttpClientInterface $rawBody = $this->facebookStream->fileGetContents($url); $rawHeaders = $this->facebookStream->getResponseHeaders(); - if ($rawBody === false || !$rawHeaders) { + if ($rawBody === false || empty($rawHeaders)) { throw new FacebookSDKException('Stream returned an empty response', 660); } diff --git a/lib/facebook-graph-sdk/src/Facebook/HttpClients/HttpClientsFactory.php b/lib/facebook-graph-sdk/src/Facebook/HttpClients/HttpClientsFactory.php new file mode 100644 index 0000000..d9f2a8d --- /dev/null +++ b/lib/facebook-graph-sdk/src/Facebook/HttpClients/HttpClientsFactory.php @@ -0,0 +1,99 @@ +<?php +/** + * Copyright 2017 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\HttpClients; + +use GuzzleHttp\Client; +use InvalidArgumentException; +use Exception; + +class HttpClientsFactory +{ + private function __construct() + { + // a factory constructor should never be invoked + } + + /** + * HTTP client generation. + * + * @param FacebookHttpClientInterface|Client|string|null $handler + * + * @throws Exception If the cURL extension or the Guzzle client aren't available (if required). + * @throws InvalidArgumentException If the http client handler isn't "curl", "stream", "guzzle", or an instance of Facebook\HttpClients\FacebookHttpClientInterface. + * + * @return FacebookHttpClientInterface + */ + public static function createHttpClient($handler) + { + if (!$handler) { + return self::detectDefaultClient(); + } + + if ($handler instanceof FacebookHttpClientInterface) { + return $handler; + } + + if ('stream' === $handler) { + return new FacebookStreamHttpClient(); + } + if ('curl' === $handler) { + if (!extension_loaded('curl')) { + throw new Exception('The cURL extension must be loaded in order to use the "curl" handler.'); + } + + return new FacebookCurlHttpClient(); + } + + if ('guzzle' === $handler && !class_exists('GuzzleHttp\Client')) { + throw new Exception('The Guzzle HTTP client must be included in order to use the "guzzle" handler.'); + } + + if ($handler instanceof Client) { + return new FacebookGuzzleHttpClient($handler); + } + if ('guzzle' === $handler) { + return new FacebookGuzzleHttpClient(); + } + + throw new InvalidArgumentException('The http client handler must be set to "curl", "stream", "guzzle", be an instance of GuzzleHttp\Client or an instance of Facebook\HttpClients\FacebookHttpClientInterface'); + } + + /** + * Detect default HTTP client. + * + * @return FacebookHttpClientInterface + */ + private static function detectDefaultClient() + { + if (extension_loaded('curl')) { + return new FacebookCurlHttpClient(); + } + + if (class_exists('GuzzleHttp\Client')) { + return new FacebookGuzzleHttpClient(); + } + + return new FacebookStreamHttpClient(); + } +} |