]> git.mxchange.org Git - hub.git/commitdiff
RawDataInputStream does now throw exceptions if the BASE64-encoded data is not well...
authorRoland Häder <roland@mxchange.org>
Tue, 7 Aug 2012 15:34:58 +0000 (15:34 +0000)
committerRoland Häder <roland@mxchange.org>
Tue, 7 Aug 2012 15:34:58 +0000 (15:34 +0000)
application/hub/main/class_BaseHubSystem.php
application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php
application/hub/main/streams/raw_data/input/class_RawDataInputStream.php

index 03579227987b32f3114470a5900b96f0ebb9471d..e89050f66334c041c5bd8eea8c38709ed63872f0 100644 (file)
  */
 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';
index 2f485eaae75d47adc0fbab3536b3d09651f27141..23bb556dd0e9cc0dee55956d9c0af919ee1cd27a 100644 (file)
@@ -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);
        }
 }
 
index d7cb9d8f3fa9843bd527aa35741e9fe276dae478..ba34700b7bf56505c2547a4bff487a5305f0ae16 100644 (file)
@@ -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);