changeset 6223:da6430b05c8d

merge
author Alain Mazy <am@orthanc.team>
date Mon, 07 Jul 2025 17:13:09 +0200
parents 96c7a44a595b (current diff) 67dbb5004469 (diff)
children 83e1282c5403 71e404609278
files
diffstat 2 files changed, 58 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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;
+  }
 }
--- 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<std::string, std::string>& headers,
                                   const std::string& body,
                                   const std::string& boundary);
+
+    bool IsRedirectNotAuthenticatedToRoot() const;
+
+    void SetRedirectNotAuthenticatedToRoot(bool redirect);
   };
 }