mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Composer > do not require symfony/http-foundation package
Miscellaneous > remove getCurlResponseWithHeaders() method
This commit is contained in:
@@ -13,8 +13,7 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.5.9",
|
"php": ">=5.5.9",
|
||||||
"doctrine/orm": "^2.5",
|
"doctrine/orm": "^2.5",
|
||||||
"gedmo/doctrine-extensions": "^2.4",
|
"gedmo/doctrine-extensions": "^2.4"
|
||||||
"symfony/http-foundation": "^3.3"
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"friendsofphp/php-cs-fixer": "^2.2",
|
"friendsofphp/php-cs-fixer": "^2.2",
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace Meritoo\Common\Utilities;
|
|||||||
use Gedmo\Sluggable\Util\Urlizer;
|
use Gedmo\Sluggable\Util\Urlizer;
|
||||||
use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
|
use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
|
||||||
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
|
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
|
||||||
use Symfony\Component\HttpFoundation\Cookie;
|
|
||||||
use Transliterator;
|
use Transliterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1205,146 +1204,6 @@ class Miscellaneous
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a CURL response with parsed HTTP headers as array with "headers", "cookies" and "content" keys
|
|
||||||
*
|
|
||||||
* The headers and cookies are parsed and returned as an array, and an array of Cookie objects. Returned array looks
|
|
||||||
* like this example:
|
|
||||||
* <code>
|
|
||||||
* [
|
|
||||||
* 'headers' => [
|
|
||||||
* 'Content-Type' => 'text/html; charset=UTF-8',
|
|
||||||
* ...
|
|
||||||
* ],
|
|
||||||
* 'cookies' => [
|
|
||||||
* new Symfony\Component\HttpFoundation\Cookie(),
|
|
||||||
* new Symfony\Component\HttpFoundation\Cookie(),
|
|
||||||
* ...
|
|
||||||
* ],
|
|
||||||
* 'content' => '<html>...</html>'
|
|
||||||
* ]
|
|
||||||
* </code>
|
|
||||||
*
|
|
||||||
* If you want to attach HTTP headers into response content by CURL you need to set "CURLOPT_HEADER" option
|
|
||||||
* to "true". To read exact length of HTTP headers from CURL you can use "curl_getinfo()" function
|
|
||||||
* and read "CURLINFO_HEADER_SIZE" option.
|
|
||||||
*
|
|
||||||
* @param string $response Full content of response, including HTTP headers
|
|
||||||
* @param int $headerSize Length of HTTP headers in content
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public static function getCurlResponseWithHeaders($response, $headerSize)
|
|
||||||
{
|
|
||||||
$headers = [];
|
|
||||||
$cookies = [];
|
|
||||||
|
|
||||||
$headerContent = '';
|
|
||||||
$content = '';
|
|
||||||
|
|
||||||
if (0 < $headerSize) {
|
|
||||||
$headerContent = mb_substr($response, 0, $headerSize);
|
|
||||||
$content = mb_substr($response, $headerSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Let's transform headers content into two arrays: headers and cookies
|
|
||||||
*/
|
|
||||||
if (!empty($headerContent)) {
|
|
||||||
foreach (explode("\r\n", $headerContent) as $i => $line) {
|
|
||||||
/*
|
|
||||||
* First line is only HTTP status and is unneeded so skip it
|
|
||||||
*/
|
|
||||||
if (0 === $i) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Regex::contains($line, ':')) {
|
|
||||||
list($key, $value) = explode(': ', $line);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* If the header is a "set-cookie" let's save it to "cookies" array
|
|
||||||
*/
|
|
||||||
if ('Set-Cookie' === $key) {
|
|
||||||
$cookieParameters = explode(';', $value);
|
|
||||||
|
|
||||||
$name = '';
|
|
||||||
$value = '';
|
|
||||||
$expire = 0;
|
|
||||||
$path = '/';
|
|
||||||
$domain = null;
|
|
||||||
$secure = false;
|
|
||||||
$httpOnly = true;
|
|
||||||
|
|
||||||
foreach ($cookieParameters as $j => $parameter) {
|
|
||||||
$param = explode('=', $parameter);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* First parameter will be always a cookie name and it's value. It is not needed to run
|
|
||||||
* further actions for them, so save the values and move to next parameter.
|
|
||||||
*/
|
|
||||||
if (0 === $j) {
|
|
||||||
$name = trim($param[0]);
|
|
||||||
$value = trim($param[1]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Now there would be the rest of cookie parameters, names of params are sent different way so
|
|
||||||
* I need to lowercase the names and remove unneeded spaces.
|
|
||||||
*/
|
|
||||||
$paramName = mb_strtolower(trim($param[0]));
|
|
||||||
$paramValue = true;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Some parameters don't have value e.g. "secure", but the value for them if they're specified
|
|
||||||
* is "true". Otherwise - just read a value for parameter if exists.
|
|
||||||
*/
|
|
||||||
if (array_key_exists(1, $param)) {
|
|
||||||
$paramValue = trim($param[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($paramName) {
|
|
||||||
case 'expires':
|
|
||||||
$expire = $paramValue;
|
|
||||||
break;
|
|
||||||
case 'path':
|
|
||||||
$path = $paramValue;
|
|
||||||
break;
|
|
||||||
case 'domain':
|
|
||||||
$domain = $paramValue;
|
|
||||||
break;
|
|
||||||
case 'secure':
|
|
||||||
$secure = $paramValue;
|
|
||||||
break;
|
|
||||||
case 'httponly':
|
|
||||||
$httpOnly = $paramValue;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Create new Cookie object and add it to "cookies" array.
|
|
||||||
* I must skip to next header as cookies shouldn't be saved in "headers" array.
|
|
||||||
*/
|
|
||||||
$cookies[] = new Cookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Save response header which is not a cookie
|
|
||||||
*/
|
|
||||||
$headers[$key] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
'headers' => $headers,
|
|
||||||
'cookies' => $cookies,
|
|
||||||
'content' => $content,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds missing the "0" characters to given number until given length is reached
|
* Adds missing the "0" characters to given number until given length is reached
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -770,21 +770,6 @@ class MiscellaneousTest extends BaseTestCase
|
|||||||
self::assertNotEmpty(Miscellaneous::getProjectRootPath());
|
self::assertNotEmpty(Miscellaneous::getProjectRootPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $headerSize Length of HTTP headers in content
|
|
||||||
* @dataProvider provideHeaderSizeForEmptyCurlResponse
|
|
||||||
*/
|
|
||||||
public function testGetCurlResponseWithHeadersUsingEmptyResponse($headerSize)
|
|
||||||
{
|
|
||||||
$expected = [
|
|
||||||
'headers' => [],
|
|
||||||
'cookies' => [],
|
|
||||||
'content' => '',
|
|
||||||
];
|
|
||||||
|
|
||||||
self::assertEquals($expected, Miscellaneous::getCurlResponseWithHeaders('', $headerSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides string to convert characters to latin characters and not lower cased and not human-readable
|
* Provides string to convert characters to latin characters and not lower cased and not human-readable
|
||||||
*
|
*
|
||||||
@@ -1195,30 +1180,6 @@ class MiscellaneousTest extends BaseTestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides length/size of HTTP headers for an empty response
|
|
||||||
*
|
|
||||||
* @return Generator
|
|
||||||
*/
|
|
||||||
public function provideHeaderSizeForEmptyCurlResponse()
|
|
||||||
{
|
|
||||||
yield[
|
|
||||||
-10,
|
|
||||||
];
|
|
||||||
|
|
||||||
yield[
|
|
||||||
-1,
|
|
||||||
];
|
|
||||||
|
|
||||||
yield[
|
|
||||||
0,
|
|
||||||
];
|
|
||||||
|
|
||||||
yield[
|
|
||||||
10,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user