소스 검색

HttpProcessor: Fix connection upgrade sequence.

lganzzzo 2 년 전
부모
커밋
73747dd444
1개의 변경된 파일35개의 추가작업 그리고 23개의 파일을 삭제
  1. 35 23
      src/oatpp/web/server/HttpProcessor.cpp

+ 35 - 23
src/oatpp/web/server/HttpProcessor.cpp

@@ -190,17 +190,8 @@ HttpProcessor::ConnectionState HttpProcessor::processNextRequest(ProcessingResou
         response->putHeaderIfNotExists(protocol::http::Header::CONNECTION, protocol::http::Header::Value::CONNECTION_CLOSE);
         break;
 
-      case ConnectionState::DELEGATED: {
-        auto handler = response->getConnectionUpgradeHandler();
-        if(handler) {
-          handler->handleConnection(resources.connection, response->getConnectionUpgradeParameters());
-          connectionState = ConnectionState::DELEGATED;
-        } else {
-          OATPP_LOGW("[oatpp::web::server::HttpProcessor::processNextRequest()]", "Warning. ConnectionUpgradeHandler not set!");
-          connectionState = ConnectionState::CLOSING;
-        }
+      default:
         break;
-      }
 
     }
 
@@ -211,6 +202,18 @@ HttpProcessor::ConnectionState HttpProcessor::processNextRequest(ProcessingResou
 
   response->send(resources.connection.object.get(), &resources.headersOutBuffer, contentEncoderProvider.get());
 
+  /* Delegate connection handling to another handler only after the response is sent to the client */
+  if(connectionState == ConnectionState::DELEGATED) {
+    auto handler = response->getConnectionUpgradeHandler();
+    if(handler) {
+      handler->handleConnection(resources.connection, response->getConnectionUpgradeParameters());
+      connectionState = ConnectionState::DELEGATED;
+    } else {
+      OATPP_LOGW("[oatpp::web::server::HttpProcessor::processNextRequest()]", "Warning. ConnectionUpgradeHandler not set!");
+      connectionState = ConnectionState::CLOSING;
+    }
+  }
+
   return connectionState;
 
 }
@@ -370,17 +373,8 @@ HttpProcessor::Coroutine::Action HttpProcessor::Coroutine::onResponseFormed() {
       m_currentResponse->putHeaderIfNotExists(protocol::http::Header::CONNECTION, protocol::http::Header::Value::CONNECTION_CLOSE);
       break;
 
-    case ConnectionState::DELEGATED: {
-      auto handler = m_currentResponse->getConnectionUpgradeHandler();
-      if(handler) {
-        handler->handleConnection(m_connection, m_currentResponse->getConnectionUpgradeParameters());
-        m_connectionState = ConnectionState::DELEGATED;
-      } else {
-        OATPP_LOGW("[oatpp::web::server::HttpProcessor::Coroutine::onResponseFormed()]", "Warning. ConnectionUpgradeHandler not set!");
-        m_connectionState = ConnectionState::CLOSING;
-      }
+    default:
       break;
-    }
 
   }
 
@@ -393,9 +387,27 @@ HttpProcessor::Coroutine::Action HttpProcessor::Coroutine::onResponseFormed() {
 }
   
 HttpProcessor::Coroutine::Action HttpProcessor::Coroutine::onRequestDone() {
-  
-  if(m_connectionState == ConnectionState::ALIVE) {
-    return yieldTo(&HttpProcessor::Coroutine::parseHeaders);
+
+  switch (m_connectionState) {
+    case ConnectionState::ALIVE:
+      return yieldTo(&HttpProcessor::Coroutine::parseHeaders);
+
+    /* Delegate connection handling to another handler only after the response is sent to the client */
+    case ConnectionState::DELEGATED: {
+      auto handler = m_currentResponse->getConnectionUpgradeHandler();
+      if(handler) {
+        handler->handleConnection(m_connection, m_currentResponse->getConnectionUpgradeParameters());
+        m_connectionState = ConnectionState::DELEGATED;
+      } else {
+        OATPP_LOGW("[oatpp::web::server::HttpProcessor::Coroutine::onResponseFormed()]", "Warning. ConnectionUpgradeHandler not set!");
+        m_connectionState = ConnectionState::CLOSING;
+      }
+      break;
+    }
+
+    default:
+      break;
+
   }
   
   return finish();