From 1356be0fe9e15c8205e660e491185827d74bcb07 Mon Sep 17 00:00:00 2001
From: Rider Linden <rider@lindenlab.com>
Date: Wed, 7 Oct 2015 16:09:08 -0700
Subject: [PATCH] MAINT-5691: Browser was using deprecated outbox display type
 AND not correctly returning error body to application. LLCore:HTTP now will
 provide and LLSD translation of the message body when possible in the case of
 an error HTTP result VMM alert boxes now use type="alertmodal" rather than
 "outbox"

---
 indra/llmessage/llcorehttputil.cpp            | 92 ++++++++++++++++++-
 indra/llmessage/llcorehttputil.h              |  1 +
 indra/newview/llappearancemgr.cpp             | 15 ---
 indra/newview/llmarketplacefunctions.cpp      | 41 +++++++--
 .../skins/default/xui/en/notifications.xml    | 22 ++---
 5 files changed, 132 insertions(+), 39 deletions(-)

diff --git a/indra/llmessage/llcorehttputil.cpp b/indra/llmessage/llcorehttputil.cpp
index db1cfbe638b..a93bc03edd6 100644
--- a/indra/llmessage/llcorehttputil.cpp
+++ b/indra/llmessage/llcorehttputil.cpp
@@ -249,12 +249,30 @@ void HttpCoroHandler::onCompleted(LLCore::HttpHandle handle, LLCore::HttpRespons
     if (!status)
     {
         result = LLSD::emptyMap();
+        LLCore::HttpStatus::type_enum_t errType = status.getType();
+
         LL_WARNS()
             << "\n--------------------------------------------------------------------------\n"
-            << " Error[" << status.getType() << "] cannot access url '" << response->getRequestURL()
+            << " Error[" << errType << "] cannot access url '" << response->getRequestURL()
             << "' because " << status.toString()
             << "\n--------------------------------------------------------------------------"
             << LL_ENDL;
+        if ((errType >= 400) && (errType < 500))
+        {
+            LLSD body = this->parseBody(response);
+            if (!body.isUndefined())
+            {
+                if (!body.isMap())
+                {
+                    result[HttpCoroutineAdapter::HTTP_RESULTS_CONTENT] = body;
+                }
+                else
+                {
+                    result = body;
+                }
+            }
+
+        }
     }
     else
     {
@@ -344,6 +362,7 @@ class HttpCoroLLSDHandler : public HttpCoroHandler
 
 protected:
     virtual LLSD handleSuccess(LLCore::HttpResponse * response, LLCore::HttpStatus &status);
+    virtual LLSD parseBody(LLCore::HttpResponse *response);
 };
 
 //-------------------------------------------------------------------------
@@ -357,8 +376,12 @@ LLSD HttpCoroLLSDHandler::handleSuccess(LLCore::HttpResponse * response, LLCore:
 {
     LLSD result;
 
-    const bool emit_parse_errors = false;
+//    const bool emit_parse_errors = false;
+
+
+    result = parseBody(response);
 
+#if 0
     bool parsed = !((response->getBodySize() == 0) ||
         !LLCoreHttpUtil::responseToLLSD(response, emit_parse_errors, result));
 
@@ -378,9 +401,26 @@ LLSD HttpCoroLLSDHandler::handleSuccess(LLCore::HttpResponse * response, LLCore:
             status = LLCore::HttpStatus(499, "Failed to deserialize LLSD.");
         }
     }
+#endif
 
     if (result.isUndefined())
-    {   // If we've gotten to this point and the result LLSD is still undefined 
+    {   
+#if 1
+        // Only emit a warning if we failed to parse when 'content-type' == 'application/llsd+xml'
+        LLCore::HttpHeaders::ptr_t headers(response->getHeaders());
+        const std::string *contentType = (headers) ? headers->find(HTTP_IN_HEADER_CONTENT_TYPE) : NULL;
+
+        if (contentType && (HTTP_CONTENT_LLSD_XML == *contentType))
+        {
+            std::string thebody = LLCoreHttpUtil::responseToString(response);
+            LL_WARNS() << "Failed to deserialize . " << response->getRequestURL() << " [status:" << response->getStatus().toString() << "] "
+                << " body: " << thebody << LL_ENDL;
+
+            // Replace the status with a new one indicating the failure.
+            status = LLCore::HttpStatus(499, "Failed to deserialize LLSD.");
+        }
+#endif
+        // If we've gotten to this point and the result LLSD is still undefined 
         // either there was an issue deserializing the body or the response was
         // blank.  Create an empty map to hold the result either way.
         result = LLSD::emptyMap();
@@ -397,6 +437,22 @@ LLSD HttpCoroLLSDHandler::handleSuccess(LLCore::HttpResponse * response, LLCore:
     return result;
 }
 
+LLSD HttpCoroLLSDHandler::parseBody(LLCore::HttpResponse *response)
+{
+    if (response->getBodySize() == 0)
+        return LLSD();
+
+    LLSD result;
+
+    if (!LLCoreHttpUtil::responseToLLSD(response, true, result))
+    {
+        return LLSD();
+    }
+
+    return result;
+}
+
+
 //========================================================================
 /// The HttpCoroRawHandler is a specialization of the LLCore::HttpHandler for 
 /// interacting with coroutines. 
@@ -411,6 +467,7 @@ class HttpCoroRawHandler : public HttpCoroHandler
     HttpCoroRawHandler(LLEventStream &reply);
 
     virtual LLSD handleSuccess(LLCore::HttpResponse * response, LLCore::HttpStatus &status);
+    virtual LLSD parseBody(LLCore::HttpResponse *response);
 };
 
 //-------------------------------------------------------------------------
@@ -465,6 +522,11 @@ LLSD HttpCoroRawHandler::handleSuccess(LLCore::HttpResponse * response, LLCore::
     return result;
 }
 
+LLSD HttpCoroRawHandler::parseBody(LLCore::HttpResponse *response)
+{
+    return LLSD();
+}
+
 //========================================================================
 /// The HttpCoroJSONHandler is a specialization of the LLCore::HttpHandler for 
 /// interacting with coroutines. 
@@ -479,6 +541,7 @@ class HttpCoroJSONHandler : public HttpCoroHandler
     HttpCoroJSONHandler(LLEventStream &reply);
 
     virtual LLSD handleSuccess(LLCore::HttpResponse * response, LLCore::HttpStatus &status);
+    virtual LLSD parseBody(LLCore::HttpResponse *response);
 };
 
 //-------------------------------------------------------------------------
@@ -516,6 +579,29 @@ LLSD HttpCoroJSONHandler::handleSuccess(LLCore::HttpResponse * response, LLCore:
     return result;
 }
 
+LLSD HttpCoroJSONHandler::parseBody(LLCore::HttpResponse *response)
+{
+    BufferArray * body(response->getBody());
+    if (!body || !body->size())
+    {
+        return LLSD();
+    }
+
+    LLCore::BufferArrayStream bas(body);
+    Json::Value jsonRoot;
+
+    try
+    {
+        bas >> jsonRoot;
+    }
+    catch (std::runtime_error e)
+    {   
+        return LLSD();
+    }
+
+    // Convert the JSON structure to LLSD
+    return LlsdFromJson(jsonRoot);
+}
 
 //========================================================================
 HttpRequestPumper::HttpRequestPumper(const LLCore::HttpRequest::ptr_t &request) :
diff --git a/indra/llmessage/llcorehttputil.h b/indra/llmessage/llcorehttputil.h
index 9328427c341..64601551340 100644
--- a/indra/llmessage/llcorehttputil.h
+++ b/indra/llmessage/llcorehttputil.h
@@ -282,6 +282,7 @@ class HttpCoroHandler : public LLCore::HttpHandler
 protected:
     /// this method may modify the status value
     virtual LLSD handleSuccess(LLCore::HttpResponse * response, LLCore::HttpStatus &status) = 0;
+    virtual LLSD parseBody(LLCore::HttpResponse *response) = 0;
 
 private:
     void buildStatusEntry(LLCore::HttpResponse *response, LLCore::HttpStatus status, LLSD &result);
diff --git a/indra/newview/llappearancemgr.cpp b/indra/newview/llappearancemgr.cpp
index 720a4ff2dff..025009d40b3 100755
--- a/indra/newview/llappearancemgr.cpp
+++ b/indra/newview/llappearancemgr.cpp
@@ -3440,21 +3440,6 @@ void LLAppearanceMgr::serverAppearanceUpdateCoro()
 
         if (!status || !result["success"].asBoolean())
         {
-            if (httpResults.has("error_body"))
-            {
-                std::istringstream bodystream(httpResults["error_body"].asStringRef());
-                LLSD body_llsd;
-
-                if (LLSDSerialize::fromXML(body_llsd, bodystream, true) == LLSDParser::PARSE_FAILURE)
-                {
-                    LL_WARNS() << "Unable to parse body as LLSD" << LL_ENDL;
-                }
-                else
-                {
-                    result = body_llsd;
-                }
-            }
-
             std::string message = (result.has("error")) ? result["error"].asString() : status.toString();
             LL_WARNS("Avatar") << "Appearance Failure. server responded with \"" << message << "\"" << LL_ENDL;
 
diff --git a/indra/newview/llmarketplacefunctions.cpp b/indra/newview/llmarketplacefunctions.cpp
index e8e56ef0cda..dfa33b37efe 100755
--- a/indra/newview/llmarketplacefunctions.cpp
+++ b/indra/newview/llmarketplacefunctions.cpp
@@ -106,10 +106,13 @@ namespace {
 
     ///////////////////////////////////////////////////////////////////////////////
     // SLM Reporters
-    void log_SLM_warning(const std::string& request, U32 status, const std::string& reason, const std::string& code, const std::string& description)
+    void log_SLM_warning(const std::string& request, U32 status, const std::string& reason, const std::string& code, const LLSD& result)
     {
-        LL_WARNS("SLM") << "SLM API : Responder to " << request << ". status : " << status << ", reason : " << reason << ", code : " << code << ", description : " << description << LL_ENDL;
-        if ((status == 422) && (description == "[\"You must have an English description to list the product\", \"You must choose a category for your product before it can be listed\", \"Listing could not change state.\", \"Price can't be blank\"]"))
+
+        LL_WARNS("SLM") << "SLM API : Responder to " << request << ". status : " << status << ", reason : " << reason << ", code : " << code << ", description : " << ll_pretty_print_sd(result) << LL_ENDL;
+        if ((status == 422) && (result.has(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS_CONTENT) && 
+            result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS_CONTENT].isArray() &&
+            result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS_CONTENT].size() > 4))
         {
             // Unprocessable Entity : Special case that error as it is a frequent answer when trying to list an incomplete listing
             LLNotificationsUtil::add("MerchantUnprocessableEntity");
@@ -120,14 +123,32 @@ namespace {
             LLSD subs;
             subs["[ERROR_REASON]"] = reason;
             // We do show long descriptions in the alert (unlikely to be readable). The description string will be in the log though.
-            subs["[ERROR_DESCRIPTION]"] = (description.length() <= 512 ? description : "");
+            std::string description;
+            if (result.has(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS_CONTENT))
+            {
+                LLSD content = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS_CONTENT];
+                if (content.isArray())
+                {
+                    for (LLSD::array_iterator it = content.beginArray(); it != content.endArray(); ++it)
+                    {
+                        if (!description.empty())
+                            description += "\n";
+                        description += (*it).asString();
+                    }
+                }
+                else
+                {
+                    description = content.asString();
+                }
+            }
+            else
+            {
+                description = result.asString();
+            }
+            subs["[ERROR_DESCRIPTION]"] = description;
             LLNotificationsUtil::add("MerchantTransactionFailed", subs);
         }
-    }
 
-    void log_SLM_warning(const std::string& request, U32 status, const std::string& reason, const std::string& code, const LLSD& description)
-    {
-        log_SLM_warning(request, status, reason, code, std::string(ll_pretty_print_sd(description)));
     }
 
     void log_SLM_infos(const std::string& request, U32 status, const std::string& body)
@@ -777,8 +798,8 @@ void LLMarketplaceData::getMerchantStatusCoro()
         else
         {
             std::string err_code = result["error_code"].asString();
-            std::string err_description = result["error_description"].asString();
-            log_SLM_warning("Get /merchant", httpCode, status.toString(), err_code, err_description);
+            //std::string err_description = result["error_description"].asString();
+            log_SLM_warning("Get /merchant", httpCode, status.toString(), err_code, result["error_description"]);
             setSLMStatus(MarketplaceStatusCodes::MARKET_PLACE_CONNECTION_FAILURE);
         }
         return;
diff --git a/indra/newview/skins/default/xui/en/notifications.xml b/indra/newview/skins/default/xui/en/notifications.xml
index f847c732879..66b52c586f7 100755
--- a/indra/newview/skins/default/xui/en/notifications.xml
+++ b/indra/newview/skins/default/xui/en/notifications.xml
@@ -244,7 +244,7 @@ You don't have permission to copy one or more of these items to the Merchant Out
   <notification
    icon="OutboxStatus_Success"
    name="OutboxFolderCreated"
-   type="outbox">
+   type="alertmodal">
     <unique/>
 A new folder has been created for each item you have transferred into the top level of your Merchant Outbox.
 
@@ -257,7 +257,7 @@ A new folder has been created for each item you have transferred into the top le
   <notification
    icon="OutboxStatus_Success"
    name="OutboxImportComplete"
-   type="outbox">
+   type="alertmodal">
 Success
 
 All folders were successfully sent to the Marketplace.
@@ -271,7 +271,7 @@ All folders were successfully sent to the Marketplace.
   <notification
    icon="OutboxStatus_Warning"
    name="OutboxImportHadErrors"
-   type="outbox">
+   type="alertmodal">
 Some folders did not transfer
 
 Errors occurred when some folders were sent to the Marketplace.  Those folders are still in your Merchant Outbox.
@@ -286,7 +286,7 @@ See the [[MARKETPLACE_IMPORTS_URL] error log] for more information.
   <notification
    icon="OutboxStatus_Error"
    name="OutboxImportFailed"
-   type="outbox">
+   type="alertmodal">
 Transfer failed with error &apos;[ERROR_CODE]&apos;
 
 No folders were sent to the Marketplace because of a system or network error.  Try again later.
@@ -299,7 +299,7 @@ No folders were sent to the Marketplace because of a system or network error.  T
   <notification
    icon="OutboxStatus_Error"
    name="OutboxInitFailed"
-   type="outbox">
+   type="alertmodal">
 Marketplace initialization failed with error &apos;[ERROR_CODE]&apos;
 
 Initialization with the Marketplace failed because of a system or network error.  Try again later.
@@ -312,7 +312,7 @@ Initialization with the Marketplace failed because of a system or network error.
     <notification
         icon="OutboxStatus_Error"
         name="StockPasteFailed"
-        type="outbox">
+        type="alertmodal">
         Copy or move to Stock Folder failed with error :
         
         &apos;[ERROR_CODE]&apos;
@@ -325,7 +325,7 @@ Initialization with the Marketplace failed because of a system or network error.
     <notification
         icon="OutboxStatus_Error"
         name="MerchantPasteFailed"
-        type="outbox">
+        type="alertmodal">
         Copy or move to Marketplace Listings failed with error :
         
         &apos;[ERROR_CODE]&apos;
@@ -338,7 +338,7 @@ Initialization with the Marketplace failed because of a system or network error.
     <notification
         icon="OutboxStatus_Error"
         name="MerchantTransactionFailed"
-        type="outbox">
+        type="alertmodal">
         The transaction with the Marketplace failed with the following error :
         
         Reason : &apos;[ERROR_REASON]&apos;
@@ -352,7 +352,7 @@ Initialization with the Marketplace failed because of a system or network error.
     <notification
         icon="OutboxStatus_Error"
         name="MerchantUnprocessableEntity"
-        type="outbox">
+        type="alertmodal">
         We are unable to list this product or activate the version folder. Usually this is caused by missing information in the listing description form, but it may be due to errors in the folder structure. Either edit the listing or check the listing folder for errors.
         
         <usetemplate
@@ -363,7 +363,7 @@ Initialization with the Marketplace failed because of a system or network error.
     <notification
         icon="OutboxStatus_Error"
         name="MerchantListingFailed"
-        type="outbox">
+        type="alertmodal">
         Listing to Marketplace failed with error :
         
         &apos;[ERROR_CODE]&apos;
@@ -376,7 +376,7 @@ Initialization with the Marketplace failed because of a system or network error.
     <notification
         icon="OutboxStatus_Error"
         name="MerchantFolderActivationFailed"
-        type="outbox">
+        type="alertmodal">
         Activating this version folder failed with error :
 
         &apos;[ERROR_CODE]&apos;
-- 
GitLab