Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TCivetweb.cxx
Go to the documentation of this file.
1// Author: Sergey Linev 21/12/2013
2
3/*************************************************************************
4 * Copyright (C) 1995-2022, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "TCivetweb.h"
12
13#include <cstdlib>
14#include <cstring>
15
16#ifdef _MSC_VER
17#include <windows.h>
18#include <tchar.h>
19#endif
20
21#include "THttpServer.h"
22#include "THttpWSEngine.h"
23#include "TUrl.h"
24#include "TSystem.h"
25#include "TError.h"
26
27//////////////////////////////////////////////////////////////////////////
28/// TCivetwebWSEngine
29///
30/// Implementation of THttpWSEngine for Civetweb
31
33protected:
35
36 /// True websocket requires extra thread to parallelize sending
37 Bool_t SupportSendThrd() const override { return kTRUE; }
38
39public:
41
42 ~TCivetwebWSEngine() override = default;
43
44 UInt_t GetId() const override { return TString::Hash((void *)&fWSconn, sizeof(void *)); }
45
46 void ClearHandle(Bool_t terminate) override
47 {
48 if (fWSconn && terminate)
50 fWSconn = nullptr;
51 }
52
53 void Send(const void *buf, int len) override
54 {
55 if (fWSconn)
57 }
58
59 /////////////////////////////////////////////////////////
60 /// Special method to send binary data with text header
61 /// For normal websocket it is two separated operation, for other engines could be combined together,
62 /// but emulates as two messages on client side
63 void SendHeader(const char *hdr, const void *buf, int len) override
64 {
65 if (fWSconn) {
68 }
69 }
70
71 void SendCharStar(const char *str) override
72 {
73 if (fWSconn)
75 }
76};
77
78//////////////////////////////////////////////////////////////////////////
79/// Check if engine has enough threads to process connect to new websocket handle
80
81Bool_t CheckEngineThreads(TCivetweb *engine, const char *uri, Bool_t longpoll)
82{
83 Int_t num_avail = engine->GetNumAvailableThreads();
84 if (longpoll) num_avail++;
85
86 if ((num_avail <= 0.1 * engine->GetNumThreads()) || (num_avail <= 2)) {
87 const char *cfg = engine->IsWebGui() ? "WebGui.HttpThreads parameter in rootrc" : "thrds=N parameter in config URL";
88 const char *place = longpoll ? "TCivetweb::LongpollHandler" : "TCivetweb::WebSocketHandler";
89 ::Error(place, "Only %d threads are available, reject connection request for %s. Increase %s, now it is %d", num_avail, uri, cfg, engine->GetNumThreads());
90 return kFALSE;
91 }
92
93 return kTRUE;
94}
95
96//////////////////////////////////////////////////////////////////////////
97
98int websocket_connect_handler(const struct mg_connection *conn, void *)
99{
100 const struct mg_request_info *request_info = mg_get_request_info(conn);
101 if (!request_info)
102 return 1;
103
104 TCivetweb *engine = (TCivetweb *)request_info->user_data;
105 if (!engine || engine->IsTerminating())
106 return 1;
107 THttpServer *serv = engine->GetServer();
108 if (!serv)
109 return 1;
110
111 auto arg = std::make_shared<THttpCallArg>();
112 arg->SetPathAndFileName(request_info->local_uri); // path and file name
113 arg->SetQuery(request_info->query_string); // query arguments
114 arg->SetWSId(TString::Hash((void *)&conn, sizeof(void *)));
115 arg->SetMethod("WS_CONNECT");
116
117 if (!CheckEngineThreads(engine, arg->GetPathName(), kFALSE))
118 return 1;
119
120 Bool_t execres = serv->ExecuteWS(arg, kTRUE, kTRUE);
121
122 return execres && !arg->Is404() ? 0 : 1;
123}
124
125//////////////////////////////////////////////////////////////////////////
126
127void websocket_ready_handler(struct mg_connection *conn, void *)
128{
129 const struct mg_request_info *request_info = mg_get_request_info(conn);
130
131 TCivetweb *engine = (TCivetweb *)request_info->user_data;
132 if (!engine || engine->IsTerminating())
133 return;
134 THttpServer *serv = engine->GetServer();
135 if (!serv)
136 return;
137
138 engine->ChangeNumActiveThrerads(1);
139
140 auto arg = std::make_shared<THttpCallArg>();
141 arg->SetPathAndFileName(request_info->local_uri); // path and file name
142 arg->SetQuery(request_info->query_string); // query arguments
143 arg->SetMethod("WS_READY");
144
145 // delegate ownership to the arg, id will be automatically set
146 arg->CreateWSEngine<TCivetwebWSEngine>(conn);
147
148 serv->ExecuteWS(arg, kTRUE, kTRUE);
149}
150
151
152//////////////////////////////////////////////////////////////////////////
153
154void websocket_close_handler(const struct mg_connection *conn, void *)
155{
156 const struct mg_request_info *request_info = mg_get_request_info(conn);
157
158 // check if connection was already closed
159 if (mg_get_user_connection_data(conn) == (void *) conn)
160 return;
161
162 TCivetweb *engine = (TCivetweb *)request_info->user_data;
163 if (!engine || engine->IsTerminating())
164 return;
165 THttpServer *serv = engine->GetServer();
166 if (!serv)
167 return;
168
169 auto arg = std::make_shared<THttpCallArg>();
170 arg->SetPathAndFileName(request_info->local_uri); // path and file name
171 arg->SetQuery(request_info->query_string); // query arguments
172 arg->SetWSId(TString::Hash((void *)&conn, sizeof(void *)));
173 arg->SetMethod("WS_CLOSE");
174
175 serv->ExecuteWS(arg, kTRUE, kFALSE); // do not wait for result of execution
176
177 engine->ChangeNumActiveThrerads(-1);
178}
179
180//////////////////////////////////////////////////////////////////////////
181
182int websocket_data_handler(struct mg_connection *conn, int code, char *data, size_t len, void *)
183{
184 const struct mg_request_info *request_info = mg_get_request_info(conn);
185
186 // check if connection data set to connection itself - means connection was closed already
187 std::string *conn_data = (std::string *) mg_get_user_connection_data(conn);
188 if ((void *) conn_data == (void *) conn)
189 return 1;
190
191 // see https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
192 int fin = code & 0x80, opcode = code & 0x0F;
193
194 // recognized operation codes, all other should fails
195 enum { OP_CONTINUE = 0, OP_TEXT = 1, OP_BINARY = 2, OP_CLOSE = 8 };
196
197 // close when normal close is detected
198 if (fin && (opcode == OP_CLOSE)) {
199 if (conn_data) delete conn_data;
200 websocket_close_handler(conn, nullptr);
201 mg_set_user_connection_data(conn, conn); // mark connection as closed
202 return 1;
203 }
204
205 // ignore empty data
206 if (len == 0)
207 return 1;
208
209 // close connection when unrecognized opcode is detected
210 if ((opcode != OP_CONTINUE) && (opcode != OP_TEXT) && (opcode != OP_BINARY)) {
211 if (conn_data) delete conn_data;
212 websocket_close_handler(conn, nullptr);
213 mg_set_user_connection_data(conn, conn); // mark connection as closed
214 return 1;
215 }
216
217 TCivetweb *engine = (TCivetweb *)request_info->user_data;
218 if (!engine || engine->IsTerminating())
219 return 1;
220 THttpServer *serv = engine->GetServer();
221 if (!serv)
222 return 1;
223
224 // this is continuation of the request
225 if (!fin) {
226 if (!conn_data) {
227 conn_data = new std::string(data,len);
229 } else {
230 conn_data->append(data,len);
231 }
232 return 1;
233 }
234
235 auto arg = std::make_shared<THttpCallArg>();
236 arg->SetPathAndFileName(request_info->local_uri); // path and file name
237 arg->SetQuery(request_info->query_string); // query arguments
238 arg->SetWSId(TString::Hash((void *)&conn, sizeof(void *)));
239 arg->SetMethod("WS_DATA");
240
241 if (conn_data) {
242 mg_set_user_connection_data(conn, nullptr);
243 conn_data->append(data,len);
244 arg->SetPostData(std::move(*conn_data));
245 delete conn_data;
246 } else {
247 arg->SetPostData(std::string(data,len));
248 }
249
250 serv->ExecuteWS(arg, kTRUE, kTRUE);
251
252 return 1;
253}
254
255
256//////////////////////////////////////////////////////////////////////////
257
258static int log_message_handler(const struct mg_connection *conn, const char *message)
259{
260 const struct mg_context *ctx = mg_get_context(conn);
261
262 TCivetweb *engine = (TCivetweb *)mg_get_user_data(ctx);
263
264 if (engine)
265 return engine->ProcessLog(message);
266
267 // provide debug output
268 if ((gDebug > 0) || (strstr(message, "cannot bind to") != 0))
269 fprintf(stderr, "Error in <TCivetweb::Log> %s\n", message);
270
271 return 0;
272}
273
277 {
278 fEngine = engine;
280 }
282 {
284 }
285};
286
287//////////////////////////////////////////////////////////////////////////
288/// Returns kTRUE in case of longpoll connection request - or at least looks like that
289
290Bool_t IsBadLongPollConnect(TCivetweb *engine, const std::shared_ptr<THttpCallArg> &arg)
291{
292 if (strcmp(arg->GetFileName(), "root.longpoll") != 0)
293 return kFALSE;
294
295 const char *q = arg->GetQuery();
296 if (!q || !*q)
297 return kFALSE;
298
299 if ((strstr(q, "raw_connect") != q) && (strstr(q, "txt_connect") != q))
300 return kFALSE;
301
302 return !CheckEngineThreads(engine, arg->GetPathName(), kTRUE);
303}
304
305//////////////////////////////////////////////////////////////////////////
306
307static int begin_request_handler(struct mg_connection *conn, void *)
308{
309 const struct mg_request_info *request_info = mg_get_request_info(conn);
310
311 TCivetweb *engine = (TCivetweb *)request_info->user_data;
312 if (!engine || engine->IsTerminating())
313 return 0;
314 THttpServer *serv = engine->GetServer();
315 if (!serv)
316 return 0;
317
318 TEngineHolder thrd_cnt_holder(engine);
319
320 auto arg = std::make_shared<THttpCallArg>();
321
323
324 Bool_t execres = kTRUE, debug = engine->IsDebugMode();
325
326 if (!debug && serv->IsFileRequested(request_info->local_uri, filename)) {
327 if ((filename.Length() > 3) && ((filename.Index(".js") != kNPOS) || (filename.Index(".css") != kNPOS))) {
328 std::string buf = THttpServer::ReadFileContent(filename.Data());
329 if (buf.empty()) {
330 arg->Set404();
331 } else {
332 arg->SetContentType(THttpServer::GetMimeType(filename.Data()));
333 arg->SetContent(std::move(buf));
334 if (engine->GetMaxAge() > 0)
335 arg->AddHeader("Cache-Control", TString::Format("max-age=%d", engine->GetMaxAge()));
336 else
337 arg->AddNoCacheHeader();
338 arg->SetZipping();
339 }
340 } else {
341 arg->SetFile(filename.Data());
342 }
343 } else {
344 arg->SetPathAndFileName(request_info->local_uri); // path and file name
345 arg->SetQuery(request_info->query_string); // query arguments
346 arg->SetTopName(engine->GetTopName());
347 arg->SetMethod(request_info->request_method); // method like GET or POST
348 if (request_info->remote_user)
349 arg->SetUserName(request_info->remote_user);
350
351 TString header;
352 for (int n = 0; n < request_info->num_headers; n++)
353 header.Append(
354 TString::Format("%s: %s\r\n", request_info->http_headers[n].name, request_info->http_headers[n].value));
355 arg->SetRequestHeader(header);
356
357 const char *len = mg_get_header(conn, "Content-Length");
358 Int_t ilen = len ? TString(len).Atoi() : 0;
359
360 if (ilen > 0) {
361 std::string buf;
362 buf.resize(ilen);
363 Int_t iread = mg_read(conn, (void *) buf.data(), ilen);
364 if (iread == ilen)
365 arg->SetPostData(std::move(buf));
366 }
367
368 if (debug) {
369 TString cont;
370 cont.Append("<title>Civetweb echo</title>");
371 cont.Append("<h1>Civetweb echo</h1>\n");
372
373 static int count = 0;
374
375 cont.Append(TString::Format("Request %d:<br/>\n<pre>\n", ++count));
376 cont.Append(TString::Format(" Method : %s\n", arg->GetMethod()));
377 cont.Append(TString::Format(" PathName : %s\n", arg->GetPathName()));
378 cont.Append(TString::Format(" FileName : %s\n", arg->GetFileName()));
379 cont.Append(TString::Format(" Query : %s\n", arg->GetQuery()));
380 cont.Append(TString::Format(" PostData : %ld\n", arg->GetPostDataLength()));
381 if (arg->GetUserName())
382 cont.Append(TString::Format(" User : %s\n", arg->GetUserName()));
383
384 cont.Append("</pre><p>\n");
385
386 cont.Append("Environment:<br/>\n<pre>\n");
387 for (int n = 0; n < request_info->num_headers; n++)
388 cont.Append(
389 TString::Format(" %s = %s\n", request_info->http_headers[n].name, request_info->http_headers[n].value));
390 cont.Append("</pre><p>\n");
391
392 arg->SetContentType("text/html");
393
394 arg->SetContent(cont);
395
396 } else if (IsBadLongPollConnect(engine, arg)) {
397 execres = kFALSE;
398 arg->Set404();
399 } else {
400 execres = serv->ExecuteHttp(arg);
401 }
402 }
403
404 if (!execres || arg->Is404()) {
405 std::string hdr = arg->FillHttpHeader("HTTP/1.1");
406 mg_printf(conn, "%s", hdr.c_str());
407 } else if (arg->IsFile()) {
408 filename = (const char *)arg->GetContent();
409#ifdef _MSC_VER
410 if (engine->IsWinSymLinks()) {
411 // resolve Windows links which are not supported by civetweb
412 auto hFile = CreateFile(filename.Data(), // file to open
413 GENERIC_READ, // open for reading
414 FILE_SHARE_READ, // share for reading
415 NULL, // default security
416 OPEN_EXISTING, // existing file only
417 FILE_ATTRIBUTE_NORMAL, // normal file
418 NULL); // no attr. template
419
420 if( hFile != INVALID_HANDLE_VALUE) {
421 const int BUFSIZE = 2048;
422 TCHAR Path[BUFSIZE];
423 auto dwRet = GetFinalPathNameByHandle( hFile, Path, BUFSIZE, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS );
424 // produced file name may include \\?\ symbols, which are indicating long file name
425 if(dwRet < BUFSIZE) {
426 if (dwRet > 4 && Path[0] == '\\' && Path[1] == '\\' && Path[2] == '?' && Path[3] == '\\')
427 filename = Path + 4;
428 else
429 filename = Path;
430 }
431 CloseHandle(hFile);
432 }
433 }
434#endif
435 const char *mime_type = THttpServer::GetMimeType(filename.Data());
436 if (mime_type)
437 mg_send_mime_file(conn, filename.Data(), mime_type);
438 else
439 mg_send_file(conn, filename.Data());
440 } else {
441
442 Bool_t dozip = kFALSE;
443 switch (arg->GetZipping()) {
444 case THttpCallArg::kNoZip: dozip = kFALSE; break;
446 if (arg->GetContentLength() < 10000) break;
448 // check if request header has Accept-Encoding
449 for (int n = 0; n < request_info->num_headers; n++) {
450 TString name = request_info->http_headers[n].name;
451 if (name.Index("Accept-Encoding", 0, TString::kIgnoreCase) != 0)
452 continue;
453 TString value = request_info->http_headers[n].value;
454 dozip = (value.Index("gzip", 0, TString::kIgnoreCase) != kNPOS);
455 break;
456 }
457
458 break;
459 case THttpCallArg::kZipAlways: dozip = kTRUE; break;
460 }
461
462 if (dozip)
463 arg->CompressWithGzip();
464
465 std::string hdr = arg->FillHttpHeader("HTTP/1.1");
466 mg_printf(conn, "%s", hdr.c_str());
467
468 if (arg->GetContentLength() > 0)
469 mg_write(conn, arg->GetContent(), (size_t)arg->GetContentLength());
470 }
471
472 // Returning non-zero tells civetweb that our function has replied to
473 // the client, and civetweb should not send client any more data.
474 return 1;
475}
476
477/** \class TCivetweb
478\ingroup http
479
480THttpEngine implementation, based on civetweb embedded server
481
482It is default kind of engine, created for THttpServer
483Currently v1.15 from https://github.com/civetweb/civetweb is used
484
485Additional options can be specified:
486
487 top=foldername - name of top folder, seen in the browser
488 thrds=N - use N threads to run civetweb server (default 5)
489 auth_file - global authentication file
490 auth_domain - domain name, used for authentication
491
492Example:
493
494 new THttpServer("http:8080?top=MyApp&thrds=3");
495
496For the full list of supported options see TCivetweb::Create() documentation
497
498When `auth_file` and `auth_domain` parameters are specified, access
499to running http server will be possible only after user
500authentication, using so-call digest method. To generate
501authentication file, htdigest routine should be used:
502
503 [shell] htdigest -c .htdigest domain_name user
504
505When creating server, parameters should be:
506
507 auto serv = new THttpServer("http:8080?auth_file=.htdigets&auth_domain=domain_name");
508
509*/
510
511////////////////////////////////////////////////////////////////////////////////
512/// constructor
513
515 : THttpEngine("civetweb", "compact embedded http server"),
516 fOnlySecured(only_secured)
517{
518}
519
520////////////////////////////////////////////////////////////////////////////////
521/// destructor
522
524{
525 if (fCtx && !fTerminating)
526 mg_stop(fCtx);
527}
528
529////////////////////////////////////////////////////////////////////////////////
530/// process civetweb log message, can be used to detect critical errors
531
532Int_t TCivetweb::ProcessLog(const char *message)
533{
534 if ((gDebug > 0) || (strstr(message, "cannot bind to") != 0))
535 Error("Log", "%s", message);
536
537 return 0;
538}
539
540////////////////////////////////////////////////////////////////////////////////
541/// Returns number of actively used threads
542
544{
545 std::lock_guard<std::mutex> guard(fMutex);
547}
548
549////////////////////////////////////////////////////////////////////////////////
550/// Returns number of actively used threads
551
553{
554 std::lock_guard<std::mutex> guard(fMutex);
555 fNumActiveThreads += cnt;
556 return fNumActiveThreads;
557}
558
559
560////////////////////////////////////////////////////////////////////////////////
561/// Creates embedded civetweb server
562///
563/// @param args string with civetweb server configuration
564///
565/// As main argument, http port should be specified like "8090".
566/// Or one can provide combination of ipaddress and portnumber like "127.0.0.1:8090"
567/// Or one can specify unix socket name like "x/tmp/root.socket"
568/// Extra parameters like in URL string could be specified after '?' mark:
569///
570/// thrds=N - there N is number of threads used by the civetweb (default is 10)
571/// top=name - configure top name, visible in the web browser
572/// ssl_certificate=filename - SSL certificate, see docs/OpenSSL.md from civetweb
573/// auth_file=filename - authentication file name, created with htdigets utility
574/// auth_domain=domain - authentication domain
575/// websocket_timeout=tm - set web sockets timeout in seconds (default 300)
576/// websocket_disable - disable web sockets handling (default enabled)
577/// bind - ip address to bind server socket
578/// loopback - bind specified port to loopback 127.0.0.1 address
579/// debug - enable debug mode, server always returns html page with request info
580/// log=filename - configure civetweb log file
581/// max_age=value - configures "Cache-Control: max_age=value" http header for all file-related requests, default 3600
582/// socket_mode=value - configures unix socket mode, default is 0700
583/// nocache - try to fully disable cache control for file requests
584/// winsymlinks=no - do not resolve symbolic links on file system (Windows only), default true
585/// dirlisting=no - enable/disable directory listing for browsing filesystem (default no)
586///
587/// Examples of valid args values:
588///
589/// serv->CreateEngine("http:8080?websocket_disable");
590/// serv->CreateEngine("http:7546?thrds=30&websocket_timeout=20");
591
592Bool_t TCivetweb::Create(const char *args)
593{
594 memset(&fCallbacks, 0, sizeof(struct mg_callbacks));
595 // fCallbacks.begin_request = begin_request_handler;
597 TString sport = IsSecured() ? "8480s" : "8080",
598 num_threads,
599 websocket_timeout = "300000",
600 dir_listening = "no",
601 auth_file,
602 auth_domain,
603 log_file,
604 ssl_cert,
605 max_age;
606 Int_t socket_mode = 0700;
607 bool use_ws = kTRUE, is_socket = false;
608
609 // extract arguments
610 if (args && *args) {
611
612 // first extract port number
613 sport = "";
614
615 is_socket = *args == 'x';
616
617 while ((*args != 0) && (*args != '?') && (is_socket || (*args != '/')))
618 sport.Append(*args++);
619 if (IsSecured() && (sport.Index("s") == kNPOS) && !is_socket)
620 sport.Append("s");
621
622 // than search for extra parameters
623 while ((*args != 0) && (*args != '?'))
624 args++;
625
626 if (*args == '?') {
627 TUrl url(TString::Format("http://localhost/folder%s", args));
628
629 if (url.IsValid()) {
630 url.ParseOptions();
631
632 fWebGui = url.HasOption("webgui");
633
634 const char *top = url.GetValueFromOptions("top");
635 if (top)
636 fTopName = top;
637
638 const char *log = url.GetValueFromOptions("log");
639 if (log)
640 log_file = log;
641
642 Int_t thrds = url.GetIntValueFromOptions("thrds");
643 if (thrds > 0)
644 fNumThreads = thrds;
645
646 const char *afile = url.GetValueFromOptions("auth_file");
647 if (afile)
648 auth_file = afile;
649
650 const char *adomain = url.GetValueFromOptions("auth_domain");
651 if (adomain)
652 auth_domain = adomain;
653
654 const char *sslc = url.GetValueFromOptions("ssl_cert");
655 if (sslc)
656 ssl_cert = sslc;
657
658 Int_t wtmout = url.GetIntValueFromOptions("websocket_timeout");
659 if (wtmout > 0) {
660 websocket_timeout.Format("%d", wtmout * 1000);
661 use_ws = kTRUE;
662 }
663
664 if (url.HasOption("websocket_disable"))
665 use_ws = kFALSE;
666
667 if (url.HasOption("debug"))
668 fDebug = kTRUE;
669
670 const char *winsymlinks = url.GetValueFromOptions("winsymlinks");
671 if (winsymlinks)
672 fWinSymLinks = strcmp(winsymlinks,"no") != 0;
673
674 const char *dls = url.GetValueFromOptions("dirlisting");
675 if (dls && (!strcmp(dls,"no") || !strcmp(dls,"yes")))
676 dir_listening = dls;
677
678 const char *smode = url.GetValueFromOptions("socket_mode");
679 if (smode)
680 socket_mode = std::stoi(smode, nullptr, *smode=='0' ? 8 : 10);
681
682 if (url.HasOption("loopback") && (sport.Index(":") == kNPOS))
683 sport = TString("127.0.0.1:") + sport;
684
685 if (url.HasOption("bind") && (sport.Index(":") == kNPOS)) {
686 const char *addr = url.GetValueFromOptions("bind");
687 if (addr && strlen(addr))
688 sport = TString(addr) + ":" + sport;
689 }
690
691 if (GetServer() && url.HasOption("cors")) {
692 const char *cors = url.GetValueFromOptions("cors");
693 GetServer()->SetCors(cors && *cors ? cors : "*");
694 }
695
696 if (GetServer() && url.HasOption("cred_cors")) {
697 const char *cred = url.GetValueFromOptions("cred_cors");
698 GetServer()->SetCorsCredentials(cred && *cred ? cred : "true");
699 }
700
701 if (url.HasOption("nocache"))
702 fMaxAge = 0;
703
704 if (url.HasOption("max_age"))
705 fMaxAge = url.GetIntValueFromOptions("max_age");
706
707 max_age.Form("%d", fMaxAge);
708 }
709 }
710 }
711
712 num_threads.Form("%d", fNumThreads);
713
714 const char *options[30];
715 int op = 0;
716
717 Info("Create", "Starting HTTP server on port %s", sport.Data());
718
719 options[op++] = "listening_ports";
720 options[op++] = sport.Data();
721 options[op++] = "num_threads";
722 options[op++] = num_threads.Data();
723
724 if (use_ws) {
725 options[op++] = "websocket_timeout_ms";
726 options[op++] = websocket_timeout.Data();
727 }
728
729 if ((auth_file.Length() > 0) && (auth_domain.Length() > 0)) {
730 options[op++] = "global_auth_file";
731 options[op++] = auth_file.Data();
732 options[op++] = "authentication_domain";
733 options[op++] = auth_domain.Data();
734 } else {
735 options[op++] = "enable_auth_domain_check";
736 options[op++] = "no";
737 }
738
739 if (log_file.Length() > 0) {
740 options[op++] = "error_log_file";
741 options[op++] = log_file.Data();
742 }
743
744 if (ssl_cert.Length() > 0) {
745 options[op++] = "ssl_certificate";
746 options[op++] = ssl_cert.Data();
747 } else if (IsSecured()) {
748 Error("Create", "No SSL certificate file configured");
749 }
750
751 if (max_age.Length() > 0) {
752 options[op++] = "static_file_max_age";
753 options[op++] = max_age.Data();
754 }
755
756 if (GetServer() && GetServer()->IsCors()) {
757 // also used for the file transfer
758 options[op++] = "access_control_allow_origin";
759 options[op++] = GetServer()->GetCors();
760 }
761
762 if (GetServer() && GetServer()->IsCorsCredentials()) {
763 options[op++] = "access_control_allow_credentials";
764 options[op++] = GetServer()->GetCorsCredentials();
765 // enables partial files reading with credentials
766 // can be enabled after nect civetweb upgrade
767 // options[op++] = "access_control_expose_headers";
768 // options[op++] = "Accept-Ranges";
769 // options[op++] = "access_control_allow_methods";
770 // options[op++] = "GET, HEAD, OPTIONS";
771 }
772
773 options[op++] = "enable_directory_listing";
774 options[op++] = dir_listening.Data();
775
776 options[op++] = nullptr;
777
778 // try to remove socket file - if any
779 if (is_socket && !sport.Contains(","))
780 gSystem->Unlink(sport.Data()+1);
781
782 // Start the web server.
783 fCtx = mg_start(&fCallbacks, this, options);
784
785 if (!fCtx)
786 return kFALSE;
787
789
790 if (use_ws)
793
794 // try to remove socket file - if any
795 if (is_socket && !sport.Contains(","))
796 gSystem->Chmod(sport.Data()+1, socket_mode);
797
798 return kTRUE;
799}
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
int websocket_connect_handler(const struct mg_connection *conn, void *)
Definition TCivetweb.cxx:98
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 *)
Bool_t CheckEngineThreads(TCivetweb *engine, const char *uri, Bool_t longpoll)
Check if engine has enough threads to process connect to new websocket handle.
Definition TCivetweb.cxx:81
void websocket_ready_handler(struct mg_connection *conn, void *)
Bool_t IsBadLongPollConnect(TCivetweb *engine, const std::shared_ptr< THttpCallArg > &arg)
Returns kTRUE in case of longpoll connection request - or at least looks like that.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
char name[80]
Definition TGX11.cxx:110
float * q
#define INVALID_HANDLE_VALUE
Definition TMapFile.cxx:84
Int_t gDebug
Definition TROOT.cxx:597
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
#define BUFSIZE
const char * mime_type
Definition civetweb.c:8028
int mg_printf(struct mg_connection *conn, const char *fmt,...)
Definition civetweb.c:6937
void mg_send_file(struct mg_connection *conn, const char *path)
Definition civetweb.c:10206
struct mg_context * mg_start(const struct mg_callbacks *callbacks, void *user_data, const char **options)
Definition civetweb.c:20285
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)
Definition civetweb.c:13797
void mg_send_mime_file(struct mg_connection *conn, const char *path, const char *mime_type)
Definition civetweb.c:10213
const char * mg_get_header(const struct mg_connection *conn, const char *name)
Definition civetweb.c:3802
int mg_write(struct mg_connection *conn, const void *buf, size_t len)
Definition civetweb.c:6696
const struct mg_request_info * mg_get_request_info(const struct mg_connection *conn)
Definition civetweb.c:3488
void mg_set_user_connection_data(const struct mg_connection *const_conn, void *data)
Definition civetweb.c:3190
void * mg_get_user_connection_data(const struct mg_connection *conn)
Definition civetweb.c:3203
int mg_read(struct mg_connection *conn, void *buf, size_t len)
Definition civetweb.c:6588
void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
Definition civetweb.c:13775
struct mg_context * mg_get_context(const struct mg_connection *conn)
Definition civetweb.c:3153
void mg_stop(struct mg_context *ctx)
Definition civetweb.c:19494
void * mg_get_user_data(const struct mg_context *ctx)
Definition civetweb.c:3160
int mg_websocket_write(struct mg_connection *conn, int opcode, const char *data, size_t data_len)
@ MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE
Definition civetweb.h:861
@ MG_WEBSOCKET_OPCODE_BINARY
Definition civetweb.h:860
@ MG_WEBSOCKET_OPCODE_TEXT
Definition civetweb.h:859
TCivetwebWSEngine.
Definition TCivetweb.cxx:32
struct mg_connection * fWSconn
Definition TCivetweb.cxx:34
UInt_t GetId() const override
Definition TCivetweb.cxx:44
void SendCharStar(const char *str) override
Envelope for sending string via the websocket.
Definition TCivetweb.cxx:71
void ClearHandle(Bool_t terminate) override
Definition TCivetweb.cxx:46
Bool_t SupportSendThrd() const override
True websocket requires extra thread to parallelize sending.
Definition TCivetweb.cxx:37
~TCivetwebWSEngine() override=default
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...
Definition TCivetweb.cxx:63
void Send(const void *buf, int len) override
Definition TCivetweb.cxx:53
TCivetwebWSEngine(struct mg_connection *conn)
Definition TCivetweb.cxx:40
THttpEngine implementation, based on civetweb embedded server.
Definition TCivetweb.h:21
Int_t fNumThreads
! number of configured threads
Definition TCivetweb.h:25
Int_t fNumActiveThreads
! number of active threads - used in request and websocket handling
Definition TCivetweb.h:26
struct mg_context * fCtx
! civetweb context
Definition TCivetweb.h:23
std::mutex fMutex
! mutex to read/write fNumActiveThreads
Definition TCivetweb.h:27
Bool_t fWinSymLinks
! resolve symbolic links on Windows
Definition TCivetweb.h:34
Bool_t IsTerminating() const
Definition TCivetweb.h:58
Int_t GetMaxAge() const
Definition TCivetweb.h:64
const char * GetTopName() const
Definition TCivetweb.h:52
TCivetweb(Bool_t only_secured=kFALSE)
constructor
Int_t fMaxAge
! max-age parameter
Definition TCivetweb.h:33
Bool_t IsWebGui() const
Definition TCivetweb.h:54
Int_t ProcessLog(const char *message)
process civetweb log message, can be used to detect critical errors
TString fTopName
! name of top item
Definition TCivetweb.h:28
Int_t GetNumAvailableThreads()
Returns number of actively used threads.
Bool_t fTerminating
! server doing shutdown and not react on requests
Definition TCivetweb.h:31
Bool_t Create(const char *args) override
Creates embedded civetweb server.
Int_t ChangeNumActiveThrerads(int cnt=0)
Returns number of actively used threads.
Int_t GetNumThreads() const
Definition TCivetweb.h:46
Bool_t IsSecured() const
Definition TCivetweb.h:38
Bool_t IsDebugMode() const
Definition TCivetweb.h:56
virtual ~TCivetweb()
destructor
Bool_t IsWinSymLinks() const
Definition TCivetweb.h:60
Bool_t fWebGui
! if server used for webgui
Definition TCivetweb.h:29
Bool_t fDebug
! debug mode
Definition TCivetweb.h:30
struct mg_callbacks fCallbacks
! call-back table for civetweb webserver
Definition TCivetweb.h:24
Abstract class for implementing http protocol for THttpServer.
Definition THttpEngine.h:19
THttpServer * GetServer() const
Returns pointer to THttpServer associated with engine.
Definition THttpEngine.h:40
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
const char * GetCors() const
Returns specified CORS domain.
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 SetCorsCredentials(const std::string &value="true")
Enable/disable usage Access-Control-Allow-Credentials response header.
void SetCors(const std::string &domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
const char * GetCorsCredentials() const
Returns specified CORS credentials value - if any.
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.
Internal instance used to exchange WS functionality between THttpServer and THttpWSHandler.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:961
Basic string class.
Definition TString.h:139
Int_t Atoi() const
Return integer value of string.
Definition TString.cxx:1966
const char * Data() const
Definition TString.h:380
@ kIgnoreCase
Definition TString.h:279
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:670
TString & Append(const char *cs)
Definition TString.h:576
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2356
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:636
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
virtual int Chmod(const char *file, UInt_t mode)
Set the file permission bits. Returns -1 in case or error, 0 otherwise.
Definition TSystem.cxx:1493
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1368
This class represents a WWW compatible URL.
Definition TUrl.h:33
const char * GetValueFromOptions(const char *key) const
Return a value for a given key from the URL options.
Definition TUrl.cxx:660
Bool_t IsValid() const
Definition TUrl.h:79
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.
Definition TUrl.cxx:672
void ParseOptions() const
Parse URL options into a key/value map.
Definition TUrl.cxx:626
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
Definition TUrl.cxx:683
const Int_t n
Definition legend1.C:16
TCivetweb * fEngine
TEngineHolder(TCivetweb *engine)
int(* log_message)(const struct mg_connection *, const char *message)
Definition civetweb.h:240
const char * value
Definition civetweb.h:145
const char * name
Definition civetweb.h:144
struct mg_header http_headers[(64)]
Definition civetweb.h:179
const char * local_uri
Definition civetweb.h:157
void * user_data
Definition civetweb.h:175
const char * request_method
Definition civetweb.h:151
const char * query_string
Definition civetweb.h:162
void * conn_data
Definition civetweb.h:176
const char * remote_user
Definition civetweb.h:164