Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+41
-33
@@ -11,54 +11,62 @@
|
||||
namespace IPS\Http;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use InvalidArgumentException;
|
||||
use IPS\Xml\SimpleXML;
|
||||
use RuntimeException;
|
||||
use function defined;
|
||||
use function strpos;
|
||||
use function substr;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP Response Class
|
||||
*/
|
||||
class _Response
|
||||
class Response
|
||||
{
|
||||
/**
|
||||
* @brief HTTP Response Version
|
||||
*/
|
||||
public $httpResponseVersion = NULL;
|
||||
public mixed $httpResponseVersion = NULL;
|
||||
|
||||
/**
|
||||
* @brief HTTP Response Code
|
||||
*/
|
||||
public $httpResponseCode = NULL;
|
||||
public mixed $httpResponseCode = NULL;
|
||||
|
||||
/**
|
||||
* @brief HTTP Response Text
|
||||
*/
|
||||
public $httpResponseText = NULL;
|
||||
public mixed $httpResponseText = NULL;
|
||||
|
||||
/**
|
||||
* @brief HTTP Headers
|
||||
*/
|
||||
public $httpHeaders = NULL;
|
||||
public ?array $httpHeaders = NULL;
|
||||
|
||||
/**
|
||||
* @brief Cookies returned in the response
|
||||
*/
|
||||
public $cookies = array();
|
||||
public array $cookies = array();
|
||||
|
||||
/**
|
||||
* @brief Response Content
|
||||
*/
|
||||
public $content = '';
|
||||
public mixed $content = '';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $content Response Content
|
||||
* @param string $content Response Content
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $content )
|
||||
public function __construct( string $content )
|
||||
{
|
||||
/* Do we have any content? */
|
||||
if( !$content )
|
||||
@@ -169,19 +177,19 @@ class _Response
|
||||
/**
|
||||
* Decode chunked response body from HTTP 1.1 response
|
||||
*
|
||||
* @see <a href='http://stackoverflow.com/questions/10793017/how-to-easily-decode-http-chunked-encoded-string-when-making-raw-http-request'>Decoding chunked response in PHP</a>
|
||||
* @param string $body Response body
|
||||
* @return string
|
||||
* @note We are working with bytes and intentionally are not using mb* functions
|
||||
*@see <a href='http://stackoverflow.com/questions/10793017/how-to-easily-decode-http-chunked-encoded-string-when-making-raw-http-request'>Decoding chunked response in PHP</a>
|
||||
*/
|
||||
public function decodeChunked( $body )
|
||||
public function decodeChunked( string $body ): string
|
||||
{
|
||||
$response = '';
|
||||
$initial = $body;
|
||||
|
||||
for( $response = ''; !empty($body); $body = trim($body) )
|
||||
{
|
||||
$_pos = \strpos( $body, "\r\n" );
|
||||
$_pos = strpos( $body, "\r\n" );
|
||||
|
||||
/* The last line could be 0000000 which is valid but doesn't have \r\n */
|
||||
if( trim( $body, '0' ) === '' )
|
||||
@@ -190,20 +198,20 @@ class _Response
|
||||
}
|
||||
|
||||
/* Make sure this is valid hex */
|
||||
if( $_pos === FALSE OR !preg_match( "/^([a-f0-9]+)$/mi", \substr( $body, 0, $_pos ) ) )
|
||||
if( $_pos === FALSE OR !preg_match( "/^([a-f0-9]+)$/mi", substr( $body, 0, $_pos ) ) )
|
||||
{
|
||||
return $response ?: $initial;
|
||||
}
|
||||
|
||||
$_len = (int) hexdec( \substr( $body, 0, $_pos ) );
|
||||
$_len = (int) hexdec( substr( $body, 0, $_pos ) );
|
||||
|
||||
if( $_len < 1 )
|
||||
{
|
||||
return $initial;
|
||||
}
|
||||
|
||||
$response .= \substr( $body, $_pos + 2, $_len );
|
||||
$body = \substr( $body, $_pos + 2 + $_len );
|
||||
$response .= substr( $body, $_pos + 2, $_len );
|
||||
$body = substr( $body, $_pos + 2 + $_len );
|
||||
$body = trim($body);
|
||||
}
|
||||
|
||||
@@ -223,17 +231,17 @@ class _Response
|
||||
/**
|
||||
* Decode JSON
|
||||
*
|
||||
* @param bool $asArray Whether to decode as an array or not
|
||||
* @param bool $asArray Whether to decode as an array or not
|
||||
* @return array
|
||||
* @throws \RuntimeException
|
||||
* @throws RuntimeException
|
||||
* @li BAD_JSON
|
||||
*/
|
||||
public function decodeJson( $asArray=TRUE )
|
||||
public function decodeJson( bool $asArray=TRUE ): mixed
|
||||
{
|
||||
$json = json_decode( $this->content, $asArray );
|
||||
if ( $json === FALSE )
|
||||
if ( $json === FALSE or $json === NULL )
|
||||
{
|
||||
throw new \RuntimeException('BAD_JSON');
|
||||
throw new RuntimeException('BAD_JSON');
|
||||
}
|
||||
return $json;
|
||||
}
|
||||
@@ -241,19 +249,19 @@ class _Response
|
||||
/**
|
||||
* Decode XML
|
||||
*
|
||||
* @return \IPS\Xml\SimpleXML
|
||||
* @throws \RuntimeException
|
||||
* @return SimpleXML
|
||||
* @throws RuntimeException
|
||||
* @li BAD_XML
|
||||
*/
|
||||
public function decodeXml()
|
||||
public function decodeXml(): SimpleXML
|
||||
{
|
||||
try
|
||||
{
|
||||
$xml = \IPS\Xml\SimpleXML::loadString( $this->content );
|
||||
$xml = SimpleXML::loadString( $this->content );
|
||||
}
|
||||
catch ( \InvalidArgumentException $e )
|
||||
catch ( InvalidArgumentException $e )
|
||||
{
|
||||
throw new \RuntimeException('BAD_XML');
|
||||
throw new RuntimeException('BAD_XML');
|
||||
}
|
||||
|
||||
return $xml;
|
||||
@@ -262,18 +270,18 @@ class _Response
|
||||
/**
|
||||
* Decode Query String
|
||||
*
|
||||
* @param string $expectedKey If provided, the return value will check for an element with this key and if it isn't present, throw an Exception
|
||||
* @param string|null $expectedKey If provided, the return value will check for an element with this key and if it isn't present, throw an Exception
|
||||
* @return array
|
||||
* @throws \RuntimeException
|
||||
* @throws RuntimeException
|
||||
* @li EXPECTED_KEY_NOT_PRESENT
|
||||
*/
|
||||
public function decodeQueryString( $expectedKey=NULL )
|
||||
public function decodeQueryString( string $expectedKey=NULL ): array
|
||||
{
|
||||
@parse_str( $this->content, $queryString );
|
||||
|
||||
if ( $expectedKey !== NULL and !array_key_exists( $expectedKey, $queryString ) )
|
||||
{
|
||||
throw new \RuntimeException( 'EXPECTED_KEY_NOT_PRESENT' );
|
||||
throw new RuntimeException( 'EXPECTED_KEY_NOT_PRESENT' );
|
||||
}
|
||||
|
||||
return $queryString;
|
||||
|
||||
Reference in new issue
Block a user