14 #include "../civetweb/civetweb.h" 33 struct mg_connection *fWSconn;
36 TCivetwebWSEngine(
const char *
name,
const char *title,
struct mg_connection *conn)
41 virtual ~TCivetwebWSEngine() {}
47 virtual void Send(
const void *buf,
int len)
59 if (request_info == 0)
77 return execres && !arg.
Is404() ? 0 : 1;
99 arg.
SetWSHandle(
new TCivetwebWSEngine(
"websocket",
"title", conn));
122 if ((len == 2) && ((
int)data[0] == 3) && ((
int)data[1] == -23))
172 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
173 fprintf(stderr,
"Error in <TCivetweb::Log> %s\n", message);
206 arg.
AddHeader(
"Cache-Control",
"max-age=3600");
230 void *buf =
malloc(ilen + 1);
240 cont.
Append(
"<title>Civetweb echo</title>");
241 cont.
Append(
"<h1>Civetweb echo</h1>\n");
243 static int count = 0;
254 cont.
Append(
"</pre><p>\n");
256 cont.
Append(
"Environment:<br/>\n<pre>\n");
260 cont.
Append(
"</pre><p>\n");
271 if (!execres || arg.
Is404()) {
275 }
else if (arg.
IsFile()) {
299 case 3: dozip =
kTRUE;
break;
355 :
THttpEngine(
"civetweb",
"compact embedded http server"), fCtx(0), fCallbacks(0), fTopName(), fDebug(
kFALSE)
377 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
378 Error(
"Log",
"%s", message);
403 TString sport =
"8080", num_threads =
"5", websocket_timeout =
"300000";
404 TString auth_file, auth_domain, log_file, ssl_cert;
407 if ((args != 0) && (strlen(args) > 0)) {
411 while ((*args != 0) && (*args !=
'?') && (*args !=
'/'))
415 while ((*args != 0) && (*args !=
'?'))
434 num_threads.Form(
"%d", thrds);
442 auth_domain = adomain;
450 websocket_timeout.Format(
"%d", wtmout * 1000);
456 sport =
TString(
"127.0.0.1:") + sport;
466 const char *options[20];
469 Info(
"Create",
"Starting HTTP server on port %s", sport.
Data());
471 options[op++] =
"listening_ports";
472 options[op++] = sport.
Data();
473 options[op++] =
"num_threads";
474 options[op++] = num_threads.Data();
475 options[op++] =
"websocket_timeout_ms";
476 options[op++] = websocket_timeout.Data();
478 if ((auth_file.
Length() > 0) && (auth_domain.
Length() > 0)) {
479 options[op++] =
"global_auth_file";
480 options[op++] = auth_file.
Data();
481 options[op++] =
"authentication_domain";
482 options[op++] = auth_domain.
Data();
485 if (log_file.
Length() > 0) {
486 options[op++] =
"error_log_file";
487 options[op++] = log_file.
Data();
490 if (ssl_cert.
Length() > 0) {
491 options[op++] =
"ssl_certificate";
492 options[op++] = ssl_cert.
Data();
const char * GetFileName() const
returns file name from request URL
void SetZipping(Int_t kind)
Set kind of content zipping 0 - none 1 - only when supported in request header 2 - if supported and c...
void SetRequestHeader(const char *h)
set full set of request header
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
TString fTopName
! name of top item
void SetCors(const char *domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
void websocket_ready_handler(struct mg_connection *conn, void *)
void SetWSHandle(TNamed *handle)
assign websocket handle with HTTP call
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"...
const char * GetPathName() const
returns path name from request URL
void SetQuery(const char *q)
set request query
struct mg_request_info::mg_header http_headers[64]
virtual void Send(const void *buf, int len)=0
const char * GetQuery() const
returns request query (string after ? in request URL)
Bool_t fDebug
! debug mode
const char * GetTopName() const
void SetContentType(const char *typ)
set content type like "text/xml" or "application/json"
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
void SetUserName(const char *n)
set name of authenticated user
void * fCallbacks
! call-back table for civetweb webserver
THttpServer * GetServer() const
Returns pointer to THttpServer associated with engine.
void SetContent(const char *c)
Set content directly.
void SetPostData(void *data, Long_t length, Bool_t make_copy=kFALSE)
set data, posted with the request buffer should be allocated with malloc(length+1) call...
void SetTopName(const char *topname)
set engine-specific top-name
void websocket_close_handler(const struct mg_connection *conn, void *)
const char * request_method
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
void SetPathAndFileName(const char *fullpath)
set complete path of requested http element For instance, it could be "/folder/subfolder/get.bin" Here "/folder/subfolder/" is element path and "get.bin" requested file.
virtual ~TCivetweb()
destructor
void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
void Set404()
mark reply as 404 error - page/request not exists or refused
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)
const char * GetUserName() const
return authenticated user name (0 - when no authentication)
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,...)
void SetBinData(void *data, Long_t length)
set binary data, which will be returned as reply body
virtual UInt_t GetId() const =0
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
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)
Bool_t ExecuteHttp(THttpCallArg *arg)
Execute HTTP request.
const void * GetContent() const
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 SetWSId(UInt_t id)
set web-socket id
void SetMethod(const char *method)
set request method kind like GET or POST
void ParseOptions() const
Parse URL options into a key/value map.
Bool_t IsDebugMode() const
static const char * GetMimeType(const char *path)
Guess mime type base on file extension.
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)
void AddHeader(const char *name, const char *value)
Set name: value pair to reply header Content-Type field handled separately - one should use SetConten...
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
void SetFile(const char *filename=0)
indicate that http request should response with file content
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 *)
Int_t GetZipping() const
return kind of content zipping
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)
Long_t GetPostDataLength() const
return length of posted with request data
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
const char * GetMethod() const
returns request method like GET or POST
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
Bool_t CompressWithGzip()
compress reply data with gzip compression
void FillHttpHeader(TString &buf, const char *header=0)
fill HTTP header
const char * Data() const
Long_t GetContentLength() const