Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RWebWindowWSHandler.hxx
Go to the documentation of this file.
1// Author: Sergey Linev <s.linev@gsi.de>
2// Date: 2018-08-20
3
4/*************************************************************************
5 * Copyright (C) 1995-2019, 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_RWebWindowWSHandler
13#define ROOT_RWebWindowWSHandler
14
15#include "THttpWSHandler.h"
16#include "TEnv.h"
17#include "TUrl.h"
18
19#include <ROOT/RWebWindow.hxx>
20
21#include <string>
22
23using namespace std::string_literals;
24
25namespace ROOT {
26
27/// just wrapper to deliver websockets call-backs to the RWebWindow class
28
30
31protected:
32 Bool_t ProcessBatchHolder(std::shared_ptr<THttpCallArg> &arg) override
33 {
35 }
36
37 std::string GetCodeVersion() override { return fWindow.GetClientVersion(); }
38
39
40 void VerifyDefaultPageContent(std::shared_ptr<THttpCallArg> &arg) override
41 {
42 auto token = fWindow.GetConnToken();
43 if (!token.empty()) {
44 TUrl url;
45 url.SetOptions(arg->GetQuery());
46 // refuse connection which does not provide proper token
47 if (!url.HasOption("token") || (token != url.GetValueFromOptions("token"))) {
48 // refuse loading of default web page without token
49 arg->SetContent("refused");
50 arg->Set404();
51 return;
52 }
53 }
54
56 TUrl url;
57 url.SetOptions(arg->GetQuery());
58 TString key = url.GetValueFromOptions("key");
59 if (key.IsNull() || !fWindow.HasKey(key.Data(), true)) {
60 // refuse loading of default web page without valid key
61 arg->SetContent("refused");
62 arg->Set404();
63 return;
64 }
65 }
66
68 if (!version.empty()) {
69 // replace link to JSROOT modules in import statements emulating new version for browser
70 std::string search = "from './jsrootsys/"s;
71 std::string replace = "from './"s + version + "/jsrootsys/"s;
72 arg->ReplaceAllinContent(search, replace);
73 // replace link to ROOT ui5 modules in import statements emulating new version for browser
74 search = "from './rootui5sys/"s;
75 replace = "from './"s + version + "/rootui5sys/"s;
76 arg->ReplaceAllinContent(search, replace);
77 // replace link on old JSRoot.core.js script - if still appears
78 search = "jsrootsys/scripts/JSRoot.core."s;
79 replace = version + "/jsrootsys/scripts/JSRoot.core."s;
80 arg->ReplaceAllinContent(search, replace, true);
81 arg->AddNoCacheHeader();
82 }
83
84 std::string more_args;
85
86 std::string wskind = arg->GetWSKind();
87 if ((wskind == "websocket") && (GetBoolEnv("WebGui.WSLongpoll") == 1))
88 wskind = "longpoll";
89 if (!wskind.empty() && (wskind != "websocket"))
90 more_args.append("socket_kind: \""s + wskind + "\","s);
91 std::string wsplatform = arg->GetWSPlatform();
92 if (!wsplatform.empty() && (wsplatform != "http"))
93 more_args.append("platform: \""s + wsplatform + "\","s);
94 const char *ui5source = gEnv->GetValue("WebGui.openui5src","");
95 if (ui5source && *ui5source)
96 more_args.append("openui5src: \""s + ui5source + "\","s);
97 const char *ui5libs = gEnv->GetValue("WebGui.openui5libs","");
98 if (ui5libs && *ui5libs)
99 more_args.append("openui5libs: \""s + ui5libs + "\","s);
100 const char *ui5theme = gEnv->GetValue("WebGui.openui5theme","");
101 if (ui5theme && *ui5theme)
102 more_args.append("openui5theme: \""s + ui5theme + "\","s);
103 if (GetBoolEnv("WebGui.DarkMode") == 1)
104 more_args.append("dark: true,"s);
105 int credits = gEnv->GetValue("WebGui.ConnCredits", 10);
106 if ((credits > 0) && (credits != 10))
107 more_args.append("credits: "s + std::to_string(credits) + ","s);
108 if ((fWindow.GetWidth() > 0) && (fWindow.GetHeight() > 0))
109 more_args.append("winW:"s + std::to_string(fWindow.GetWidth()) + ",winH:"s + std::to_string(fWindow.GetHeight()) + ","s);
110 if ((fWindow.GetX() >= 0) && (fWindow.GetY() >= 0))
111 more_args.append("winX:"s + std::to_string(fWindow.GetX()) + ",winY:"s + std::to_string(fWindow.GetY()) + ","s);
112 if (GetBoolEnv("WebGui.Debug") == 1)
113 more_args.append("debug: true,");
115 if (!user_args.empty())
116 more_args.append("user_args: "s + user_args + ","s);
117 if (!RWebWindow::gJSROOTsettings.empty())
118 more_args.append("settings: "s + RWebWindow::gJSROOTsettings + ","s);
119 if (!more_args.empty()) {
120 std::string search = "connectWebWindow({"s;
121 std::string replace = search + more_args;
122 arg->ReplaceAllinContent(search, replace, true);
123 arg->AddNoCacheHeader();
124 }
125 }
126
127public:
128 RWebWindow &fWindow; ///<! window reference
129
130 /// constructor
132 : THttpWSHandler(name, "RWebWindow websockets handler", kFALSE), fWindow(wind)
133 {
134 }
135
136 ~RWebWindowWSHandler() override = default;
137
138 /// returns content of default web-page
139 /// THttpWSHandler interface
140 TString GetDefaultPageContent() override { return IsDisabled() ? "" : fWindow.fDefaultPage.c_str(); }
141
142 /// returns true when window allowed to serve files relative to default page
143 Bool_t CanServeFiles() const override { return !IsDisabled(); }
144
145 /// Process websocket request - called from THttpServer thread
146 /// THttpWSHandler interface
148 {
149 if (!arg || IsDisabled()) return kFALSE;
150 return fWindow.ProcessWS(*arg);
151 }
152
153 /// Allow processing of WS actions in arbitrary thread
154 Bool_t AllowMTProcess() const override { return fWindow.fProcessMT; }
155
156 /// Allows usage of special threads for send operations
157 Bool_t AllowMTSend() const override { return fWindow.fSendMT; }
158
159 /// React on completion of multi-threaded send operation
161
162 static int GetBoolEnv(const std::string &name, int dfl = -1);
163};
164
165} // namespace ROOT
166
167#endif
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TEnv * gEnv
Definition TEnv.h:126
char name[80]
Definition TGX11.cxx:148
just wrapper to deliver websockets call-backs to the RWebWindow class
std::string GetCodeVersion() override
Method generate extra suffix for all kinds of loaded code.
RWebWindow & fWindow
! window reference
~RWebWindowWSHandler() override=default
Bool_t AllowMTSend() const override
Allows usage of special threads for send operations.
Bool_t CanServeFiles() const override
returns true when window allowed to serve files relative to default page
Bool_t ProcessWS(THttpCallArg *arg) override
Process websocket request - called from THttpServer thread THttpWSHandler interface.
Bool_t ProcessBatchHolder(std::shared_ptr< THttpCallArg > &arg) override
Method used to accept or reject root_batch_holder.js request.
Bool_t AllowMTProcess() const override
Allow processing of WS actions in arbitrary thread.
void VerifyDefaultPageContent(std::shared_ptr< THttpCallArg > &arg) override
Method called when default page content is prepared for use By default no-cache header is provided.
void CompleteWSSend(UInt_t wsid) override
React on completion of multi-threaded send operation.
static int GetBoolEnv(const std::string &name, int dfl=-1)
Parse boolean gEnv variable which should be "yes" or "no".
TString GetDefaultPageContent() override
returns content of default web-page THttpWSHandler interface
RWebWindowWSHandler(RWebWindow &wind, const char *name)
constructor
Represents web window, which can be shown in web browser or any other supported environment.
std::string fDefaultPage
! HTML page (or file name) returned when window URL is opened
bool fSendMT
! true is special threads should be used for sending data
std::string GetClientVersion() const
Returns current client version.
int GetX() const
returns configured window X position (-1 - default)
bool ProcessBatchHolder(std::shared_ptr< THttpCallArg > &arg)
Process special http request, used to hold headless browser running Such requests should not be repli...
unsigned GetHeight() const
returns configured window height (0 - default)
std::string GetConnToken() const
Returns configured connection token.
bool IsRequireAuthKey() const
returns true if authentication string is required
std::string GetUserArgs() const
Returns configured user arguments for web window See SetUserArgs method for more details.
int GetY() const
returns configured window Y position (-1 - default)
bool ProcessWS(THttpCallArg &arg)
Processing of websockets call-backs, invoked from RWebWindowWSHandler Method invoked from http server...
unsigned GetWidth() const
returns configured window width (0 - default) actual window width can be different
bool HasKey(const std::string &key, bool also_newkey=false) const
Returns true if provided key value already exists (in processes map or in existing connections) In sp...
void CompleteWSSend(unsigned wsid)
Complete websocket send operation Clear "doing send" flag and check if next operation has to be start...
bool fProcessMT
! if window event processing performed in dedicated thread
static std::string gJSROOTsettings
! custom settings for JSROOT
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:511
Contains arguments for single HTTP call.
Class for user-side handling of websocket with THttpServer.
Bool_t IsDisabled() const
Returns true when processing of websockets is disabled, set shortly before handler need to be destroy...
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:386
Bool_t IsNull() const
Definition TString.h:424
This class represents a WWW compatible URL.
Definition TUrl.h:33