6 #include "../civetweb/civetweb.h" 26 struct mg_connection *fWSconn;
30 TCivetwebWSEngine(
const char*
name,
const char* title,
struct mg_connection *conn) :
36 virtual ~TCivetwebWSEngine()
45 virtual void Send(
const void* buf,
int len)
59 if (request_info == 0)
return 1;
64 if (engine == 0)
return 1;
66 if (serv == 0)
return 1;
77 return execres && !arg.
Is404() ? 0 : 1;
89 if (engine == 0)
return;
91 if (serv == 0)
return;
98 arg.
SetWSHandle(
new TCivetwebWSEngine(
"websocket",
"title", conn));
110 if (engine == 0)
return 1;
112 if (serv == 0)
return 1;
119 void* buf =
malloc(len+1);
120 memcpy(buf, data, len);
148 if (engine == 0)
return;
150 if (serv == 0)
return;
169 if (engine)
return engine->
ProcessLog(message);
172 if ((
gDebug>0) || (strstr(message,
"cannot bind to")!=0))
173 fprintf(stderr,
"Error in <TCivetweb::Log> %s\n",message);
186 if (engine == 0)
return 0;
188 if (serv == 0)
return 0;
205 arg.
AddHeader(
"Cache-Control",
"max-age=3600");
228 void* buf =
malloc(ilen+1);
236 cont.
Append(
"<title>Civetweb echo</title>");
237 cont.
Append(
"<h1>Civetweb echo</h1>\n");
239 static int count = 0;
250 cont.
Append(
"</pre><p>\n");
252 cont.
Append(
"Environment:<br/>\n<pre>\n");
255 cont.
Append(
"</pre><p>\n");
266 if (!execres || arg.
Is404()) {
270 }
else if (arg.
IsFile()) {
351 THttpEngine("civetweb", "compact embedded http server"),
364 if (fCtx != 0)
mg_stop((
struct mg_context *) fCtx);
365 if (fCallbacks != 0)
free(fCallbacks);
375 if ((
gDebug>0) || (strstr(message,
"cannot bind to")!=0))
Error(
"Log",
"%s", message);
400 TString auth_file, auth_domain, log_file;
403 if ((args != 0) && (strlen(args) > 0)) {
407 while ((*args != 0) && (*args !=
'?') && (*args !=
'/'))
411 while ((*args != 0) && (*args !=
'?')) args++;
420 if (top != 0) fTopName = top;
423 if (log != 0) log_file =
log;
426 if (thrds > 0) num_threads.
Form(
"%d", thrds);
429 if (afile != 0) auth_file = afile;
432 if (adomain != 0) auth_domain = adomain;
437 sport =
TString(
"127.0.0.1:") + sport;
442 const char *options[20];
445 Info(
"Create",
"Starting HTTP server on port %s", sport.
Data());
447 options[op++] =
"listening_ports";
448 options[op++] = sport.
Data();
449 options[op++] =
"num_threads";
450 options[op++] = num_threads.
Data();
452 if ((auth_file.
Length() > 0) && (auth_domain.
Length() > 0)) {
453 options[op++] =
"global_auth_file";
454 options[op++] = auth_file.
Data();
455 options[op++] =
"authentication_domain";
456 options[op++] = auth_domain.
Data();
459 if (log_file.
Length() > 0) {
460 options[op++] =
"error_log_file";
461 options[op++] = log_file.
Data();
469 if (fCtx == 0)
return kFALSE;
const char * GetFileName() const
void SetZipping(Int_t kind)
void SetRequestHeader(const char *h)
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
void SetQuery(const char *q)
struct mg_request_info::mg_header http_headers[64]
virtual void Send(const void *buf, int len)=0
const char * GetQuery() const
const char * GetTopName() const
void SetContentType(const char *typ)
void SetUserName(const char *n)
THttpServer * GetServer() const
void SetContent(const char *c)
void SetTopName(const char *topname)
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)
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
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
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
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 SetMethod(const char *method)
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)
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 *)
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
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
const char * GetMethod() const
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
if(line.BeginsWith("/*"))
void FillHttpHeader(TString &buf, const char *header=0)
fill HTTP header
const char * Data() const
Long_t GetContentLength() const