14#include "../civetweb/civetweb.h"
40 struct mg_connection *fWSconn;
46 TCivetwebWSEngine(
struct mg_connection *conn) :
THttpWSEngine(), fWSconn(conn) {}
48 virtual ~TCivetwebWSEngine() =
default;
54 if (fWSconn && terminate)
59 void Send(
const void *buf,
int len)
override
69 void SendHeader(
const char *hdr,
const void *buf,
int len)
override
99 auto arg = std::make_shared<THttpCallArg>();
100 arg->SetPathAndFileName(request_info->
local_uri);
103 arg->SetMethod(
"WS_CONNECT");
107 return execres && !arg->Is404() ? 0 : 1;
123 auto arg = std::make_shared<THttpCallArg>();
124 arg->SetPathAndFileName(request_info->
local_uri);
126 arg->SetMethod(
"WS_READY");
129 arg->CreateWSEngine<TCivetwebWSEngine>(conn);
154 if (!(code & 0x80)) {
164 auto arg = std::make_shared<THttpCallArg>();
165 arg->SetPathAndFileName(request_info->
local_uri);
168 arg->SetMethod(
"WS_DATA");
176 arg->SetPostData(std::string(
data,len));
197 auto arg = std::make_shared<THttpCallArg>();
198 arg->SetPathAndFileName(request_info->
local_uri);
201 arg->SetMethod(
"WS_CLOSE");
218 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
219 fprintf(stderr,
"Error in <TCivetweb::Log> %s\n", message);
237 auto arg = std::make_shared<THttpCallArg>();
245 std::string buf = THttpServer::ReadFileContent(filename.
Data());
249 arg->SetContentType(THttpServer::GetMimeType(filename.
Data()));
250 arg->SetContent(std::move(buf));
254 arg->AddNoCacheHeader();
258 arg->SetFile(filename.
Data());
261 arg->SetPathAndFileName(request_info->
local_uri);
272 arg->SetRequestHeader(header);
282 arg->SetPostData(std::move(buf));
287 cont.
Append(
"<title>Civetweb echo</title>");
288 cont.
Append(
"<h1>Civetweb echo</h1>\n");
290 static int count = 0;
298 if (arg->GetUserName())
301 cont.
Append(
"</pre><p>\n");
303 cont.
Append(
"Environment:<br/>\n<pre>\n");
307 cont.
Append(
"</pre><p>\n");
309 arg->SetContentType(
"text/html");
311 arg->SetContent(cont);
314 execres = serv->ExecuteHttp(arg);
318 if (!execres || arg->Is404()) {
319 std::string hdr = arg->FillHttpHeader(
"HTTP/1.1");
321 }
else if (arg->IsFile()) {
322 filename = (
const char *)arg->GetContent();
328 auto hFile = CreateFile(filename.
Data(),
333 FILE_ATTRIBUTE_NORMAL,
337 auto dwRet = GetFinalPathNameByHandle( hFile, Path,
BUFSIZE, VOLUME_NAME_DOS );
348 switch (arg->GetZipping()) {
351 if (arg->GetContentLength() < 10000)
break;
368 arg->CompressWithGzip();
370 std::string hdr = arg->FillHttpHeader(
"HTTP/1.1");
373 if (arg->GetContentLength() > 0)
374 mg_write(conn, arg->GetContent(), (
size_t)arg->GetContentLength());
417 :
THttpEngine(
"civetweb",
"compact embedded http server"), fCtx(nullptr), fCallbacks(nullptr), fTopName(),
418 fDebug(
kFALSE), fTerminating(
kFALSE), fOnlySecured(only_secured)
438 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
439 Error(
"Log",
"%s", message);
472 TString sport =
IsSecured() ?
"8480s" :
"8080", num_threads =
"10", websocket_timeout =
"300000";
473 TString auth_file, auth_domain, log_file, ssl_cert, max_age;
477 if (args && (strlen(args) > 0)) {
481 while ((*args != 0) && (*args !=
'?') && (*args !=
'/'))
486 while ((*args != 0) && (*args !=
'?'))
505 num_threads.Form(
"%d", thrds);
513 auth_domain = adomain;
521 websocket_timeout.Format(
"%d", wtmout * 1000);
532 sport =
TString(
"127.0.0.1:") + sport;
536 if (addr && strlen(addr))
537 sport =
TString(addr) +
":" + sport;
556 const char *options[20];
559 Info(
"Create",
"Starting HTTP server on port %s", sport.
Data());
561 options[op++] =
"listening_ports";
562 options[op++] = sport.
Data();
563 options[op++] =
"num_threads";
564 options[op++] = num_threads.Data();
567 options[op++] =
"websocket_timeout_ms";
568 options[op++] = websocket_timeout.Data();
571 if ((auth_file.
Length() > 0) && (auth_domain.
Length() > 0)) {
572 options[op++] =
"global_auth_file";
573 options[op++] = auth_file.
Data();
574 options[op++] =
"authentication_domain";
575 options[op++] = auth_domain.
Data();
578 if (log_file.
Length() > 0) {
579 options[op++] =
"error_log_file";
580 options[op++] = log_file.
Data();
583 if (ssl_cert.
Length() > 0) {
584 options[op++] =
"ssl_certificate";
585 options[op++] = ssl_cert.
Data();
587 Error(
"Create",
"No SSL certificate file configured");
590 if (max_age.
Length() > 0) {
591 options[op++] =
"static_file_max_age";
592 options[op++] = max_age.
Data();
595 options[op++] =
nullptr;
int websocket_connect_handler(const struct mg_connection *conn, void *)
static int begin_request_handler(struct mg_connection *conn, void *)
static int log_message_handler(const struct mg_connection *conn, const char *message)
int websocket_data_handler(struct mg_connection *conn, int code, char *data, size_t len, void *)
void websocket_close_handler(const struct mg_connection *conn, void *)
void websocket_ready_handler(struct mg_connection *conn, void *)
#define INVALID_HANDLE_VALUE
int mg_printf(struct mg_connection *conn, const char *fmt,...)
void mg_send_file(struct mg_connection *conn, const char *path)
void mg_set_user_connection_data(struct mg_connection *conn, void *data)
struct mg_context * mg_start(const struct mg_callbacks *callbacks, void *user_data, const char **options)
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)
const char * mg_get_header(const struct mg_connection *conn, const char *name)
int mg_write(struct mg_connection *conn, const void *buf, size_t len)
const struct mg_request_info * mg_get_request_info(const struct mg_connection *conn)
void * mg_get_user_connection_data(const struct mg_connection *conn)
int mg_read(struct mg_connection *conn, void *buf, size_t len)
void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
struct mg_context * mg_get_context(const struct mg_connection *conn)
void mg_stop(struct mg_context *ctx)
void * mg_get_user_data(const struct mg_context *ctx)
CIVETWEB_API int mg_websocket_write(struct mg_connection *conn, int opcode, const char *data, size_t data_len)
@ MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE
@ MG_WEBSOCKET_OPCODE_BINARY
@ MG_WEBSOCKET_OPCODE_TEXT
Bool_t IsTerminating() const
const char * GetTopName() const
void * fCtx
! civetweb context
TCivetweb(Bool_t only_secured=kFALSE)
constructor
Int_t fMaxAge
! max-age parameter
Int_t ProcessLog(const char *message)
process civetweb log message, can be used to detect critical errors
TString fTopName
! name of top item
Bool_t fTerminating
! server doing shutdown and not react on requests
Bool_t IsDebugMode() const
virtual ~TCivetweb()
destructor
Bool_t fDebug
! debug mode
void * fCallbacks
! call-back table for civetweb webserver
virtual Bool_t Create(const char *args)
Creates embedded civetweb server As main argument, http port should be specified like "8090".
THttpServer * GetServer() const
Returns pointer to THttpServer associated with engine.
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
void SetCors(const std::string &domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
virtual void SendCharStar(const char *str)
Envelope for sending string via the websocket.
virtual UInt_t GetId() const =0
virtual void SendHeader(const char *hdr, const void *buf, int len)=0
virtual Bool_t SupportSendThrd() const
Indicate if engine require extra thread to complete postponed thread operation.
virtual void Send(const void *buf, int len)=0
virtual void ClearHandle(Bool_t)=0
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Int_t Atoi() const
Return integer value of string.
const char * Data() const
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
TString & Append(const char *cs)
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
This class represents a WWW compatible URL.
const char * GetValueFromOptions(const char *key) const
Return a value for a given key from the URL options.
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.
void ParseOptions() const
Parse URL options into a key/value map.
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
struct mg_header http_headers[MG_MAX_HEADERS]
const char * request_method
const char * query_string