33 #ifndef _IDENTT_QUERY_SERVICEBASE_HPP_ 34 #define _IDENTT_QUERY_SERVICEBASE_HPP_ 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 "&" 54 template <
class HttpServerT>
59 const unsigned int myscope;
65 ServiceBase(
const unsigned int myscope_) : myscope(myscope_) {}
85 void HttpErrorAction(
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request,
86 int ec,
const char* em)
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;
120 void HttpErrorAction(
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request,
121 int ec,
const char* em, std::string payload)
123 *response <<
"HTTP/" << request->http_version <<
" " << ec <<
" " << em;
124 *response <<
"\r\nContent-Length: " << payload.length() <<
"\r\n\r\n" << payload.c_str();
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)
157 *response <<
"HTTP/" << request->http_version <<
" " << ec <<
" " << em <<
"\r\n";
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";
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;
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);
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