14 #include "../civetweb/civetweb.h" 33 struct mg_connection *fWSconn;
36 TCivetwebWSEngine(
struct mg_connection *conn) :
THttpWSEngine(), fWSconn(conn) {}
42 virtual void Send(
const void *buf,
int len)
52 virtual void SendHeader(
const char *hdr,
const void *buf,
int len)
82 auto arg = std::make_shared<THttpCallArg>();
83 arg->SetPathAndFileName(request_info->
local_uri);
86 arg->SetMethod(
"WS_CONNECT");
88 Bool_t execres = serv->ExecuteHttp(arg);
90 return execres && !arg->Is404() ? 0 : 1;
106 auto arg = std::make_shared<THttpCallArg>();
107 arg->SetPathAndFileName(request_info->
local_uri);
109 arg->SetMethod(
"WS_READY");
112 arg->CreateWSEngine<TCivetwebWSEngine>(conn);
114 serv->ExecuteHttp(arg);
135 if ((len == 2) && ((
int)data[0] == 3) && ((
int)data[1] == -23))
138 auto arg = std::make_shared<THttpCallArg>();
139 arg->SetPathAndFileName(request_info->
local_uri);
142 arg->SetMethod(
"WS_DATA");
144 arg->SetPostData(std::string(data,len));
146 serv->ExecuteHttp(arg);
164 auto arg = std::make_shared<THttpCallArg>();
165 arg->SetPathAndFileName(request_info->
local_uri);
168 arg->SetMethod(
"WS_CLOSE");
170 serv->SubmitHttp(arg);
185 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
186 fprintf(stderr,
"Error in <TCivetweb::Log> %s\n", message);
204 auto arg = std::make_shared<THttpCallArg>();
211 if ((filename.Index(
".js") !=
kNPOS) || (filename.Index(
".css") !=
kNPOS)) {
212 std::string buf = THttpServer::ReadFileContent(filename.Data());
216 arg->SetContentType(THttpServer::GetMimeType(filename.Data()));
217 arg->SetContent(std::move(buf));
218 arg->AddHeader(
"Cache-Control",
"max-age=3600");
222 arg->SetFile(filename.Data());
225 arg->SetPathAndFileName(request_info->
local_uri);
236 arg->SetRequestHeader(header);
246 arg->SetPostData(std::move(buf));
251 cont.
Append(
"<title>Civetweb echo</title>");
252 cont.
Append(
"<h1>Civetweb echo</h1>\n");
254 static int count = 0;
262 if (arg->GetUserName())
265 cont.
Append(
"</pre><p>\n");
267 cont.
Append(
"Environment:<br/>\n<pre>\n");
271 cont.
Append(
"</pre><p>\n");
273 arg->SetContentType(
"text/html");
275 arg->SetContent(cont);
278 execres = serv->ExecuteHttp(arg);
282 if (!execres || arg->Is404()) {
283 std::string hdr = arg->FillHttpHeader(
"HTTP/1.1");
285 }
else if (arg->IsFile()) {
290 switch (arg->GetZipping()) {
293 if (arg->GetContentLength() < 10000)
break;
310 arg->CompressWithGzip();
312 std::string hdr = arg->FillHttpHeader(
"HTTP/1.1");
315 if (arg->GetContentLength() > 0)
316 mg_write(conn, arg->GetContent(), (size_t)arg->GetContentLength());
359 :
THttpEngine(
"civetweb",
"compact embedded http server"), fCtx(nullptr), fCallbacks(nullptr), fTopName(),
360 fDebug(
kFALSE), fTerminating(
kFALSE), fOnlySecured(only_secured)
380 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
381 Error(
"Log",
"%s", message);
412 TString sport =
IsSecured() ?
"8480s" :
"8080", num_threads =
"10", websocket_timeout =
"300000";
413 TString auth_file, auth_domain, log_file, ssl_cert;
417 if (args && (strlen(args) > 0)) {
421 while ((*args != 0) && (*args !=
'?') && (*args !=
'/'))
426 while ((*args != 0) && (*args !=
'?'))
445 num_threads.Form(
"%d", thrds);
453 auth_domain = adomain;
461 websocket_timeout.Format(
"%d", wtmout * 1000);
472 sport =
TString(
"127.0.0.1:") + sport;
476 if (addr && strlen(addr))
477 sport =
TString(addr) +
":" + sport;
488 const char *options[20];
491 Info(
"Create",
"Starting HTTP server on port %s", sport.
Data());
493 options[op++] =
"listening_ports";
494 options[op++] = sport.
Data();
495 options[op++] =
"num_threads";
496 options[op++] = num_threads.Data();
499 options[op++] =
"websocket_timeout_ms";
500 options[op++] = websocket_timeout.Data();
503 if ((auth_file.
Length() > 0) && (auth_domain.
Length() > 0)) {
504 options[op++] =
"global_auth_file";
505 options[op++] = auth_file.
Data();
506 options[op++] =
"authentication_domain";
507 options[op++] = auth_domain.
Data();
510 if (log_file.
Length() > 0) {
511 options[op++] =
"error_log_file";
512 options[op++] = log_file.
Data();
515 if (ssl_cert.
Length() > 0) {
516 options[op++] =
"ssl_certificate";
517 options[op++] = ssl_cert.
Data();
519 Error(
"Create",
"No SSL certificate file configured");
521 options[op++] =
nullptr;
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
TString fTopName
! name of top item
void websocket_ready_handler(struct mg_connection *conn, void *)
This class represents a WWW compatible URL.
int mg_read(struct mg_connection *conn, void *buf, size_t len)
void mg_stop(struct mg_context *ctx)
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
virtual Bool_t Create(const char *args)
Creates embedded civetweb server As main argument, http port should be specified like "8090"...
virtual void Send(const void *buf, int len)=0
Bool_t fDebug
! debug mode
const char * GetTopName() const
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
void * fCallbacks
! call-back table for civetweb webserver
THttpServer * GetServer() const
Returns pointer to THttpServer associated with engine.
virtual void SendCharStar(const char *str)
Envelope for sending string via the websocket.
void websocket_close_handler(const struct mg_connection *conn, void *)
const char * request_method
virtual void SendHeader(const char *hdr, const void *buf, int len)=0
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
virtual ~TCivetweb()
destructor
void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
Int_t ProcessLog(const char *message)
process civetweb log message, can be used to detect critical errors
int websocket_connect_handler(const struct mg_connection *conn, void *)
TString & Append(const char *cs)
void SetCors(const std::string &domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
void mg_send_file(struct mg_connection *conn, const char *path)
const char * GetValueFromOptions(const char *key) const
Return a value for a given key from the URL options.
int mg_printf(struct mg_connection *conn, const char *fmt,...)
Bool_t fTerminating
! server doing shutdown and not react on requests
virtual UInt_t GetId() const =0
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
TCivetweb(Bool_t only_secured=kFALSE)
constructor
struct mg_header http_headers[MG_MAX_HEADERS]
struct mg_context * mg_start(const struct mg_callbacks *callbacks, void *user_data, const char **options)
int mg_write(struct mg_connection *conn, const void *buf, size_t len)
const char * mg_get_header(const struct mg_connection *conn, const char *name)
static int log_message_handler(const struct mg_connection *conn, const char *message)
CIVETWEB_API int mg_websocket_write(struct mg_connection *conn, int opcode, const char *data, size_t data_len)
void * mg_get_user_data(const struct mg_context *ctx)
void ParseOptions() const
Parse URL options into a key/value map.
Bool_t IsDebugMode() const
int websocket_data_handler(struct mg_connection *conn, int, char *data, size_t len, void *)
const struct mg_request_info * mg_get_request_info(const struct mg_connection *conn)
Int_t Atoi() const
Return integer value of string.
virtual void ClearHandle()=0
Int_t GetIntValueFromOptions(const char *key) const
Return a value for a given key from the URL options as an Int_t, a missing key returns -1...
const char * query_string
static int begin_request_handler(struct mg_connection *conn, void *)
void mg_set_websocket_handler(struct mg_context *ctx, const char *uri, mg_websocket_connect_handler connect_handler, mg_websocket_ready_handler ready_handler, mg_websocket_data_handler data_handler, mg_websocket_close_handler close_handler, void *cbdata)
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
struct mg_context * mg_get_context(const struct mg_connection *conn)
void * fCtx
! civetweb context
const char * Data() const
Bool_t IsTerminating() const