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());
37 std::string response_content;
42 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
43 opOk = my_app.eventOperation(body_content, response_content);
44 } catch(RuntimeException &ex) {
49 anna::http::Response* response = allocateResponse();
50 response->setStatusCode(200); // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
52 // EXAMPLES TO SET HEADERS:
53 // response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT");
54 // anna::http::Header* keepAlive = response->find("Keep-Alive");
56 // if (keepAlive == NULL)
57 // keepAlive = response->createHeader("Keep-Alive");
59 // keepAlive->setValue("Verificacion del cambio 1.0.7");
61 // Content-Type header:
62 anna::http::Header* contentType = response->find("Content-Type");
63 if (contentType == NULL)
64 contentType = response->createHeader("Content-Type");
65 contentType->setValue("application/json");
67 // Json Body for response:
70 ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })";
71 anna::DataBlock db_content(true);
72 db_content = ss.str();
73 response->setBody(db_content);
76 clientSocket.send(*response);
77 } catch(Exception& ex) {