use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
use Org\Mxchange\CoreFramework\Client\BaseClient;
use Org\Mxchange\CoreFramework\Client\Client;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+
+// Import SPL stuff
+use \InvalidArgumentException;
+use \UnexpectedValueException;
/**
* A HTTP client class
*/
public final static function createHttpClient ($socketResouce = FALSE) {
// Get a new instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: socketResource[%s]=%s - CALLED!', gettype($socketResource), $socketResource));
$clientInstance = new HttpClient();
// Set socket resource
$clientInstance->setSocketResource($socketResource);
// Return the prepared instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: clientInstance=%s - EXIT!', $clientInstance->__toString()));
return $clientInstance;
}
*/
protected function isProxyUsed () {
// Do we have cache?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HTTP-CLIENT: CALLED!');
if (!isset($GLOBALS[__METHOD__])) {
// Determine it
$GLOBALS[__METHOD__] = ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_host') != '') && (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_port') > 0));
}
// Return cache
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: isProxyUsed=%d - EXIT!', $GLOBALS[__METHOD__]));
return $GLOBALS[__METHOD__];
}
* @param $host Host to connect to
* @param $port Port number to connect to
* @return $response Response array
+ * @throws InvalidArgumentException If a paramter has an invalid value
+ * @throws UnexpectedValueException If an unexpected value was found
*/
protected function setupProxyTunnel (string $host, int $port) {
+ // Check paramters
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: host=%s,port=%d - CALLED!', $host, $port));
+ if (empty($host)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "host" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+ } elseif ($port < 1) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('port=%d is not a valid port number', $port), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+ }
+
// Initialize array
$response = ['', '', ''];
// Do the connect
- $respArray = $this->doConnectRequest($host, $port);
+ $responseArray = $this->doConnectRequest($host, $port);
+
+ // Check array
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d', count($responseArray)));
+ if (count($responseArray) < 2) {
+ // Not expected count
+ throw new UnexpectedValueException(sprintf('responseArray()=%d must have at least two elements', count($responseArray)));
+ }
// Analyze first header line
- if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
+ if (((strtolower($responseArray[0]) !== 'http/1.0') && (strtolower($responseArray[0]) !== 'http/1.1')) || ($responseArray[1] != '200')) {
// Response code is not 200
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HTTP-CLIENT: Returning empty response array - EXIT!');
return $response;
}
// All fine!
- return $respArray;
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d - EXIT!', count($responseArray)));
+ return $responseArray;
}
/**
*/
private function sendRawHttpRequest (string $method, string $host, int $port, array $header = []) {
// Minimum raw HTTP/1.1 request
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: method=%s,host=%s,port=%d,header()=%d - CALLED!', $method, $host, $port, count($header)));
$rawRequest = $method . ' ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL;
$rawRequest .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL;
//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HTTP-CLIENT: rawRequest=' . $rawRequest);
// Write request
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: Sending %d bytes to this->socketResource=%s ...', strlen($rawRequest), $this->getSocketResource()));
fwrite($this->getSocketResource(), $rawRequest);
// Got response?
- if (feof($this->getSocketResource())) {
+ $feof = feof($this->getSocketResource());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: this->socketResource=%s,feof=%d', $this->getSocketResource(), intval($feof)));
+ if ($feof) {
// No response received
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: this->socketResource=%s has reached EOF - EXIT!', $this->getSocketResource()));
return $response;
}
// Read the first line
- $resp = trim(fgets($this->getSocketResource(), 10240));
+ $rawResponse = trim(fgets($this->getSocketResource(), 10240));
// "Explode" the string to an array
- $responseArray = explode(' ', $resp);
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: Received %d bytes back from this->socketResource=%s ...', strlen($rawResponse), $this->getSocketResource()));
+ $responseArray = explode(' ', $rawResponse);
// And return it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d - EXIT!', count($responseArray)));
return $responseArray;
}
* @param $host Host to connect to
* @param $port Port number to connect to
* @return $responseArray An array with the read response
+ * @throws InvalidArgumentException If a paramter has an invalid value
*/
public function doConnectRequest (string $host, int $port) {
+ // Check paramters
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: host=%s,port=%d - CALLED!', $host, $port));
+ if (empty($host)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "host" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+ } elseif ($port < 1) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('port=%d is not a valid port number', $port), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+ }
+
// Prepare extra header(s)
$headers = [
'Proxy-Connection' => 'Keep-Alive'
];
// Prepare raw request
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: Invoking this->sendRawHttpRequest(CONNECT,%s,%d,headers()=%d) ...', $host, $port, count($headers)));
$responseArray = $this->sendRawHttpRequest('CONNECT', $host, $port, $headers);
// Return response array
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d - EXIT!', count($responseArray)));
return $responseArray;
}
use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
use Org\Mxchange\CoreFramework\Request\Requestable;
use Org\Mxchange\CoreFramework\Traits\Resolver\ResolverTrait;
use Org\Mxchange\CoreFramework\Traits\Template\CompileableTemplateTrait;
+// Import SPL stuff
+use \InvalidArgumentException;
+
/**
* A general (base) command
*
* @return void
*/
public final function initTemplateEngine (string $templateType) {
+ // Check paramter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-COMMAND: templateType=%s - CALLED!', $templateType));
+ if (empty($templateType)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "templateType" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+ }
+
// Prepare a template instance
$templateInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_template_class', $templateType));
// Set it here
$this->setTemplateInstance($templateInstance);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-COMMAND: EXIT!');
}
/**