Logo ROOT  
Reference Guide
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 {
27namespace Experimental {
28
29/// just wrapper to deliver websockets call-backs to the RWebWindow class
30
32
33protected:
34 Bool_t ProcessBatchHolder(std::shared_ptr<THttpCallArg> &arg) override
35 {
37 }
38
39 void VerifyDefaultPageContent(std::shared_ptr<THttpCallArg> &arg) override
40 {
41 auto token = fWindow.GetConnToken();
42 if (!token.empty()) {
43 TUrl url;
44 url.SetOptions(arg->GetQuery());
45 // refuse connection which does not provide proper token
46 if (!url.HasOption("token") || (token != url.GetValueFromOptions("token"))) {
47 // refuce loading of default web page without token
48 arg->SetContent("refused");
49 arg->Set404();
50 return;
51 }
52 }
53
54 auto version = fWindow.GetClientVersion();
55 if (!version.empty()) {
56 // replace link to JSROOT modules in import statements emulating new version for browser
57 std::string search = "from './jsrootsys/"s;
58 std::string replace = "from './"s + version + "/jsrootsys/"s;
59 arg->ReplaceAllinContent(search, replace);
60 // replace link to ROOT ui5 modules in import statements emulating new version for browser
61 search = "from './rootui5sys/"s;
62 replace = "from './"s + version + "/rootui5sys/"s;
63 arg->ReplaceAllinContent(search, replace);
64 // replace link on old JSRoot.core.js script - if still appears
65 search = "jsrootsys/scripts/JSRoot.core."s;
66 replace = version + "/jsrootsys/scripts/JSRoot.core."s;
67 arg->ReplaceAllinContent(search, replace, true);
68 arg->AddNoCacheHeader();
69 }
70
71 std::string more_args;
72
73 std::string wskind = arg->GetWSKind();
74 if ((wskind == "websocket") && (GetBoolEnv("WebGui.WSLongpoll") == 1))
75 wskind = "longpoll";
76 if (!wskind.empty() && (wskind != "websocket"))
77 more_args.append("socket_kind: \""s + wskind + "\","s);
78 std::string wsplatform = arg->GetWSPlatform();
79 if (!wsplatform.empty() && (wsplatform != "http"))
80 more_args.append("platform: \""s + wsplatform + "\","s);
81 const char *ui5source = gEnv->GetValue("WebGui.openui5src","");
82 if (ui5source && *ui5source)
83 more_args.append("openui5src: \""s + ui5source + "\","s);
84 const char *ui5libs = gEnv->GetValue("WebGui.openui5libs","");
85 if (ui5libs && *ui5libs)
86 more_args.append("openui5libs: \""s + ui5libs + "\","s);
87 const char *ui5theme = gEnv->GetValue("WebGui.openui5theme","");
88 if (ui5theme && *ui5theme)
89 more_args.append("openui5theme: \""s + ui5theme + "\","s);
90 int credits = gEnv->GetValue("WebGui.ConnCredits", 10);
91 if ((credits > 0) && (credits != 10))
92 more_args.append("credits: "s + std::to_string(credits) + ","s);
93 auto user_args = fWindow.GetUserArgs();
94 if (!user_args.empty())
95 more_args.append("user_args: "s + user_args + ","s);
96 if (!more_args.empty()) {
97 std::string search = "connectWebWindow({"s;
98 std::string replace = search + more_args;
99 arg->ReplaceAllinContent(search, replace, true);
100 arg->AddNoCacheHeader();
101 }
102 }
103
104public:
105 RWebWindow &fWindow; ///<! window reference
106
107 /// constructor
109 : THttpWSHandler(name, "RWebWindow websockets handler", kFALSE), fWindow(wind)
110 {
111 }
112
113 virtual ~RWebWindowWSHandler() = default;
114
115 /// returns content of default web-page
116 /// THttpWSHandler interface
117 TString GetDefaultPageContent() override { return IsDisabled() ? "" : fWindow.fDefaultPage.c_str(); }
118
119 /// returns true when window allowed to serve files relative to default page
120 Bool_t CanServeFiles() const override { return !IsDisabled(); }
121
122 /// Process websocket request - called from THttpServer thread
123 /// THttpWSHandler interface
125 {
126 if (!arg || IsDisabled()) return kFALSE;
127 auto res = fWindow.ProcessWS(*arg);
129 return res;
130 }
131
132 /// Allow processing of WS actions in arbitrary thread
133 Bool_t AllowMTProcess() const override { return fWindow.fProcessMT; }
134
135 /// Allows usage of special threads for send operations
136 Bool_t AllowMTSend() const override { return fWindow.fSendMT; }
137
138 /// React on completion of multi-threaded send operation
139 void CompleteWSSend(UInt_t wsid) override { if (!IsDisabled()) fWindow.CompleteWSSend(wsid); }
140
141 static int GetBoolEnv(const std::string &name, int dfl = -1);
142};
143
144} // namespace Experimental
145} // namespace ROOT
146
147#endif
const 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
TString GetDefaultPageContent() override
returns content of default web-page THttpWSHandler interface
RWebWindowWSHandler(RWebWindow &wind, const char *name)
constructor
Bool_t AllowMTSend() const override
Allows usage of special threads for send operations.
void CompleteWSSend(UInt_t wsid) override
React on completion of multi-threaded send operation.
Bool_t CanServeFiles() const override
returns true when window allowed to serve files relative to default page
static int GetBoolEnv(const std::string &name, int dfl=-1)
Parse boolean gEnv variable which should be "yes" or "no".
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.
RWebWindow & fWindow
! window reference
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.
Bool_t ProcessWS(THttpCallArg *arg) override
Process websocket request - called from THttpServer thread THttpWSHandler interface.
Represents web window, which can be shown in web browser or any other supported environment.
Definition: RWebWindow.hxx:53
std::string fDefaultPage
! HTML page (or file name) returned when window URL is opened
Definition: RWebWindow.hxx:129
std::string GetClientVersion() const
Returns current client version.
bool fSendMT
! true is special threads should be used for sending data
Definition: RWebWindow.hxx:134
bool ProcessBatchHolder(std::shared_ptr< THttpCallArg > &arg)
Process special http request, used to hold headless browser running Such requests should not be repli...
Definition: RWebWindow.cxx:315
std::string GetConnToken() const
Returns configured connection token.
Definition: RWebWindow.cxx:594
std::string GetUserArgs() const
Returns configured user arguments for web window See SetUserArgs method for more details.
void CheckThreadAssign()
Internal method to verify and thread id has to be assigned from manager again Special case when Proce...
Definition: RWebWindow.cxx:605
bool ProcessWS(THttpCallArg &arg)
Processing of websockets call-backs, invoked from RWebWindowWSHandler Method invoked from http server...
Definition: RWebWindow.cxx:615
bool fProcessMT
! if window event processing performed in dedicated thread
Definition: RWebWindow.hxx:133
void CompleteWSSend(unsigned wsid)
Complete websocket send operation Clear "doing send" flag and check if next operation has to be start...
Definition: RWebWindow.cxx:844
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.
Definition: THttpCallArg.h:27
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:136
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
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static constexpr double s