14#include "../civetweb/civetweb.h"
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");
152 auto arg = std::make_shared<THttpCallArg>();
153 arg->SetPathAndFileName(request_info->
local_uri);
156 arg->SetMethod(
"WS_CLOSE");
173 int fin = code & 0x80, opcode = code & 0x0F;
176 enum { OP_CONTINUE = 0, OP_TEXT = 1, OP_BINARY = 2, OP_CLOSE = 8 };
179 if (fin && (opcode == OP_CLOSE)) {
191 if ((opcode != OP_CONTINUE) && (opcode != OP_TEXT) && (opcode != OP_BINARY)) {
216 auto arg = std::make_shared<THttpCallArg>();
217 arg->SetPathAndFileName(request_info->
local_uri);
220 arg->SetMethod(
"WS_DATA");
228 arg->SetPostData(std::string(data,len));
249 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
250 fprintf(stderr,
"Error in <TCivetweb::Log> %s\n", message);
268 auto arg = std::make_shared<THttpCallArg>();
281 arg->SetContent(std::move(buf));
285 arg->AddNoCacheHeader();
289 arg->SetFile(filename.
Data());
292 arg->SetPathAndFileName(request_info->
local_uri);
303 arg->SetRequestHeader(header);
313 arg->SetPostData(std::move(buf));
318 cont.
Append(
"<title>Civetweb echo</title>");
319 cont.
Append(
"<h1>Civetweb echo</h1>\n");
321 static int count = 0;
329 if (arg->GetUserName())
332 cont.
Append(
"</pre><p>\n");
334 cont.
Append(
"Environment:<br/>\n<pre>\n");
338 cont.
Append(
"</pre><p>\n");
340 arg->SetContentType(
"text/html");
342 arg->SetContent(cont);
349 if (!execres || arg->Is404()) {
350 std::string hdr = arg->FillHttpHeader(
"HTTP/1.1");
352 }
else if (arg->IsFile()) {
353 filename = (
const char *)arg->GetContent();
359 auto hFile = CreateFile(filename.
Data(),
364 FILE_ATTRIBUTE_NORMAL,
368 auto dwRet = GetFinalPathNameByHandle( hFile, Path,
BUFSIZE, VOLUME_NAME_DOS );
383 switch (arg->GetZipping()) {
386 if (arg->GetContentLength() < 10000)
break;
403 arg->CompressWithGzip();
405 std::string hdr = arg->FillHttpHeader(
"HTTP/1.1");
408 if (arg->GetContentLength() > 0)
409 mg_write(conn, arg->GetContent(), (
size_t)arg->GetContentLength());
452 :
THttpEngine(
"civetweb",
"compact embedded http server"), fCtx(nullptr), fCallbacks(nullptr), fTopName(),
453 fDebug(
kFALSE), fTerminating(
kFALSE), fOnlySecured(only_secured)
473 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
474 Error(
"Log",
"%s", message);
507 TString sport =
IsSecured() ?
"8480s" :
"8080", num_threads =
"10", websocket_timeout =
"300000";
508 TString auth_file, auth_domain, log_file, ssl_cert, max_age;
512 if (args && (strlen(args) > 0)) {
516 while ((*args != 0) && (*args !=
'?') && (*args !=
'/'))
521 while ((*args != 0) && (*args !=
'?'))
540 num_threads.Form(
"%d", thrds);
548 auth_domain = adomain;
556 websocket_timeout.Format(
"%d", wtmout * 1000);
567 sport =
TString(
"127.0.0.1:") + sport;
571 if (addr && strlen(addr))
572 sport =
TString(addr) +
":" + sport;
591 const char *options[20];
594 Info(
"Create",
"Starting HTTP server on port %s", sport.
Data());
596 options[op++] =
"listening_ports";
597 options[op++] = sport.
Data();
598 options[op++] =
"num_threads";
599 options[op++] = num_threads.Data();
602 options[op++] =
"websocket_timeout_ms";
603 options[op++] = websocket_timeout.Data();
606 if ((auth_file.
Length() > 0) && (auth_domain.
Length() > 0)) {
607 options[op++] =
"global_auth_file";
608 options[op++] = auth_file.
Data();
609 options[op++] =
"authentication_domain";
610 options[op++] = auth_domain.
Data();
612 options[op++] =
"enable_auth_domain_check";
613 options[op++] =
"no";
616 if (log_file.
Length() > 0) {
617 options[op++] =
"error_log_file";
618 options[op++] = log_file.
Data();
621 if (ssl_cert.
Length() > 0) {
622 options[op++] =
"ssl_certificate";
623 options[op++] = ssl_cert.
Data();
625 Error(
"Create",
"No SSL certificate file configured");
628 if (max_age.
Length() > 0) {
629 options[op++] =
"static_file_max_age";
630 options[op++] = max_age.
Data();
633 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)
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
struct mg_connection * fWSconn
UInt_t GetId() const override
void SendCharStar(const char *str) override
Envelope for sending string via the websocket.
void ClearHandle(Bool_t terminate) override
Bool_t SupportSendThrd() const override
True websocket requires extra thread to parallelize sending.
void SendHeader(const char *hdr, const void *buf, int len) override
Special method to send binary data with text header For normal websocket it is two separated operatio...
void Send(const void *buf, int len) override
virtual ~TCivetwebWSEngine()=default
TCivetwebWSEngine(struct mg_connection *conn)
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 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