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>
22 void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request)
23 throw(anna::RuntimeException) {
24 const anna::DataBlock& body = request.getBody();
26 if(body.getSize() == 0)
27 throw anna::RuntimeException("Missing operation parameters on HTTP request", ANNA_FILE_LOCATION);
30 std::string msg("Received body: ");
31 msg += anna::functions::asString(body);
32 anna::Logger::information(msg, ANNA_FILE_LOCATION);
34 std::string body_content;
35 body_content.assign(body.getData(), body.getSize());
38 std::string response_content;
43 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
44 opOk = my_app.eventOperation(body_content, response_content);
45 } catch(RuntimeException &ex) {
50 anna::http::Response* response = allocateResponse();
51 response->setStatusCode(200); // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
53 // EXAMPLES TO SET HEADERS:
54 // response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
55 // anna::http::Header* keepAlive = response->find("Keep-Alive");
57 // if (keepAlive == NULL)
58 // keepAlive = response->createHeader("Keep-Alive");
60 // keepAlive->setValue("Verificacion del cambio 1.0.7");
62 // Content-Type header:
63 anna::http::Header* contentType = response->find("Content-Type");
64 if (contentType == NULL)
65 contentType = response->createHeader("Content-Type");
66 contentType->setValue("application/json");
68 // Json Body for response:
71 ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })";
72 anna::DataBlock db_content(true);
73 db_content = ss.str();
74 response->setBody(db_content);
77 clientSocket.send(*response);
78 } catch(Exception& ex) {