#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
class THttpEngine;
class THttpTimer;
class TRootSniffer;
class THttpServer;
class THttpCallArg : public TObject {
protected:
friend class THttpServer;
TString fTopName;
TString fPathName;
TString fFileName;
TString fQuery;
TCondition fCond;
TString fContentType;
TString fHeader;
TString fContent;
Int_t fZipping;
void *fBinData;
Long_t fBinDataLength;
Bool_t IsBinData() const
{
return fBinData && fBinDataLength > 0;
}
public:
THttpCallArg();
~THttpCallArg();
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();
}
void SetContentType(const char *typ)
{
fContentType = typ;
}
void Set404()
{
SetContentType("_404_");
}
void SetFile(const char* filename = 0)
{
SetContentType("_file_");
if (filename!=0) fContent = filename;
}
void SetXml()
{
SetContentType("text/xml");
}
void SetJson()
{
SetContentType("application/json");
}
void AddHeader(const char* name, const char* value)
{
fHeader.Append(TString::Format("%s: %s\r\n", name, value));
}
void SetEncoding(const char *typ)
{
AddHeader("Content-Encoding", typ);
}
void SetContent(const char *c)
{
fContent = c;
}
Bool_t CompressWithGzip();
void SetZipping(Int_t kind)
{
fZipping = kind;
}
Int_t GetZipping() const
{
return fZipping;
}
void SetExtraHeader(const char* name, const char* value)
{
AddHeader(name, value);
}
void FillHttpHeader(TString &buf, const char* header = 0);
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();
}
void SetBinData(void* data, Long_t length);
Long_t GetContentLength() const
{
return IsBinData() ? fBinDataLength : fContent.Length();
}
const void *GetContent() const
{
return IsBinData() ? fBinData : fContent.Data();
}
ClassDef(THttpCallArg, 0)
};
class THttpServer : public TNamed {
protected:
TList fEngines;
THttpTimer *fTimer;
TRootSniffer *fSniffer;
Long_t fMainThrdId;
TString fJsRootSys;
TString fTopName;
TString fDefaultPage;
TString fDefaultPageCont;
TString fDrawPage;
TString fDrawPageCont;
TMutex fMutex;
TList fCallArgs;
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
{
return fSniffer;
}
void SetSniffer(TRootSniffer *sniff);
Bool_t IsReadOnly() const;
void SetReadOnly(Bool_t readonly);
void SetTopName(const char *top)
{
fTopName = top;
}
const char *GetTopName() const
{
return fTopName.Data();
}
void SetTimer(Long_t milliSec = 100, Bool_t mode = kTRUE);
Bool_t IsFileRequested(const char *uri, TString &res) const;
Bool_t ExecuteHttp(THttpCallArg *arg);
void ProcessRequests();
Bool_t Register(const char *subfolder, TObject *obj);
Bool_t Unregister(TObject *obj);
static const char *GetMimeType(const char *path);
static char* ReadFileContent(const char* filename, Int_t& len);
ClassDef(THttpServer, 0)
};
#endif