# HG changeset patch # User Alain Mazy # Date 1751901189 -7200 # Node ID da6430b05c8d4297c227d710bed1416d581b1fb7 # Parent 96c7a44a595b69490a7e0459189ebc9089cb9715# Parent 67dbb5004469bc09965db7a77dbf385247d8c4e1 merge diff -r 96c7a44a595b -r da6430b05c8d OrthancFramework/Sources/HttpServer/HttpServer.cpp --- a/OrthancFramework/Sources/HttpServer/HttpServer.cpp Mon Jul 07 17:12:55 2025 +0200 +++ b/OrthancFramework/Sources/HttpServer/HttpServer.cpp Mon Jul 07 17:13:09 2025 +0200 @@ -621,7 +621,7 @@ enum AccessMode { - AccessMode_Forbidden, + AccessMode_NotAuthenticated, AccessMode_AuthorizationToken, AccessMode_RegisteredUser }; @@ -657,7 +657,7 @@ } } - return AccessMode_Forbidden; + return AccessMode_NotAuthenticated; } @@ -1203,15 +1203,6 @@ AccessMode accessMode = IsAccessGranted(server, headers); - // Authenticate this connection - if (server.IsAuthenticationEnabled() && - accessMode == AccessMode_Forbidden) - { - output.SendUnauthorized(server.GetRealm()); // 401 error - return; - } - - #if ORTHANC_ENABLE_MONGOOSE == 1 // Apply the filter, if it is installed char remoteIp[24]; @@ -1248,6 +1239,41 @@ } + // Authenticate this connection + if (server.IsAuthenticationEnabled() && + accessMode == AccessMode_NotAuthenticated) + { + if (server.IsRedirectNotAuthenticatedToRoot() && + uri.size() > 0) + { + // This is new in Orthanc 1.12.9 + std::string redirectionToRoot; + + std::string tmp(requestUri); + if (tmp.back() == '/') + { + redirectionToRoot = "../"; + } + else + { + redirectionToRoot = "./"; + } + + for (size_t i = 0; i < uri.size() - 1; i++) + { + redirectionToRoot += "../"; + } + + output.Redirect(redirectionToRoot); + } + else + { + output.SendUnauthorized(server.GetRealm()); // 401 error + } + return; + } + + // Compute the HTTP method, taking method faking into consideration method = HttpMethod_Get; @@ -1300,7 +1326,7 @@ // filter. In the case of an authorization bearer token, grant // full access to the API. - assert(accessMode == AccessMode_Forbidden || // Could be the case if "!server.IsAuthenticationEnabled()" + assert(accessMode == AccessMode_NotAuthenticated || // Could be the case if "!server.IsAuthenticationEnabled()" accessMode == AccessMode_RegisteredUser); IIncomingHttpRequestFilter *filter = server.GetIncomingHttpRequestFilter(); @@ -1601,7 +1627,8 @@ realm_(ORTHANC_REALM), threadsCount_(50), // Default value in mongoose/civetweb tcpNoDelay_(true), - requestTimeout_(30) // Default value in mongoose/civetweb (30 seconds) + requestTimeout_(30), // Default value in mongoose/civetweb (30 seconds) + redirectNotAuthenticatedToRoot_(false) { #if ORTHANC_ENABLE_MONGOOSE == 1 CLOG(INFO, HTTP) << "This Orthanc server uses Mongoose as its embedded HTTP server"; @@ -2204,4 +2231,17 @@ } } #endif + + + bool HttpServer::IsRedirectNotAuthenticatedToRoot() const + { + return redirectNotAuthenticatedToRoot_; + } + + + void HttpServer::SetRedirectNotAuthenticatedToRoot(bool redirect) + { + Stop(); + redirectNotAuthenticatedToRoot_ = redirect; + } } diff -r 96c7a44a595b -r da6430b05c8d OrthancFramework/Sources/HttpServer/HttpServer.h --- a/OrthancFramework/Sources/HttpServer/HttpServer.h Mon Jul 07 17:12:55 2025 +0200 +++ b/OrthancFramework/Sources/HttpServer/HttpServer.h Mon Jul 07 17:13:09 2025 +0200 @@ -114,6 +114,7 @@ unsigned int threadsCount_; bool tcpNoDelay_; unsigned int requestTimeout_; // In seconds + bool redirectNotAuthenticatedToRoot_; // New in Orthanc 1.12.9 #if ORTHANC_ENABLE_PUGIXML == 1 WebDavBuckets webDavBuckets_; @@ -226,5 +227,9 @@ const std::map& headers, const std::string& body, const std::string& boundary); + + bool IsRedirectNotAuthenticatedToRoot() const; + + void SetRedirectNotAuthenticatedToRoot(bool redirect); }; }