diff --git a/composer.lock b/composer.lock index c5a4e19..2f1a336 100644 --- a/composer.lock +++ b/composer.lock @@ -533,16 +533,16 @@ }, { "name": "netresearch/jsonmapper", - "version": "v4.2.0", + "version": "v4.1.0", "source": { "type": "git", "url": "https://github.com/cweiske/jsonmapper.git", - "reference": "f60565f8c0566a31acf06884cdaa591867ecc956" + "reference": "cfa81ea1d35294d64adb9c68aa4cb9e92400e53f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/cweiske/jsonmapper/zipball/f60565f8c0566a31acf06884cdaa591867ecc956", - "reference": "f60565f8c0566a31acf06884cdaa591867ecc956", + "url": "https://api.github.com/repos/cweiske/jsonmapper/zipball/cfa81ea1d35294d64adb9c68aa4cb9e92400e53f", + "reference": "cfa81ea1d35294d64adb9c68aa4cb9e92400e53f", "shasum": "" }, "require": { @@ -578,9 +578,9 @@ "support": { "email": "cweiske@cweiske.de", "issues": "https://github.com/cweiske/jsonmapper/issues", - "source": "https://github.com/cweiske/jsonmapper/tree/v4.2.0" + "source": "https://github.com/cweiske/jsonmapper/tree/v4.1.0" }, - "time": "2023-04-09T17:37:40+00:00" + "time": "2022-12-08T20:46:14+00:00" }, { "name": "nikic/php-parser", @@ -998,16 +998,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.19.0", + "version": "1.16.1", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "178b33aa1c8b8d7725f0abee618ef47337e607ce" + "reference": "e27e92d939e2e3636f0a1f0afaba59692c0bf571" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/178b33aa1c8b8d7725f0abee618ef47337e607ce", - "reference": "178b33aa1c8b8d7725f0abee618ef47337e607ce", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/e27e92d939e2e3636f0a1f0afaba59692c0bf571", + "reference": "e27e92d939e2e3636f0a1f0afaba59692c0bf571", "shasum": "" }, "require": { @@ -1037,9 +1037,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.19.0" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.16.1" }, - "time": "2023-04-17T13:16:52+00:00" + "time": "2023-02-07T18:11:17+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1361,16 +1361,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.7", + "version": "9.6.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "c993f0d3b0489ffc42ee2fe0bd645af1538a63b2" + "reference": "b65d59a059d3004a040c16a82e07bbdf6cfdd115" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c993f0d3b0489ffc42ee2fe0bd645af1538a63b2", - "reference": "c993f0d3b0489ffc42ee2fe0bd645af1538a63b2", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b65d59a059d3004a040c16a82e07bbdf6cfdd115", + "reference": "b65d59a059d3004a040c16a82e07bbdf6cfdd115", "shasum": "" }, "require": { @@ -1444,7 +1444,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.7" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.6" }, "funding": [ { @@ -1460,7 +1460,7 @@ "type": "tidelift" } ], - "time": "2023-04-14T08:58:40+00:00" + "time": "2023-03-27T11:43:46+00:00" }, { "name": "psr/container", diff --git a/src/HttpClient.php b/src/HttpClient.php index 2163c1b..59fa048 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -9,7 +9,6 @@ use CoreInterfaces\Core\Response\ResponseInterface; use CoreInterfaces\Http\HttpClientInterface; use CoreInterfaces\Http\RetryOption; -use CURLFile; use DateTime; use Unirest\Request\Request; @@ -86,77 +85,33 @@ protected function initializeHandle() $this->totalNumberOfConnections = 0; } - /** - * Get Body along with its additionally required headers as an array - * i.e. [mixed $body, array $headers] - */ - protected function getBody(RequestInterface $request): array + protected function getBody(RequestInterface $request) { if (empty($request->getParameters())) { - return [$request->getBody(), []]; + return $request->getBody(); } // special handling for form parameters i.e. // returning flatten array with encoded keys if any multipart parameter exists // OR returning concatenated encoded parameters string - // Also adding content-type header for simple form params - // While adding content-type and content-length for multipart form params $encodedBody = join('&', $request->getEncodedParameters()); $multipartParameters = $request->getMultipartParameters(); if (empty($multipartParameters)) { - return [$encodedBody, ['content-type' => 'application/x-www-form-urlencoded']]; + return $encodedBody; } if (empty($encodedBody)) { - return $this->createMultiPartFormData($multipartParameters); + return $multipartParameters; } foreach (explode('&', $encodedBody) as $param) { $keyValue = explode('=', $param); - $multipartParameters[urldecode($keyValue[0])] = [ - 'data' => urldecode($keyValue[1]), - 'headers' => [] - ]; - } - return $this->createMultiPartFormData($multipartParameters); - } - - /** - * Get Multipart Form Body along with its additionally required headers - * as an array - * i.e. [string $body, array $headers] - */ - protected function createMultiPartFormData(array $multipartParameters): array - { - $boundary = uniqid(); - $body = ''; - foreach ($multipartParameters as $key => $parameter) { - $body .= "--$boundary\r\n"; - $body .= "Content-Disposition: form-data; name=\"$key\""; - if ($parameter instanceof CURLFile) { - $filename = $parameter->getFilename(); - $body .= "; filename=\"$filename\""; - $parameter = [ - 'data' => file_get_contents($filename), - 'headers' => ['Content-Type' => $parameter->getMimeType()] - ]; - } - foreach ($parameter['headers'] as $headerKey => $headerValue) { - $body .= "\r\n$headerKey: $headerValue"; - } - $body .= "\r\n\r\n" . $parameter['data'] . "\r\n"; + $multipartParameters[urldecode($keyValue[0])] = urldecode($keyValue[1]); } - $body .= "--$boundary--\r\n"; - - $headers = [ - 'content-type' => "multipart/form-data; boundary=$boundary", - 'content-length' => strlen($body) - ]; - - return [$body, $headers]; + return $multipartParameters; } protected function setCurlOptions($handle, RequestInterface $request): void { $queryUrl = $request->getQueryUrl(); - list($body, $headers) = $this->getBody($request); + $body = $this->getBody($request); if ($request->getHttpMethod() !== RequestMethod::GET) { if ($request->getHttpMethod() === RequestMethod::POST) { curl_setopt($handle, CURLOPT_POST, true); @@ -184,7 +139,7 @@ protected function setCurlOptions($handle, RequestInterface $request): void CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 10, - CURLOPT_HTTPHEADER => $this->getFormattedHeaders($request, $headers), + CURLOPT_HTTPHEADER => $this->getFormattedHeaders($request), CURLOPT_HEADER => true, CURLOPT_SSL_VERIFYPEER => $this->config->shouldVerifyPeer(), // CURLOPT_SSL_VERIFYHOST accepts only 0 (false) or 2 (true). @@ -378,21 +333,23 @@ public function getInfo(?int $option = null) return curl_getinfo($this->handle, $option); } - protected function getFormattedHeaders(RequestInterface $request, $additionalHeaders = []): array + protected function getFormattedHeaders(RequestInterface $request): array { $combinedHeaders = array_change_key_case(array_merge( ['user-agent' => 'unirest-php/4.0', 'expect' => ''], $this->config->getDefaultHeaders(), - $request->getHeaders(), - $additionalHeaders + $request->getHeaders() )); - $formattedHeaders = []; foreach ($combinedHeaders as $key => $val) { $key = trim($key); + if (!empty($request->getParameters()) && $key == 'content-type') { + // special handling for form parameters i.e. removing content-type header + // As, Curl will automatically add content-type for form params + continue; + } $formattedHeaders[] = "$key: $val"; } - return $formattedHeaders; }