Logo ROOT  
Reference Guide
THttpServer.h
Go to the documentation of this file.
1 // $Id$
2 // Author: Sergey Linev 21/12/2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_THttpServer
13 #define ROOT_THttpServer
14 
15 #include "TNamed.h"
16 #include "TList.h"
17 #include "THttpCallArg.h"
18 
19 #include <mutex>
20 #include <map>
21 #include <string>
22 #include <memory>
23 #include <queue>
24 #include <thread>
25 #include <vector>
26 
27 class THttpEngine;
28 class THttpTimer;
29 class TRootSniffer;
30 
31 class THttpServer : public TNamed {
32 
33 protected:
34  TList fEngines; ///<! engines which runs http server
35  THttpTimer *fTimer{nullptr}; ///<! timer used to access main thread
36  TRootSniffer *fSniffer{nullptr}; ///<! sniffer provides access to ROOT objects hierarchy
37  Bool_t fTerminated{kFALSE}; ///<! termination flag, disables all requests processing
38  Long_t fMainThrdId{0}; ///<! id of the thread for processing requests
39  Bool_t fOwnThread{kFALSE}; ///<! true when specialized thread allocated for processing requests
40  std::thread fThrd; ///<! own thread
41  Bool_t fOldProcessSignature{kFALSE}; ///<! flag used to detect usage of old signature of Process() method
42 
43  TString fJSROOTSYS; ///<! location of local JSROOT files
44  TString fTopName{"ROOT"}; ///<! name of top folder, default - "ROOT"
45  TString fJSROOT; ///<! location of external JSROOT files
46 
47  std::map<std::string, std::string> fLocations; ///<! list of local directories, which could be accessed via server
48 
49  std::string fDefaultPage; ///<! file name for default page name
50  std::string fDefaultPageCont; ///<! content of default html page
51  std::string fDrawPage; ///<! file name for drawing of single element
52  std::string fDrawPageCont; ///<! content of draw html page
53  std::string fCors; ///<! CORS: sets Access-Control-Allow-Origin header for ProcessRequest responses
54 
55  std::mutex fMutex; ///<! mutex to protect list with arguments
56  std::queue<std::shared_ptr<THttpCallArg>> fArgs; ///<! submitted arguments
57 
58  std::mutex fWSMutex; ///<! mutex to protect WS handler lists
59  std::vector<std::shared_ptr<THttpWSHandler>> fWSHandlers; ///<! list of WS handlers
60 
61  virtual void MissedRequest(THttpCallArg *arg);
62 
63  virtual void ProcessRequest(std::shared_ptr<THttpCallArg> arg);
64 
65  virtual void ProcessBatchHolder(std::shared_ptr<THttpCallArg> &arg);
66 
67  virtual void ProcessRequest(THttpCallArg *arg);
68 
69  void StopServerThread();
70 
71  static Bool_t VerifyFilePath(const char *fname);
72 
73 public:
74  THttpServer(const char *engine = "civetweb:8080");
75  virtual ~THttpServer();
76 
77  Bool_t CreateEngine(const char *engine);
78 
79  Bool_t IsAnyEngine() const { return fEngines.GetSize() > 0; }
80 
81  /** returns pointer on objects sniffer */
82  TRootSniffer *GetSniffer() const { return fSniffer; }
83 
84  void SetSniffer(TRootSniffer *sniff);
85 
86  Bool_t IsReadOnly() const;
87 
88  void SetReadOnly(Bool_t readonly);
89 
90  /** set termination flag, no any further requests will be processed */
91  void SetTerminate();
92 
93  /** returns kTRUE, if server was terminated */
94  Bool_t IsTerminated() const { return fTerminated; }
95 
96  /** Enable CORS header to ProcessRequests() responses
97  * Specified location (typically "*") add as "Access-Control-Allow-Origin" header */
98  void SetCors(const std::string &domain = "*") { fCors = domain; }
99 
100  /** Returns kTRUE if CORS was configured */
101  Bool_t IsCors() const { return !fCors.empty(); }
102 
103  /** Returns specified CORS domain */
104  const char *GetCors() const { return fCors.c_str(); }
105 
106  /** set name of top item in objects hierarchy */
107  void SetTopName(const char *top) { fTopName = top; }
108 
109  /** returns name of top item in objects hierarchy */
110  const char *GetTopName() const { return fTopName.Data(); }
111 
112  void SetJSROOT(const char *location);
113 
114  void AddLocation(const char *prefix, const char *path);
115 
116  void SetDefaultPage(const std::string &filename = "");
117 
118  void SetDrawPage(const std::string &filename = "");
119 
120  void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);
121 
122  void CreateServerThread();
123 
124  /** Check if file is requested, thread safe */
125  Bool_t IsFileRequested(const char *uri, TString &res) const;
126 
127  /** Execute HTTP request */
128  Bool_t ExecuteHttp(std::shared_ptr<THttpCallArg> arg);
129 
130  /** Submit HTTP request */
131  Bool_t SubmitHttp(std::shared_ptr<THttpCallArg> arg, Bool_t can_run_immediately = kFALSE);
132 
133  /** Process submitted requests, must be called from appropriate thread */
135 
136  /** Register object in subfolder */
137  Bool_t Register(const char *subfolder, TObject *obj);
138 
139  /** Unregister object */
140  Bool_t Unregister(TObject *obj);
141 
142  /** Register WS handler*/
143  void RegisterWS(std::shared_ptr<THttpWSHandler> ws);
144 
145  /** Unregister WS handler*/
146  void UnregisterWS(std::shared_ptr<THttpWSHandler> ws);
147 
148  /** Find web-socket handler with given name */
149  std::shared_ptr<THttpWSHandler> FindWS(const char *name);
150 
151  /** Execute WS request */
152  Bool_t ExecuteWS(std::shared_ptr<THttpCallArg> &arg, Bool_t external_thrd = kFALSE, Bool_t wait_process = kFALSE);
153 
154  /** Restrict access to specified object */
155  void Restrict(const char *path, const char *options);
156 
157  Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon = 0);
158 
159  Bool_t Hide(const char *fullname, Bool_t hide = kTRUE);
160 
161  Bool_t SetIcon(const char *fullname, const char *iconname);
162 
163  Bool_t CreateItem(const char *fullname, const char *title);
164 
165  Bool_t SetItemField(const char *fullname, const char *name, const char *value);
166 
167  const char *GetItemField(const char *fullname, const char *name);
168 
169  /** Guess mime type base on file extension */
170  static const char *GetMimeType(const char *path);
171 
172  /** Reads content of file from the disk */
173  static char *ReadFileContent(const char *filename, Int_t &len);
174 
175  /** Reads content of file from the disk, use std::string in return value */
176  static std::string ReadFileContent(const std::string &filename);
177 
178  ClassDef(THttpServer, 0) // HTTP server for ROOT analysis
179 };
180 
181 #endif
THttpServer::IsFileRequested
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
Definition: THttpServer.cxx:528
THttpServer::fWSMutex
std::mutex fWSMutex
! mutex to protect WS handler lists
Definition: THttpServer.h:58
THttpServer::IsReadOnly
Bool_t IsReadOnly() const
returns read-only mode
Definition: THttpServer.cxx:255
THttpServer::IsCors
Bool_t IsCors() const
Returns kTRUE if CORS was configured.
Definition: THttpServer.h:101
THttpServer::fWSHandlers
std::vector< std::shared_ptr< THttpWSHandler > > fWSHandlers
! list of WS handlers
Definition: THttpServer.h:59
THttpServer::fJSROOT
TString fJSROOT
! location of external JSROOT files
Definition: THttpServer.h:45
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
THttpServer::ExecuteHttp
Bool_t ExecuteHttp(std::shared_ptr< THttpCallArg > arg)
Execute HTTP request.
Definition: THttpServer.cxx:557
THttpServer::ProcessRequests
Int_t ProcessRequests()
Process submitted requests, must be called from appropriate thread.
Definition: THttpServer.cxx:616
THttpServer::GetSniffer
TRootSniffer * GetSniffer() const
returns pointer on objects sniffer
Definition: THttpServer.h:82
THttpServer::fThrd
std::thread fThrd
! own thread
Definition: THttpServer.h:40
THttpServer::SetItemField
Bool_t SetItemField(const char *fullname, const char *name, const char *value)
Definition: THttpServer.cxx:1184
THttpServer::Restrict
void Restrict(const char *path, const char *options)
Restrict access to specified object.
Definition: THttpServer.cxx:1118
THttpEngine
Definition: THttpEngine.h:19
THttpServer::fDrawPageCont
std::string fDrawPageCont
! content of draw html page
Definition: THttpServer.h:52
THttpServer::ProcessRequest
virtual void ProcessRequest(std::shared_ptr< THttpCallArg > arg)
Process single http request Depending from requested path and filename different actions will be perf...
Definition: THttpServer.cxx:704
TString::Data
const char * Data() const
Definition: TString.h:369
THttpServer::CreateItem
Bool_t CreateItem(const char *fullname, const char *title)
Definition: THttpServer.cxx:1177
THttpServer::CreateServerThread
void CreateServerThread()
Creates special thread to process all requests, directed to http server.
Definition: THttpServer.cxx:434
TNamed.h
THttpServer::fEngines
TList fEngines
! engines which runs http server
Definition: THttpServer.h:34
THttpServer::UnregisterWS
void UnregisterWS(std::shared_ptr< THttpWSHandler > ws)
Unregister WS handler.
Definition: THttpServer.cxx:1009
THttpServer::GetItemField
const char * GetItemField(const char *fullname, const char *name)
Definition: THttpServer.cxx:1191
THttpServer::MissedRequest
virtual void MissedRequest(THttpCallArg *arg)
Method called when THttpServer cannot process request By default such requests replied with 404 code ...
Definition: THttpServer.cxx:679
THttpServer::IsTerminated
Bool_t IsTerminated() const
returns kTRUE, if server was terminated
Definition: THttpServer.h:94
THttpServer::fOwnThread
Bool_t fOwnThread
! true when specialized thread allocated for processing requests
Definition: THttpServer.h:39
Int_t
int Int_t
Definition: RtypesCore.h:45
THttpCallArg
Definition: THttpCallArg.h:27
THttpServer::SetDefaultPage
void SetDefaultPage(const std::string &filename="")
Set file name of HTML page, delivered by the server when http address is opened in the browser.
Definition: THttpServer.cxx:308
TList.h
THttpServer::SetDrawPage
void SetDrawPage(const std::string &filename="")
Set file name of HTML page, delivered by the server when objects drawing page is requested from the b...
Definition: THttpServer.cxx:325
THttpServer::fTerminated
Bool_t fTerminated
! termination flag, disables all requests processing
Definition: THttpServer.h:37
THttpServer::SetReadOnly
void SetReadOnly(Bool_t readonly)
Set read-only mode for the server (default on) In read-only server is not allowed to change any ROOT ...
Definition: THttpServer.cxx:265
TString
Basic string class.
Definition: TString.h:136
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
THttpServer::SetCors
void SetCors(const std::string &domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
Definition: THttpServer.h:98
bool
THttpServer::fTopName
TString fTopName
! name of top folder, default - "ROOT"
Definition: THttpServer.h:44
THttpServer::THttpServer
THttpServer(const char *engine="civetweb:8080")
constructor
Definition: THttpServer.cxx:139
THttpServer::fTimer
THttpTimer * fTimer
! timer used to access main thread
Definition: THttpServer.h:35
THttpServer::fOldProcessSignature
Bool_t fOldProcessSignature
! flag used to detect usage of old signature of Process() method
Definition: THttpServer.h:41
ws
void ws()
Definition: ws.C:66
THttpServer::fDrawPage
std::string fDrawPage
! file name for drawing of single element
Definition: THttpServer.h:51
THttpServer::fSniffer
TRootSniffer * fSniffer
! sniffer provides access to ROOT objects hierarchy
Definition: THttpServer.h:36
THttpServer
Definition: THttpServer.h:31
THttpServer::fArgs
std::queue< std::shared_ptr< THttpCallArg > > fArgs
! submitted arguments
Definition: THttpServer.h:56
THttpServer::fJSROOTSYS
TString fJSROOTSYS
! location of local JSROOT files
Definition: THttpServer.h:43
THttpServer::SetTerminate
void SetTerminate()
set termination flag, no any further requests will be processed
Definition: THttpServer.cxx:247
THttpServer::Register
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
Definition: THttpServer.cxx:980
THttpServer::fCors
std::string fCors
! CORS: sets Access-Control-Allow-Origin header for ProcessRequest responses
Definition: THttpServer.h:53
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
THttpServer::fMutex
std::mutex fMutex
! mutex to protect list with arguments
Definition: THttpServer.h:55
Long_t
long Long_t
Definition: RtypesCore.h:54
THttpServer::CreateEngine
Bool_t CreateEngine(const char *engine)
factory method to create different http engines At the moment two engine kinds are supported: civetwe...
Definition: THttpServer.cxx:346
THttpServer::SetTimer
void SetTimer(Long_t milliSec=100, Bool_t mode=kTRUE)
create timer which will invoke ProcessRequests() function periodically Timer is required to perform a...
Definition: THttpServer.cxx:408
THttpServer::fLocations
std::map< std::string, std::string > fLocations
! list of local directories, which could be accessed via server
Definition: THttpServer.h:47
THttpServer::ExecuteWS
Bool_t ExecuteWS(std::shared_ptr< THttpCallArg > &arg, Bool_t external_thrd=kFALSE, Bool_t wait_process=kFALSE)
Execute WS request.
Definition: THttpServer.cxx:1036
THttpCallArg.h
THttpServer::GetMimeType
static const char * GetMimeType(const char *path)
Guess mime type base on file extension.
Definition: THttpServer.cxx:1199
THttpServer::SubmitHttp
Bool_t SubmitHttp(std::shared_ptr< THttpCallArg > arg, Bool_t can_run_immediately=kFALSE)
Submit HTTP request.
Definition: THttpServer.cxx:590
THttpServer::SetSniffer
void SetSniffer(TRootSniffer *sniff)
Set TRootSniffer to the server Server takes ownership over sniffer.
Definition: THttpServer.cxx:236
THttpServer::StopServerThread
void StopServerThread()
Stop server thread Normally called shortly before http server destructor.
Definition: THttpServer.cxx:465
THttpServer::fDefaultPage
std::string fDefaultPage
! file name for default page name
Definition: THttpServer.h:49
THttpServer::AddLocation
void AddLocation(const char *prefix, const char *path)
add files location, which could be used in the server one could map some system folder to the server ...
Definition: THttpServer.cxx:277
THttpServer::~THttpServer
virtual ~THttpServer()
destructor delete all http engines and sniffer
Definition: THttpServer.cxx:215
THttpServer::fDefaultPageCont
std::string fDefaultPageCont
! content of default html page
Definition: THttpServer.h:50
TCollection::GetSize
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
THttpServer::GetCors
const char * GetCors() const
Returns specified CORS domain.
Definition: THttpServer.h:104
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
name
char name[80]
Definition: TGX11.cxx:110
THttpServer::RegisterCommand
Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon=0)
Register command which can be executed from web interface.
Definition: THttpServer.cxx:1151
THttpServer::SetTopName
void SetTopName(const char *top)
set name of top item in objects hierarchy
Definition: THttpServer.h:107
THttpServer::SetJSROOT
void SetJSROOT(const char *location)
Set location of JSROOT to use with the server One could specify address like: https://root....
Definition: THttpServer.cxx:297
THttpServer::VerifyFilePath
static Bool_t VerifyFilePath(const char *fname)
Checked that filename does not contains relative path below current directory Used to prevent access ...
Definition: THttpServer.cxx:479
THttpServer::Unregister
Bool_t Unregister(TObject *obj)
Unregister object.
Definition: THttpServer.cxx:990
TRootSniffer
Definition: TRootSniffer.h:115
THttpServer::Hide
Bool_t Hide(const char *fullname, Bool_t hide=kTRUE)
hides folder or element from web gui
Definition: THttpServer.cxx:1159
THttpServer::fMainThrdId
Long_t fMainThrdId
! id of the thread for processing requests
Definition: THttpServer.h:38
THttpServer::ReadFileContent
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
Definition: THttpServer.cxx:1272
THttpServer::SetIcon
Bool_t SetIcon(const char *fullname, const char *iconname)
set name of icon, used in browser together with the item
Definition: THttpServer.cxx:1170
THttpServer::FindWS
std::shared_ptr< THttpWSHandler > FindWS(const char *name)
Find web-socket handler with given name.
Definition: THttpServer.cxx:1022
THttpServer::IsAnyEngine
Bool_t IsAnyEngine() const
Definition: THttpServer.h:79
THttpServer::ProcessBatchHolder
virtual void ProcessBatchHolder(std::shared_ptr< THttpCallArg > &arg)
Process special http request for root_batch_holder.js script This kind of requests used to hold web b...
Definition: THttpServer.cxx:689
THttpServer::RegisterWS
void RegisterWS(std::shared_ptr< THttpWSHandler > ws)
Register WS handler.
Definition: THttpServer.cxx:1000
TList
A doubly linked list.
Definition: TList.h:44
THttpServer::GetTopName
const char * GetTopName() const
returns name of top item in objects hierarchy
Definition: THttpServer.h:110