Commit b600c562 by Amir Aharon

remove civetweb completly

parent 67e80a1c
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(LOCAL_INSTALLATION(/home/amir/.sdkman/candidates/gradle/current))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/usr/lib/jvm/java-8-oracle
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
......@@ -36,7 +36,7 @@ include_directories(SYSTEM ../3party/cereal-1.2.1/include)
include_directories(SYSTEM ../3party/rapidjson-cereal-1.2.1)
#include_directories(SYSTEM ../3party/rapidjson-0.11/include/rapidjson)
include_directories(SYSTEM ../3party/cppmetrics-0.1.1-Linux/include)
include_directories(SYSTEM ../3party/civetweb/include)
#include_directories(SYSTEM ../3party/civetweb/include)
#include_directories(SYSTEM ../3party/cpprest/Release/include)
include_directories(SYSTEM ../3party/rabbitmq)
include_directories(SYSTEM ../3party/flatbuffers/include)
......@@ -49,7 +49,7 @@ include_directories(SYSTEM /usr/include/hiredis)
# recursive search files cpp files
file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.h")
set (3PARTY_SOURCES ../3party/civetweb/src/civetweb.c ../3party/civetweb/src/CivetServer.cpp ../3party/civetweb/src/md5.inl ../3party/civetweb/src/handle_form.inl)
set (3PARTY_SOURCES )
# remove RMQ for now
get_filename_component(RMQHandler_file_path ${CMAKE_CURRENT_SOURCE_DIR}/src/handlers/Microservice_RMQHandler.cpp ABSOLUTE)
......
/*
* Microservice_IRequestRestImpl.cpp
*
* Created on: Mar 23, 2015
* Author: amir
*/
#include <impl/Microservice_IRequestRestImpl.h>
//#include <mongoose.h>
#include <civetweb.h>
#include <stdlib.h>
cMicroservice_IRequestRestImpl::cMicroservice_IRequestRestImpl():
p_reqInfo_(nullptr),mba_BodyBuffer(nullptr)
{
// TODO Auto-generated constructor stub
}
cMicroservice_IRequestRestImpl::~cMicroservice_IRequestRestImpl() {
if(mba_BodyBuffer)
free(mba_BodyBuffer);
}
void cMicroservice_IRequestRestImpl::Reset() {
p_reqInfo_ = nullptr;
}
const char* cMicroservice_IRequestRestImpl::GetQueryString()
{
if (p_reqInfo_)
return p_reqInfo_->query_string;
return nullptr;
}
const char* cMicroservice_IRequestRestImpl::GetRelativePath()
{
if (p_reqInfo_)
return p_reqInfo_->local_uri;
return nullptr;
}
const char* cMicroservice_IRequestRestImpl::GetContent()
{
if (p_reqInfo_)
{
auto len = p_reqInfo_->content_length;
if(len > 0)
{
mba_BodyBuffer = (char*)realloc(mba_BodyBuffer,len + 1);
len = mg_read((mg_connection *) p_conn_, mba_BodyBuffer, len);
*(mba_BodyBuffer + len) = CNULL;
return (const char*)mba_BodyBuffer;
}
}
return NULL;
}
/*
* Microservice_IRequestRestImpl.h
*
* Created on: Mar 23, 2015
* Author: amir
*/
#ifndef MICROSERVICE_IREQUESTRESTIMPL_H_
#define MICROSERVICE_IREQUESTRESTIMPL_H_
#include <common/Microservice_Iface.h>
#include <civetweb.h>
struct mg_connection;
struct http_message;
class cMicroservice_IRequestRestImpl: public nsMicroservice_Iface::IRequest
{
//mg_connection *mpt_MgConn;
const mg_request_info *p_reqInfo_;
const mg_connection* p_conn_;
char *mba_BodyBuffer;
public:
cMicroservice_IRequestRestImpl();
~cMicroservice_IRequestRestImpl();
const char* GetQueryString();
const char* GetRelativePath();
const char* GetContent();
void Reset();
//void setConn(mg_connection* pt_conn) { this->mpt_MgConn = pt_conn;}
void SetParams(const mg_connection* p_conn,const mg_request_info *mpt_MgMsg) {
p_conn_ = p_conn;
p_reqInfo_ = mpt_MgMsg;
}
};
#endif /* MICROSERVICE_IREQUESTRESTIMPL_H_ */
/*
* Microservice_IResponseRestImpl.cpp
*
* Created on: Mar 25, 2015
* Author: amir
*/
#include <impl/Microservice_IResponseRestImpl.h>
//#include <mongoose.h>
#include <civetweb.h>
cMicroservice_IResponseRestImpl::cMicroservice_IResponseRestImpl():
mpt_MgConn(NULL)
{
// TODO Auto-generated constructor stub
}
void cMicroservice_IResponseRestImpl::Send(const char* response)
{
//mg_printf(mpt_MgConn, response);
mg_printf(mpt_MgConn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json\r\n"
"Content-Length: %d\r\n"
"\r\n"
"%s",
(int) strlen(response), response);
// std::string str;
// str.append("HTTP/1.1 200 OK\r\n")
// .append("Connection: close\r\n")
// .append("Content-Type: application/json\r\n")
// .append("Content-Length: 0\r\n");
// mg_send(mpt_MgConn,str.c_str(),str.length());
}
nsMicroservice_Iface::IResponse *cMicroservice_IResponseRestImpl::clone() {
return new cMicroservice_IResponseRestImpl(this->mpt_MgConn);
}
cMicroservice_IResponseRestImpl::cMicroservice_IResponseRestImpl(mg_connection *pConnection) {
this->mpt_MgConn = pConnection;
}
/*
* Microservice_IResponseRestImpl.h
*
* Created on: Mar 25, 2015
* Author: amir
*/
#ifndef _MICROSERVICE_IRESPONSE_REST_IMPL_H_
#define _MICROSERVICE_IRESPONSE_REST_IMPL_H_
#include <common/Microservice_Iface.h>
struct mg_connection;
class cMicroservice_IResponseRestImpl: public nsMicroservice_Iface::IResponse
{
cMicroservice_IResponseRestImpl(mg_connection *pConnection);
mg_connection *mpt_MgConn;
public:
cMicroservice_IResponseRestImpl();
void Send(const char* response);
void Reset() {mpt_MgConn = NULL; }
void setConn(mg_connection* pt_conn) { this->mpt_MgConn = pt_conn;}
virtual nsMicroservice_Iface::IResponse *clone() override;
};
#endif /* _MICROSERVICE_IRESPONSE_REST_IMPL_H_ */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Microservice_IRestServerMongooseImpl.cpp
* Author: amir
*
* Created on May 3, 2016, 7:23 PM
*/
#include "Microservice_IRestServerCivetWebImpl.h"
#include <Microservice_BaseRestResponse.h>
#include <civetweb.h>
#include <signal.h>
#include <handlers/Microservice_Reactor.h>
#include <impl/Microservice_IResponseRestImpl.h>
#include <impl/Microservice_IRequestRestImpl.h>
#include <utils/CommonUtils.h>
static const int SLEEP_INTERVAL = 1; // IN SECONDS
static int s_sig_num = 0;
void run_thread(cMicroservice_IRestServerCivetWebImpl* pc_Obj)
{
pc_Obj->start();
}
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler);
s_sig_num = sig_num;
}
static int ev_handler(struct mg_connection *p_conn, void *cbdata) {
const struct mg_request_info *p_ri = mg_get_request_info(p_conn);
//((cMicroservice_IRestServerCivetWebImpl*)p_conn->ctx->user_data)->HandleRequest(p_conn,p_ri);
((cMicroservice_IRestServerCivetWebImpl*)cbdata)->HandleRequest(p_conn,p_ri);
return 1;
}
cMicroservice_IRestServerCivetWebImpl::cMicroservice_IRestServerCivetWebImpl(cMicroservice_RestServerParams* pc_Param)
:p_ctx_(nullptr),mpc_RunThread(nullptr)
{
serverType_.assign(getType());
mpc_Param = pc_Param;
p_requestRestImpl_ = new cMicroservice_IRequestRestImpl();
p_restResponseImpl_ = new cMicroservice_IResponseRestImpl();
}
cMicroservice_IRestServerCivetWebImpl::cMicroservice_IRestServerCivetWebImpl(const cMicroservice_IRestServerCivetWebImpl& orig) {
}
cMicroservice_IRestServerCivetWebImpl::~cMicroservice_IRestServerCivetWebImpl() {
}
bool cMicroservice_IRestServerCivetWebImpl::build(std::string& appName,
const std::map<std::string, nsMicroservice_Iface::IHandler*>& msHandlersMap,
nsMicroservice_Iface::ILogger* pc_Logger,
nsMicroservice_Iface::IPubSub* pc_PubSub,
nsMicroservice_Iface::IMetricsFactory* p_metrics_factory) {
bool result = false;
if (this->mpc_Param)
{
mc_AppName.assign(appName);
mpc_Logger = pc_Logger;
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
result = true;
}
return result;
}
void cMicroservice_IRestServerCivetWebImpl::registerService(nsMicroservice_Iface::IServiceDiscovery* pc_ServiceDiscovery, std::string& id) {
if (pc_ServiceDiscovery != nullptr)
pc_ServiceDiscovery->registerService(this->mc_AppName, id, mpc_Param->getHost(), mpc_Param->getPort());
}
void cMicroservice_IRestServerCivetWebImpl::run() {
mpc_RunThread = new std::thread(run_thread,this);
}
void cMicroservice_IRestServerCivetWebImpl::start() {
struct mg_callbacks callbacks;
mpc_Logger->info("staring http server on: %s, port: %u",mpc_Param->getHost().c_str(),mpc_Param->getPort());
// create host:port
std::string str_hostport;
std::stringstream portStr;
portStr << mpc_Param->getPort();
if(mpc_Param->getHost().compare(nsMicroservice_Constants::LOCALHOST) != 0)
{
str_hostport.assign(mpc_Param->getHost());
str_hostport.append(":");
}
str_hostport.append(portStr.str());
/*
* init params and server context
*/
if (mpc_Param->getWorkerThreadsNum() == 0)
mpc_Param->setWorkerThreadsNum(CommonUtils::numofcpu());
memset(&callbacks, 0, sizeof(callbacks));
const char *options[] = {
"listening_ports",str_hostport.c_str(),
"enable_keep_alive","yes",
"num_threads",std::to_string(mpc_Param->getWorkerThreadsNum()).c_str()};
mg_init_library(0);
p_ctx_ = mg_start(&callbacks,this,options);
if (p_ctx_) {
mg_set_request_handler(p_ctx_, "/", ev_handler, this);
while (s_sig_num == 0) {
// just sleep and check for exit every now and then
std::this_thread::sleep_for(std::chrono::seconds(SLEEP_INTERVAL));
}
// Cleanup, and free server instance
mg_stop(p_ctx_);
} else
std::cerr << __PRETTY_FUNCTION__ << " >> Failed to start server" << std::endl;
mg_exit_library();
}
void cMicroservice_IRestServerCivetWebImpl::stop() {
if(mpc_RunThread)
{
s_sig_num = 1;
mpc_RunThread->join();
}
}
void cMicroservice_IRestServerCivetWebImpl::HandleRequest(mg_connection* conn, const mg_request_info *p_reqInfo) {
const char* pba_Uri = p_reqInfo->local_uri;
if (pba_Uri[0] == '/')
{
const char* pba_NextSlash = strchr(pba_Uri + 1, '/');
if (pba_NextSlash)
{
std::string apiContextPath(pba_Uri,(int)(pba_NextSlash - pba_Uri));
std::string key(nsMicroservice_Iface::IRestServer::TYPE);
key.append(nsMicroservice_Constants::TYPE_PREFIX_SEPERATOR).append(apiContextPath);
if(p_reactor_)
HandleNewRequest(conn, key, p_reqInfo, apiContextPath);
else
SendGeneralError(conn,500,"Missing Reactor body!");
}
else
SendNotImplemented(conn);
}
else
SendNotImplemented(conn);
/* Close connection for non-keep-alive requests */
// const char* hdr;
// if (CommonUtils::strcmp(p_reqInfo->http_version, HTTP_1_1) != 0 ||
// ((hdr = mg_get_header(conn, "Connection")) != nullptr &&
// CommonUtils::strcmp(hdr, "keep-alive") != 0)) {
// mg_close_connection(conn);
// //conn->flags |= MG_F_SEND_AND_CLOSE;
// }
}
void cMicroservice_IRestServerCivetWebImpl::SendNotImplemented(mg_connection* conn) {
mg_printf(conn, "%s",
"HTTP/1.0 501 Not Implemented\r\n"
"Content-Length: 0\r\n\r\n");
}
bool cMicroservice_IRestServerCivetWebImpl::init() {
return true;
}
void cMicroservice_IRestServerCivetWebImpl::HandleNewRequest(mg_connection *p_connection, std::string key,
const mg_request_info *p_reqInfo, std::string& apiContextPath) {
MSRetStat retStat;
cMicroservice_RequestContext ctx(this,
p_restResponseImpl_,
p_requestRestImpl_);
retStat = ParseRequest(p_connection, p_reqInfo, ctx,apiContextPath);
if (retStat.IsSuccess())
p_reactor_->Delegate(key, &ctx);
else
SendGeneralError(p_connection,500,"Failed in parsing...kus restek! yored lekafa..");
}
void
cMicroservice_IRestServerCivetWebImpl::SendGeneralError(mg_connection *p_connection, int respCode, const char *error) {
mg_printf(p_connection, "HTTP/1.0 %u %s\r\nContent-Length: 0\r\n\r\n",respCode,error);
}
MSRetStat
cMicroservice_IRestServerCivetWebImpl::ParseRequest(mg_connection *p_conn,
const mg_request_info *p_reqInfo,
cMicroservice_RequestContext& ctx,
std::string& apiContextPath) {
/*
* get request context
*/
/*
* getting/setting request/response ifaces
*/
((cMicroservice_IRequestRestImpl *) ctx.mpti_Request)->SetParams(p_conn,p_reqInfo);
((cMicroservice_IResponseRestImpl*)ctx.mpti_Response)->setConn(p_conn);
/*
* getting params
*/
const auto uriLen = strlen(p_reqInfo->local_uri);
memcpy(mba_Buff, p_reqInfo->local_uri,
(uriLen < nsMicroservice_Constants::MAX_URI_LENGTH) ? uriLen : nsMicroservice_Constants::MAX_URI_LENGTH - 1);
mba_Buff[uriLen] = CNULL;
char* pba_ParamsStr = &mba_Buff[apiContextPath.length()];
char* pba_token = strtok(pba_ParamsStr,nsMicroservice_Constants::SLASH_SEPERATOR);
while(pba_token)
{
ctx.mc_Params.push_back(pba_token);
pba_token = strtok(NULL,nsMicroservice_Constants::SLASH_SEPERATOR);
}
/*
* getting query parameters
*/
GetQueryParams(ctx, p_reqInfo);
/*
* Log request
*/
LogRequest(p_reqInfo);
/**
* get crud method
*/
ctx.crudMethod = GetCrudMethod(p_reqInfo);
return MSRetStat();
}
void cMicroservice_IRestServerCivetWebImpl::LogRequest(const mg_request_info *p_reqInfo) {
if (mpc_Logger->getLevel() == cMicroservice_Enums::eLogLevel::eDebug) {
std::string str("Received request: ");
str.append(p_reqInfo->request_method);
str.append(", uri: ").append(p_reqInfo->local_uri);
if (p_reqInfo->query_string)
str.append(", query string: ").append(p_reqInfo->query_string);
mpc_Logger->debug(str);
}
}
void
cMicroservice_IRestServerCivetWebImpl::GetQueryParams(cMicroservice_RequestContext &ctx, const mg_request_info *p_reqInfo) {
/*
* getting query parameters
*/
if (!p_reqInfo->query_string)
return;
DequeStringMap* pc_queryParams = &ctx.mc_QueryParameters;
const auto queryLen = strlen(p_reqInfo->query_string);
memcpy(mba_Buff, p_reqInfo->query_string,
(queryLen < nsMicroservice_Constants::MAX_URI_LENGTH) ? queryLen : nsMicroservice_Constants::MAX_URI_LENGTH - 1);
mba_Buff[queryLen] = CNULL;
CommonUtils::BuildQueryParams(mba_Buff,pc_queryParams);
}
eCrudMethod cMicroservice_IRestServerCivetWebImpl::GetCrudMethod(const mg_request_info *p_reqInfo) {
auto iter = _microservice_RestCrudMap.find(std::string(p_reqInfo->request_method));
if (iter != _microservice_RestCrudMap.end())
return iter->second;
return eCrudMethod::eMaxMethods;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Microservice_IRestServerMongooseImpl.h
* Author: amir
*
* Created on May 3, 2016, 7:23 PM
*/
#ifndef MICROSERVICE_IRESTSERVERMONGOOSEIMPL_H
#define MICROSERVICE_IRESTSERVERMONGOOSEIMPL_H
static const char *const HTTP_1_1 = "1.1";
#include "common/Microservice_Iface.h"
#include <thread>
#include <common/Microservice_RequestContext.h>
#include "../../params/Microservice_Params.h"
struct mg_connection;
//struct mg_mgr;
//struct http_message;
struct mg_context;
struct mg_request_info;
//class Microservice_RestHandler;
class cMicroservice_IResponseRestImpl;
class cMicroservice_IRequestRestImpl;
class cMicroservice_IRestServerCivetWebImpl : public nsMicroservice_Iface::IRestServer , public nsMicroservice_Iface::IContainer {
public:
cMicroservice_IRestServerCivetWebImpl(cMicroservice_RestServerParams* pc_Param);
cMicroservice_IRestServerCivetWebImpl(const cMicroservice_IRestServerCivetWebImpl& orig);
virtual ~cMicroservice_IRestServerCivetWebImpl();
bool build(std::string& appName, const std::map<std::string,nsMicroservice_Iface::IHandler*>& msHandlersMap,
nsMicroservice_Iface::ILogger* pc_Logger,
nsMicroservice_Iface::IPubSub* pc_PubSub,
nsMicroservice_Iface::IMetricsFactory* p_metrics_factory) override;
void registerService(nsMicroservice_Iface::IServiceDiscovery* pc_ServiceDiscovery, std::string& id) override;
void run() override;
void start();
void stop() override;
virtual bool init() override;
void HandleRequest(mg_connection *conn,const mg_request_info *req_info);
void SendNotImplemented(mg_connection* conn);
private:
cMicroservice_RestServerParams* mpc_Param;
struct mg_context *p_ctx_;
std::string mc_AppName;
// mg_mgr *mpt_ServerManager;
char mba_UriBuff[nsMicroservice_Constants::MAX_URI_LENGTH];
std::thread* mpc_RunThread;
nsMicroservice_Iface::ILogger* mpc_Logger;
std::string serverType_;
char mba_Buff[nsMicroservice_Constants::MAX_URI_LENGTH]; // should be thread safe
cMicroservice_IResponseRestImpl* p_restResponseImpl_;
cMicroservice_IRequestRestImpl* p_requestRestImpl_;
void HandleNewRequest(mg_connection *p_connection, std::string key,
const mg_request_info *req_info, std::string& apiContextPath);
void SendGeneralError(mg_connection *p_connection, int respCode, const char *error);
MSRetStat ParseRequest(mg_connection *p_conn,
const mg_request_info *p_reqInfo,
cMicroservice_RequestContext& ctx,
std::string& apiContextPath);
void LogRequest(const mg_request_info *p_reqInfo);
void GetQueryParams(cMicroservice_RequestContext &ctx, const mg_request_info *p_reqInfo);
eCrudMethod GetCrudMethod(const mg_request_info *p_reqInfo);
};
#endif /* MICROSERVICE_IRESTSERVERMONGOOSEIMPL_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment