ROOT  6.06/09
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 #ifndef ROOT_TCondition
16 #include "TCondition.h"
17 #endif
18 
19 
20 class THttpServer;
21 
22 class THttpCallArg : public TObject {
23 
24 protected:
25  friend class THttpServer;
26 
27  TString fTopName; //! top item name
28  TString fMethod; //! request method like GET or POST
29  TString fPathName; //! item path
30  TString fFileName; //! file name
31  TString fUserName; //! authenticated user name (if any)
32  TString fQuery; //! additional arguments
33 
34  void *fPostData; //! binary data received with post request
35  Long_t fPostDataLength; //! length of binary data
36 
37  TCondition 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 SetRequestHeader(const char* h)
111  {
112  // set full set of request header
113 
114  fRequestHeader = h ? h : "";
115  }
116 
118  {
119  // returns number of fields in request header
120 
121  return CountHeader(fRequestHeader).Atoi();
122  }
123 
125  {
126  // returns field name in request header
127 
128  return CountHeader(fRequestHeader, number);
129  }
130 
132  {
133  // get named field from request header
134 
135  return AccessHeader(fRequestHeader, name);
136  }
137 
138  const char *GetTopName() const
139  {
140  // returns engine-specific top-name
141 
142  return fTopName.Data();
143  }
144 
145  const char *GetMethod() const
146  {
147  // returns request method like GET or POST
148 
149  return fMethod.Data();
150  }
151 
153  {
154  // returns kTRUE if post method is used
155 
156  return fMethod.CompareTo("POST")==0;
157  }
158 
159  void* GetPostData() const
160  {
161  // return pointer on posted with request data
162 
163  return fPostData;
164  }
165 
167  {
168  // return length of posted with request data
169 
170  return fPostDataLength;
171  }
172 
173  const char *GetPathName() const
174  {
175  // returns path name from request URL
176 
177  return fPathName.Data();
178  }
179 
180  const char *GetFileName() const
181  {
182  // returns file name from request URL
183 
184  return fFileName.Data();
185  }
186 
187  const char *GetUserName() const
188  {
189  // return authenticated user name (0 - when no authentication)
190 
191  return fUserName.Length() > 0 ? fUserName.Data() : 0;
192  }
193 
194  const char *GetQuery() const
195  {
196  // returns request query (string after ? in request URL)
197 
198  return fQuery.Data();
199  }
200 
201  // these methods used in THttpServer to set results of request processing
202 
203  void SetContentType(const char *typ)
204  {
205  // set content type like "text/xml" or "application/json"
206 
207  fContentType = typ;
208  }
209 
210  void Set404()
211  {
212  // mark reply as 404 error - page/request not exists
213 
214  SetContentType("_404_");
215  }
216 
217  void SetFile(const char *filename = 0)
218  {
219  // indicate that http request should response with file content
220 
221  SetContentType("_file_");
222  if (filename != 0) fContent = filename;
223  }
224 
225  void SetXml()
226  {
227  // set content type as JSON
228 
229  SetContentType("text/xml");
230  }
231 
232  void SetJson()
233  {
234  // set content type as JSON
235 
236  SetContentType("application/json");
237  }
238 
239  void AddHeader(const char *name, const char *value);
240 
241  Int_t NumHeader() const
242  {
243  // returns number of fields in header
244 
245  return CountHeader(fHeader).Atoi();
246  }
247 
249  {
250  // returns field name in header
251 
252  return CountHeader(fHeader, number);
253  }
254 
255  TString GetHeader(const char* name);
256 
257  void SetEncoding(const char *typ)
258  {
259  // Set Content-Encoding header like gzip
260 
261  AccessHeader(fHeader, "Content-Encoding", typ, kTRUE);
262  }
263 
264  void SetContent(const char *c)
265  {
266  // Set content directly
267 
268  fContent = c;
269  }
270 
272 
273  void SetZipping(Int_t kind)
274  {
275  // Set kind of content zipping
276  // 0 - none
277  // 1 - only when supported in request header
278  // 2 - if supported and content size bigger than 10K
279  // 3 - always
280 
281  fZipping = kind;
282  }
283 
285  {
286  // return kind of content zipping
287 
288  return fZipping;
289  }
290 
291  void SetExtraHeader(const char *name, const char *value)
292  {
293  AddHeader(name, value);
294  }
295 
296  // Fill http header
297  void FillHttpHeader(TString &buf, const char *header = 0);
298 
299  // these methods used to return results of http request processing
300 
301  Bool_t IsContentType(const char *typ) const
302  {
303  return fContentType == typ;
304  }
305 
306  Bool_t Is404() const
307  {
308  return IsContentType("_404_");
309  }
310 
311  Bool_t IsFile() const
312  {
313  return IsContentType("_file_");
314  }
315 
316  const char *GetContentType() const
317  {
318  return fContentType.Data();
319  }
320 
321  void SetBinData(void *data, Long_t length);
322 
324  {
325  return IsBinData() ? fBinDataLength : fContent.Length();
326  }
327 
328  const void *GetContent() const
329  {
330  return IsBinData() ? fBinData : fContent.Data();
331  }
332 
333  ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
334 };
335 
336 #endif
void SetZipping(Int_t kind)
Definition: THttpCallArg.h:273
Bool_t IsContentType(const char *typ) const
Definition: THttpCallArg.h:301
void SetRequestHeader(const char *h)
Definition: THttpCallArg.h:110
const char * GetTopName() const
Definition: THttpCallArg.h:138
void SetPathName(const char *p)
Definition: THttpCallArg.h:80
Ssiz_t Length() const
Definition: TString.h:390
TH1 * h
Definition: legend2.C:5
Long_t GetContentLength() const
Definition: THttpCallArg.h:323
Bool_t Is404() const
Definition: THttpCallArg.h:306
static const char * filename()
TString fQuery
authenticated user name (if any)
Definition: THttpCallArg.h:32
const char * GetPathName() const
Definition: THttpCallArg.h:173
Basic string class.
Definition: TString.h:137
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 SetContentType(const char *typ)
Definition: THttpCallArg.h:203
const void * GetContent() const
Definition: THttpCallArg.h:328
void SetUserName(const char *n)
Definition: THttpCallArg.h:94
TString GetRequestHeaderName(Int_t number) const
Definition: THttpCallArg.h:124
void SetContent(const char *c)
Definition: THttpCallArg.h:264
const char * GetMethod() const
Definition: THttpCallArg.h:145
void SetTopName(const char *topname)
Definition: THttpCallArg.h:71
const char * Data() const
Definition: TString.h:349
#define ClassDef(name, id)
Definition: Rtypes.h:254
void SetEncoding(const char *typ)
Definition: THttpCallArg.h:257
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:39
Bool_t IsFile() const
Definition: THttpCallArg.h:311
TString fPathName
request method like GET or POST
Definition: THttpCallArg.h:29
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1964
void * GetPostData() const
Definition: THttpCallArg.h:159
const char * GetQuery() const
Definition: THttpCallArg.h:194
TString fRequestHeader
type of content
Definition: THttpCallArg.h:40
Long_t fPostDataLength
binary data received with post request
Definition: THttpCallArg.h:35
Long_t fBinDataLength
binary data, assigned with http call
Definition: THttpCallArg.h:46
TString fTopName
Definition: THttpCallArg.h:27
void SetBinData(void *data, Long_t length)
set binary data, which will be returned as reply body
void SetJson()
Definition: THttpCallArg.h:232
Double_t length(const TVector2 &v)
Definition: CsgOps.cxx:347
void SetExtraHeader(const char *name, const char *value)
Definition: THttpCallArg.h:291
const char * GetFileName() const
Definition: THttpCallArg.h:180
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:248
Bool_t IsPostMethod() const
Definition: THttpCallArg.h:152
Int_t NumHeader() const
Definition: THttpCallArg.h:241
void * fPostData
additional arguments
Definition: THttpCallArg.h:34
const char * GetUserName() const
Definition: THttpCallArg.h:187
void SetFileName(const char *f)
Definition: THttpCallArg.h:87
TString fUserName
file name
Definition: THttpCallArg.h:31
long Long_t
Definition: RtypesCore.h:50
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 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:41
Int_t GetZipping() const
Definition: THttpCallArg.h:284
TCondition fCond
length of binary data
Definition: THttpCallArg.h:37
Int_t NumRequestHeader() const
Definition: THttpCallArg.h:117
#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: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:28
void SetFile(const char *filename=0)
Definition: THttpCallArg.h:217
Long_t GetPostDataLength() const
Definition: THttpCallArg.h:166
Bool_t IsBinData() const
length of binary data
Definition: THttpCallArg.h:48
const Bool_t kTRUE
Definition: Rtypes.h:91
float * q
Definition: THbookFile.cxx:87
TString GetRequestHeader(const char *name)
Definition: THttpCallArg.h:131
float value
Definition: math.cpp:443
~THttpCallArg()
destructor
const Int_t n
Definition: legend1.C:16
TString fFileName
item path
Definition: THttpCallArg.h:30
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:385
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:316