33 #ifndef _IDENTT_HTTP_SERVICEBASE_HPP_ 34 #define _IDENTT_HTTP_SERVICEBASE_HPP_ 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 "&" 56 template <
class HttpServerT>
61 const unsigned int myscope;
67 ServiceBase(
const unsigned int myscope_) : myscope(myscope_) {}
87 void HttpErrorAction(
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request,
88 int ec,
const char* em)
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();
121 void HttpErrorAction(
typename HttpServerT::RespPtr response,
typename HttpServerT::ReqPtr request,
122 int ec,
const char* em, std::string payload)
124 *response <<
"HTTP/" << request->http_version <<
" " << ec <<
" " << em;
125 *response <<
"\r\nContent-Length: " << payload.length() <<
"\r\n\r\n" << payload.c_str();
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)
158 *response <<
"HTTP/" << request->http_version <<
" " << ec <<
" " << em <<
"\r\n";
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";
164 *response <<
"Content-type: " << content_type <<
"\r\n";
165 *response <<
"Content-Length: " << payload.length() <<
"\r\n";
166 *response <<
"\r\n" << payload.c_str();
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: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