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