<commit_before><?php

namespace FenzHTTP;

use FenzHelpers\TGetter;

////////////////////////////////////////////////////////////////

class Response
{
	use TGetter;

	/**
	 * Var handle
	 *
	 * @access protected
	 *
	 * @var    resource
	 */
	protected $handle;

	/**
	 * Var version
	 *
	 * @access protected
	 *
	 * @var    string
	 */
	protected $version= '1.1';

	/**
	 * Var statusCode
	 *
	 * @access protected
	 *
	 * @var    int
	 */
	protected $statusCode= 100;

	/**
	 * Var statusMessage
	 *
	 * @access protected
	 *
	 * @var    string
	 */
	protected $statusMessage= '';

	/**
	 * Var headers
	 *
	 * @access protected
	 *
	 * @var    array
	 */
	protected $headers= [];

	/**
	 * Var body
	 *
	 * @access protected
	 *
	 * @var    string
	 */
	protected $body= '';

	/**
	 * Method __construct
	 *
	 * @access public
	 *
	 * @param  resource $handle
	 */
	public function __construct( $handle )
	{
		$this->handle= $handle;

		$this->parsMessageLine(trim(fgets($this->handle)));

		while( $headerLine= trim(fgets($this->handle)) ){
			$this->parseHeader($headerLine);
		}

		$this->readBody();
	}

	/**
	 * Method getStatus
	 *
	 * @access public
	 *
	 * @return int
	 */
	public function getStatus():int
	{
		return $this->statusCode;
	}

	/**
	 * Method getStatusCode
	 *
	 * @access public
	 *
	 * @return int
	 */
	public function getStatusCode():int
	{
		return $this->getStatus();
	}

	/**
	 * Method getStatusMessage
	 *
	 * @access public
	 *
	 * @return string
	 */
	public function getStatusMessage():string
	{
		return $this->getStatusMessage;
	}

	/**
	 * Method header
	 *
	 * @access public
	 *
	 * @param  string $key
	 *
	 * @return mixed
	 */
	public function header( string$key=null )
	{
		return ( isset($key) ?
			$this->headers[ucwords( strtolower($key), '-' )]
			:
			$this->getHeaders()
		);
	}

	/**
	 * Method getHeaders
	 *
	 * @access public
	 *
	 * @return array
	 */
	public function getHeaders():array
	{
		return $this->headers;
	}

	/**
	 * Method getBody
	 *
	 * @access public
	 *
	 * @return string
	 */
	public function getBody():string
	{
		return $this->body;
	}

	/**
	 * Method getJson
	 *
	 * @access public
	 *
	 * @return mixed
	 */
	public function getJson()
	{
		try{
			return json_decode( $this->body, true );
		}
		catch( \Throwable$e )
		{
			return null;
		}
	}

	/**
	 * Method parsMessageLine
	 *
	 * @access private
	 *
	 * @param  string $messageLine
	 *
	 * @return void
	 */
	private function parsMessageLine( string$messageLine )
	{
		list( $this->version, $this->statusCode, $this->statusMessage )= explode( ' ', str_replace( 'HTTP/', '', $messageLine ) );
	}

	/**
	 * Method parseHeader
	 *
	 * @access private
	 *
	 * @param  string $headerLine
	 *
	 * @return void
	 */
	private function parseHeader( string$headerLine )
	{
		list( $key, $value, )= explode(': ', $headerLine);

		$this->headers[ucwords( strtolower($key), '-' )]= $value;
	}

	/**
	 * Method readBody
	 *
	 * @access private
	 *
	 * @return void<commit_msg>Fix status message cannot contains space bug<commit_after><?php

namespace FenzHTTP;

use FenzHelpers\TGetter;

////////////////////////////////////////////////////////////////

class Response
{
	use TGetter;

	/**
	 * Var handle
	 *
	 * @access protected
	 *
	 * @var    resource
	 */
	protected $handle;

	/**
	 * Var version
	 *
	 * @access protected
	 *
	 * @var    string
	 */
	protected $version= '1.1';

	/**
	 * Var statusCode
	 *
	 * @access protected
	 *
	 * @var    int
	 */
	protected $statusCode= 100;

	/**
	 * Var statusMessage
	 *
	 * @access protected
	 *
	 * @var    string
	 */
	protected $statusMessage= '';

	/**
	 * Var headers
	 *
	 * @access protected
	 *
	 * @var    array
	 */
	protected $headers= [];

	/**
	 * Var body
	 *
	 * @access protected
	 *
	 * @var    string
	 */
	protected $body= '';

	/**
	 * Method __construct
	 *
	 * @access public
	 *
	 * @param  resource $handle
	 */
	public function __construct( $handle )
	{
		$this->handle= $handle;

		$this->parsMessageLine(trim(fgets($this->handle)));

		while( $headerLine= trim(fgets($this->handle)) ){
			$this->parseHeader($headerLine);
		}

		$this->readBody();
	}

	/**
	 * Method getStatus
	 *
	 * @access public
	 *
	 * @return int
	 */
	public function getStatus():int
	{
		return $this->statusCode;
	}

	/**
	 * Method getStatusCode
	 *
	 * @access public
	 *
	 * @return int
	 */
	public function getStatusCode():int
	{
		return $this->getStatus();
	}

	/**
	 * Method getStatusMessage
	 *
	 * @access public
	 *
	 * @return string
	 */
	public function getStatusMessage():string
	{
		return $this->getStatusMessage;
	}

	/**
	 * Method header
	 *
	 * @access public
	 *
	 * @param  string $key
	 *
	 * @return mixed
	 */
	public function header( string$key=null )
	{
		return ( isset($key) ?
			$this->headers[ucwords( strtolower($key), '-' )]
			:
			$this->getHeaders()
		);
	}

	/**
	 * Method getHeaders
	 *
	 * @access public
	 *
	 * @return array
	 */
	public function getHeaders():array
	{
		return $this->headers;
	}

	/**
	 * Method getBody
	 *
	 * @access public
	 *
	 * @return string
	 */
	public function getBody():string
	{
		return $this->body;
	}

	/**
	 * Method getJson
	 *
	 * @access public
	 *
	 * @return mixed
	 */
	public function getJson()
	{
		try{
			return json_decode( $this->body, true );
		}
		catch( \Throwable$e )
		{
			return null;
		}
	}

	/**
	 * Method parsMessageLine
	 *
	 * @access private
	 *
	 * @param  string $messageLine
	 *
	 * @return void
	 */
	private function parsMessageLine( string$messageLine )
	{
		preg_replace_callback( '/^HTTP\\/(\\S+) (\\S+) (.+)$/', function( array$matches ){
			list( $null, $this->version, $this->statusCode, $this->statusMessage )= $matches;
		}, $messageLine );
	}

	/**
	 * Method parseHeader
	 *
	 * @access private
	 *
	 * @param  string $headerLine
	 *
	 * @return void
	 */
	private function parseHeader( string$headerLine )
	{
		list( $key, $value, )= explode(': ', $headerLine);

		$this->headers[ucwords( strtolower($key), '-' )]= $value;
	}

	/**
	 * Method readBody
	 *
	 * @access private
	 *
	 * @return void
{'fraction_non_alphanumeric': 0.17970159042465977, 'fraction_numerical': 0.0016396130513198885, 'mean_word_length': 2.3964365256124722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2710245', 'n_tokens_mistral': 2307, 'n_tokens_neox': 1984, 'n_words': 444}