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