]> WPIA git - cassiopeia.git/blobdiff - src/io/recordHandler.cpp
upd: only throwing exceptions now
[cassiopeia.git] / src / io / recordHandler.cpp
index 87b11763342f65a4c89b61ae09cb9b49ab4924fa..f8c2185f395bdcba617e32d5e5410af74947549f 100644 (file)
@@ -67,39 +67,23 @@ public:
         rh.command = static_cast<uint16_t>( res );
         rh.flags = 0;
         rh.command_count = 0; // TODO i++
-        rh.totalLength = payload.size();
         sendCommand( rh, payload, io );
     }
 
     void work() {
-        std::vector<char> buffer( 2048 );
-        int res = io->read( buffer.data(), buffer.size() );
-
-        if( res <= 0 ) {
-            logger::error( "Stream error, resetting SSL" );
-            parent->reset();
-            return;
-        }
-
-        std::string content( buffer.data(), res );
-
         try {
             RecordHeader head;
-            std::string payload = parseCommand( head, content );
-            execute( head, payload );
-        } catch( const char* msg ) {
-            logger::error( "ERROR: ", msg );
+            std::string all = parseCommandChunked( head, io );
+            execute( static_cast<RecordHeader::SignerCommand>( head.command ), all );
+        } catch( const std::exception& msg ) {
+            logger::error( "ERROR: ", msg.what() );
             parent->reset();
             return;
         }
     }
 
-    void execute( RecordHeader& head, std::string data ) {
-        if( head.totalLength != head.payloadLength || head.offset != 0 ) {
-            throw "Error, chunking not supported yet";
-        }
-
-        switch( static_cast<RecordHeader::SignerCommand>( head.command )) {
+    void execute( RecordHeader::SignerCommand command, std::string data ) {
+        switch( command ) {
         case RecordHeader::SignerCommand::SET_CSR:
             tbs->csr_content = data;
             tbs->csr_type = "CSR";
@@ -175,9 +159,12 @@ public:
                 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
             }
 
+            logger::note( "Shutting down SSL" );
             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
                 logger::warn( "ERROR: SSL shutdown failed." );
             }
+            io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
+            logger::note( "Shutted down SSL" );
 
             parent->reset(); // Connection ended
 
@@ -207,17 +194,20 @@ public:
                 auto ca = CAs.at( data );
                 CRL c( ca->path + "/ca.crl" );
                 respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
-
+                
+                logger::note( "Shutting down SSL" );
                 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
                     logger::error( "ERROR: SSL shutdown failed." );
                 }
+                io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
+                logger::note( "Shutted down SSL" );
 
                 parent->reset(); // Connection ended
             }
             break;
 
         default:
-            throw "Unimplemented";
+            throw std::runtime_error("Unimplemented");
         }
     }
 };
@@ -232,9 +222,13 @@ void DefaultRecordHandler::reset() {
 
 void DefaultRecordHandler::handle() {
     if( !currentSession ) {
+        (void) BIO_reset( bio.get() );
         logger::note( "New session allocated." );
         currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
     }
-
-    currentSession->work();
+    try {
+        currentSession->work();
+    } catch( eof_exception e ){
+        reset();
+    }
 }