diff options
Diffstat (limited to '')
4 files changed, 169 insertions, 122 deletions
diff --git a/vendor/maxmind/web-service-common/src/WebService/Client.php b/vendor/maxmind/web-service-common/src/WebService/Client.php index 98e6e3b..1c02a6c 100644 --- a/vendor/maxmind/web-service-common/src/WebService/Client.php +++ b/vendor/maxmind/web-service-common/src/WebService/Client.php @@ -1,5 +1,7 @@ <?php
+declare(strict_types=1);
+
namespace MaxMind\WebService;
use Composer\CaBundle\CaBundle;
@@ -22,16 +24,56 @@ use MaxMind\WebService\Http\RequestFactory; */
class Client
{
- const VERSION = '0.2.0';
+ public const VERSION = '0.2.0';
+ /**
+ * @var string|null
+ */
private $caBundle;
+
+ /**
+ * @var float|null
+ */
private $connectTimeout;
+
+ /**
+ * @var string
+ */
private $host = 'api.maxmind.com';
+
+ /**
+ * @var bool
+ */
+ private $useHttps = true;
+
+ /**
+ * @var RequestFactory
+ */
private $httpRequestFactory;
+
+ /**
+ * @var string
+ */
private $licenseKey;
+
+ /**
+ * @var string|null
+ */
private $proxy;
+
+ /**
+ * @var float|null
+ */
private $timeout;
+
+ /**
+ * @var string
+ */
private $userAgentPrefix;
+
+ /**
+ * @var int
+ */
private $accountId;
/**
@@ -39,6 +81,7 @@ class Client * @param string $licenseKey your MaxMind license key
* @param array $options an array of options. Possible keys:
* * `host` - The host to use when connecting to the web service.
+ * * `useHttps` - A boolean flag for sending the request via https.(True by default)
* * `userAgent` - The prefix of the User-Agent to use in the request.
* * `caBundle` - The bundle of CA root certificates to use in the request.
* * `connectTimeout` - The connect timeout to use for the request.
@@ -47,9 +90,9 @@ class Client * username, and password, e.g., `http://username:password@127.0.0.1:10`.
*/
public function __construct(
- $accountId,
- $licenseKey,
- $options = []
+ int $accountId,
+ string $licenseKey,
+ array $options = []
) {
$this->accountId = $accountId;
$this->licenseKey = $licenseKey;
@@ -61,6 +104,9 @@ class Client if (isset($options['host'])) {
$this->host = $options['host'];
}
+ if (isset($options['useHttps'])) {
+ $this->useHttps = $options['useHttps'];
+ }
if (isset($options['userAgent'])) {
$this->userAgentPrefix = $options['userAgent'] . ' ';
}
@@ -96,9 +142,9 @@ class Client * @throws WebServiceException when some other error occurs. This also
* serves as the base class for the above exceptions.
*
- * @return array The decoded content of a successful response
+ * @return array|null The decoded content of a successful response
*/
- public function post($service, $path, $input)
+ public function post(string $service, string $path, array $input): ?array
{
$requestBody = json_encode($input);
if ($requestBody === false) {
@@ -113,7 +159,7 @@ class Client ['Content-Type: application/json']
);
- list($statusCode, $contentType, $responseBody) = $request->post($requestBody);
+ [$statusCode, $contentType, $responseBody] = $request->post($requestBody);
return $this->handleResponse(
$statusCode,
@@ -124,11 +170,13 @@ class Client );
}
- public function get($service, $path)
+ public function get(string $service, string $path): ?array
{
- $request = $this->createRequest($path);
+ $request = $this->createRequest(
+ $path
+ );
- list($statusCode, $contentType, $responseBody) = $request->get();
+ [$statusCode, $contentType, $responseBody] = $request->get();
return $this->handleResponse(
$statusCode,
@@ -139,15 +187,15 @@ class Client );
}
- private function userAgent()
+ private function userAgent(): string
{
$curlVersion = curl_version();
- return $this->userAgentPrefix . 'MaxMind-WS-API/' . self::VERSION . ' PHP/' . PHP_VERSION .
+ return $this->userAgentPrefix . 'MaxMind-WS-API/' . self::VERSION . ' PHP/' . \PHP_VERSION .
' curl/' . $curlVersion['version'];
}
- private function createRequest($path, $headers = [])
+ private function createRequest(string $path, array $headers = []): Http\Request
{
array_push(
$headers,
@@ -170,11 +218,11 @@ class Client }
/**
- * @param int $statusCode the HTTP status code of the response
- * @param string $contentType the Content-Type of the response
- * @param string $responseBody the response body
- * @param string $service the name of the service
- * @param string $path the path used in the request
+ * @param int $statusCode the HTTP status code of the response
+ * @param string|null $contentType the Content-Type of the response
+ * @param string|null $responseBody the response body
+ * @param string $service the name of the service
+ * @param string $path the path used in the request
*
* @throws AuthenticationException when there is an issue authenticating the
* request
@@ -185,15 +233,15 @@ class Client * @throws WebServiceException when some other error occurs. This also
* serves as the base class for the above exceptions
*
- * @return array The decoded content of a successful response
+ * @return array|null The decoded content of a successful response
*/
private function handleResponse(
- $statusCode,
- $contentType,
- $responseBody,
- $service,
- $path
- ) {
+ int $statusCode,
+ ?string $contentType,
+ ?string $responseBody,
+ string $service,
+ string $path
+ ): ?array {
if ($statusCode >= 400 && $statusCode <= 499) {
$this->handle4xx($statusCode, $contentType, $responseBody, $service, $path);
} elseif ($statusCode >= 500) {
@@ -208,20 +256,26 @@ class Client /**
* @return string describing the JSON error
*/
- private function jsonErrorDescription()
+ private function jsonErrorDescription(): string
{
$errno = json_last_error();
+
switch ($errno) {
- case JSON_ERROR_DEPTH:
+ case \JSON_ERROR_DEPTH:
return 'The maximum stack depth has been exceeded.';
- case JSON_ERROR_STATE_MISMATCH:
+
+ case \JSON_ERROR_STATE_MISMATCH:
return 'Invalid or malformed JSON.';
- case JSON_ERROR_CTRL_CHAR:
+
+ case \JSON_ERROR_CTRL_CHAR:
return 'Control character error.';
- case JSON_ERROR_SYNTAX:
+
+ case \JSON_ERROR_SYNTAX:
return 'Syntax error.';
- case JSON_ERROR_UTF8:
+
+ case \JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters.';
+
default:
return "Other JSON error ($errno).";
}
@@ -232,17 +286,17 @@ class Client *
* @return string the constructed URL
*/
- private function urlFor($path)
+ private function urlFor(string $path): string
{
- return 'https://' . $this->host . $path;
+ return ($this->useHttps ? 'https://' : 'http://') . $this->host . $path;
}
/**
- * @param int $statusCode the HTTP status code
- * @param string $contentType the response content-type
- * @param string $body the response body
- * @param string $service the service name
- * @param string $path the path used in the request
+ * @param int $statusCode the HTTP status code
+ * @param string|null $contentType the response content-type
+ * @param string|null $body the response body
+ * @param string $service the service name
+ * @param string $path the path used in the request
*
* @throws AuthenticationException
* @throws HttpException
@@ -250,20 +304,20 @@ class Client * @throws InvalidRequestException
*/
private function handle4xx(
- $statusCode,
- $contentType,
- $body,
- $service,
- $path
- ) {
- if (\strlen($body) === 0) {
+ int $statusCode,
+ ?string $contentType,
+ ?string $body,
+ string $service,
+ string $path
+ ): void {
+ if ($body === null || $body === '') {
throw new HttpException(
"Received a $statusCode error for $service with no body",
$statusCode,
$this->urlFor($path)
);
}
- if (!strstr($contentType, 'json')) {
+ if ($contentType === null || !strstr($contentType, 'json')) {
throw new HttpException(
"Received a $statusCode error for $service with " .
'the following body: ' . $body,
@@ -311,11 +365,11 @@ class Client * @throws InsufficientFundsException
*/
private function handleWebServiceError(
- $message,
- $code,
- $statusCode,
- $path
- ) {
+ string $message,
+ string $code,
+ int $statusCode,
+ string $path
+ ): void {
switch ($code) {
case 'IP_ADDRESS_NOT_FOUND':
case 'IP_ADDRESS_RESERVED':
@@ -325,6 +379,7 @@ class Client $statusCode,
$this->urlFor($path)
);
+
case 'ACCOUNT_ID_REQUIRED':
case 'ACCOUNT_ID_UNKNOWN':
case 'AUTHORIZATION_INVALID':
@@ -337,6 +392,7 @@ class Client $statusCode,
$this->urlFor($path)
);
+
case 'OUT_OF_QUERIES':
case 'INSUFFICIENT_FUNDS':
throw new InsufficientFundsException(
@@ -345,6 +401,7 @@ class Client $statusCode,
$this->urlFor($path)
);
+
case 'PERMISSION_REQUIRED':
throw new PermissionRequiredException(
$message,
@@ -352,6 +409,7 @@ class Client $statusCode,
$this->urlFor($path)
);
+
default:
throw new InvalidRequestException(
$message,
@@ -369,7 +427,7 @@ class Client *
* @throws HttpException
*/
- private function handle5xx($statusCode, $service, $path)
+ private function handle5xx(int $statusCode, string $service, string $path): void
{
throw new HttpException(
"Received a server error ($statusCode) for $service",
@@ -385,7 +443,7 @@ class Client *
* @throws HttpException
*/
- private function handleUnexpectedStatus($statusCode, $service, $path)
+ private function handleUnexpectedStatus(int $statusCode, string $service, string $path): void
{
throw new HttpException(
'Received an unexpected HTTP status ' .
@@ -396,22 +454,22 @@ class Client }
/**
- * @param int $statusCode the HTTP status code
- * @param string $body the successful request body
- * @param string $service the service name
+ * @param int $statusCode the HTTP status code
+ * @param string|null $body the successful request body
+ * @param string $service the service name
*
* @throws WebServiceException if a response body is included but not
* expected, or is not expected but not
* included, or is expected and included
* but cannot be decoded as JSON
*
- * @return array the decoded request body
+ * @return array|null the decoded request body
*/
- private function handleSuccess($statusCode, $body, $service)
+ private function handleSuccess(int $statusCode, ?string $body, string $service): ?array
{
// A 204 should have no response body
if ($statusCode === 204) {
- if (\strlen($body) !== 0) {
+ if ($body !== null && $body !== '') {
throw new WebServiceException(
"Received a 204 response for $service along with an " .
"unexpected HTTP body: $body"
@@ -422,7 +480,7 @@ class Client }
// A 200 should have a valid JSON body
- if (\strlen($body) === 0) {
+ if ($body === null || $body === '') {
throw new WebServiceException(
"Received a 200 response for $service but did not " .
'receive a HTTP body.'
@@ -441,7 +499,7 @@ class Client return $decodedContent;
}
- private function getCaBundle()
+ private function getCaBundle(): ?string
{
$curlVersion = curl_version();
diff --git a/vendor/maxmind/web-service-common/src/WebService/Http/CurlRequest.php b/vendor/maxmind/web-service-common/src/WebService/Http/CurlRequest.php index 501b2af..4b9a70b 100644 --- a/vendor/maxmind/web-service-common/src/WebService/Http/CurlRequest.php +++ b/vendor/maxmind/web-service-common/src/WebService/Http/CurlRequest.php @@ -1,5 +1,7 @@ <?php
+declare(strict_types=1);
+
namespace MaxMind\WebService\Http;
use MaxMind\Exception\HttpException;
@@ -12,7 +14,7 @@ use MaxMind\Exception\HttpException; class CurlRequest implements Request
{
/**
- * @var resource
+ * @var \CurlHandle
*/
private $ch;
@@ -26,11 +28,7 @@ class CurlRequest implements Request */
private $options;
- /**
- * @param string $url
- * @param array $options
- */
- public function __construct($url, $options)
+ public function __construct(string $url, array $options)
{
$this->url = $url;
$this->options = $options;
@@ -38,69 +36,65 @@ class CurlRequest implements Request }
/**
- * @param string $body
- *
* @throws HttpException
- *
- * @return array
*/
- public function post($body)
+ public function post(string $body): array
{
$curl = $this->createCurl();
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
+ curl_setopt($curl, \CURLOPT_POST, true);
+ curl_setopt($curl, \CURLOPT_POSTFIELDS, $body);
return $this->execute($curl);
}
- public function get()
+ public function get(): array
{
$curl = $this->createCurl();
- curl_setopt($curl, CURLOPT_HTTPGET, true);
+ curl_setopt($curl, \CURLOPT_HTTPGET, true);
return $this->execute($curl);
}
/**
- * @return resource
+ * @return \CurlHandle
*/
private function createCurl()
{
curl_reset($this->ch);
$opts = [];
- $opts[CURLOPT_URL] = $this->url;
+ $opts[\CURLOPT_URL] = $this->url;
if (!empty($this->options['caBundle'])) {
- $opts[CURLOPT_CAINFO] = $this->options['caBundle'];
+ $opts[\CURLOPT_CAINFO] = $this->options['caBundle'];
}
- $opts[CURLOPT_ENCODING] = '';
- $opts[CURLOPT_SSL_VERIFYHOST] = 2;
- $opts[CURLOPT_FOLLOWLOCATION] = false;
- $opts[CURLOPT_SSL_VERIFYPEER] = true;
- $opts[CURLOPT_RETURNTRANSFER] = true;
+ $opts[\CURLOPT_ENCODING] = '';
+ $opts[\CURLOPT_SSL_VERIFYHOST] = 2;
+ $opts[\CURLOPT_FOLLOWLOCATION] = false;
+ $opts[\CURLOPT_SSL_VERIFYPEER] = true;
+ $opts[\CURLOPT_RETURNTRANSFER] = true;
- $opts[CURLOPT_HTTPHEADER] = $this->options['headers'];
- $opts[CURLOPT_USERAGENT] = $this->options['userAgent'];
- $opts[CURLOPT_PROXY] = $this->options['proxy'];
+ $opts[\CURLOPT_HTTPHEADER] = $this->options['headers'];
+ $opts[\CURLOPT_USERAGENT] = $this->options['userAgent'];
+ $opts[\CURLOPT_PROXY] = $this->options['proxy'];
// The defined()s are here as the *_MS opts are not available on older
// cURL versions
$connectTimeout = $this->options['connectTimeout'];
if (\defined('CURLOPT_CONNECTTIMEOUT_MS')) {
- $opts[CURLOPT_CONNECTTIMEOUT_MS] = ceil($connectTimeout * 1000);
+ $opts[\CURLOPT_CONNECTTIMEOUT_MS] = ceil($connectTimeout * 1000);
} else {
- $opts[CURLOPT_CONNECTTIMEOUT] = ceil($connectTimeout);
+ $opts[\CURLOPT_CONNECTTIMEOUT] = ceil($connectTimeout);
}
$timeout = $this->options['timeout'];
if (\defined('CURLOPT_TIMEOUT_MS')) {
- $opts[CURLOPT_TIMEOUT_MS] = ceil($timeout * 1000);
+ $opts[\CURLOPT_TIMEOUT_MS] = ceil($timeout * 1000);
} else {
- $opts[CURLOPT_TIMEOUT] = ceil($timeout);
+ $opts[\CURLOPT_TIMEOUT] = ceil($timeout);
}
curl_setopt_array($this->ch, $opts);
@@ -109,13 +103,11 @@ class CurlRequest implements Request }
/**
- * @param resource $curl
+ * @param \CurlHandle $curl
*
* @throws HttpException
- *
- * @return array
*/
- private function execute($curl)
+ private function execute($curl): array
{
$body = curl_exec($curl);
if ($errno = curl_errno($curl)) {
@@ -128,9 +120,17 @@ class CurlRequest implements Request );
}
- $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
-
- return [$statusCode, $contentType, $body];
+ $statusCode = curl_getinfo($curl, \CURLINFO_HTTP_CODE);
+ $contentType = curl_getinfo($curl, \CURLINFO_CONTENT_TYPE);
+
+ return [
+ $statusCode,
+ // The PHP docs say "Content-Type: of the requested document. NULL
+ // indicates server did not send valid Content-Type: header" for
+ // CURLINFO_CONTENT_TYPE. However, it will return FALSE if no header
+ // is set. To keep our types simple, we return null in this case.
+ ($contentType === false ? null : $contentType),
+ $body,
+ ];
}
}
diff --git a/vendor/maxmind/web-service-common/src/WebService/Http/Request.php b/vendor/maxmind/web-service-common/src/WebService/Http/Request.php index 283e05c..6f5a5ab 100644 --- a/vendor/maxmind/web-service-common/src/WebService/Http/Request.php +++ b/vendor/maxmind/web-service-common/src/WebService/Http/Request.php @@ -1,5 +1,7 @@ <?php
+declare(strict_types=1);
+
namespace MaxMind\WebService\Http;
/**
@@ -9,21 +11,9 @@ namespace MaxMind\WebService\Http; */
interface Request
{
- /**
- * @param string $url
- * @param array $options
- */
- public function __construct($url, $options);
+ public function __construct(string $url, array $options);
- /**
- * @param string $body
- *
- * @return mixed
- */
- public function post($body);
+ public function post(string $body): array;
- /**
- * @return mixed
- */
- public function get();
+ public function get(): array;
}
diff --git a/vendor/maxmind/web-service-common/src/WebService/Http/RequestFactory.php b/vendor/maxmind/web-service-common/src/WebService/Http/RequestFactory.php index 54e6d54..4bc460a 100644 --- a/vendor/maxmind/web-service-common/src/WebService/Http/RequestFactory.php +++ b/vendor/maxmind/web-service-common/src/WebService/Http/RequestFactory.php @@ -1,5 +1,7 @@ <?php
+declare(strict_types=1);
+
namespace MaxMind\WebService\Http;
/**
@@ -14,7 +16,7 @@ class RequestFactory * done the connection is kept alive, SSL resumption can be used
* etcetera.
*
- * @var resource
+ * @var \CurlHandle|null
*/
private $ch;
@@ -25,6 +27,9 @@ class RequestFactory }
}
+ /**
+ * @return \CurlHandle
+ */
private function getCurlHandle()
{
if (empty($this->ch)) {
@@ -34,13 +39,7 @@ class RequestFactory return $this->ch;
}
- /**
- * @param string $url
- * @param array $options
- *
- * @return Request
- */
- public function request($url, $options)
+ public function request(string $url, array $options): Request
{
$options['curlHandle'] = $this->getCurlHandle();
|