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>();
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);
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 );
352 switch (arg->GetZipping()) {
355 if (arg->GetContentLength() < 10000)
break;
372 arg->CompressWithGzip();
374 std::string hdr = arg->FillHttpHeader(
"HTTP/1.1");
377 if (arg->GetContentLength() > 0)
378 mg_write(conn, arg->GetContent(), (
size_t)arg->GetContentLength());
421 :
THttpEngine(
"civetweb",
"compact embedded http server"), fCtx(nullptr), fCallbacks(nullptr), fTopName(),
422 fDebug(
kFALSE), fTerminating(
kFALSE), fOnlySecured(only_secured)
442 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
443 Error(
"Log",
"%s", message);
476 TString sport =
IsSecured() ?
"8480s" :
"8080", num_threads =
"10", websocket_timeout =
"300000";
477 TString auth_file, auth_domain, log_file, ssl_cert, max_age;
481 if (args && (strlen(args) > 0)) {
485 while ((*args != 0) && (*args !=
'?') && (*args !=
'/'))
490 while ((*args != 0) && (*args !=
'?'))
509 num_threads.Form(
"%d", thrds);
517 auth_domain = adomain;
525 websocket_timeout.Format(
"%d", wtmout * 1000);
536 sport =
TString(
"127.0.0.1:") + sport;
540 if (addr && strlen(addr))
541 sport =
TString(addr) +
":" + sport;
560 const char *options[20];
563 Info(
"Create",
"Starting HTTP server on port %s", sport.
Data());
565 options[op++] =
"listening_ports";
566 options[op++] = sport.
Data();
567 options[op++] =
"num_threads";
568 options[op++] = num_threads.Data();
571 options[op++] =
"websocket_timeout_ms";
572 options[op++] = websocket_timeout.Data();
575 if ((auth_file.
Length() > 0) && (auth_domain.
Length() > 0)) {
576 options[op++] =
"global_auth_file";
577 options[op++] = auth_file.
Data();
578 options[op++] =
"authentication_domain";
579 options[op++] = auth_domain.
Data();
581 options[op++] =
"enable_auth_domain_check";
582 options[op++] =
"no";
585 if (log_file.
Length() > 0) {
586 options[op++] =
"error_log_file";
587 options[op++] = log_file.
Data();
590 if (ssl_cert.
Length() > 0) {
591 options[op++] =
"ssl_certificate";
592 options[op++] = ssl_cert.
Data();
594 Error(
"Create",
"No SSL certificate file configured");
597 if (max_age.
Length() > 0) {
598 options[op++] =
"static_file_max_age";
599 options[op++] = max_age.
Data();
602 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)
void mg_send_mime_file(struct mg_connection *conn, const char *path, const char *mime_type)
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)
@ MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE
@ MG_WEBSOCKET_OPCODE_BINARY
@ MG_WEBSOCKET_OPCODE_TEXT
CIVETWEB_API int mg_websocket_write(struct mg_connection *conn, int opcode, const char *data, size_t data_len)
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 Create(const char *args) override
Creates embedded civetweb server As main argument, http port should be specified like "8090".
Bool_t IsDebugMode() const
virtual ~TCivetweb()
destructor
Bool_t fDebug
! debug mode
void * fCallbacks
! call-back table for civetweb webserver
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.
Bool_t ExecuteWS(std::shared_ptr< THttpCallArg > &arg, Bool_t external_thrd=kFALSE, Bool_t wait_process=kFALSE)
Execute WS request.
Bool_t ExecuteHttp(std::shared_ptr< THttpCallArg > arg)
Execute HTTP request.
void SetCors(const std::string &domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
static const char * GetMimeType(const char *path)
Guess mime type base on file extension.
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