Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
27class THttpEngine;
28class THttpTimer;
29class TRootSniffer;
30
31class THttpServer : public TNamed {
32
33protected:
34 TList fEngines; ///<! engines which runs http server
35 std::unique_ptr<THttpTimer> fTimer; ///<! timer used to access main thread
36 std::unique_ptr<TRootSniffer> fSniffer; ///<! 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 fWSOnly{kFALSE}; ///<! when true, handle only websockets / longpoll engine
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 void StopServerThread();
68
69 std::string BuildWSEntryPage();
70
71 void ReplaceJSROOTLinks(std::shared_ptr<THttpCallArg> &arg);
72
73 static Bool_t VerifyFilePath(const char *fname);
74
75 THttpServer(const THttpServer &) = delete;
76 THttpServer &operator=(const THttpServer &) = delete;
77
78public:
79 THttpServer(const char *engine = "http:8080");
80 virtual ~THttpServer();
81
82 Bool_t CreateEngine(const char *engine);
83
84 Bool_t IsAnyEngine() const { return fEngines.GetSize() > 0; }
85
86 /** returns pointer on objects sniffer */
87 TRootSniffer *GetSniffer() const { return fSniffer.get(); }
88
89 void SetSniffer(TRootSniffer *sniff);
90
91 Bool_t IsReadOnly() const;
92
93 void SetReadOnly(Bool_t readonly = kTRUE);
94
95 Bool_t IsWSOnly() const;
96
97 void SetWSOnly(Bool_t on = kTRUE);
98
99 /** set termination flag, no any further requests will be processed */
100 void SetTerminate();
101
102 /** returns kTRUE, if server was terminated */
103 Bool_t IsTerminated() const { return fTerminated; }
104
105 /** Enable CORS header to ProcessRequests() responses
106 * Specified location (typically "*") add as "Access-Control-Allow-Origin" header */
107 void SetCors(const std::string &domain = "*") { fCors = domain; }
108
109 /** Returns kTRUE if CORS was configured */
110 Bool_t IsCors() const { return !fCors.empty(); }
111
112 /** Returns specified CORS domain */
113 const char *GetCors() const { return fCors.c_str(); }
114
115 /** set name of top item in objects hierarchy */
116 void SetTopName(const char *top) { fTopName = top; }
117
118 /** returns name of top item in objects hierarchy */
119 const char *GetTopName() const { return fTopName.Data(); }
120
121 void SetJSROOT(const char *location);
122
123 void AddLocation(const char *prefix, const char *path);
124
125 void SetDefaultPage(const std::string &filename = "");
126
127 void SetDrawPage(const std::string &filename = "");
128
129 void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);
130
131 void CreateServerThread();
132
133 /** Check if file is requested, thread safe */
134 Bool_t IsFileRequested(const char *uri, TString &res) const;
135
136 /** Execute HTTP request */
137 Bool_t ExecuteHttp(std::shared_ptr<THttpCallArg> arg);
138
139 /** Submit HTTP request */
140 Bool_t SubmitHttp(std::shared_ptr<THttpCallArg> arg, Bool_t can_run_immediately = kFALSE);
141
142 /** Process submitted requests, must be called from appropriate thread */
144
145 /** Register object in subfolder */
146 Bool_t Register(const char *subfolder, TObject *obj);
147
148 /** Unregister object */
150
151 /** Register WS handler*/
152 void RegisterWS(std::shared_ptr<THttpWSHandler> ws);
153
154 /** Unregister WS handler*/
155 void UnregisterWS(std::shared_ptr<THttpWSHandler> ws);
156
157 /** Find web-socket handler with given name */
158 std::shared_ptr<THttpWSHandler> FindWS(const char *name);
159
160 /** Execute WS request */
161 Bool_t ExecuteWS(std::shared_ptr<THttpCallArg> &arg, Bool_t external_thrd = kFALSE, Bool_t wait_process = kFALSE);
162
163 /** Restrict access to specified object */
164 void Restrict(const char *path, const char *options);
165
166 Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon = nullptr);
167
168 Bool_t Hide(const char *fullname, Bool_t hide = kTRUE);
169
170 Bool_t SetIcon(const char *fullname, const char *iconname);
171
172 Bool_t CreateItem(const char *fullname, const char *title);
173
174 Bool_t SetItemField(const char *fullname, const char *name, const char *value);
175
176 const char *GetItemField(const char *fullname, const char *name);
177
178 /** Guess mime type base on file extension */
179 static const char *GetMimeType(const char *path);
180
181 /** Reads content of file from the disk */
182 static char *ReadFileContent(const char *filename, Int_t &len);
183
184 /** Reads content of file from the disk, use std::string in return value */
185 static std::string ReadFileContent(const std::string &filename);
186
187 ClassDefOverride(THttpServer, 0) // HTTP server for ROOT analysis
188};
189
190#endif
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
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 on
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
Option_t Option_t TPoint TPoint const char mode
char name[80]
Definition TGX11.cxx:110
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Contains arguments for single HTTP call.
Abstract class for implementing http protocol for THttpServer.
Definition THttpEngine.h:19
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31
Bool_t IsReadOnly() const
returns read-only mode
Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon=nullptr)
Register command which can be executed from web interface.
TString fJSROOT
! location of external JSROOT files
Definition THttpServer.h:45
virtual void ProcessRequest(std::shared_ptr< THttpCallArg > arg)
Process single http request.
const char * GetTopName() const
returns name of top item in objects hierarchy
std::shared_ptr< THttpWSHandler > FindWS(const char *name)
Find web-socket handler with given name.
std::unique_ptr< TRootSniffer > fSniffer
! sniffer provides access to ROOT objects hierarchy
Definition THttpServer.h:36
void SetTimer(Long_t milliSec=100, Bool_t mode=kTRUE)
Create timer which will invoke ProcessRequests() function periodically.
virtual void ProcessBatchHolder(std::shared_ptr< THttpCallArg > &arg)
Process special http request for root_batch_holder.js script.
std::vector< std::shared_ptr< THttpWSHandler > > fWSHandlers
! list of WS handlers
Definition THttpServer.h:59
virtual ~THttpServer()
destructor
void SetTerminate()
set termination flag, no any further requests will be processed
virtual void MissedRequest(THttpCallArg *arg)
Method called when THttpServer cannot process request.
Bool_t fOwnThread
! true when specialized thread allocated for processing requests
Definition THttpServer.h:39
void SetSniffer(TRootSniffer *sniff)
Set TRootSniffer to the server.
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
void SetReadOnly(Bool_t readonly=kTRUE)
Set read-only mode for the server (default on)
const char * GetItemField(const char *fullname, const char *name)
Get item field from sniffer.
const char * GetCors() const
Returns specified CORS domain.
std::thread fThrd
! own thread
Definition THttpServer.h:40
void StopServerThread()
Stop server thread.
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:44
void SetDrawPage(const std::string &filename="")
Set drawing HTML page.
Bool_t CreateItem(const char *fullname, const char *title)
Create item in sniffer.
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.
void AddLocation(const char *prefix, const char *path)
Add files location, which could be used in the server.
Bool_t IsAnyEngine() const
Definition THttpServer.h:84
std::map< std::string, std::string > fLocations
! list of local directories, which could be accessed via server
Definition THttpServer.h:47
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:38
std::string fCors
! CORS: sets Access-Control-Allow-Origin header for ProcessRequest responses
Definition THttpServer.h:53
Bool_t IsTerminated() const
returns kTRUE, if server was terminated
TString fJSROOTSYS
! location of local JSROOT files
Definition THttpServer.h:43
std::unique_ptr< THttpTimer > fTimer
! timer used to access main thread
Definition THttpServer.h:35
Bool_t fWSOnly
! when true, handle only websockets / longpoll engine
Definition THttpServer.h:41
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
TList fEngines
! engines which runs http server
Definition THttpServer.h:34
void SetCors(const std::string &domain="*")
Enable CORS header to ProcessRequests() responses Specified location (typically "*") add as "Access-C...
Bool_t IsCors() const
Returns kTRUE if CORS was configured.
std::queue< std::shared_ptr< THttpCallArg > > fArgs
! submitted arguments
Definition THttpServer.h:56
void SetDefaultPage(const std::string &filename="")
Set default HTML page.
THttpServer(const THttpServer &)=delete
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
void CreateServerThread()
Creates special thread to process all requests, directed to http server.
std::string fDrawPageCont
! content of draw html page
Definition THttpServer.h:52
Bool_t Unregister(TObject *obj)
Unregister object.
void SetWSOnly(Bool_t on=kTRUE)
Set websocket-only mode.
void ReplaceJSROOTLinks(std::shared_ptr< THttpCallArg > &arg)
Replaces all references like "jsrootsys/...".
std::string BuildWSEntryPage()
Create summary page with active WS handlers.
Bool_t IsWSOnly() const
returns true if only websockets are handled by the server
std::mutex fWSMutex
! mutex to protect WS handler lists
Definition THttpServer.h:58
Bool_t CreateEngine(const char *engine)
Factory method to create different http engines.
Bool_t SetIcon(const char *fullname, const char *iconname)
Set name of icon, used in browser together with the item.
THttpServer & operator=(const THttpServer &)=delete
std::string fDrawPage
! file name for drawing of single element
Definition THttpServer.h:51
std::string fDefaultPageCont
! content of default html page
Definition THttpServer.h:50
static Bool_t VerifyFilePath(const char *fname)
Checked that filename does not contains relative path below current directory.
Bool_t SetItemField(const char *fullname, const char *name, const char *value)
Set item field in sniffer.
void SetJSROOT(const char *location)
Set location of JSROOT to use with the server.
std::mutex fMutex
! mutex to protect list with arguments
Definition THttpServer.h:55
std::string fDefaultPage
! file name for default page name
Definition THttpServer.h:49
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:87
Bool_t fTerminated
! termination flag, disables all requests processing
Definition THttpServer.h:37
void SetTopName(const char *top)
set name of top item in objects hierarchy
void Restrict(const char *path, const char *options)
Restrict access to specified object.
A doubly linked list.
Definition TList.h:38
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
Mother of all ROOT objects.
Definition TObject.h:41
Sniffer of ROOT objects, data provider for THttpServer.
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380