1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
15 #include <anna/http/Request.hpp>
18 #include <MyHandler.hpp>
19 #include <Launcher.hpp>
20 #include <EventOperation.hpp>
23 void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request)
24 throw(anna::RuntimeException) {
25 const anna::DataBlock& body = request.getBody();
27 if(body.getSize() == 0)
28 throw anna::RuntimeException("Missing operation parameters on HTTP request", ANNA_FILE_LOCATION);
31 std::string msg("Received body: ");
32 msg += anna::functions::asString(body);
33 anna::Logger::information(msg, ANNA_FILE_LOCATION);
35 std::string body_content;
36 body_content.assign(body.getData(), body.getSize());
37 auto json_body = nlohmann::json::parse(body_content);
40 std::string response_content;
45 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
46 opOk = my_app.eventOperation(EventOperation::json2piped(json_body), response_content);
47 } catch(RuntimeException &ex) {
52 anna::http::Response* response = allocateResponse();
53 response->setStatusCode(200); // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
55 // EXAMPLES TO SET HEADERS:
56 // response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
57 // anna::http::Header* keepAlive = response->find("Keep-Alive");
59 // if (keepAlive == NULL)
60 // keepAlive = response->createHeader("Keep-Alive");
62 // keepAlive->setValue("Verificacion del cambio 1.0.7");
64 // Content-Type header:
65 anna::http::Header* contentType = response->find("Content-Type");
66 if (contentType == NULL)
67 contentType = response->createHeader("Content-Type");
68 contentType->setValue("application/json");
70 // Json Body for response:
73 ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })";
74 anna::DataBlock db_content(true);
75 db_content = ss.str();
76 response->setBody(db_content);
79 clientSocket.send(*response);
80 } catch(Exception& ex) {