Logo ROOT   6.12/07
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 
25 class THttpEngine;
26 class THttpTimer;
27 class TRootSniffer;
28 
29 class THttpServer : public TNamed {
30 
31 protected:
32  TList fEngines; ///<! engines which runs http server
33  THttpTimer *fTimer; ///<! timer used to access main thread
34  TRootSniffer *fSniffer; ///<! sniffer provides access to ROOT objects hierarchy
35 
36  Long_t fMainThrdId; ///<! id of the main ROOT process
37 
38  TString fJSROOTSYS; ///<! location of local JSROOT files
39  TString fTopName; ///<! name of top folder, default - "ROOT"
40  TString fJSROOT; ///<! location of external JSROOT files
41  TList fLocations; ///<! list of local directories, which could be accessed via server
42 
43  TString fDefaultPage; ///<! file name for default page name
44  TString fDefaultPageCont; ///<! content of the file content
45  TString fDrawPage; ///<! file name for drawing of single element
46  TString fDrawPageCont; ///<! content of draw page
47  TString
48  fCors; ///<! CORS (cross-origin resource sharing): sets Access-Control-Allow-Origin for ProcessRequest responses
49 
50  std::mutex fMutex; ///<! mutex to protect list with arguments
51  TList fCallArgs; ///<! submitted arguments
52 
53  /** Function called for every processed request */
54  virtual void ProcessRequest(THttpCallArg *arg);
55 
56  static Bool_t VerifyFilePath(const char *fname);
57 
58 public:
59  THttpServer(const char *engine = "civetweb:8080");
60  virtual ~THttpServer();
61 
62  Bool_t CreateEngine(const char *engine);
63 
64  Bool_t IsAnyEngine() const { return fEngines.GetSize() > 0; }
65 
66  /** returns pointer on objects sniffer */
67  TRootSniffer *GetSniffer() const { return fSniffer; }
68 
69  void SetSniffer(TRootSniffer *sniff);
70 
71  Bool_t IsReadOnly() const;
72 
73  void SetReadOnly(Bool_t readonly);
74 
75  /** Enable CORS header to ProcessRequests() responses
76  * Specified location (typically "*") add as "Access-Control-Allow-Origin" header */
77  void SetCors(const char *domain = "*") { fCors = domain; }
78 
79  /** Returns kTRUE if CORS was configured */
80  Bool_t IsCors() const { return fCors.Length() > 0; }
81 
82  /** Returns specified CORS domain */
83  const char *GetCors() const { return fCors.Data(); }
84 
85  /** set name of top item in objects hierarchy */
86  void SetTopName(const char *top) { fTopName = top; }
87 
88  /** returns name of top item in objects hierarchy */
89  const char *GetTopName() const { return fTopName.Data(); }
90 
91  void SetJSROOT(const char *location);
92 
93  void AddLocation(const char *prefix, const char *path);
94 
95  void SetDefaultPage(const char *filename);
96 
97  void SetDrawPage(const char *filename);
98 
99  void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);
100 
101  /** Check if file is requested, thread safe */
102  Bool_t IsFileRequested(const char *uri, TString &res) const;
103 
104  /** Execute HTTP request */
106 
107  /** Submit HTTP request */
108  Bool_t SubmitHttp(THttpCallArg *arg, Bool_t can_run_immediately = kFALSE);
109 
110  /** Process submitted requests, must be called from main thread */
111  void ProcessRequests();
112 
113  /** Register object in subfolder */
114  Bool_t Register(const char *subfolder, TObject *obj);
115 
116  /** Unregister object */
117  Bool_t Unregister(TObject *obj);
118 
119  /** Restrict access to specified object */
120  void Restrict(const char *path, const char *options);
121 
122  Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon = 0);
123 
124  Bool_t Hide(const char *fullname, Bool_t hide = kTRUE);
125 
126  Bool_t SetIcon(const char *fullname, const char *iconname);
127 
128  Bool_t CreateItem(const char *fullname, const char *title);
129 
130  Bool_t SetItemField(const char *fullname, const char *name, const char *value);
131 
132  const char *GetItemField(const char *fullname, const char *name);
133 
134  /** Guess mime type base on file extension */
135  static const char *GetMimeType(const char *path);
136 
137  /** Reads content of file from the disk */
138  static char *ReadFileContent(const char *filename, Int_t &len);
139 
140  ClassDef(THttpServer, 0) // HTTP server for ROOT analysis
141 };
142 
143 #endif
void SetCors(const char *domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
Definition: THttpServer.h:77
const char * GetTopName() const
returns name of top item in objects hierarchy
Definition: THttpServer.h:89
const char * GetItemField(const char *fullname, const char *name)
Bool_t IsReadOnly() const
returns read-only mode
Bool_t IsCors() const
Returns kTRUE if CORS was configured.
Definition: THttpServer.h:80
Basic string class.
Definition: TString.h:125
TString fTopName
! name of top folder, default - "ROOT"
Definition: THttpServer.h:39
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TList fEngines
! engines which runs http server
Definition: THttpServer.h:32
void ProcessRequests()
Process submitted requests, must be called from main thread.
Bool_t CreateItem(const char *fullname, const char *title)
TRootSniffer * fSniffer
! sniffer provides access to ROOT objects hierarchy
Definition: THttpServer.h:34
THttpServer(const char *engine="civetweb:8080")
constructor
#define ClassDef(name, id)
Definition: Rtypes.h:320
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:33
void SetTimer(Long_t milliSec=100, Bool_t mode=kTRUE)
create timer which will invoke ProcessRequests() function periodically Timer is required to perform a...
TList fLocations
! list of local directories, which could be accessed via server
Definition: THttpServer.h:41
TString fJSROOTSYS
! location of local JSROOT files
Definition: THttpServer.h:38
Bool_t SubmitHttp(THttpCallArg *arg, Bool_t can_run_immediately=kFALSE)
Submit HTTP request.
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
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...
virtual ~THttpServer()
destructor delete all http engines and sniffer
Ssiz_t Length() const
Definition: TString.h:386
TString fDefaultPage
! file name for default page name
Definition: THttpServer.h:43
void SetSniffer(TRootSniffer *sniff)
Set TRootSniffer to the server Server takes ownership over sniffer.
Bool_t ExecuteHttp(THttpCallArg *arg)
Execute HTTP request.
const Bool_t kFALSE
Definition: RtypesCore.h:88
TString fDrawPage
! file name for drawing of single element
Definition: THttpServer.h:45
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 ...
Bool_t Hide(const char *fullname, Bool_t hide=kTRUE)
hides folder or element from web gui
std::mutex fMutex
! mutex to protect list with arguments
Definition: THttpServer.h:50
virtual void ProcessRequest(THttpCallArg *arg)
Function called for every processed request.
TString fDrawPageCont
! content of draw page
Definition: THttpServer.h:46
TString fDefaultPageCont
! content of the file content
Definition: THttpServer.h:44
TString fCors
! CORS (cross-origin resource sharing): sets Access-Control-Allow-Origin for ProcessRequest responses...
Definition: THttpServer.h:48
Bool_t IsAnyEngine() const
Definition: THttpServer.h:64
Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon=0)
Register command which can be executed from web interface.
Mother of all ROOT objects.
Definition: TObject.h:37
static const char * GetMimeType(const char *path)
Guess mime type base on file extension.
Bool_t Unregister(TObject *obj)
Unregister object.
Long_t fMainThrdId
! id of the main ROOT process
Definition: THttpServer.h:36
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
const char * GetCors() const
Returns specified CORS domain.
Definition: THttpServer.h:83
Bool_t SetIcon(const char *fullname, const char *iconname)
set name of icon, used in browser together with the item
TList fCallArgs
! submitted arguments
Definition: THttpServer.h:51
static Bool_t VerifyFilePath(const char *fname)
Checked that filename does not contains relative path below current directory Used to prevent access ...
TString fJSROOT
! location of external JSROOT files
Definition: THttpServer.h:40
virtual Int_t GetSize() const
Definition: TCollection.h:180
const Bool_t kTRUE
Definition: RtypesCore.h:87
void SetDrawPage(const char *filename)
Set file name of HTML page, delivered by the server when objects drawing page is requested from the b...
TRootSniffer * GetSniffer() const
returns pointer on objects sniffer
Definition: THttpServer.h:67
Bool_t SetItemField(const char *fullname, const char *name, const char *value)
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
char name[80]
Definition: TGX11.cxx:109
void Restrict(const char *path, const char *options)
Restrict access to specified object.
void SetDefaultPage(const char *filename)
Set file name of HTML page, delivered by the server when http address is opened in the browser...
void SetTopName(const char *top)
set name of top item in objects hierarchy
Definition: THttpServer.h:86
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:345