14 #include "../civetweb/civetweb.h" 32 struct mg_connection *fWSconn;
35 TCivetwebWSEngine(
const char *
name,
const char *title,
struct mg_connection *conn)
40 virtual ~TCivetwebWSEngine() {}
46 virtual void Send(
const void *buf,
int len)
57 if (request_info == 0)
return 1;
60 if (engine == 0)
return 1;
62 if (serv == 0)
return 1;
72 return execres && !arg.
Is404() ? 0 : 1;
82 if (engine == 0)
return;
84 if (serv == 0)
return;
92 arg.
SetWSHandle(
new TCivetwebWSEngine(
"websocket",
"title", conn));
104 if (engine == 0)
return 1;
106 if (serv == 0)
return 1;
114 void *buf =
malloc(len + 1);
115 memcpy(buf, data, len);
130 if (engine == 0)
return;
132 if (serv == 0)
return;
151 if (engine)
return engine->
ProcessLog(message);
154 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
155 fprintf(stderr,
"Error in <TCivetweb::Log> %s\n", message);
167 if (engine == 0)
return 0;
169 if (serv == 0)
return 0;
186 arg.
AddHeader(
"Cache-Control",
"max-age=3600");
209 void *buf =
malloc(ilen + 1);
219 cont.
Append(
"<title>Civetweb echo</title>");
220 cont.
Append(
"<h1>Civetweb echo</h1>\n");
222 static int count = 0;
232 cont.
Append(
"</pre><p>\n");
234 cont.
Append(
"Environment:<br/>\n<pre>\n");
238 cont.
Append(
"</pre><p>\n");
249 if (!execres || arg.
Is404()) {
253 }
else if (arg.
IsFile()) {
276 case 3: dozip =
kTRUE;
break;
329 :
THttpEngine("civetweb", "compact embedded http server"), fCtx(0), fCallbacks(0), fTopName(), fDebug(
kFALSE)
338 if (fCtx != 0)
mg_stop((
struct mg_context *)fCtx);
339 if (fCallbacks != 0)
free(fCallbacks);
349 if ((
gDebug > 0) || (strstr(message,
"cannot bind to") != 0))
Error(
"Log",
"%s", message);
373 TString sport =
"8080", num_threads =
"5", websocket_timeout =
"300000";
374 TString auth_file, auth_domain, log_file;
377 if ((args != 0) && (strlen(args) > 0)) {
381 while ((*args != 0) && (*args !=
'?') && (*args !=
'/')) sport.
Append(*args++);
384 while ((*args != 0) && (*args !=
'?')) args++;
393 if (top != 0) fTopName = top;
396 if (log != 0) log_file =
log;
399 if (thrds > 0) num_threads.Form(
"%d", thrds);
402 if (afile != 0) auth_file = afile;
405 if (adomain != 0) auth_domain = adomain;
408 if (wtmout > 0) websocket_timeout.Format(
"%d", wtmout * 1000);
417 const char *options[20];
420 Info(
"Create",
"Starting HTTP server on port %s", sport.
Data());
422 options[op++] =
"listening_ports";
423 options[op++] = sport.
Data();
424 options[op++] =
"num_threads";
425 options[op++] = num_threads.Data();
426 options[op++] =
"websocket_timeout_ms";
427 options[op++] = websocket_timeout.Data();
429 if ((auth_file.
Length() > 0) && (auth_domain.
Length() > 0)) {
430 options[op++] =
"global_auth_file";
431 options[op++] = auth_file.
Data();
432 options[op++] =
"authentication_domain";
433 options[op++] = auth_domain.
Data();
436 if (log_file.
Length() > 0) {
437 options[op++] =
"error_log_file";
438 options[op++] = log_file.
Data();
446 if (fCtx == 0)
return kFALSE;
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.
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)
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
THttpServer * GetServer() const
Returns pointer to THttpServer associated with engine.
void SetContent(const char *c)
Set content directly.
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 SetPostData(void *data, Long_t length)
set data, posted with the request buffer should be allocated with malloc(length+1) call...
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)
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