Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
httpaccess.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_http
3/// This program demonstrates access control to the THttpServer with digest methods.
4/// Authentication file auth.txt was generated with following shell commands:
5/// ~~~
6/// [shell] htdigest -c auth.txt root guest
7/// typing <empty> password for guest account
8/// [shell] htdigest auth.txt root admin
9/// typing 'admin' as password for admin account
10/// ~~~
11/// When macro started and opening in browser with url
12/// ~~~
13/// http://localhost:8080
14/// ~~~
15/// User name and password will be requested. One should
16/// either specify guest account without password or
17/// admin account with password 'admin'
18///
19/// User with guest account only can monitor histograms
20/// User with admin account see commands, which can be executed
21///
22/// \macro_code
23///
24/// \author Sergey Linev
25
26#include "TH1.h"
27#include "TH2.h"
28#include "TRandom3.h"
29#include "TSystem.h"
30#include "THttpServer.h"
31
32void httpaccess()
33{
34 // create histograms
35 TH1D *hpx = new TH1D("hpx","This is the px distribution",100,-4,4);
36 hpx->SetFillColor(48);
37 hpx->SetDirectory(nullptr);
38 TH2D *hpxpy = new TH2D("hpxpy","py vs px",40,-4,4,40,-4,4);
39 hpxpy->SetDirectory(nullptr);
40
41 if (gSystem->AccessPathName("auth.txt") != 0) {
42 printf("Please start macro from directory where auth.txt file is available\n");
43 printf("It required to supply authentication information for the http server\n");
44 return;
45 }
46
47 // start http server
48 THttpServer* serv = new THttpServer("http:8080?auth_file=auth.txt&auth_domain=root");
49
50 // start http server and allows CORS access to local files
51 // first copy hsimple.root file to current directory
52 // THttpServer* serv = new THttpServer("http:8080?auth_file=auth.txt&auth_domain=root&cred_cors&cors=https://root.cern");
53 // And finally file can be opened via url: https://root.cern/js/dev/?with_credentials&file=http://localhost:8080/currentdir/hsimple.root
54
55 // or start FastCGI server, where host server (like Apache or lighttpd) should enable own authentication
56 // for apache one should add correspondent module and authentication for fastcgi location
57 // for lighttpd one add following lines to configuration file:
58 // server.modules += ( "mod_auth" )
59 // auth.backend = "htdigest"
60 // auth.backend.htdigest.userfile = "/srv/auth/auth.txt"
61 // auth.require = ( "/root.app" => ( "method" => "digest", "realm" => "root", "require" => "valid-user" ))
62 // THttpServer* serv = new THttpServer("fastcgi:9000");
63
64 // One could specify location of newer version of JSROOT
65 // serv->SetJSROOT("https://root.cern/js/latest/");
66 // serv->SetJSROOT("https://jsroot.gsi.de/dev/");
67
68 // register histograms
69 serv->Register("/", hpx);
70 serv->Register("/", hpxpy);
71
72 // register commands, invoking object methods
73 serv->RegisterCommand("/ResetHPX","/hpx/->Reset();", "button;rootsys/icons/ed_delete.png");
74 serv->SetItemField("/ResetHPX","_update_item", "hpx"); // let browser update histogram view after commands execution
75 serv->RegisterCommand("/ResetHPXPY","/hpxpy/->Reset();", "button;rootsys/icons/bld_delete.png");
76 serv->SetItemField("/ResetHPXPY","_update_item", "hpxpy"); // let browser update histogram view after commands execution
77 // here also example how command with arguments can be invoked
78 serv->RegisterCommand("/RebinHPX","/hpx/->Rebin(%arg1%);", "button;rootsys/icons/ed_execute.png");
79 serv->SetItemField("/RebinHPX","_update_item", "hpx"); // let browser update histogram view after commands execution
80
81 // these two commands fully hidden for other accounts,
82 // only admin can see and execute these commands
83 serv->Restrict("/ResetHPX", "visible=admin");
84 serv->Restrict("/ResetHPXPY", "visible=admin");
85
86 // this command visible for other, but will be refused (return false)
87 // when executed from any other account
88 serv->Restrict("/RebinHPX", "allow=admin");
89
90 // Fill histograms randomly
91 TRandom3 random;
92 Float_t px, py;
93 const Long_t kUPDATE = 1000;
94 Long_t cnt = 0;
95 while (kTRUE) {
96 random.Rannor(px,py);
97 hpx->Fill(px);
98 hpxpy->Fill(px,py);
99
100 // IMPORTANT: one should regularly call ProcessEvents
101 if (cnt++ % kUPDATE == 0) {
102 if (gSystem->ProcessEvents()) break;
103 }
104 }
105}
long Long_t
Definition RtypesCore.h:54
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:664
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8901
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3340
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:338
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:350
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31
Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon=nullptr)
Register command which can be executed from web interface.
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
Bool_t SetItemField(const char *fullname, const char *name, const char *value)
Set item field in sniffer.
void Restrict(const char *path, const char *options)
Restrict access to specified object.
Random number generator class based on M.
Definition TRandom3.h:27
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition TRandom.cxx:507
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1296
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:416
const char * cnt
Definition TXMLSetup.cxx:75