Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/THttpServer.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // $Id$
0002 // Author: Sergey Linev   21/12/2013
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
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;                      ///<! engines which runs http server
0035    std::unique_ptr<THttpTimer> fTimer;   ///<! timer used to access main thread
0036    std::unique_ptr<TRootSniffer> fSniffer; ///<! sniffer provides access to ROOT objects hierarchy
0037    Bool_t fTerminated{kFALSE};          ///<! termination flag, disables all requests processing
0038    Long_t fMainThrdId{0};               ///<! id of the thread for processing requests
0039    Long_t fProcessingThrdId{0};         ///<! id of the thread where events are recently processing
0040    Bool_t fOwnThread{kFALSE};           ///<! true when specialized thread allocated for processing requests
0041    std::thread fThrd;                   ///<! own thread
0042    Bool_t fWSOnly{kFALSE};              ///<! when true, handle only websockets / longpoll engine
0043 
0044    TString fJSROOTSYS;       ///<! location of local JSROOT files
0045    TString fTopName{"ROOT"}; ///<! name of top folder, default - "ROOT"
0046    TString fJSROOT;          ///<! location of external JSROOT files
0047 
0048    std::map<std::string, std::string> fLocations; ///<! list of local directories, which could be accessed via server
0049 
0050    std::string fDefaultPage;     ///<! file name for default page name
0051    std::string fDefaultPageCont; ///<! content of default html page
0052    std::string fDrawPage;        ///<! file name for drawing of single element
0053    std::string fDrawPageCont;    ///<! content of draw html page
0054    std::string fCors;            ///<! CORS: sets Access-Control-Allow-Origin header for ProcessRequest responses
0055    std::string fCorsCredentials; ///<! CORS: add Access-Control-Allow-Credentials: true response header
0056 
0057    std::mutex fMutex;                                        ///<! mutex to protect list with arguments
0058    std::queue<std::shared_ptr<THttpCallArg>> fArgs;          ///<! submitted arguments
0059 
0060    std::mutex fWSMutex;                                      ///<! mutex to protect WS handler lists
0061    std::vector<std::shared_ptr<THttpWSHandler>> fWSHandlers; ///<! list of WS handlers
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    Bool_t CreateEngine(const char *engine);
0085 
0086    Bool_t IsAnyEngine() const { return fEngines.GetSize() > 0; }
0087 
0088    /** returns pointer on objects sniffer */
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 IsWSOnly() const;
0098 
0099    void SetWSOnly(Bool_t on = kTRUE);
0100 
0101    /** set termination flag, no any further requests will be processed */
0102    void SetTerminate();
0103 
0104    /** returns kTRUE, if server was terminated */
0105    Bool_t IsTerminated() const { return fTerminated; }
0106 
0107    /** Enable CORS header to ProcessRequests() responses
0108     * Specified location (typically "*") add as "Access-Control-Allow-Origin" header */
0109    void SetCors(const std::string &domain = "*") { fCors = domain; }
0110 
0111    /** Returns kTRUE if CORS was configured */
0112    Bool_t IsCors() const { return !fCors.empty(); }
0113 
0114    /** Returns specified CORS domain */
0115    const char *GetCors() const { return fCors.c_str(); }
0116 
0117    /** Enable/disable usage Access-Control-Allow-Credentials response header */
0118    void SetCorsCredentials(const std::string &value = "true") { fCorsCredentials = value; }
0119 
0120    /** Returns kTRUE if Access-Control-Allow-Credentials header should be used */
0121    Bool_t IsCorsCredentials() const { return !fCorsCredentials.empty(); }
0122 
0123    /** Returns specified CORS credentials value - if any */
0124    const char *GetCorsCredentials() const { return fCorsCredentials.c_str(); }
0125 
0126    /** set name of top item in objects hierarchy */
0127    void SetTopName(const char *top) { fTopName = top; }
0128 
0129    /** returns name of top item in objects hierarchy */
0130    const char *GetTopName() const { return fTopName.Data(); }
0131 
0132    void SetJSROOT(const char *location);
0133 
0134    void AddLocation(const char *prefix, const char *path);
0135 
0136    void SetDefaultPage(const std::string &filename = "");
0137 
0138    void SetDrawPage(const std::string &filename = "");
0139 
0140    void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);
0141 
0142    void CreateServerThread();
0143 
0144    /** Check if file is requested, thread safe */
0145    Bool_t IsFileRequested(const char *uri, TString &res) const;
0146 
0147    /** Execute HTTP request */
0148    Bool_t ExecuteHttp(std::shared_ptr<THttpCallArg> arg);
0149 
0150    /** Submit HTTP request */
0151    Bool_t SubmitHttp(std::shared_ptr<THttpCallArg> arg, Bool_t can_run_immediately = kFALSE);
0152 
0153    /** Process submitted requests, must be called from appropriate thread */
0154    Int_t ProcessRequests();
0155 
0156    /** Register object in subfolder */
0157    Bool_t Register(const char *subfolder, TObject *obj);
0158 
0159    /** Unregister object */
0160    Bool_t Unregister(TObject *obj);
0161 
0162    /** Register WS handler*/
0163    void RegisterWS(std::shared_ptr<THttpWSHandler> ws);
0164 
0165    /** Unregister WS handler*/
0166    void UnregisterWS(std::shared_ptr<THttpWSHandler> ws);
0167 
0168    /** Find web-socket handler with given name */
0169    std::shared_ptr<THttpWSHandler> FindWS(const char *name);
0170 
0171    /** Execute WS request */
0172    Bool_t ExecuteWS(std::shared_ptr<THttpCallArg> &arg, Bool_t external_thrd = kFALSE, Bool_t wait_process = kFALSE);
0173 
0174    /** Restrict access to specified object */
0175    void Restrict(const char *path, const char *options);
0176 
0177    Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon = nullptr);
0178 
0179    Bool_t Hide(const char *fullname, Bool_t hide = kTRUE);
0180 
0181    Bool_t SetIcon(const char *fullname, const char *iconname);
0182 
0183    Bool_t CreateItem(const char *fullname, const char *title);
0184 
0185    Bool_t SetItemField(const char *fullname, const char *name, const char *value);
0186 
0187    const char *GetItemField(const char *fullname, const char *name);
0188 
0189    /** Guess mime type base on file extension */
0190    static const char *GetMimeType(const char *path);
0191 
0192    /** Reads content of file from the disk */
0193    static char *ReadFileContent(const char *filename, Int_t &len);
0194 
0195    /** Reads content of file from the disk, use std::string in return value */
0196    static std::string ReadFileContent(const std::string &filename);
0197 
0198    ClassDefOverride(THttpServer, 0) // HTTP server for ROOT analysis
0199 };
0200 
0201 #endif