// $Id$
// Author: Sergey Linev   21/12/2013

#ifndef ROOT_THttpServer
#define ROOT_THttpServer

#ifndef ROOT_TObject
#include "TObject.h"
#endif

#ifndef ROOT_TList
#include "TList.h"
#endif

#ifndef ROOT_TMutex
#include "TMutex.h"
#endif

#ifndef ROOT_THttpCallArg
#include "THttpCallArg.h"
#endif


class THttpEngine;
class THttpTimer;
class TRootSniffer;


class THttpServer : public TNamed {

protected:

   TList        fEngines;     //! engines which runs http server
   THttpTimer  *fTimer;       //! timer used to access main thread
   TRootSniffer *fSniffer;    //! sniffer provides access to ROOT objects hierarchy

   Long_t       fMainThrdId;  //! id of the main ROOT process

   TString      fJSROOTSYS;   //! location of local JSROOT files
   TString      fTopName;     //! name of top folder, default - "ROOT"
   TString      fJSROOT;      //! location of external JSROOT files
   TList        fLocations;   //! list of local directories, which could be accessed via server

   TString      fDefaultPage; //! file name for default page name
   TString      fDefaultPageCont; //! content of the file content
   TString      fDrawPage;    //! file name for drawing of single element
   TString      fDrawPageCont; //! content of draw page

   TMutex       fMutex;       //! mutex to protect list with arguments
   TList        fCallArgs;    //! submitted arguments

   // Here any request can be processed
   virtual void ProcessRequest(THttpCallArg *arg);

   static Bool_t VerifyFilePath(const char *fname);

public:

   THttpServer(const char *engine = "civetweb:8080");
   virtual ~THttpServer();

   Bool_t CreateEngine(const char *engine);

   TRootSniffer *GetSniffer() const
   {
      // returns pointer on objects sniffer

      return fSniffer;
   }

   void SetSniffer(TRootSniffer *sniff);

   Bool_t IsReadOnly() const;

   void SetReadOnly(Bool_t readonly);

   void SetTopName(const char *top)
   {
      // set name of top item in objects hierarchy
      fTopName = top;
   }

   const char *GetTopName() const
   {
      // returns name of top item in objects hierarchy
      return fTopName.Data();
   }

   void SetJSROOT(const char *location);

   void AddLocation(const char *prefix, const char *path);

   void SetDefaultPage(const char *filename);

   void SetDrawPage(const char *filename);

   void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);

   /** Check if file is requested, thread safe */
   Bool_t  IsFileRequested(const char *uri, TString &res) const;

   /** Execute HTTP request */
   Bool_t ExecuteHttp(THttpCallArg *arg);

   /** Process submitted requests, must be called from main thread */
   void ProcessRequests();

   /** Register object in subfolder */
   Bool_t Register(const char *subfolder, TObject *obj);

   /** Unregister object */
   Bool_t Unregister(TObject *obj);

   /** Restrict access to specified object */
   void Restrict(const char *path, const char* options);

   Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon = 0);

   Bool_t Hide(const char *fullname, Bool_t hide = kTRUE);

   Bool_t SetIcon(const char *fullname, const char *iconname);

   Bool_t CreateItem(const char *fullname, const char *title);

   Bool_t SetItemField(const char *fullname, const char *name, const char *value);

   const char *GetItemField(const char *fullname, const char *name);

   /** Guess mime type base on file extension */
   static const char *GetMimeType(const char *path);

   /** Reads content of file from the disk */
   static char *ReadFileContent(const char *filename, Int_t &len);

   ClassDef(THttpServer, 0) // HTTP server for ROOT analysis
};

#endif
 THttpServer.h:1
 THttpServer.h:2
 THttpServer.h:3
 THttpServer.h:4
 THttpServer.h:5
 THttpServer.h:6
 THttpServer.h:7
 THttpServer.h:8
 THttpServer.h:9
 THttpServer.h:10
 THttpServer.h:11
 THttpServer.h:12
 THttpServer.h:13
 THttpServer.h:14
 THttpServer.h:15
 THttpServer.h:16
 THttpServer.h:17
 THttpServer.h:18
 THttpServer.h:19
 THttpServer.h:20
 THttpServer.h:21
 THttpServer.h:22
 THttpServer.h:23
 THttpServer.h:24
 THttpServer.h:25
 THttpServer.h:26
 THttpServer.h:27
 THttpServer.h:28
 THttpServer.h:29
 THttpServer.h:30
 THttpServer.h:31
 THttpServer.h:32
 THttpServer.h:33
 THttpServer.h:34
 THttpServer.h:35
 THttpServer.h:36
 THttpServer.h:37
 THttpServer.h:38
 THttpServer.h:39
 THttpServer.h:40
 THttpServer.h:41
 THttpServer.h:42
 THttpServer.h:43
 THttpServer.h:44
 THttpServer.h:45
 THttpServer.h:46
 THttpServer.h:47
 THttpServer.h:48
 THttpServer.h:49
 THttpServer.h:50
 THttpServer.h:51
 THttpServer.h:52
 THttpServer.h:53
 THttpServer.h:54
 THttpServer.h:55
 THttpServer.h:56
 THttpServer.h:57
 THttpServer.h:58
 THttpServer.h:59
 THttpServer.h:60
 THttpServer.h:61
 THttpServer.h:62
 THttpServer.h:63
 THttpServer.h:64
 THttpServer.h:65
 THttpServer.h:66
 THttpServer.h:67
 THttpServer.h:68
 THttpServer.h:69
 THttpServer.h:70
 THttpServer.h:71
 THttpServer.h:72
 THttpServer.h:73
 THttpServer.h:74
 THttpServer.h:75
 THttpServer.h:76
 THttpServer.h:77
 THttpServer.h:78
 THttpServer.h:79
 THttpServer.h:80
 THttpServer.h:81
 THttpServer.h:82
 THttpServer.h:83
 THttpServer.h:84
 THttpServer.h:85
 THttpServer.h:86
 THttpServer.h:87
 THttpServer.h:88
 THttpServer.h:89
 THttpServer.h:90
 THttpServer.h:91
 THttpServer.h:92
 THttpServer.h:93
 THttpServer.h:94
 THttpServer.h:95
 THttpServer.h:96
 THttpServer.h:97
 THttpServer.h:98
 THttpServer.h:99
 THttpServer.h:100
 THttpServer.h:101
 THttpServer.h:102
 THttpServer.h:103
 THttpServer.h:104
 THttpServer.h:105
 THttpServer.h:106
 THttpServer.h:107
 THttpServer.h:108
 THttpServer.h:109
 THttpServer.h:110
 THttpServer.h:111
 THttpServer.h:112
 THttpServer.h:113
 THttpServer.h:114
 THttpServer.h:115
 THttpServer.h:116
 THttpServer.h:117
 THttpServer.h:118
 THttpServer.h:119
 THttpServer.h:120
 THttpServer.h:121
 THttpServer.h:122
 THttpServer.h:123
 THttpServer.h:124
 THttpServer.h:125
 THttpServer.h:126
 THttpServer.h:127
 THttpServer.h:128
 THttpServer.h:129
 THttpServer.h:130
 THttpServer.h:131
 THttpServer.h:132
 THttpServer.h:133
 THttpServer.h:134
 THttpServer.h:135
 THttpServer.h:136
 THttpServer.h:137
 THttpServer.h:138