Logo ROOT   6.08/07
Reference Guide
THttpCallArg.h
Go to the documentation of this file.
1 // $Id$
2 // Author: Sergey Linev 21/05/2015
3 
4 #ifndef ROOT_THttpCallArg
5 #define ROOT_THttpCallArg
6 
7 #ifndef ROOT_TObject
8 #include "TObject.h"
9 #endif
10 
11 #ifndef ROOT_TString
12 #include "TString.h"
13 #endif
14 
15 #include <condition_variable>
16 
17 class THttpServer;
18 class TNamed;
19 
20 class THttpCallArg : public TObject {
21 
22 protected:
23  friend class THttpServer;
24 
25  TString fTopName; //! top item name
26  TString fMethod; //! request method like GET or POST
27  TString fPathName; //! item path
28  TString fFileName; //! file name
29  TString fUserName; //! authenticated user name (if any)
30  TString fQuery; //! additional arguments
31 
32  void *fPostData; //! binary data received with post request
33  Long_t fPostDataLength; //! length of binary data
34 
35  TNamed *fWSHandle; //! web-socket handle, derived from TNamed class
36 
37  std::condition_variable fCond; //! condition used to wait for processing
38 
39  TString fContentType; //! type of content
40  TString fRequestHeader; //! complete header, provided with request
41  TString fHeader; //! response header like ContentEncoding, Cache-Control and so on
42  TString fContent; //! text content (if any)
43  Int_t fZipping; //! indicate if content should be zipped
44 
45  void *fBinData; //! binary data, assigned with http call
46  Long_t fBinDataLength; //! length of binary data
47 
48  Bool_t IsBinData() const
49  {
50  return fBinData && fBinDataLength > 0;
51  }
52 
53  TString AccessHeader(TString& buf, const char* name, const char* value = 0, Bool_t doing_set = kFALSE);
54 
55  TString CountHeader(const TString& buf, Int_t number = -1111) const;
56 
57 public:
58 
59  THttpCallArg();
60  ~THttpCallArg();
61 
62  // these methods used to set http request arguments
63 
64  void SetMethod(const char *method)
65  {
66  // set request method kind like GET or POST
67 
68  fMethod = method;
69  }
70 
71  void SetTopName(const char *topname)
72  {
73  // set engine-specific top-name
74 
75  fTopName = topname;
76  }
77 
78  void SetPathAndFileName(const char *fullpath);
79 
80  void SetPathName(const char *p)
81  {
82  // set request path name
83 
84  fPathName = p;
85  }
86 
87  void SetFileName(const char *f)
88  {
89  // set request file name
90 
91  fFileName = f;
92  }
93 
94  void SetUserName(const char *n)
95  {
96  // set name of authenticated user
97 
98  fUserName = n;
99  }
100 
101  void SetQuery(const char *q)
102  {
103  // set request query
104 
105  fQuery = q;
106  }
107 
108  void SetPostData(void *data, Long_t length);
109 
110  void SetWSHandle(TNamed* handle);
111 
112  TNamed* TakeWSHandle();
113 
114  void SetRequestHeader(const char* h)
115  {
116  // set full set of request header
117 
118  fRequestHeader = h ? h : "";
119  }
120 
122  {
123  // returns number of fields in request header
124 
125  return CountHeader(fRequestHeader).Atoi();
126  }
127 
129  {
130  // returns field name in request header
131 
132  return CountHeader(fRequestHeader, number);
133  }
134 
135  TString GetRequestHeader(const char* name)
136  {
137  // get named field from request header
138 
139  return AccessHeader(fRequestHeader, name);
140  }
141 
142  const char *GetTopName() const
143  {
144  // returns engine-specific top-name
145 
146  return fTopName.Data();
147  }
148 
149  const char *GetMethod() const
150  {
151  // returns request method like GET or POST
152 
153  return fMethod.Data();
154  }
155 
157  {
158  // returns kTRUE if post method is used
159 
160  return fMethod.CompareTo("POST")==0;
161  }
162 
163  void* GetPostData() const
164  {
165  // return pointer on posted with request data
166 
167  return fPostData;
168  }
169 
171  {
172  // return length of posted with request data
173 
174  return fPostDataLength;
175  }
176 
177  const char *GetPathName() const
178  {
179  // returns path name from request URL
180 
181  return fPathName.Data();
182  }
183 
184  const char *GetFileName() const
185  {
186  // returns file name from request URL
187 
188  return fFileName.Data();
189  }
190 
191  const char *GetUserName() const
192  {
193  // return authenticated user name (0 - when no authentication)
194 
195  return fUserName.Length() > 0 ? fUserName.Data() : 0;
196  }
197 
198  const char *GetQuery() const
199  {
200  // returns request query (string after ? in request URL)
201 
202  return fQuery.Data();
203  }
204 
205  // these methods used in THttpServer to set results of request processing
206 
207  void SetContentType(const char *typ)
208  {
209  // set content type like "text/xml" or "application/json"
210 
211  fContentType = typ;
212  }
213 
214  void Set404()
215  {
216  // mark reply as 404 error - page/request not exists
217 
218  SetContentType("_404_");
219  }
220 
221  void SetFile(const char *filename = 0)
222  {
223  // indicate that http request should response with file content
224 
225  SetContentType("_file_");
226  if (filename != 0) fContent = filename;
227  }
228 
229  void SetXml()
230  {
231  // set content type as JSON
232 
233  SetContentType("text/xml");
234  }
235 
236  void SetJson()
237  {
238  // set content type as JSON
239 
240  SetContentType("application/json");
241  }
242 
243  void AddHeader(const char *name, const char *value);
244 
245  Int_t NumHeader() const
246  {
247  // returns number of fields in header
248 
249  return CountHeader(fHeader).Atoi();
250  }
251 
253  {
254  // returns field name in header
255 
256  return CountHeader(fHeader, number);
257  }
258 
259  TString GetHeader(const char* name);
260 
261  void SetEncoding(const char *typ)
262  {
263  // Set Content-Encoding header like gzip
264 
265  AccessHeader(fHeader, "Content-Encoding", typ, kTRUE);
266  }
267 
268  void SetContent(const char *c)
269  {
270  // Set content directly
271 
272  fContent = c;
273  }
274 
276 
277  void SetZipping(Int_t kind)
278  {
279  // Set kind of content zipping
280  // 0 - none
281  // 1 - only when supported in request header
282  // 2 - if supported and content size bigger than 10K
283  // 3 - always
284 
285  fZipping = kind;
286  }
287 
289  {
290  // return kind of content zipping
291 
292  return fZipping;
293  }
294 
295  void SetExtraHeader(const char *name, const char *value)
296  {
297  AddHeader(name, value);
298  }
299 
300  // Fill http header
301  void FillHttpHeader(TString &buf, const char *header = 0);
302 
303  // these methods used to return results of http request processing
304 
305  Bool_t IsContentType(const char *typ) const
306  {
307  return fContentType == typ;
308  }
309 
310  Bool_t Is404() const
311  {
312  return IsContentType("_404_");
313  }
314 
315  Bool_t IsFile() const
316  {
317  return IsContentType("_file_");
318  }
319 
320  const char *GetContentType() const
321  {
322  return fContentType.Data();
323  }
324 
325  void SetBinData(void *data, Long_t length);
326 
328  {
329  return IsBinData() ? fBinDataLength : fContent.Length();
330  }
331 
332  const void *GetContent() const
333  {
334  return IsBinData() ? fBinData : fContent.Data();
335  }
336 
337  ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
338 };
339 
340 #endif
const char * GetFileName() const
Definition: THttpCallArg.h:184
void SetZipping(Int_t kind)
Definition: THttpCallArg.h:277
void SetRequestHeader(const char *h)
Definition: THttpCallArg.h:114
Bool_t IsBinData() const
length of binary data
Definition: THttpCallArg.h:48
Bool_t IsPostMethod() const
Definition: THttpCallArg.h:156
void SetPathName(const char *p)
Definition: THttpCallArg.h:80
void SetWSHandle(TNamed *handle)
assign websocket handle with HTTP call
return c
Int_t NumHeader() const
Definition: THttpCallArg.h:245
TH1 * h
Definition: legend2.C:5
TString fQuery
authenticated user name (if any)
Definition: THttpCallArg.h:30
Basic string class.
Definition: TString.h:137
const char * GetPathName() const
Definition: THttpCallArg.h:177
void SetQuery(const char *q)
Definition: THttpCallArg.h:101
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void * fBinData
indicate if content should be zipped
Definition: THttpCallArg.h:45
void * GetPostData() const
Definition: THttpCallArg.h:163
const char * GetQuery() const
Definition: THttpCallArg.h:198
void SetContentType(const char *typ)
Definition: THttpCallArg.h:207
void SetUserName(const char *n)
Definition: THttpCallArg.h:94
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 *c)
Definition: THttpCallArg.h:268
void SetTopName(const char *topname)
Definition: THttpCallArg.h:71
#define ClassDef(name, id)
Definition: Rtypes.h:254
void SetEncoding(const char *typ)
Definition: THttpCallArg.h:261
void SetPostData(void *data, Long_t length)
set data, posted with the request buffer should be allocated with malloc(length+1) call...
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
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
condition used to wait for processing
Definition: THttpCallArg.h:39
TString fPathName
request method like GET or POST
Definition: THttpCallArg.h:27
TNamed * fWSHandle
length of binary data
Definition: THttpCallArg.h:35
Int_t NumRequestHeader() const
Definition: THttpCallArg.h:121
const char * GetUserName() const
Definition: THttpCallArg.h:191
TString fRequestHeader
type of content
Definition: THttpCallArg.h:40
Long_t fPostDataLength
binary data received with post request
Definition: THttpCallArg.h:33
Long_t fBinDataLength
binary data, assigned with http call
Definition: THttpCallArg.h:46
TString fTopName
Definition: THttpCallArg.h:25
void SetBinData(void *data, Long_t length)
set binary data, which will be returned as reply body
void SetJson()
Definition: THttpCallArg.h:236
void SetExtraHeader(const char *name, const char *value)
Definition: THttpCallArg.h:295
TString AccessHeader(TString &buf, const char *name, const char *value=0, Bool_t doing_set=kFALSE)
method used to get or set http header in the string buffer Header has following format: field1 : valu...
Ssiz_t Length() const
Definition: TString.h:390
const char * GetTopName() const
Definition: THttpCallArg.h:142
void * fPostData
additional arguments
Definition: THttpCallArg.h:32
void SetFileName(const char *f)
Definition: THttpCallArg.h:87
const void * GetContent() const
Definition: THttpCallArg.h:332
TString fUserName
file name
Definition: THttpCallArg.h:29
long Long_t
Definition: RtypesCore.h:50
std::condition_variable fCond
web-socket handle, derived from TNamed class
Definition: THttpCallArg.h:37
Int_t fZipping
text content (if any)
Definition: THttpCallArg.h:43
double f(double x)
void SetMethod(const char *method)
Definition: THttpCallArg.h:64
TString fHeader
complete header, provided with request
Definition: THttpCallArg.h:41
const char * GetContentType() const
Definition: THttpCallArg.h:320
THttpCallArg()
constructor
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:386
TNamed * TakeWSHandle()
takeout websocket handle with HTTP call can be done only once
Mother of all ROOT objects.
Definition: TObject.h:37
TString fContent
response header like ContentEncoding, Cache-Control and so on
Definition: THttpCallArg.h:42
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
top item name
Definition: THttpCallArg.h:26
Bool_t IsFile() const
Definition: THttpCallArg.h:315
Bool_t Is404() const
Definition: THttpCallArg.h:310
void SetFile(const char *filename=0)
Definition: THttpCallArg.h:221
Bool_t IsContentType(const char *typ) const
Definition: THttpCallArg.h:305
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1965
TString GetRequestHeaderName(Int_t number) const
Definition: THttpCallArg.h:128
Int_t GetZipping() const
Definition: THttpCallArg.h:288
const Bool_t kTRUE
Definition: Rtypes.h:91
float * q
Definition: THbookFile.cxx:87
TString GetRequestHeader(const char *name)
Definition: THttpCallArg.h:135
Long_t GetPostDataLength() const
Definition: THttpCallArg.h:170
~THttpCallArg()
destructor
const Int_t n
Definition: legend1.C:16
TString fFileName
item path
Definition: THttpCallArg.h:28
const char * GetMethod() const
Definition: THttpCallArg.h:149
char name[80]
Definition: TGX11.cxx:109
Bool_t CompressWithGzip()
compress reply data with gzip compression
TString GetHeader(const char *name)
return specified header
void FillHttpHeader(TString &buf, const char *header=0)
fill HTTP header
TString GetHeaderName(Int_t number) const
Definition: THttpCallArg.h:252
const char * Data() const
Definition: TString.h:349
Long_t GetContentLength() const
Definition: THttpCallArg.h:327