diff options
Diffstat (limited to 'indra/llmessage')
-rw-r--r-- | indra/llmessage/llcorehttputil.cpp | 75 | ||||
-rw-r--r-- | indra/llmessage/llcorehttputil.h | 20 |
2 files changed, 72 insertions, 23 deletions
diff --git a/indra/llmessage/llcorehttputil.cpp b/indra/llmessage/llcorehttputil.cpp index 2ef9f59e2f..521131a986 100644 --- a/indra/llmessage/llcorehttputil.cpp +++ b/indra/llmessage/llcorehttputil.cpp @@ -39,6 +39,16 @@ using namespace LLCore; namespace LLCoreHttpUtil { +void logMessageSuccess(std::string logAuth, std::string url, std::string message) +{ + LL_INFOS() << logAuth << " Success '" << message << "' for " << url << LL_ENDL; +} + +void logMessageFail(std::string logAuth, std::string url, std::string message) +{ + LL_WARNS() << logAuth << " Failure '" << message << "' for " << url << LL_ENDL; +} + //========================================================================= /// The HttpRequestPumper is a utility class. When constructed it will poll the /// supplied HttpRequest once per frame until it is destroyed. @@ -698,14 +708,24 @@ LLCore::HttpStatus HttpCoroutineAdapter::getStatusFromLLSD(const LLSD &httpResul } /*static*/ -void HttpCoroutineAdapter::genericHttpGet(const std::string &url, const std::string &success, const std::string &failure) +void HttpCoroutineAdapter::callbackHttpGet(const std::string &url, completionCallback_t success, completionCallback_t failure) { LLCoros::instance().launch("HttpCoroutineAdapter::genericGetCoro", - boost::bind(&HttpCoroutineAdapter::genericGetCoro, _1, url, success, failure)); + boost::bind(&HttpCoroutineAdapter::trivialGetCoro, _1, url, success, failure)); } /*static*/ -void HttpCoroutineAdapter::genericGetCoro(LLCoros::self& self, std::string &url, std::string success, std::string failure) +void HttpCoroutineAdapter::messageHttpGet(const std::string &url, const std::string &success, const std::string &failure) +{ + completionCallback_t cbSuccess = (success.empty()) ? NULL : + static_cast<completionCallback_t>(boost::bind(&logMessageSuccess, "HttpCoroutineAdapter", url, success)); + completionCallback_t cbFailure = (failure.empty()) ? NULL : + static_cast<completionCallback_t>(boost::bind(&logMessageFail, "HttpCoroutineAdapter", url, failure)); + callbackHttpGet(url, cbSuccess, cbFailure); +} + +/*static*/ +void HttpCoroutineAdapter::trivialGetCoro(LLCoros::self& self, std::string &url, completionCallback_t success, completionCallback_t failure) { LLCore::HttpRequest::policy_t httpPolicy(LLCore::HttpRequest::DEFAULT_POLICY_ID); LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t @@ -719,28 +739,43 @@ void HttpCoroutineAdapter::genericGetCoro(LLCoros::self& self, std::string &url, LLSD httpResults = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS]; LLCore::HttpStatus status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults); - if (status) + if (!status) { - LL_INFOS("HttpCoroutineAdapter", "genericGetCoro") << "Success for " << url << std::endl << - "Message: '" << success << "'" << LL_ENDL; + if (failure) + { + failure(httpResults); + } } else { - LL_WARNS("HttpCoroutineAdapter", "genericGetCoro") << "Failure for " << url << std::endl << - "Message: '" << failure << "'" << std::endl << - "Status: " << status.toTerseString() << ": " << status.getMessage() << LL_ENDL; + if (success) + { // remove the added http_result entry from the results before calling the callback. + result.erase(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS); + success(result); + } } } /*static*/ -void HttpCoroutineAdapter::genericHttpPost(const std::string &url, const LLSD &postData, const std::string &success, const std::string &failure) +void HttpCoroutineAdapter::callbackHttpPost(const std::string &url, const LLSD &postData, completionCallback_t success, completionCallback_t failure) { LLCoros::instance().launch("HttpCoroutineAdapter::genericPostCoro", - boost::bind(&HttpCoroutineAdapter::genericPostCoro, _1, url, postData, success, failure)); + boost::bind(&HttpCoroutineAdapter::trivialPostCoro, _1, url, postData, success, failure)); } /*static*/ -void HttpCoroutineAdapter::genericPostCoro(LLCoros::self& self, std::string &url, LLSD postData, std::string success, std::string failure) +void HttpCoroutineAdapter::messageHttpPost(const std::string &url, const LLSD &postData, const std::string &success, const std::string &failure) +{ + completionCallback_t cbSuccess = (success.empty()) ? NULL : + static_cast<completionCallback_t>(boost::bind(&logMessageSuccess, "HttpCoroutineAdapter", url, success)); + completionCallback_t cbFailure = (failure.empty()) ? NULL : + static_cast<completionCallback_t>(boost::bind(&logMessageFail, "HttpCoroutineAdapter", url, failure)); + + callbackHttpPost(url, postData, cbSuccess, cbFailure); +} + +/*static*/ +void HttpCoroutineAdapter::trivialPostCoro(LLCoros::self& self, std::string &url, LLSD postData, completionCallback_t success, completionCallback_t failure) { LLCore::HttpRequest::policy_t httpPolicy(LLCore::HttpRequest::DEFAULT_POLICY_ID); LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t @@ -754,16 +789,20 @@ void HttpCoroutineAdapter::genericPostCoro(LLCoros::self& self, std::string &url LLSD httpResults = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS]; LLCore::HttpStatus status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults); - if (status) + if (!status) { - LL_INFOS("HttpCoroutineAdapter", "genericPostCoro") << "Success for " << url << std::endl << - "Message: '" << success << "'" << LL_ENDL; + if (failure) + { + failure(httpResults); + } } else { - LL_WARNS("HttpCoroutineAdapter", "genericPostCoro") << "Failure for " << url << std::endl << - "Message: '" << failure << "'" << std::endl << - "Status: " << status.toTerseString() << ": " << status.getMessage() << LL_ENDL; + if (success) + { // remove the added http_result entry from the results before calling the callback. + result.erase(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS); + success(result); + } } } diff --git a/indra/llmessage/llcorehttputil.h b/indra/llmessage/llcorehttputil.h index 7d60bfbbaf..73a51fe83a 100644 --- a/indra/llmessage/llcorehttputil.h +++ b/indra/llmessage/llcorehttputil.h @@ -387,16 +387,26 @@ public: /// void cancelYieldingOperation(); - static LLCore::HttpStatus getStatusFromLLSD(const LLSD &httpResults); + /// The convenience routines below can be provided with callback functors + /// which will be invoked in the case of success or failure. These callbacks + /// should match this form. + /// @sa callbackHttpGet + /// @sa callbackHttpPost + typedef boost::function<void (const LLSD &)> completionCallback_t; + + static void callbackHttpGet(const std::string &url, completionCallback_t success = NULL, completionCallback_t failure = NULL); + static void callbackHttpPost(const std::string &url, const LLSD &postData, completionCallback_t success = NULL, completionCallback_t failure = NULL); + /// Generic Get and post routines for HTTP via coroutines. /// These static methods do all required setup for the GET or POST operation. /// When the operation completes successfully they will put the success message in the log at INFO level, /// If the operation fails the failure message is written to the log at WARN level. /// - static void genericHttpGet(const std::string &url, const std::string &success = std::string(), const std::string &failure = std::string()); - static void genericHttpPost(const std::string &url, const LLSD &postData, const std::string &success, const std::string &failure); + static void messageHttpGet(const std::string &url, const std::string &success = std::string(), const std::string &failure = std::string()); + static void messageHttpPost(const std::string &url, const LLSD &postData, const std::string &success, const std::string &failure); + private: static LLSD buildImmediateErrorResult(const LLCore::HttpRequest::ptr_t &request, const std::string &url); @@ -428,8 +438,8 @@ private: const std::string & url, LLCore::HttpOptions::ptr_t &options, LLCore::HttpHeaders::ptr_t &headers, HttpCoroHandler::ptr_t &handler); - static void genericGetCoro(LLCoros::self& self, std::string &url, std::string success, std::string failure); - static void genericPostCoro(LLCoros::self& self, std::string &url, LLSD postData, std::string success, std::string failure); + static void trivialGetCoro(LLCoros::self& self, std::string &url, completionCallback_t success, completionCallback_t failure); + static void trivialPostCoro(LLCoros::self& self, std::string &url, LLSD postData, completionCallback_t success, completionCallback_t failure); std::string mAdapterName; LLCore::HttpRequest::priority_t mPriority; |