Loading src/authdb.cpp +34 −26 Changes for src/authdb.cpp: 34 added lines, 26 removed lines. Original line number Diff line number Diff line Loading @@ -29,8 +29,11 @@ #include <iostream> #include <cstring> #include <mutex> #include <shared_mutex> #include <fstream> #include <thread> #include <map> #include <memory> #include <cmdplus/cmdplus.h> #include <confplus/conf.h> Loading Loading @@ -266,8 +269,6 @@ namespace authdb { rep.send(curreq,e.what(),strlen(e.what())); } }else if(curreq.getRequestURL().compare(0,4,"/api",4)==0){ DomainBackend db(_AdminBackend); AuthBackend *apidb=nullptr; char domain[255]={0}; if(sscanf(curreq.getRequestURL().c_str(),"/api/%254[^/]s",domain)<0){ Loading @@ -288,12 +289,9 @@ namespace authdb { return; } DomainInfo dinfo; { AuthBackend::Guard admlock(_AdminBackend); AuthBackend *apidb = nullptr; try { dinfo=db.resolve(did.value); apidb = getDomainBackend(did); }catch(std::exception &e){ libhttppp::HttpResponse rep; rep.setState(HTTP500); Loading @@ -307,23 +305,6 @@ namespace authdb { rep.send(curreq,"internal error loading domain",29); return; } } try { apidb=db.create(dinfo); }catch(std::exception &e){ libhttppp::HttpResponse rep; rep.setState(HTTP500); rep.setContentType("text/html"); rep.send(curreq,e.what(),strlen(e.what())); return; }catch(...){ libhttppp::HttpResponse rep; rep.setState(HTTP500); rep.setContentType("text/html"); rep.send(curreq,"internal error creating domain backend",38); return; } if(apidb){ AuthBackend::Guard guard(*apidb); Loading Loading @@ -380,7 +361,6 @@ namespace authdb { void getAvatar(libhttppp::HttpRequest &curreq, const int tid, ULONG_PTR args){ libhttppp::HttpResponse rep; DomainBackend dbackend(_AdminBackend); AuthBackend *backend=nullptr; char domain[255],cuid[255],ext[16]; Loading @@ -392,7 +372,7 @@ namespace authdb { if(strcmp(domain,"admin") == 0 ){ backend=&_AdminBackend; }else if(did.parse(domain) == 0 ){ backend=dbackend.data(did); backend=getDomainBackend(did); }else{ throw AuthBackendError("createUser: could not parse uuid!"); } Loading Loading @@ -431,12 +411,40 @@ namespace authdb { }; AuthBackend *getDomainBackend(const ::uuid::uuid &did){ { std::shared_lock<std::shared_mutex> rl(_DomainCacheMtx); auto it = _DomainCache.find(std::string(did.value, did.value+16)); if(it != _DomainCache.end()) return it->second.get(); } DomainBackend db(_AdminBackend); DomainInfo dinfo; { AuthBackend::Guard admlock(_AdminBackend); dinfo = db.resolve(did.value); } if(!dinfo.found) return nullptr; auto backend = std::make_unique<AuthBackend>( dinfo.storageType, dinfo.storageOptions, dinfo.domainName); std::unique_lock<std::shared_mutex> wl(_DomainCacheMtx); auto key = std::string(did.value, did.value+16); auto [it, inserted] = _DomainCache.emplace(key, std::move(backend)); return it->second.get(); } private: AuthBackend &_AdminBackend; Session _Session; AdminInterface _AdminInterface; std::vector<char> _AuthJs; std::vector<char> _AuthCss; std::shared_mutex _DomainCacheMtx; std::map<std::string, std::unique_ptr<AuthBackend>> _DomainCache; }; int searchValue(authdb::AuthBackend &backend,const char*fieldname,const char *value){ Loading Loading
src/authdb.cpp +34 −26 Changes for src/authdb.cpp: 34 added lines, 26 removed lines. Original line number Diff line number Diff line Loading @@ -29,8 +29,11 @@ #include <iostream> #include <cstring> #include <mutex> #include <shared_mutex> #include <fstream> #include <thread> #include <map> #include <memory> #include <cmdplus/cmdplus.h> #include <confplus/conf.h> Loading Loading @@ -266,8 +269,6 @@ namespace authdb { rep.send(curreq,e.what(),strlen(e.what())); } }else if(curreq.getRequestURL().compare(0,4,"/api",4)==0){ DomainBackend db(_AdminBackend); AuthBackend *apidb=nullptr; char domain[255]={0}; if(sscanf(curreq.getRequestURL().c_str(),"/api/%254[^/]s",domain)<0){ Loading @@ -288,12 +289,9 @@ namespace authdb { return; } DomainInfo dinfo; { AuthBackend::Guard admlock(_AdminBackend); AuthBackend *apidb = nullptr; try { dinfo=db.resolve(did.value); apidb = getDomainBackend(did); }catch(std::exception &e){ libhttppp::HttpResponse rep; rep.setState(HTTP500); Loading @@ -307,23 +305,6 @@ namespace authdb { rep.send(curreq,"internal error loading domain",29); return; } } try { apidb=db.create(dinfo); }catch(std::exception &e){ libhttppp::HttpResponse rep; rep.setState(HTTP500); rep.setContentType("text/html"); rep.send(curreq,e.what(),strlen(e.what())); return; }catch(...){ libhttppp::HttpResponse rep; rep.setState(HTTP500); rep.setContentType("text/html"); rep.send(curreq,"internal error creating domain backend",38); return; } if(apidb){ AuthBackend::Guard guard(*apidb); Loading Loading @@ -380,7 +361,6 @@ namespace authdb { void getAvatar(libhttppp::HttpRequest &curreq, const int tid, ULONG_PTR args){ libhttppp::HttpResponse rep; DomainBackend dbackend(_AdminBackend); AuthBackend *backend=nullptr; char domain[255],cuid[255],ext[16]; Loading @@ -392,7 +372,7 @@ namespace authdb { if(strcmp(domain,"admin") == 0 ){ backend=&_AdminBackend; }else if(did.parse(domain) == 0 ){ backend=dbackend.data(did); backend=getDomainBackend(did); }else{ throw AuthBackendError("createUser: could not parse uuid!"); } Loading Loading @@ -431,12 +411,40 @@ namespace authdb { }; AuthBackend *getDomainBackend(const ::uuid::uuid &did){ { std::shared_lock<std::shared_mutex> rl(_DomainCacheMtx); auto it = _DomainCache.find(std::string(did.value, did.value+16)); if(it != _DomainCache.end()) return it->second.get(); } DomainBackend db(_AdminBackend); DomainInfo dinfo; { AuthBackend::Guard admlock(_AdminBackend); dinfo = db.resolve(did.value); } if(!dinfo.found) return nullptr; auto backend = std::make_unique<AuthBackend>( dinfo.storageType, dinfo.storageOptions, dinfo.domainName); std::unique_lock<std::shared_mutex> wl(_DomainCacheMtx); auto key = std::string(did.value, did.value+16); auto [it, inserted] = _DomainCache.emplace(key, std::move(backend)); return it->second.get(); } private: AuthBackend &_AdminBackend; Session _Session; AdminInterface _AdminInterface; std::vector<char> _AuthJs; std::vector<char> _AuthCss; std::shared_mutex _DomainCacheMtx; std::map<std::string, std::unique_ptr<AuthBackend>> _DomainCache; }; int searchValue(authdb::AuthBackend &backend,const char*fieldname,const char *value){ Loading