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// Warning: This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef ROOT7_RWebWindowWSHandler
14#define ROOT7_RWebWindowWSHandler
15
16#include "THttpWSHandler.h"
17#include "TEnv.h"
18#include "TUrl.h"
19
20#include <ROOT/RWebWindow.hxx>
21
22#include <string>
23
24using namespace std::string_literals;
25
26namespace ROOT {
27
28/// just wrapper to deliver websockets call-backs to the RWebWindow class
29
31
32protected:
33 Bool_t ProcessBatchHolder(std::shared_ptr<THttpCallArg> &arg) override
34 {
36 }
37
38 void VerifyDefaultPageContent(std::shared_ptr<THttpCallArg> &arg) override
39 {
40 auto token = fWindow.GetConnToken();
41 if (!token.empty()) {
42 TUrl url;
43 url.SetOptions(arg->GetQuery());
44 // refuse connection which does not provide proper token
45 if (!url.HasOption("token") || (token != url.GetValueFromOptions("token"))) {
46 // refuce loading of default web page without token
47 arg->SetContent("refused");
48 arg->Set404();
49 return;
50 }
51 }
52
54 TUrl url;
55 url.SetOptions(arg->GetQuery());
56 TString key = url.GetValueFromOptions("key");
57 if (key.IsNull() || !fWindow.HasKey(key.Data())) {
58 // refuce loading of default web page without valid key
59 arg->SetContent("refused");
60 arg->Set404();
61 return;
62 }
63 }
64
65 auto version = fWindow.GetClientVersion();
66 if (!version.empty()) {
67 // replace link to JSROOT modules in import statements emulating new version for browser
68 std::string search = "from './jsrootsys/"s;
69 std::string replace = "from './"s + version + "/jsrootsys/"s;
70 arg->ReplaceAllinContent(search, replace);
71 // replace link to ROOT ui5 modules in import statements emulating new version for browser
72 search = "from './rootui5sys/"s;
73 replace = "from './"s + version + "/rootui5sys/"s;
74 arg->ReplaceAllinContent(search, replace);
75 // replace link on old JSRoot.core.js script - if still appears
76 search = "jsrootsys/scripts/JSRoot.core."s;
77 replace = version + "/jsrootsys/scripts/JSRoot.core."s;
78 arg->ReplaceAllinContent(search, replace, true);
79 arg->AddNoCacheHeader();
80 }
81
82 std::string more_args;
83
84 std::string wskind = arg->GetWSKind();
85 if ((wskind == "websocket") && (GetBoolEnv("WebGui.WSLongpoll") == 1))
86 wskind = "longpoll";
87 if (!wskind.empty() && (wskind != "websocket"))
88 more_args.append("socket_kind: \""s + wskind + "\","s);
89 std::string wsplatform = arg->GetWSPlatform();
90 if (!wsplatform.empty() && (wsplatform != "http"))
91 more_args.append("platform: \""s + wsplatform + "\","s);
92 const char *ui5source = gEnv->GetValue("WebGui.openui5src","");
93 if (ui5source && *ui5source)
94 more_args.append("openui5src: \""s + ui5source + "\","s);
95 const char *ui5libs = gEnv->GetValue("WebGui.openui5libs","");
96 if (ui5libs && *ui5libs)
97 more_args.append("openui5libs: \""s + ui5libs + "\","s);
98 const char *ui5theme = gEnv->GetValue("WebGui.openui5theme","");
99 if (ui5theme && *ui5theme)
100 more_args.append("openui5theme: \""s + ui5theme + "\","s);
101 int credits = gEnv->GetValue("WebGui.ConnCredits", 10);
102 if ((credits > 0) && (credits != 10))
103 more_args.append("credits: "s + std::to_string(credits) + ","s);
104 if ((fWindow.GetWidth() > 0) && (fWindow.GetHeight() > 0))
105 more_args.append("winW:"s + std::to_string(fWindow.GetWidth()) + ",winH:"s + std::to_string(fWindow.GetHeight()) + ","s);
106 if ((fWindow.GetX() >= 0) && (fWindow.GetY() >= 0))
107 more_args.append("winX:"s + std::to_string(fWindow.GetX()) + ",winY:"s + std::to_string(fWindow.GetY()) + ","s);
108 auto user_args = fWindow.GetUserArgs();
109 if (!user_args.empty())
110 more_args.append("user_args: "s + user_args + ","s);
111 if (!more_args.empty()) {
112 std::string search = "connectWebWindow({"s;
113 std::string replace = search + more_args;
114 arg->ReplaceAllinContent(search, replace, true);
115 arg->AddNoCacheHeader();
116 }
117 }
118
119public:
120 RWebWindow &fWindow; ///<! window reference
121
122 /// constructor
124 : THttpWSHandler(name, "RWebWindow websockets handler", kFALSE), fWindow(wind)
125 {
126 }
127
128 ~RWebWindowWSHandler() override = default;
129
130 /// returns content of default web-page
131 /// THttpWSHandler interface
132 TString GetDefaultPageContent() override { return IsDisabled() ? "" : fWindow.fDefaultPage.c_str(); }
133
134 /// returns true when window allowed to serve files relative to default page
135 Bool_t CanServeFiles() const override { return !IsDisabled(); }
136
137 /// Process websocket request - called from THttpServer thread
138 /// THttpWSHandler interface
140 {
141 if (!arg || IsDisabled()) return kFALSE;
142 return fWindow.ProcessWS(*arg);
143 }
144
145 /// Allow processing of WS actions in arbitrary thread
146 Bool_t AllowMTProcess() const override { return fWindow.fProcessMT; }
147
148 /// Allows usage of special threads for send operations
149 Bool_t AllowMTSend() const override { return fWindow.fSendMT; }
150
151 /// React on completion of multi-threaded send operation
152 void CompleteWSSend(UInt_t wsid) override { if (!IsDisabled()) fWindow.CompleteWSSend(wsid); }
153
154 static int GetBoolEnv(const std::string &name, int dfl = -1);
155};
156
157} // namespace ROOT
158
159#endif
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
char name[80]
Definition TGX11.cxx:110
just wrapper to deliver websockets call-backs to the RWebWindow class
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.
bool HasKey(const std::string &key) const
Returns true if provided key value already exists (in processes map or in existing connections)
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
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
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:491
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:139
const char * Data() const
Definition TString.h:376
Bool_t IsNull() const
Definition TString.h:414
This class represents a WWW compatible URL.
Definition TUrl.h:33
const char * GetValueFromOptions(const char *key) const
Return a value for a given key from the URL options.
Definition TUrl.cxx:660
void SetOptions(const char *opt)
Definition TUrl.h:87
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
Definition TUrl.cxx:683
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...