From: Roland Häder <roland@mxchange.org>
Date: Tue, 7 Aug 2012 15:34:58 +0000 (+0000)
Subject: RawDataInputStream does now throw exceptions if the BASE64-encoded data is not well... 
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6f3b4ce12d6d3cb2117adc870dfc16f674726368;p=hub.git

RawDataInputStream does now throw exceptions if the BASE64-encoded data is not well-formed
---

diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php
index 035792279..e89050f66 100644
--- a/application/hub/main/class_BaseHubSystem.php
+++ b/application/hub/main/class_BaseHubSystem.php
@@ -23,11 +23,12 @@
  */
 class BaseHubSystem extends BaseFrameworkSystem {
 	// Exception codes
-	const EXCEPTION_UNSUPPORTED_ERROR_HANDLER  = 0x900;
-	const EXCEPTION_CHUNK_ALREADY_ASSEMBLED    = 0x901;
-	const EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED  = 0x902;
-	const EXCEPTION_INVALID_CONNECTION_TYPE    = 0x903;
-	const EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED = 0x904;
+	const EXCEPTION_UNSUPPORTED_ERROR_HANDLER    = 0x900;
+	const EXCEPTION_CHUNK_ALREADY_ASSEMBLED      = 0x901;
+	const EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED    = 0x902;
+	const EXCEPTION_INVALID_CONNECTION_TYPE      = 0x903;
+	const EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED   = 0x904;
+	const EXCEPTION_BASE64_ENCODING_NOT_MODULO_4 = 0x905;
 
 	// Message status codes
 	const MESSAGE_STATUS_CODE_OKAY = 'OKAY';
diff --git a/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php b/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php
index 2f485eaae..23bb556dd 100644
--- a/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php
+++ b/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php
@@ -99,10 +99,15 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable {
 		} elseif (empty($rawData)) {
 			// The peer did send nothing to us which is now being ignored
 			return;
+		} else {
+			/*
+			 * All is fine at this point. So it is okay to add the raw data to
+			 * the stacker. Here it doesn't matter if the raw data is a
+			 * well-formed BASE64-encoded message with start and markers. This
+			 * will be checked later on.
+			 */
+			$this->addRawDataToStacker($rawData);
 		}
-
-		// Add the raw data to the stacker
-		$this->addRawDataToStacker($rawData);
 	}
 }
 
diff --git a/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php b/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php
index d7cb9d8f3..ba34700b7 100644
--- a/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php
+++ b/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php
@@ -1,6 +1,11 @@
 <?php
 /**
- * A RawDataInputStream class
+ * A class for handling incoming (encoded) raw data with start and end markers.
+ * The "stream" is being verified by its length (if modulo 4 of it is always
+ * zero) and if the "stream" contains all valid characters (the BASE64
+ * "alphabet").
+ *
+ * Since the latest refacturing this class works "handler-less".
  *
  * @author		Roland Haeder <webmaster@ship-simu.org>
  * @version		0.0.0
@@ -41,9 +46,6 @@ class RawDataInputStream extends BaseStream implements InputStreamable {
 		// Get a new instance
 		$streamInstance = new RawDataInputStream();
 
-		// Set the handler instance
-		$streamInstance->setHandlerInstance($handlerInstance);
-
 		// Return the instance
 		return $streamInstance;
 	}
@@ -54,6 +56,8 @@ class RawDataInputStream extends BaseStream implements InputStreamable {
 	 * @param	$data	The data (string mostly) to "stream"
 	 * @return	$data	The data (string mostly) to "stream"
 	 * @todo	Do we need to do something more here?
+	 * @throws	Base64EncodingModuloException	If the data's length modulo 4 is not zero
+	 * @throws	Base64EncodingBadException		If the data contains characters which are not in the "alphabet" of BASE64 messages.
 	 */
 	public function streamData ($data) {
 		// Do we have start and end marker again?
@@ -65,12 +69,10 @@ class RawDataInputStream extends BaseStream implements InputStreamable {
 		// Can it be validated?
 		if ((strlen($data) % 4) != 0) {
 			// Length modulo 4 must be zero, else it is an invalid Base64 message
-			$handlerInstance->setErrorCode(BaseRawDataHandler::SOCKET_ERROR_INVALID_BASE64_MODULO);
-			$data = false;
+			throw new Base64EncodingModuloException(array($this, $data), self::EXCEPTION_BASE64_ENCODING_NOT_MODULO_4);
 		} elseif (!$this->isBase64Encoded($data)) {
 			// Is not a valid Base64-encoded message
-			$handlerInstance->setErrorCode(BaseRawDataHandler::SOCKET_ERROR_INVALID_BASE64_MESSAGE);
-			$data = false;
+			throw new Base64EncodingBadException(array($this, $data), self::EXCEPTION_BASE64_BAD_ENCODING);
 		} else {
 			// Decode the data with BASE64-encoding
 			$data = base64_decode($data);