| 135 | } |
| 136 | |
| 137 | bool RestServer::Start() |
| 138 | { |
| 139 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 140 | |
| 141 | if (m_Running) |
| 142 | { |
| 143 | return true; // Already running |
| 144 | } |
| 145 | |
| 146 | // Join any stale server thread from a previous failed start. |
| 147 | // When listen() fails asynchronously the thread exits but m_ServerThread is never |
| 148 | // joined. Destroying a joinable std::thread is undefined behaviour (terminate or |
| 149 | // resource_deadlock_would_occur on MSVC), so we must join it before proceeding. |
| 150 | if (m_ServerThread) |
| 151 | { |
| 152 | std::unique_ptr<std::thread> staleThread = std::move(m_ServerThread); |
| 153 | lock.unlock(); |
| 154 | staleThread->join(); |
| 155 | lock.lock(); |
| 156 | } |
| 157 | |
| 158 | m_RequestLog.clear(); |
| 159 | ++m_RequestLogVersion; |
| 160 | m_ClientIPs.clear(); |
| 161 | m_RateLimitMap.clear(); |
| 162 | m_RateLimitCheckCount = 0; |
| 163 | |
| 164 | if (!m_PendingConfig.enabled) |
| 165 | { |
| 166 | m_LastError = "Server is disabled in configuration"; |
| 167 | MITK_ERROR << *m_LastError; |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // Validate HTTPS configuration if enabled |
| 172 | if (m_PendingConfig.httpsEnabled) |
| 173 | { |
| 174 | if (m_PendingConfig.sslCertPath.empty()) |
| 175 | { |
| 176 | m_LastError = "HTTPS enabled but no certificate path configured"; |
| 177 | MITK_ERROR << *m_LastError; |
| 178 | return false; |
| 179 | } |
| 180 | if (m_PendingConfig.sslKeyPath.empty()) |
| 181 | { |
| 182 | m_LastError = "HTTPS enabled but no private key path configured"; |
| 183 | MITK_ERROR << *m_LastError; |
| 184 | return false; |
| 185 | } |
| 186 | if (!fs::exists(m_PendingConfig.sslCertPath)) |
| 187 | { |
| 188 | m_LastError = "SSL certificate file not found: " + SanitizeForLog(m_PendingConfig.sslCertPath); |
| 189 | MITK_ERROR << *m_LastError; |
| 190 | return false; |
| 191 | } |
| 192 | if (!fs::exists(m_PendingConfig.sslKeyPath)) |
| 193 | { |
| 194 | m_LastError = "SSL private key file not found: " + SanitizeForLog(m_PendingConfig.sslKeyPath); |