File indexing completed on 2026-09-12 09:27:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_THttpServer
0013 #define ROOT_THttpServer
0014
0015 #include "TNamed.h"
0016 #include "TList.h"
0017 #include "THttpCallArg.h"
0018
0019 #include <mutex>
0020 #include <map>
0021 #include <string>
0022 #include <memory>
0023 #include <queue>
0024 #include <thread>
0025 #include <vector>
0026
0027 class THttpEngine;
0028 class THttpTimer;
0029 class TRootSniffer;
0030
0031 class THttpServer : public TNamed {
0032
0033 protected:
0034 TList fEngines;
0035 std::unique_ptr<THttpTimer> fTimer;
0036 std::unique_ptr<TRootSniffer> fSniffer;
0037 Bool_t fTerminated{kFALSE};
0038 Long_t fMainThrdId{0};
0039 Long_t fProcessingThrdId{0};
0040 Bool_t fOwnThread{kFALSE};
0041 std::thread fThrd;
0042 Bool_t fWSOnly{kFALSE};
0043
0044 TString fJSROOTSYS;
0045 TString fTopName{"ROOT"};
0046 TString fJSROOT;
0047
0048 std::map<std::string, std::string> fLocations;
0049
0050 std::string fDefaultPage;
0051 std::string fDefaultPageCont;
0052 std::string fDrawPage;
0053 std::string fDrawPageCont;
0054 std::string fCors;
0055 std::string fCorsCredentials;
0056
0057 std::mutex fMutex;
0058 std::queue<std::shared_ptr<THttpCallArg>> fArgs;
0059
0060 std::mutex fWSMutex;
0061 std::vector<std::shared_ptr<THttpWSHandler>> fWSHandlers;
0062
0063 virtual void MissedRequest(THttpCallArg *arg);
0064
0065 virtual void ProcessRequest(std::shared_ptr<THttpCallArg> arg);
0066
0067 virtual void ProcessBatchHolder(std::shared_ptr<THttpCallArg> &arg);
0068
0069 void StopServerThread();
0070
0071 std::string BuildWSEntryPage();
0072
0073 void ReplaceJSROOTLinks(std::shared_ptr<THttpCallArg> &arg, const std::string &version = "");
0074
0075 static Bool_t VerifyFilePath(const char *fname);
0076
0077 THttpServer(const THttpServer &) = delete;
0078 THttpServer &operator=(const THttpServer &) = delete;
0079
0080 public:
0081 THttpServer(const char *engine = "http:8080");
0082 virtual ~THttpServer();
0083
0084 virtual Bool_t CreateEngine(const char *engine);
0085
0086 Bool_t IsAnyEngine() const { return fEngines.GetSize() > 0; }
0087
0088
0089 TRootSniffer *GetSniffer() const { return fSniffer.get(); }
0090
0091 void SetSniffer(TRootSniffer *sniff);
0092
0093 Bool_t IsReadOnly() const;
0094
0095 void SetReadOnly(Bool_t readonly = kTRUE);
0096
0097 Bool_t IsAllowPostObject() const;
0098
0099 void SetAllowPostObject(Bool_t allow_post_obj);
0100
0101 Bool_t IsWSOnly() const;
0102
0103 void SetWSOnly(Bool_t on = kTRUE);
0104
0105
0106 void SetTerminate();
0107
0108
0109 Bool_t IsTerminated() const { return fTerminated; }
0110
0111
0112
0113 void SetCors(const std::string &domain = "*") { fCors = domain; }
0114
0115
0116 Bool_t IsCors() const { return !fCors.empty(); }
0117
0118
0119 const char *GetCors() const { return fCors.c_str(); }
0120
0121
0122 void SetCorsCredentials(const std::string &value = "true") { fCorsCredentials = value; }
0123
0124
0125 Bool_t IsCorsCredentials() const { return !fCorsCredentials.empty(); }
0126
0127
0128 const char *GetCorsCredentials() const { return fCorsCredentials.c_str(); }
0129
0130
0131 void SetTopName(const char *top) { fTopName = top; }
0132
0133
0134 const char *GetTopName() const { return fTopName.Data(); }
0135
0136 void SetJSROOT(const char *location);
0137
0138 void AddLocation(const char *prefix, const char *path);
0139
0140 void SetDefaultPage(const std::string &filename = "");
0141
0142 void SetDrawPage(const std::string &filename = "");
0143
0144 void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);
0145
0146 void CreateServerThread();
0147
0148
0149 Bool_t IsFileRequested(const char *uri, TString &res) const;
0150
0151
0152 Bool_t ExecuteHttp(std::shared_ptr<THttpCallArg> arg);
0153
0154
0155 Bool_t SubmitHttp(std::shared_ptr<THttpCallArg> arg, Bool_t can_run_immediately = kFALSE);
0156
0157
0158 Int_t ProcessRequests();
0159
0160
0161 Bool_t Register(const char *subfolder, TObject *obj);
0162
0163
0164 Bool_t Unregister(TObject *obj);
0165
0166
0167 void RegisterWS(std::shared_ptr<THttpWSHandler> ws);
0168
0169
0170 void UnregisterWS(std::shared_ptr<THttpWSHandler> ws);
0171
0172
0173 std::shared_ptr<THttpWSHandler> FindWS(const char *name);
0174
0175
0176 Bool_t ExecuteWS(std::shared_ptr<THttpCallArg> &arg, Bool_t external_thrd = kFALSE, Bool_t wait_process = kFALSE);
0177
0178
0179 void Restrict(const char *path, const char *options);
0180
0181 Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon = nullptr);
0182
0183 Bool_t Hide(const char *fullname, Bool_t hide = kTRUE);
0184
0185 Bool_t SetIcon(const char *fullname, const char *iconname);
0186
0187 Bool_t CreateItem(const char *fullname, const char *title);
0188
0189 Bool_t SetItemField(const char *fullname, const char *name, const char *value);
0190
0191 const char *GetItemField(const char *fullname, const char *name);
0192
0193
0194 static const char *GetMimeType(const char *path);
0195
0196
0197 static char *ReadFileContent(const char *filename, Int_t &len);
0198
0199
0200 static std::string ReadFileContent(const std::string &filename);
0201
0202 ClassDefOverride(THttpServer, 0)
0203 };
0204
0205 #endif