Skip to content
fixCppTrackables.patch 6.86 KiB
Newer Older
Jerome Royan's avatar
Jerome Royan committed
--- TrackablesApi.cpp	2024-09-19 14:26:36.877177137 +0200
+++ TrackablesApi_corrected.cpp	2024-09-19 14:27:58.857174515 +0200
@@ -159,86 +159,100 @@
     session->close(status, result, { {"Connection", "close"} });
 }
 
+void extractBodyContentAsync(
+    const std::shared_ptr<restbed::Session>& session,
+    int content_length,
+    std::function<void(const std::string&)> callback)
+{
+    session->fetch(content_length,
+        [callback](const std::shared_ptr<restbed::Session> session,
+                   const restbed::Bytes &body) {
+            std::string bodyContent = restbed::String::format(
+                "%.*s\n", (int)body.size(), body.data());
+            callback(bodyContent);
+        });
+}
+
 void TrackablesResource::handler_POST_internal(const std::shared_ptr<restbed::Session> session)
 {
     const auto request = session->get_request();
-    // body params or form params here from the body content string
-    std::string bodyContent = extractBodyContent(session);
-    auto trackable = extractJsonModelBodyParam<Trackable>(bodyContent);
+
     // Getting the headers
-    std::string token = request->get_header("token", "");
-    
-    int status_code = 500;
-    Success resultObject = Success{};
-    std::string result = "";
-    
-    try {
-        std::tie(status_code, resultObject) =
-            handler_POST(trackable, token);
-    }
-    catch(const TrackablesApiException& e) {
-        std::tie(status_code, result) = handleTrackablesApiException(e);
-    }
-    catch(const std::exception& e) {
-        std::tie(status_code, result) = handleStdException(e);
-    }
-    catch(...) {
-        std::tie(status_code, result) = handleUnspecifiedException();
-    }
-    
-    std::multimap< std::string, std::string > responseHeaders {};
-    static const std::vector<std::string> contentTypes{
-        "application/json",
-    };
-    static const std::string acceptTypes{
-        "application/json, "
-    };
-    
-    if (status_code == 200) {
-        responseHeaders.insert(std::make_pair("Content-Type", selectPreferredContentType(contentTypes)));
-        if (!acceptTypes.empty()) {
-            responseHeaders.insert(std::make_pair("Accept", acceptTypes));
+    int content_length = request->get_header("Content-Length", 0);
+    extractBodyContentAsync(session, content_length, [this, session,request](const std::string& bodyContent) {
+        std::string token = request->get_header("token", "");
+    
+        int status_code = 500;
+        Success resultObject;
+        std::string result = "";
+    
+        auto trackable = extractJsonModelBodyParam<Trackable>(bodyContent);
+        try {
+            std::tie(status_code, resultObject) =
+                handler_POST(trackable, token);
         }
-    
-        result = resultObject.toJsonString();
-        returnResponse(session, 200, result.empty() ? "{}" : result, responseHeaders);
-        return;
-    }
-    if (status_code == 400) {
-        responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
-        result = "Bad request.";
-    
-        result = resultObject.toJsonString();
-        returnResponse(session, 400, result.empty() ? "{}" : result, responseHeaders);
-        return;
-    }
-    if (status_code == 409) {
-        responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
-        result = "Invalid UUID, id must be a Nil value.";
-    
-        result = resultObject.toJsonString();
-        returnResponse(session, 409, result.empty() ? "{}" : result, responseHeaders);
-        return;
-    }
-    if (status_code == 511) {
-        responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
-        result = "The secret token is not valid. Please ask an ISG ARF team member for a valid token.";
-    
-        result = resultObject.toJsonString();
-        returnResponse(session, 511, result.empty() ? "{}" : result, responseHeaders);
-        return;
-    }
-    if (status_code == 0) {
-        responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
-        result = "Unexpected error.";
-    
-        result = resultObject.toJsonString();
-        returnResponse(session, 0, result.empty() ? "{}" : result, responseHeaders);
-        return;
-    }
-    defaultSessionClose(session, status_code, result);
-    
-    
+        catch(const TrackablesApiException& e) {
+            std::tie(status_code, result) = handleTrackablesApiException(e);
+        }
+        catch(const std::exception& e) {
+            std::tie(status_code, result) = handleStdException(e);
+        }
+        catch(...) {
+            std::tie(status_code, result) = handleUnspecifiedException();
+        }
+        
+        std::multimap< std::string, std::string > responseHeaders {};
+        static const std::vector<std::string> contentTypes{
+            "application/json",
+        };
+        static const std::string acceptTypes{
+            "application/json, "
+        };
+        
+        if (status_code == 200) {
+            responseHeaders.insert(std::make_pair("Content-Type", selectPreferredContentType(contentTypes)));
+            if (!acceptTypes.empty()) {
+                responseHeaders.insert(std::make_pair("Accept", acceptTypes));
+            }
+        
+            result = resultObject.toJsonString();
+            returnResponse(session, 200, result.empty() ? "{}" : result, responseHeaders);
+            return;
+        }
+        if (status_code == 400) {
+            responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
+            result = "Bad request.";
+        
+            result = resultObject.toJsonString();
+            returnResponse(session, 400, result.empty() ? "{}" : result, responseHeaders);
+            return;
+        }
+        if (status_code == 409) {
+            responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
+            result = "Invalid UUID, id must be a Nil value.";
+        
+            result = resultObject.toJsonString();
+            returnResponse(session, 409, result.empty() ? "{}" : result, responseHeaders);
+            return;
+        }
+        if (status_code == 511) {
+            responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
+            result = "The secret token is not valid. Please ask an ISG ARF team member for a valid token.";
+        
+            result = resultObject.toJsonString();
+            returnResponse(session, 511, result.empty() ? "{}" : result, responseHeaders);
+            return;
+        }
+        if (status_code == 0) {
+            responseHeaders.insert(std::make_pair("Content-Type", "text/plain"));
+            result = "Unexpected error.";
+        
+            result = resultObject.toJsonString();
+            returnResponse(session, 0, result.empty() ? "{}" : result, responseHeaders);
+            return;
+        }
+        defaultSessionClose(session, status_code, result);
+    });       
 }
 
 // x-extension