LCOV - code coverage report
Current view: top level - OdbDesignLib/App - RouteController.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 0.0 % 55 0
Test Date: 2026-11-19 04:56:22 Functions: 0.0 % 6 0
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "RouteController.h"
       2              : #include <ETag.h>
       3              : 
       4              : 
       5              : namespace Odb::Lib::App
       6              : {
       7            0 :         RouteController::RouteController(IOdbServerApp& serverApp)
       8            0 :                 : m_serverApp(serverApp)
       9              :         {
      10            0 :         }
      11              : 
      12            0 :         void RouteController::register_route_handler(std::string route, TRouteHandlerFunction handler)
      13              :         {                               
      14            0 :                 m_serverApp.crow_app().route_dynamic(std::move(route))
      15            0 :                         ([/*&,*/ handler](const crow::request& req)
      16              :                                 {
      17              :                                         //// authenticate request before sending to handler
      18              :                                         //auto authResp = m_serverApp.request_auth().AuthenticateRequest(req);
      19              :                                         //if (authResp.code != crow::status::OK)
      20              :                                         //{
      21              :                                         //      return authResp;
      22              :                                         //}
      23              : 
      24            0 :                                         return handler(req);
      25              :                                 });
      26              : 
      27              :                 //app.route<crow::black_magick::get_parameter_tag(url)>(url)
      28              :                 // or
      29              :                 //app.route_dynamic(url)
      30              : 
      31              :                 //m_serverApp.crow_app().route_dynamic(const_cast<std::string&&>(route)).methods("GET"_method, "POST"_method)([&](const crow::request& req)
      32              :                 //      {
      33              :                 //              return handler(req);
      34              :                 //      })
      35              : 
      36              :                 ////.template register_handler<crow::black_magic::crow_internal::get_parameter_tag()>(m_pServerApp->crow_app(), handler);
      37              :                 //CROW_ROUTE(m_pServerApp->crow_app(), "/steps/edadata")
      38              :                 //      ([&](const crow::request& req)
      39              :                 //              {
      40              :                 //                      return handler(req);
      41              :                 //              });
      42            0 :         }
      43              : 
      44            0 :         crow::response Odb::Lib::App::RouteController::makeLoadedFileModelsResponse(bool created) const
      45              :         {
      46            0 :                 auto unloadedDesignNames = m_serverApp.designs().getUnloadedDesignNames();
      47            0 :                 auto loadedFileArchiveNames = m_serverApp.designs().getLoadedFileArchiveNames();
      48            0 :                 auto loadedDesignNames = m_serverApp.designs().getLoadedDesignNames();
      49              : 
      50            0 :                 crow::json::wvalue::list designs;
      51            0 :                 for (const auto& designName : unloadedDesignNames)
      52              :                 {
      53            0 :                         auto loaded = false;
      54            0 :                         auto isDesign = false;
      55            0 :                         if (std::find(loadedFileArchiveNames.begin(), loadedFileArchiveNames.end(), designName) != loadedFileArchiveNames.end())
      56              :                         {
      57            0 :                                 loaded = true;
      58            0 :                                 isDesign = false;
      59              :                         }
      60            0 :                         else if (std::find(loadedDesignNames.begin(), loadedDesignNames.end(), designName) != loadedDesignNames.end())
      61              :                         {
      62            0 :                                 loaded = true;
      63            0 :                                 isDesign = true;
      64              :                         }
      65            0 :                         crow::json::wvalue design;
      66            0 :                         design["name"] = designName;
      67            0 :                         design["loaded"] = loaded;
      68            0 :                         design["type"] = isDesign? "Design" : "FileArchive";
      69            0 :                         designs.push_back(design);
      70            0 :                 }
      71            0 :                 crow::json::wvalue jsonResponse;
      72            0 :                 jsonResponse["filearchives"] = std::move(designs);
      73              : 
      74              : #if defined(_DEBUG)
      75              :                 auto j = jsonResponse.dump();
      76              : #endif
      77              : 
      78              :                 // determine type of success code
      79            0 :                 auto httpCode = crow::status::OK;
      80            0 :                 if (created)
      81              :                 {
      82            0 :                         httpCode = crow::status::CREATED;
      83              :                 }
      84            0 :                 else if (unloadedDesignNames.empty())
      85              :                 {
      86            0 :                         httpCode = crow::status::NO_CONTENT;
      87              :                 }
      88              : 
      89            0 :                 return crow::response(httpCode , jsonResponse);
      90            0 :         }
      91              : 
      92            0 :         std::optional<crow::response> RouteController::checkConditionalGet(const crow::request& req,
      93              :                 const std::string& designName,
      94              :                 const std::string& endpointPath,
      95              :                 std::string& etag) const
      96              :         {
      97              :         // Computed from the archive file on disk plus the design's cache
      98              :         // invalidation generation, not the (possibly not yet loaded) cached
      99              :         // object, so the 304 decision never pays for a load or a serialization.
     100              :         // The generation covers in-memory-only changes (POST /filemodels with
     101              :         // save=false replaces the cache without touching the file): those bump
     102              :         // the generation, rotating the tag, so revalidation serves a fresh 200
     103              :         // instead of 304-forever. A stat-to-load race (archive replaced in
     104              :         // between) is benign: the 200 body then carries the pre-load tag, the
     105              :         // next request recomputes the post-replacement tag, and the client's
     106              :         // stale copy is revalidated and refreshed.
     107            0 :         etag = Utils::MakeDesignEtag(m_serverApp.args().designsDir(), designName, endpointPath,
     108            0 :                 m_serverApp.designs().GetResponseGeneration(designName));
     109            0 :                 if (etag.empty())
     110              :                 {
     111            0 :                         return std::nullopt;
     112              :                 }
     113              : 
     114            0 :                 const auto& ifNoneMatch = req.get_header_value("If-None-Match");
     115            0 :                 if (!ifNoneMatch.empty() && Utils::IfNoneMatchMatches(ifNoneMatch, etag))
     116              :                 {
     117              :                         // 304 short-circuits the handler: no archive load, no to_json().
     118              :                         // ETag + Cache-Control are echoed per RFC 7232 so the client can
     119              :                         // refresh its cached header set.
     120            0 :                         crow::response notModified(crow::status::NOT_MODIFIED);
     121            0 :                         notModified.set_header("ETag", etag);
     122            0 :                         notModified.set_header("Cache-Control", CACHE_CONTROL_DATA_VALUE);
     123            0 :                         return notModified;
     124            0 :                 }
     125              : 
     126            0 :                 return std::nullopt;
     127              :         }
     128              : 
     129            0 :         crow::response RouteController::withCacheHeaders(crow::response response, const std::string& etag) const
     130              :         {
     131              :                 // private: design payloads are user-scoped data and must not be served
     132              :                 // from shared/proxy caches; max-age allows short-term client reuse
     133              :                 // between revalidations.
     134            0 :                 if (!etag.empty())
     135              :                 {
     136            0 :                         response.set_header("ETag", etag);
     137            0 :                         response.set_header("Cache-Control", CACHE_CONTROL_DATA_VALUE);
     138              :                 }
     139            0 :                 return response;
     140              :         }
     141              : }
        

Generated by: LCOV version 2.0-1