Logo ROOT   6.14/05
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 "TObject.h"
16 
17 #include "TList.h"
18 
19 #include "TNamed.h"
20 
21 #include "THttpCallArg.h"
22 
23 #include <mutex>
24 #include <map>
25 #include <string>
26 #include <memory>
27 #include <queue>
28 
29 class THttpEngine;
30 class THttpTimer;
31 class TRootSniffer;
32 
33 class THttpServer : public TNamed {
34 
35 protected:
36  TList fEngines; ///<! engines which runs http server
37  THttpTimer *fTimer{nullptr}; ///<! timer used to access main thread
38  TRootSniffer *fSniffer{nullptr}; ///<! sniffer provides access to ROOT objects hierarchy
39  Bool_t fTerminated{kFALSE}; ///<! termination flag, disables all requests processing
40  Long_t fMainThrdId{0}; ///<! id of the main ROOT process
41 
42  TString fJSROOTSYS; ///<! location of local JSROOT files
43  TString fTopName{"ROOT"}; ///<! name of top folder, default - "ROOT"
44  TString fJSROOT; ///<! location of external JSROOT files
45 
46  std::map<std::string, std::string> fLocations; ///<! list of local directories, which could be accessed via server
47 
48  std::string fDefaultPage; ///<! file name for default page name
49  std::string fDefaultPageCont; ///<! content of default html page
50  std::string fDrawPage; ///<! file name for drawing of single element
51  std::string fDrawPageCont; ///<! content of draw html page
52  std::string fCors; ///<! CORS: sets Access-Control-Allow-Origin header for ProcessRequest responses
53 
54  std::mutex fMutex; ///<! mutex to protect list with arguments
55  TList fCallArgs; ///<! submitted arguments
56  std::queue<std::shared_ptr<THttpCallArg>> fArgs; ///<! submitted arguments
57 
58  virtual void MissedRequest(THttpCallArg *arg);
59 
60  virtual void ProcessRequest(std::shared_ptr<THttpCallArg> arg);
61 
62  virtual void ProcessRequest(THttpCallArg *arg);
63 
64  static Bool_t VerifyFilePath(const char *fname);
65 
66 public:
67  THttpServer(const char *engine = "civetweb:8080");
68  virtual ~THttpServer();
69 
70  Bool_t CreateEngine(const char *engine);
71 
72  Bool_t IsAnyEngine() const { return fEngines.GetSize() > 0; }
73 
74  /** returns pointer on objects sniffer */
75  TRootSniffer *GetSniffer() const { return fSniffer; }
76 
77  void SetSniffer(TRootSniffer *sniff);
78 
79  Bool_t IsReadOnly() const;
80 
81  void SetReadOnly(Bool_t readonly);
82 
83  /** set termination flag, no any further requests will be processed */
84  void SetTerminate();
85 
86  /** returns kTRUE, if server was terminated */
87  Bool_t IsTerminated() const { return fTerminated; }
88 
89  /** Enable CORS header to ProcessRequests() responses
90  * Specified location (typically "*") add as "Access-Control-Allow-Origin" header */
91  void SetCors(const std::string &domain = "*") { fCors = domain; }
92 
93  /** Returns kTRUE if CORS was configured */
94  Bool_t IsCors() const { return !fCors.empty(); }
95 
96  /** Returns specified CORS domain */
97  const char *GetCors() const { return fCors.c_str(); }
98 
99  /** set name of top item in objects hierarchy */
100  void SetTopName(const char *top) { fTopName = top; }
101 
102  /** returns name of top item in objects hierarchy */
103  const char *GetTopName() const { return fTopName.Data(); }
104 
105  void SetJSROOT(const char *location);
106 
107  void AddLocation(const char *prefix, const char *path);
108 
109  void SetDefaultPage(const std::string &filename = "");
110 
111  void SetDrawPage(const std::string &filename = "");
112 
113  void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);
114 
115  /** Check if file is requested, thread safe */
116  Bool_t IsFileRequested(const char *uri, TString &res) const;
117 
118  /** Execute HTTP request */
119  Bool_t ExecuteHttp(THttpCallArg *arg) _R__DEPRECATED_618("Please use ExecuteHttp(std::shared_ptr<THttpCallArg>)");
120 
121  /** Execute HTTP request */
122  Bool_t ExecuteHttp(std::shared_ptr<THttpCallArg> arg);
123 
124  /** Submit HTTP request */
125  Bool_t SubmitHttp(THttpCallArg *arg, Bool_t can_run_immediately = kFALSE, Bool_t ownership = kFALSE) _R__DEPRECATED_618("Please use SubmitHttp(std::shared_ptr<THttpCallArg>,bool)");
126 
127  /** Submit HTTP request */
128  Bool_t SubmitHttp(std::shared_ptr<THttpCallArg> arg, Bool_t can_run_immediately = kFALSE);
129 
130  /** Process submitted requests, must be called from main thread */
131  void ProcessRequests();
132 
133  /** Register object in subfolder */
134  Bool_t Register(const char *subfolder, TObject *obj);
135 
136  /** Unregister object */
137  Bool_t Unregister(TObject *obj);
138 
139  /** Restrict access to specified object */
140  void Restrict(const char *path, const char *options);
141 
142  Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon = 0);
143 
144  Bool_t Hide(const char *fullname, Bool_t hide = kTRUE);
145 
146  Bool_t SetIcon(const char *fullname, const char *iconname);
147 
148  Bool_t CreateItem(const char *fullname, const char *title);
149 
150  Bool_t SetItemField(const char *fullname, const char *name, const char *value);
151 
152  const char *GetItemField(const char *fullname, const char *name);
153 
154  /** Guess mime type base on file extension */
155  static const char *GetMimeType(const char *path);
156 
157  /** Reads content of file from the disk */
158  static char *ReadFileContent(const char *filename, Int_t &len);
159 
160  /** Reads content of file from the disk, use std::string in return value */
161  static std::string ReadFileContent(const std::string &filename);
162 
163  ClassDef(THttpServer, 0) // HTTP server for ROOT analysis
164 };
165 
166 #endif
Bool_t IsTerminated() const
returns kTRUE, if server was terminated
Definition: THttpServer.h:87
std::string fDrawPageCont
! content of draw html page
Definition: THttpServer.h:51
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...
const char * GetTopName() const
returns name of top item in objects hierarchy
Definition: THttpServer.h:103
virtual void MissedRequest(THttpCallArg *arg)
Method called when THttpServer cannot process request By default such requests replied with 404 code ...
Bool_t IsReadOnly() const
returns read-only mode
virtual void ProcessRequest(std::shared_ptr< THttpCallArg > arg)
Process single http request Depending from requested path and filename different actions will be perf...
Bool_t IsCors() const
Returns kTRUE if CORS was configured.
Definition: THttpServer.h:94
Basic string class.
Definition: TString.h:131
TString fTopName
! name of top folder, default - "ROOT"
Definition: THttpServer.h:43
int Int_t
Definition: RtypesCore.h:41
void SetDefaultPage(const std::string &filename="")
Set file name of HTML page, delivered by the server when http address is opened in the browser...
bool Bool_t
Definition: RtypesCore.h:59
TList fEngines
! engines which runs http server
Definition: THttpServer.h:36
Bool_t fTerminated
! termination flag, disables all requests processing
Definition: THttpServer.h:39
TRootSniffer * fSniffer
! sniffer provides access to ROOT objects hierarchy
Definition: THttpServer.h:38
THttpServer(const char *engine="civetweb:8080")
constructor
std::queue< std::shared_ptr< THttpCallArg > > fArgs
! submitted arguments
Definition: THttpServer.h:56
#define ClassDef(name, id)
Definition: Rtypes.h:320
void SetTerminate()
set termination flag, no any further requests will be processed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
THttpTimer * fTimer
! timer used to access main thread
Definition: THttpServer.h:37
#define _R__DEPRECATED_618(REASON)
Definition: RConfig.h:500
void SetCors(const std::string &domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
Definition: THttpServer.h:91
void SetTimer(Long_t milliSec=100, Bool_t mode=kTRUE)
create timer which will invoke ProcessRequests() function periodically Timer is required to perform a...
TString fJSROOTSYS
! location of local JSROOT files
Definition: THttpServer.h:42
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 ...
A doubly linked list.
Definition: TList.h:44
Bool_t CreateEngine(const char *engine)
factory method to create different http engines At the moment two engine kinds are supported: civetwe...
std::string fDrawPage
! file name for drawing of single element
Definition: THttpServer.h:50
std::map< std::string, std::string > fLocations
! list of local directories, which could be accessed via server
Definition: THttpServer.h:46
virtual ~THttpServer()
destructor delete all http engines and sniffer
RooCmdArg Restrict(const char *catName, const char *stateNameList)
void SetSniffer(TRootSniffer *sniff)
Set TRootSniffer to the server Server takes ownership over sniffer.
const Bool_t kFALSE
Definition: RtypesCore.h:88
std::string fCors
! CORS: sets Access-Control-Allow-Origin header for ProcessRequest responses
Definition: THttpServer.h:52
long Long_t
Definition: RtypesCore.h:50
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 ...
std::mutex fMutex
! mutex to protect list with arguments
Definition: THttpServer.h:54
std::string fDefaultPage
! file name for default page name
Definition: THttpServer.h:48
Bool_t IsAnyEngine() const
Definition: THttpServer.h:72
Mother of all ROOT objects.
Definition: TObject.h:37
Long_t fMainThrdId
! id of the main ROOT process
Definition: THttpServer.h:40
const char * GetCors() const
Returns specified CORS domain.
Definition: THttpServer.h:97
TList fCallArgs
! submitted arguments
Definition: THttpServer.h:55
static Bool_t VerifyFilePath(const char *fname)
Checked that filename does not contains relative path below current directory Used to prevent access ...
std::string fDefaultPageCont
! content of default html page
Definition: THttpServer.h:49
TString fJSROOT
! location of external JSROOT files
Definition: THttpServer.h:44
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
const Bool_t kTRUE
Definition: RtypesCore.h:87
TRootSniffer * GetSniffer() const
returns pointer on objects sniffer
Definition: THttpServer.h:75
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
char name[80]
Definition: TGX11.cxx:109
void SetTopName(const char *top)
set name of top item in objects hierarchy
Definition: THttpServer.h:100
void SetJSROOT(const char *location)
Set location of JSROOT to use with the server One could specify address like: https://root.cern.ch/js/3.3/ http://web-docs.gsi.de/~linev/js/3.3/ This allows to get new JSROOT features with old server, reduce load on THttpServer instance, also startup time can be improved When empty string specified (default), local copy of JSROOT is used (distributed with ROOT)
const char * Data() const
Definition: TString.h:364