| 1996 | |
| 1997 | template <UserRole role> |
| 1998 | Reindexer HTTPServer::getDB(http::Context& ctx, std::string* dbNameOut) { |
| 1999 | std::string dbName = urldecode2(ctx.request->urlParams[0]); |
| 2000 | |
| 2001 | AuthContext dummyCtx; |
| 2002 | AuthContext* actx = &dummyCtx; |
| 2003 | if (!dbMgr_.IsNoSecurity()) { |
| 2004 | auto clientData = dynamic_cast<HTTPClientData*>(ctx.clientData.get()); |
| 2005 | assertrx(clientData); |
| 2006 | actx = &clientData->auth; // -V522 |
| 2007 | } |
| 2008 | |
| 2009 | auto err = dbMgr_.OpenDatabase(dbName, *actx, false); |
| 2010 | if (!err.ok()) { |
| 2011 | throw http::HttpStatus(err); |
| 2012 | } |
| 2013 | if (dbNameOut) { |
| 2014 | *dbNameOut = std::move(dbName); |
| 2015 | } |
| 2016 | |
| 2017 | Reindexer* db = nullptr; |
| 2018 | err = actx->GetDB<AuthContext::CalledFrom::HTTPServer>(role, &db); |
| 2019 | if (!err.ok()) { |
| 2020 | throw http::HttpStatus(err); |
| 2021 | } |
| 2022 | |
| 2023 | assertrx(db); |
| 2024 | std::string_view timeoutHeader = ctx.request->headers.Get("Request-Timeout"sv); |
| 2025 | std::optional<int> timeoutSec; |
| 2026 | if (!timeoutHeader.empty()) { |
| 2027 | timeoutSec = try_stoi(timeoutHeader); |
| 2028 | if (!timeoutSec.has_value()) [[unlikely]] { |
| 2029 | logger_.warn("Unable to get integer value from 'Request-Timeout'-header('{}'). Using default value", timeoutHeader); |
| 2030 | } |
| 2031 | } |
| 2032 | std::chrono::seconds timeout; |
| 2033 | |
| 2034 | if constexpr (role == kUnauthorized || role == kRoleNone) { |
| 2035 | throw Error(errLogic, "Unexpected user's role"); |
| 2036 | } else if constexpr (role == kRoleDataRead) { |
| 2037 | timeout = timeoutSec.has_value() ? std::chrono::seconds(timeoutSec.value()) : serverConfig_.HttpReadTimeout; |
| 2038 | } else if constexpr (role >= kRoleDataWrite) { |
| 2039 | timeout = timeoutSec.has_value() ? std::chrono::seconds(timeoutSec.value()) : serverConfig_.HttpWriteTimeout(); |
| 2040 | } |
| 2041 | const auto needMaskDSN = NeedMaskingDSN(actx->UserRights() < kRoleDBAdmin); |
| 2042 | return db->NeedTraceActivity() |
| 2043 | ? db->WithContextParams(timeout, needMaskDSN, ctx.request->clientAddr, std::string(ctx.request->headers.Get("User-Agent"))) |
| 2044 | : db->WithContextParams(timeout, needMaskDSN, std::string(), std::string()); |
| 2045 | } |
| 2046 | |
| 2047 | std::string HTTPServer::getNameFromJson(std::string_view json) { |
| 2048 | try { |
nothing calls this directly
no test coverage detected