identt
ServiceBase.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_QUERY_SERVICEBASE_HPP_
34 #define _IDENTT_QUERY_SERVICEBASE_HPP_
35 
36 #include <fstream>
37 #include <iomanip>
38 #include <utils/BaseUtils.hpp>
39 
40 #define IDENTT_HTTP_CHAR_PERCENT '%'
41 #define IDENTT_HTTP_CHAR_SPACE ' '
42 #define IDENTT_HTTP_CHAR_PLUS '+'
43 #define IDENTT_HTTP_STR_EQUALTO "="
44 #define IDENTT_HTTP_STR_AMPERSAND "&"
45 
46 namespace identt {
47 namespace query {
48 
50  bool error=false;
51  std::string message;
52 };
53 
54 template <class HttpServerT>
55 class ServiceBase {
56 public:
57 
58 protected:
59  const unsigned int myscope;
60 
65  ServiceBase(const unsigned int myscope_) : myscope(myscope_) {}
66 
85  void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request,
86  int ec, const char* em)
87  {
88  std::string output="";
89  std::stringstream content_stream;
90  content_stream << output;
91  content_stream << ec << " " << em << request->method << " " << request->path << " HTTP/" << request->http_version << "";
92  content_stream.seekp(0, std::ios::end);
93  output = content_stream.str();
94  *response << "HTTP/" << request->http_version << " " << ec << " " << em;
95  *response << "\r\nContent-Length: " << output.length() << "\r\n\r\n" << output.c_str();
96  LOG(INFO) << "NOK: " << output;
97  }
98 
120  void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request,
121  int ec, const char* em, std::string payload)
122  {
123  *response << "HTTP/" << request->http_version << " " << ec << " " << em;
124  *response << "\r\nContent-Length: " << payload.length() << "\r\n\r\n" << payload.c_str();
125  }
126 
154  void HttpOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request,
155  int ec, const char* em, const char* content_type , std::string& payload, bool add_cors)
156  {
157  *response << "HTTP/" << request->http_version << " " << ec << " " << em << "\r\n";
158  if (add_cors) {
159  *response << "Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept\r\n";
160  *response << "Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS\r\n";
161  *response << "Access-Control-Allow-Origin: *\r\n";
162  }
163  *response << "Content-Type: " << content_type << "\r\n";
164  *response << "Content-Length: " << payload.length() << "\r\n";
165  *response << "\r\n" << payload.c_str();
166  DLOG(INFO) << "OK: " << payload;
167  }
168 
178  bool JsonRequest(typename HttpServerT::ReqPtr request)
179  {
180  auto it=request->header.find("Content-Type");
181  if(it==request->header.end()) it=request->header.find("Accept");
182  if(it==request->header.end()) return false;
183  return ( it->second.compare(0,16,"application/json",16)==0);
184  }
185 
186 };
187 } // namespace query
188 } // namespace identt
189 
190 #endif /* _IDENTT_QUERY_SERVICEBASE_HPP_ */
void HttpOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em, const char *content_type, std::string &payload, bool add_cors)
HttpOKAction : OK Action template.
Definition: ServiceBase.hpp:154
ServiceBase(const unsigned int myscope_)
Constructor : to be used by inherited classes.
Definition: ServiceBase.hpp:65
Definition: ServiceBase.hpp:49
void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em, std::string payload)
HttpErrorAction : Error Action Template with payload no template.
Definition: ServiceBase.hpp:120
void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em)
HttpErrorAction : Error Action Template no payload maybe template.
Definition: ServiceBase.hpp:85
Definition: ServiceBase.hpp:55
Definition: CryptoBase.hpp:49
bool JsonRequest(typename HttpServerT::ReqPtr request)
JsonRequest : Check if request is json from Content-Type and Accept fields.
Definition: ServiceBase.hpp:178