Logo ROOT   6.14/05
Reference Guide
THttpCallArg.h
Go to the documentation of this file.
1 // $Id$
2 // Author: Sergey Linev 21/05/2015
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_THttpCallArg
13 #define ROOT_THttpCallArg
14 
15 #include "TObject.h"
16 
17 #include "TString.h"
18 
19 #include <condition_variable>
20 #include <string>
21 #include <memory>
22 
23 class THttpServer;
24 class THttpWSEngine;
25 class THttpWSHandler;
26 
27 class THttpCallArg : public TObject {
28  friend class THttpServer;
29  friend class THttpWSEngine;
30  friend class THttpWSHandler;
31 
32 public:
33  enum {
34  kNoZip = 0, // no zipping
35  kZip = 1, // zip content if "Accept-Encoding" header contains "gzip"
36  kZipLarge = 2, // zip if content larger than 10K and "Accept-Encoding" contains "gzip"
37  kZipAlways = 3 // zip always
38  };
39 
40 protected:
41  TString fTopName; ///<! top item name
42  TString fMethod; ///<! request method like GET or POST
43  TString fPathName; ///<! item path
44  TString fFileName; ///<! file name
45  TString fUserName; ///<! authenticated user name (if any)
46  TString fQuery; ///<! additional arguments
47 
48  UInt_t fWSId{0}; ///<! websocket identifier, used in web-socket related operations
49 
50  std::condition_variable fCond; ///<! condition used to wait for processing
51 
52  TString fContentType; ///<! type of content
53  TString fRequestHeader; ///<! complete header, provided with request
54  TString fHeader; ///<! response header like ContentEncoding, Cache-Control and so on
55  Int_t fZipping{kNoZip}; ///<! indicate if and when content should be compressed
56 
57  Bool_t fNotifyFlag{kFALSE}; ///<! indicate that notification called
58 
59  TString AccessHeader(TString &buf, const char *name, const char *value = nullptr, Bool_t doing_set = kFALSE);
60 
61  TString CountHeader(const TString &buf, Int_t number = -1111) const;
62 
63 private:
64  std::shared_ptr<THttpWSEngine> fWSEngine; ///<! web-socket engine, which supplied to run created web socket
65 
66  std::string fContent; ///!< content - text or binary
67  std::string fPostData; ///<! data received with post request - text - or binary
68 
69  void AssignWSId();
70  std::shared_ptr<THttpWSEngine> TakeWSEngine();
71 
72  void ReplaceAllinContent(const std::string &from, const std::string &to);
73 
74 public:
75  explicit THttpCallArg() = default;
76  virtual ~THttpCallArg();
77 
78  // these methods used to set http request arguments
79 
80  /** set request method kind like GET or POST */
81  void SetMethod(const char *method) { fMethod = method; }
82 
83  /** set engine-specific top-name */
84  void SetTopName(const char *topname) { fTopName = topname; }
85 
86  void SetPathAndFileName(const char *fullpath);
87 
88  /** set request path name */
89  void SetPathName(const char *p) { fPathName = p; }
90 
91  /** set request file name */
92  void SetFileName(const char *f) { fFileName = f; }
93 
94  /** set name of authenticated user */
95  void SetUserName(const char *n) { fUserName = n; }
96 
97  /** set request query */
98  void SetQuery(const char *q) { fQuery = q; }
99 
100  void SetPostData(void *data, Long_t length, Bool_t make_copy = kFALSE);
101 
102  void SetPostData(std::string &&data);
103 
104  /** set web-socket id */
105  void SetWSId(UInt_t id) { fWSId = id; }
106 
107  /** get web-socket id */
108  UInt_t GetWSId() const { return fWSId; }
109 
110  /** set full set of request header */
111  void SetRequestHeader(const char *h) { fRequestHeader = (h ? h : ""); }
112 
113  /** returns number of fields in request header */
114  Int_t NumRequestHeader() const { return CountHeader(fRequestHeader).Atoi(); }
115 
116  /** returns field name in request header */
117  TString GetRequestHeaderName(Int_t number) const { return CountHeader(fRequestHeader, number); }
118 
119  /** get named field from request header */
120  TString GetRequestHeader(const char *name) { return AccessHeader(fRequestHeader, name); }
121 
122  /** returns engine-specific top-name */
123  const char *GetTopName() const { return fTopName.Data(); }
124 
125  /** returns request method like GET or POST */
126  const char *GetMethod() const { return fMethod.Data(); }
127 
128  /** returns kTRUE if post method is used */
129  Bool_t IsMethod(const char *name) const { return fMethod.CompareTo(name) == 0; }
130 
131  /** returns kTRUE if post method is used */
132  Bool_t IsPostMethod() const { return IsMethod("POST"); }
133 
134  /** return pointer on posted with request data */
135  const void *GetPostData() const { return fPostData.data(); }
136 
137  /** return length of posted with request data */
138  Long_t GetPostDataLength() const { return (Long_t) fPostData.length(); }
139 
140  /** returns post data as TString */
141  TString GetPostDataAsString() const _R__DEPRECATED_618("Use other methods to access POST data")
142  { return TString(fPostData.c_str()); }
143 
144  /** returns path name from request URL */
145  const char *GetPathName() const { return fPathName.Data(); }
146 
147  /** returns file name from request URL */
148  const char *GetFileName() const { return fFileName.Data(); }
149 
150  /** return authenticated user name (0 - when no authentication) */
151  const char *GetUserName() const { return fUserName.Length() > 0 ? fUserName.Data() : nullptr; }
152 
153  /** returns request query (string after ? in request URL) */
154  const char *GetQuery() const { return fQuery.Data(); }
155 
156  // these methods used in THttpServer to set results of request processing
157 
158  /** set content type like "text/xml" or "application/json" */
159  void SetContentType(const char *typ) { fContentType = typ; }
160 
161  /** mark reply as 404 error - page/request not exists or refused */
162  void Set404() { SetContentType("_404_"); }
163 
164  /** mark as postponed - reply will not be send to client immediately */
165  void SetPostponed() { SetContentType("_postponed_"); }
166 
167  /** indicate that http request should response with file content */
168  void SetFile(const char *filename = nullptr)
169  {
170  SetContentType("_file_");
171  if (filename)
172  fContent = filename;
173  }
174 
175  void SetText();
176  void SetTextContent(std::string &&txt);
177 
178  void SetXml();
179  void SetXmlContent(std::string &&xml);
180 
181  void SetJson();
182  void SetJsonContent(std::string &&json);
183 
184  void SetBinary();
185  void SetBinaryContent(std::string &&bin);
186 
187  void AddHeader(const char *name, const char *value);
188 
189  /** returns number of fields in header */
190  Int_t NumHeader() const { return CountHeader(fHeader).Atoi(); }
191 
192  /** returns field name in header */
193  TString GetHeaderName(Int_t number) const { return CountHeader(fHeader, number); }
194 
195  TString GetHeader(const char *name);
196 
197  /** Set Content-Encoding header like gzip */
198  void SetEncoding(const char *typ) { AccessHeader(fHeader, "Content-Encoding", typ, kTRUE); }
199 
200  void SetContent(const char *cont);
201  void SetContent(std::string &&cont);
202 
204 
205  void SetZipping(Int_t mode = kZipLarge) { fZipping = mode; }
206  Int_t GetZipping() const { return fZipping; }
207 
208  /** add extra http header value to the reply */
209  void SetExtraHeader(const char *name, const char *value) { AddHeader(name, value); }
210 
211  void FillHttpHeader(TString &buf, const char *header = nullptr) _R__DEPRECATED_618("Use method returning std::string");
212  std::string FillHttpHeader(const char *header = nullptr);
213 
214  // these methods used to return results of http request processing
215 
216  Bool_t IsContentType(const char *typ) const { return fContentType == typ; }
217  const char *GetContentType() const { return fContentType.Data(); }
218 
219  Bool_t Is404() const { return IsContentType("_404_"); }
220  Bool_t IsFile() const { return IsContentType("_file_"); }
221  Bool_t IsPostponed() const { return IsContentType("_postponed_"); }
222  Bool_t IsText() const { return IsContentType("text/plain"); }
223  Bool_t IsXml() const { return IsContentType("text/xml"); }
224  Bool_t IsJson() const { return IsContentType("application/json"); }
225  Bool_t IsBinary() const { return IsContentType("application/x-binary"); }
226 
227  void SetBinData(void *data, Long_t length) _R__DEPRECATED_618("Use SetContent(std::string &&)");
228 
229  Long_t GetContentLength() const { return (Long_t) fContent.length(); }
230  const void *GetContent() const { return fContent.data(); }
231 
232  void NotifyCondition();
233 
234  virtual void HttpReplied();
235 
236  template <class T, typename... Args>
237  void CreateWSEngine(Args... args)
238  {
239  fWSEngine = std::make_shared<T>(args...);
240  AssignWSId();
241  }
242 
243  ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
244 };
245 
246 #endif
const char * GetFileName() const
returns file name from request URL
Definition: THttpCallArg.h:148
void SetTextContent(std::string &&txt)
Set content type as "text/plain" and also assigns content After method call argument.
void SetRequestHeader(const char *h)
set full set of request header
Definition: THttpCallArg.h:111
std::string fContent
Definition: THttpCallArg.h:66
Bool_t IsMethod(const char *name) const
returns kTRUE if post method is used
Definition: THttpCallArg.h:129
UInt_t fWSId
! websocket identifier, used in web-socket related operations
Definition: THttpCallArg.h:48
Bool_t IsPostMethod() const
returns kTRUE if post method is used
Definition: THttpCallArg.h:132
void SetPathName(const char *p)
set request path name
Definition: THttpCallArg.h:89
void SetBinary()
Set content type as "application/x-binary".
void ReplaceAllinContent(const std::string &from, const std::string &to)
Replace all occurrences of.
std::string fPostData
!< content - text or binary
Definition: THttpCallArg.h:67
UInt_t GetWSId() const
get web-socket id
Definition: THttpCallArg.h:108
TString AccessHeader(TString &buf, const char *name, const char *value=nullptr, Bool_t doing_set=kFALSE)
method used to get or set http header in the string buffer Header has following format: field1 : valu...
Int_t NumHeader() const
returns number of fields in header
Definition: THttpCallArg.h:190
double T(double x)
Definition: ChebyshevPol.h:34
TString fQuery
! additional arguments
Definition: THttpCallArg.h:46
Basic string class.
Definition: TString.h:131
const char * GetPathName() const
returns path name from request URL
Definition: THttpCallArg.h:145
void SetQuery(const char *q)
set request query
Definition: THttpCallArg.h:98
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetFile(const char *filename=nullptr)
indicate that http request should response with file content
Definition: THttpCallArg.h:168
const char * GetQuery() const
returns request query (string after ? in request URL)
Definition: THttpCallArg.h:154
void SetContentType(const char *typ)
set content type like "text/xml" or "application/json"
Definition: THttpCallArg.h:159
void SetPostponed()
mark as postponed - reply will not be send to client immediately
Definition: THttpCallArg.h:165
void SetUserName(const char *n)
set name of authenticated user
Definition: THttpCallArg.h:95
TString CountHeader(const TString &buf, Int_t number=-1111) const
method used to counter number of headers or returns name of specified header
void SetContent(const char *cont)
Set content as text.
void SetPostData(void *data, Long_t length, Bool_t make_copy=kFALSE)
THttpCallArg()=default
void SetTopName(const char *topname)
set engine-specific top-name
Definition: THttpCallArg.h:84
#define ClassDef(name, id)
Definition: Rtypes.h:320
void SetEncoding(const char *typ)
Set Content-Encoding header like gzip.
Definition: THttpCallArg.h:198
void SetPathAndFileName(const char *fullpath)
set complete path of requested http element For instance, it could be "/folder/subfolder/get.bin" Here "/folder/subfolder/" is element path and "get.bin" requested file.
TString fContentType
! type of content
Definition: THttpCallArg.h:52
std::shared_ptr< THttpWSEngine > fWSEngine
! web-socket engine, which supplied to run created web socket
Definition: THttpCallArg.h:64
void SetBinaryContent(std::string &&bin)
Set content type as "application/x-binary" and also assigns content After method call argument...
TString fPathName
! item path
Definition: THttpCallArg.h:43
#define _R__DEPRECATED_618(REASON)
Definition: RConfig.h:500
void Set404()
mark reply as 404 error - page/request not exists or refused
Definition: THttpCallArg.h:162
Int_t NumRequestHeader() const
returns number of fields in request header
Definition: THttpCallArg.h:114
const char * GetUserName() const
return authenticated user name (0 - when no authentication)
Definition: THttpCallArg.h:151
XFontStruct * id
Definition: TGX11.cxx:108
TString fRequestHeader
! complete header, provided with request
Definition: THttpCallArg.h:53
TString GetPostDataAsString() const _R__DEPRECATED_618("Use other methods to access POST data")
returns post data as TString
Definition: THttpCallArg.h:141
void AssignWSId()
Assign websocket identifier from the engine.
TString fTopName
! top item name
Definition: THttpCallArg.h:41
void SetJson()
Set content type as "application/json".
Bool_t fNotifyFlag
! indicate that notification called
Definition: THttpCallArg.h:57
void SetExtraHeader(const char *name, const char *value)
add extra http header value to the reply
Definition: THttpCallArg.h:209
const void * GetPostData() const
return pointer on posted with request data
Definition: THttpCallArg.h:135
void SetXmlContent(std::string &&xml)
Set content type as "text/xml" and also assigns content After method call argument.
unsigned int UInt_t
Definition: RtypesCore.h:42
Ssiz_t Length() const
Definition: TString.h:405
const char * GetTopName() const
returns engine-specific top-name
Definition: THttpCallArg.h:123
void SetText()
Set content type as "text/plain".
void SetFileName(const char *f)
set request file name
Definition: THttpCallArg.h:92
#define h(i)
Definition: RSha256.hxx:106
const Bool_t kFALSE
Definition: RtypesCore.h:88
TString fUserName
! authenticated user name (if any)
Definition: THttpCallArg.h:45
long Long_t
Definition: RtypesCore.h:50
std::condition_variable fCond
! condition used to wait for processing
Definition: THttpCallArg.h:50
Int_t fZipping
! indicate if and when content should be compressed
Definition: THttpCallArg.h:55
void SetWSId(UInt_t id)
set web-socket id
Definition: THttpCallArg.h:105
void SetMethod(const char *method)
set request method kind like GET or POST
Definition: THttpCallArg.h:81
void SetXml()
Set content type as "text/xml".
TString fHeader
! response header like ContentEncoding, Cache-Control and so on
Definition: THttpCallArg.h:54
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:406
Mother of all ROOT objects.
Definition: TObject.h:37
void SetJsonContent(std::string &&json)
Set content type as "application/json" and also assigns content After method call argument...
void AddHeader(const char *name, const char *value)
Set name: value pair to reply header Content-Type field handled separately - one should use SetConten...
TString fMethod
! request method like GET or POST
Definition: THttpCallArg.h:42
basic_json<> json
default JSON class
Definition: json.hpp:12912
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1896
void SetZipping(Int_t mode=kZipLarge)
Definition: THttpCallArg.h:205
TString GetRequestHeaderName(Int_t number) const
returns field name in request header
Definition: THttpCallArg.h:117
Int_t GetZipping() const
Definition: THttpCallArg.h:206
float * q
Definition: THbookFile.cxx:87
TString GetRequestHeader(const char *name)
get named field from request header
Definition: THttpCallArg.h:120
Long_t GetPostDataLength() const
return length of posted with request data
Definition: THttpCallArg.h:138
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual ~THttpCallArg()
destructor
const Int_t n
Definition: legend1.C:16
std::shared_ptr< THttpWSEngine > TakeWSEngine()
takeout websocket handle with HTTP call can be done only once
TString fFileName
! file name
Definition: THttpCallArg.h:44
const char * GetMethod() const
returns request method like GET or POST
Definition: THttpCallArg.h:126
char name[80]
Definition: TGX11.cxx:109
Bool_t CompressWithGzip()
compress reply data with gzip compression
TString GetHeader(const char *name)
return specified header
TString GetHeaderName(Int_t number) const
returns field name in header
Definition: THttpCallArg.h:193
const char * Data() const
Definition: TString.h:364