Remove dynamic exceptions
[anna.git] / source / http / Handler.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <anna/core/DataBlock.hpp>
10 #include <anna/core/tracing/Logger.hpp>
11 #include <anna/config/defines.hpp>
12 #include <anna/core/functions.hpp>
13
14 #include <anna/comm/ClientSocket.hpp>
15
16 #include <anna/http/Handler.hpp>
17 #include <anna/http/Request.hpp>
18 #include <anna/http/Response.hpp>
19 #include <anna/http/Transport.hpp>
20
21 using namespace std;
22 using namespace anna;
23
24 http::Handler::~Handler() {
25   delete a_response;
26 }
27
28 //------------------------------------------------------------------------------------------------
29 // (1) Si ha llegado hasta aqui deberia ser porque el mensaje recibido es del protocolo HTTP.
30 //------------------------------------------------------------------------------------------------
31 void http::Handler::apply(comm::ClientSocket& clientSocket, const comm::Message& message)
32 noexcept(false) {
33   if(clientSocket.support(http::Transport::className()) == false) {
34     LOGWARNING(
35       string msg("anna::http::Handler::apply | Incoming ClientSocket has no support for HTTP | Message: ");
36       msg += functions::asString(message.getBody());
37       Logger::warning(msg, ANNA_FILE_LOCATION);
38     );
39     return;
40   }
41
42   const http::Message& httpMessage = static_cast <const http::Message&>(message);               // (1)
43
44   LOGDEBUG(
45     string msg("apply | ");
46     msg += asString();
47     msg += " | Message: ";
48     msg += httpMessage.asString();
49     Logger::debug(msg, ANNA_FILE_LOCATION);
50   );
51
52   if(httpMessage.getType() == Message::Type::Request) {
53     try {
54       evRequest(clientSocket, static_cast <const Request&>(httpMessage));
55     } catch(RuntimeException& ex) {
56       ex.trace();
57       Response* response = allocateResponse();
58       response->setStatusCode(400);
59       response->setReasonPhrase(ex.asString());
60       clientSocket.send(*response);
61     }
62   } else {
63     try {
64       evResponse(clientSocket, static_cast <const Response&>(httpMessage));
65     } catch(RuntimeException& ex) {
66       ex.trace();
67     }
68   }
69 }
70
71 http::Response* http::Handler::allocateResponse()
72 {
73   return (a_response == NULL) ? (a_response = new Response) : a_response;
74 }
75
76