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

#ifndef ROOT_THttpServer
#define ROOT_THttpServer

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif

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

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

#ifndef ROOT_TCondition
#include "TCondition.h"
#endif


// this class is used to deliver http request arguments to main process
// and also to return back results of processing


class THttpEngine;
class THttpTimer;
class TRootSniffer;
class THttpServer;


class THttpCallArg : public TObject {

protected:
   friend class THttpServer;

   TString fTopName;            //! top item name
   TString fPathName;           //! item path
   TString fFileName;           //! file name
   TString fQuery;              //! additional arguments

   TCondition fCond;            //! condition used to wait for processing

   TString fContentType;        //! type of content
   TString fContentEncoding;    //! type of content encoding
   TString fContent;            //! text content (if any)

   void *fBinData;              //! binary data, assigned with http call
   Long_t fBinDataLength;       //! length of binary data

   Bool_t IsBinData() const
   {
      return fBinData && fBinDataLength > 0;
   }

public:

   THttpCallArg();
   ~THttpCallArg();

   // these methods used to set http request arguments

   void SetTopName(const char *topname)
   {
      fTopName = topname;
   }
   void SetPathAndFileName(const char *fullpath);
   void SetPathName(const char *p)
   {
      fPathName = p;
   }
   void SetFileName(const char *f)
   {
      fFileName = f;
   }
   void SetQuery(const char *q)
   {
      fQuery = q;
   }

   const char *GetTopName() const
   {
      return fTopName.Data();
   }
   const char *GetPathName() const
   {
      return fPathName.Data();
   }
   const char *GetFileName() const
   {
      return fFileName.Data();
   }
   const char *GetQuery() const
   {
      return fQuery.Data();
   }

   // these methods used in THttpServer to set results of request processing

   void SetContentType(const char *typ)
   {
      fContentType = typ;
   }
   void Set404()
   {
      SetContentType("_404_");
   }
   void SetFile()
   {
      SetContentType("_file_");
   }
   void SetXml()
   {
      SetContentType("text/xml");
   }
   void SetJson()
   {
      SetContentType("application/json");
   }

   // Set encoding like gzip
   void SetEncoding(const char *typ)
   {
      fContentEncoding = typ;
   }

   // Fill http header
   void FillHttpHeader(TString &buf, Bool_t normal = kTRUE);

   // these methods used to return results of http request processing

   Bool_t IsContentType(const char *typ) const
   {
      return fContentType == typ;
   }

   Bool_t Is404() const
   {
      return IsContentType("_404_");
   }
   Bool_t IsFile() const
   {
      return IsContentType("_file_");
   }

   const char *GetContentType() const
   {
      return fContentType.Data();
   }

   Long_t GetContentLength() const
   {
      return IsBinData() ? fBinDataLength : fContent.Length();
   }
   const void *GetContent() const
   {
      return IsBinData() ? fBinData : fContent.Data();
   }

   ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
};


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      fHttpSys;     //! location of http plugin, need to read special files
   TString      fRootSys;     //! location of ROOT (if any)
   TString      fJSRootIOSys; //! location of JSRootIO (if any)
   TString      fTopName;     //! name of top folder, default - "ROOT"

   TString      fDefaultPage; //! file name for default page name
   TString      fDrawPage;    //! file name for drawing of single element

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

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

public:

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

   Bool_t CreateEngine(const char *engine);

   TRootSniffer *GetSniffer() const
   {
      return fSniffer;
   }
   void SetSniffer(TRootSniffer *sniff);

   void SetTopName(const char *top)
   {
      fTopName = top;
   }
   const char *GetTopName() const
   {
      return fTopName.Data();
   }

   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);

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

   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
 THttpServer.h:139
 THttpServer.h:140
 THttpServer.h:141
 THttpServer.h:142
 THttpServer.h:143
 THttpServer.h:144
 THttpServer.h:145
 THttpServer.h:146
 THttpServer.h:147
 THttpServer.h:148
 THttpServer.h:149
 THttpServer.h:150
 THttpServer.h:151
 THttpServer.h:152
 THttpServer.h:153
 THttpServer.h:154
 THttpServer.h:155
 THttpServer.h:156
 THttpServer.h:157
 THttpServer.h:158
 THttpServer.h:159
 THttpServer.h:160
 THttpServer.h:161
 THttpServer.h:162
 THttpServer.h:163
 THttpServer.h:164
 THttpServer.h:165
 THttpServer.h:166
 THttpServer.h:167
 THttpServer.h:168
 THttpServer.h:169
 THttpServer.h:170
 THttpServer.h:171
 THttpServer.h:172
 THttpServer.h:173
 THttpServer.h:174
 THttpServer.h:175
 THttpServer.h:176
 THttpServer.h:177
 THttpServer.h:178
 THttpServer.h:179
 THttpServer.h:180
 THttpServer.h:181
 THttpServer.h:182
 THttpServer.h:183
 THttpServer.h:184
 THttpServer.h:185
 THttpServer.h:186
 THttpServer.h:187
 THttpServer.h:188
 THttpServer.h:189
 THttpServer.h:190
 THttpServer.h:191
 THttpServer.h:192
 THttpServer.h:193
 THttpServer.h:194
 THttpServer.h:195
 THttpServer.h:196
 THttpServer.h:197
 THttpServer.h:198
 THttpServer.h:199
 THttpServer.h:200
 THttpServer.h:201
 THttpServer.h:202
 THttpServer.h:203
 THttpServer.h:204
 THttpServer.h:205
 THttpServer.h:206
 THttpServer.h:207
 THttpServer.h:208
 THttpServer.h:209
 THttpServer.h:210
 THttpServer.h:211
 THttpServer.h:212
 THttpServer.h:213
 THttpServer.h:214
 THttpServer.h:215
 THttpServer.h:216
 THttpServer.h:217
 THttpServer.h:218
 THttpServer.h:219
 THttpServer.h:220
 THttpServer.h:221
 THttpServer.h:222
 THttpServer.h:223
 THttpServer.h:224
 THttpServer.h:225
 THttpServer.h:226
 THttpServer.h:227
 THttpServer.h:228
 THttpServer.h:229
 THttpServer.h:230
 THttpServer.h:231
 THttpServer.h:232
 THttpServer.h:233
 THttpServer.h:234
 THttpServer.h:235