identt
ServiceBase.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_HTTP_SERVICEBASE_HPP_
34 #define _IDENTT_HTTP_SERVICEBASE_HPP_
35 
36 #include <utils/BaseUtils.hpp>
37 #include <utils/SharedTable.hpp>
38 #include <fstream>
39 #include <iomanip>
40 
41 
42 #define IDENTT_HTTP_CHAR_PERCENT '%'
43 #define IDENTT_HTTP_CHAR_SPACE ' '
44 #define IDENTT_HTTP_CHAR_PLUS '+'
45 #define IDENTT_HTTP_STR_EQUALTO "="
46 #define IDENTT_HTTP_STR_AMPERSAND "&"
47 
48 namespace identt {
49 namespace http {
50 
52  bool error=false;
53  std::string message;
54 };
55 
56 template <class HttpServerT>
57 class ServiceBase {
58 public:
59 
60 protected:
61  const unsigned int myscope;
62 
67  ServiceBase(const unsigned int myscope_) : myscope(myscope_) {}
68 
87  void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request,
88  int ec, const char* em)
89  {
90  std::string output="";
91  std::stringstream content_stream;
92  content_stream << output;
93  content_stream << ec << " " << em << request->method << " " << request->path << " HTTP/" << request->http_version << "";
94  content_stream.seekp(0, std::ios::end);
95  output = content_stream.str();
96  *response << "HTTP/" << request->http_version << " " << ec << " " << em;
97  *response << "\r\nContent-Length: " << output.length() << "\r\n\r\n" << output.c_str();
98  }
99 
121  void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request,
122  int ec, const char* em, std::string payload)
123  {
124  *response << "HTTP/" << request->http_version << " " << ec << " " << em;
125  *response << "\r\nContent-Length: " << payload.length() << "\r\n\r\n" << payload.c_str();
126  }
127 
155  void HttpOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request,
156  int ec, const char* em, const char* content_type , std::string& payload, bool add_cors)
157  {
158  *response << "HTTP/" << request->http_version << " " << ec << " " << em << "\r\n";
159  if (add_cors) {
160  *response << "Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept\r\n";
161  *response << "Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS\r\n";
162  *response << "Access-Control-Allow-Origin: *\r\n";
163  }
164  *response << "Content-type: " << content_type << "\r\n";
165  *response << "Content-Length: " << payload.length() << "\r\n";
166  *response << "\r\n" << payload.c_str();
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 http
188 } // namespace identt
189 
190 #endif /* _IDENTT_HTTP_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:155
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:121
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:87
Definition: CryptoBase.hpp:49
Definition: ServiceBase.hpp:57
bool JsonRequest(typename HttpServerT::ReqPtr request)
JsonRequest : Check if request is json from Content-Type and Accept fields.
Definition: ServiceBase.hpp:178
Definition: ServiceBase.hpp:51
ServiceBase(const unsigned int myscope_)
Constructor : to be used by inherited classes.
Definition: ServiceBase.hpp:67