identt
ProtoServiceBase.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_HRPC_PROTO_SERVICE_BASE_HPP_
34 #define _IDENTT_HRPC_PROTO_SERVICE_BASE_HPP_
35 
36 #include "utils/BaseUtils.hpp"
37 #include "../proto/Hrpc.pb.h"
38 
39 namespace identt {
40 namespace hrpc {
41 
42 template <class HttpServerT>
44 protected:
45 
55  bool ProtoRequest(typename HttpServerT::ReqPtr request)
56  {
57  auto it=request->header.find("Content-Type");
58  if(it==request->header.end())it=request->header.find("Accept");
59  if(it==request->header.end()) return false;
60  if ( it->second.compare(0,17,"application/proto",17)!=0) return false;
61  it=request->header.find("Proto-Transfer-Encoding");
62  return ( it->second.compare(0,6,"base64",6)==0);
63  }
64 
77  bool SharedSecretOK(typename HttpServerT::ReqPtr request, std::string sharedkey)
78  {
79  auto it=request->header.find("Shared-Secret");
80  if(it==request->header.end()) return false;
81  return ( it->second == sharedkey );
82  }
83 
93  int ServiceName(typename HttpServerT::ReqPtr request)
94  {
95  auto it=request->header.find("Service-Name");
96  return (it==request->header.end()) ? 0 : std::stoi(it->second);
97  }
98 
115  typename HttpServerT::RespPtr response,
116  typename HttpServerT::ReqPtr request,
117  std::string& payload)
118  {
119  *response << "HTTP/" << request->http_version << " 200 OK\r\n";
120  *response << "Service-Name: " << ServiceName(request) << "\r\n";
121  *response << "Content-Type: " << "application/proto" << "\r\n";
122  *response << "Proto-Transfer-Encoding: " << "base64" << "\r\n";
123  *response << "Content-Length: " << payload.length() << "\r\n";
124  *response << "\r\n" << payload.c_str();
125  }
126 
143  typename HttpServerT::RespPtr response,
144  typename HttpServerT::ReqPtr request,
145  std::string&& payload)
146  {
147  *response << "HTTP/" << request->http_version << " 200 OK\r\n";
148  *response << "Service-Name: " << ServiceName(request) << "\r\n";
149  *response << "Content-Type: " << "application/proto" << "\r\n";
150  *response << "Proto-Transfer-Encoding: " << "base64" << "\r\n";
151  *response << "Content-Length: " << payload.length() << "\r\n";
152  *response << "\r\n" << payload.c_str();
153  }
154 
174  typename HttpServerT::RespPtr response,
175  typename HttpServerT::ReqPtr request,
176  int ecode, std::string& emsg)
177  {
178  *response << "HTTP/" << request->http_version << " 200 OK\r\n";
179  *response << "Service-Name: " << ServiceName(request) << "\r\n";
180  *response << "Service-Error: " << ecode << "\r\n";
181  *response << "Content-Type: application/proto" << "\r\n";
182  *response << "Content-Length: " << emsg.length() << "\r\n";
183  *response << "\r\n" << emsg.c_str();
184  }
185 
186 };
187 } // namespace hrpc
188 } // namespace identt
189 
190 #endif /* _IDENTT_HRPC_PROTO_SERVICE_BASE_HPP_ */
void ServiceOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, std::string &payload)
ServiceOKAction : OK Action template for service.
Definition: ProtoServiceBase.hpp:114
int ServiceName(typename HttpServerT::ReqPtr request)
ServiceName : Check protobuf request service name.
Definition: ProtoServiceBase.hpp:93
void ServiceOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, std::string &&payload)
ServiceOKAction : OK Action template for service.
Definition: ProtoServiceBase.hpp:142
Definition: CryptoBase.hpp:49
bool SharedSecretOK(typename HttpServerT::ReqPtr request, std::string sharedkey)
SharedSecretOK : Check if shared key header is present and ok.
Definition: ProtoServiceBase.hpp:77
void ServiceErrAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ecode, std::string &emsg)
ServiceErrAction : Error Action template for service.
Definition: ProtoServiceBase.hpp:173
Definition: ProtoServiceBase.hpp:43
bool ProtoRequest(typename HttpServerT::ReqPtr request)
ProtoRequest : Check if request is protobuf from Content-Type and Accept fields.
Definition: ProtoServiceBase.hpp:55