Logo ROOT   6.12/07
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 
21 class THttpServer;
22 class TNamed;
23 
24 class THttpCallArg : public TObject {
25 
26 protected:
27  friend class THttpServer;
28 
29  TString fTopName; ///<! top item name
30  TString fMethod; ///<! request method like GET or POST
31  TString fPathName; ///<! item path
32  TString fFileName; ///<! file name
33  TString fUserName; ///<! authenticated user name (if any)
34  TString fQuery; ///<! additional arguments
35 
36  void *fPostData; ///<! binary data received with post request
37  Long_t fPostDataLength; ///<! length of binary data
38 
39  TNamed *fWSHandle; ///<! web-socket handle, derived from TNamed class
40  UInt_t fWSId; ///<! websocket identifier, used in web-socket related operations
41 
42  std::condition_variable fCond; ///<! condition used to wait for processing
43 
44  TString fContentType; ///<! type of content
45  TString fRequestHeader; ///<! complete header, provided with request
46  TString fHeader; ///<! response header like ContentEncoding, Cache-Control and so on
47  TString fContent; ///<! text content (if any)
48  Int_t fZipping; ///<! indicate if content should be zipped
49 
50  void *fBinData; ///<! binary data, assigned with http call
51  Long_t fBinDataLength; ///<! length of binary data
52 
53  Bool_t fNotifyFlag; ///<! indicate that notification called
54 
55  Bool_t IsBinData() const { return fBinData && fBinDataLength > 0; }
56 
57  TString AccessHeader(TString &buf, const char *name, const char *value = 0, Bool_t doing_set = kFALSE);
58 
59  TString CountHeader(const TString &buf, Int_t number = -1111) const;
60 
61 public:
62  THttpCallArg();
63  ~THttpCallArg();
64 
65  // these methods used to set http request arguments
66 
67  /** set request method kind like GET or POST */
68  void SetMethod(const char *method) { fMethod = method; }
69 
70  /** set engine-specific top-name */
71  void SetTopName(const char *topname) { fTopName = topname; }
72 
73  void SetPathAndFileName(const char *fullpath);
74 
75  /** set request path name */
76  void SetPathName(const char *p) { fPathName = p; }
77 
78  /** set request file name */
79  void SetFileName(const char *f) { fFileName = f; }
80 
81  /** set name of authenticated user */
82  void SetUserName(const char *n) { fUserName = n; }
83 
84  /** set request query */
85  void SetQuery(const char *q) { fQuery = q; }
86 
87  void SetPostData(void *data, Long_t length, Bool_t make_copy = kFALSE);
88 
89  void SetWSHandle(TNamed *handle);
90 
92 
93  /** set web-socket id */
94  void SetWSId(UInt_t id) { fWSId = id; }
95 
96  /** get web-socket id */
97  UInt_t GetWSId() const { return fWSId; }
98 
99  /** set full set of request header */
100  void SetRequestHeader(const char *h) { fRequestHeader = h ? h : ""; }
101 
102  /** returns number of fields in request header */
103  Int_t NumRequestHeader() const { return CountHeader(fRequestHeader).Atoi(); }
104 
105  /** returns field name in request header */
106  TString GetRequestHeaderName(Int_t number) const { return CountHeader(fRequestHeader, number); }
107 
108  /** get named field from request header */
109  TString GetRequestHeader(const char *name) { return AccessHeader(fRequestHeader, name); }
110 
111  /** returns engine-specific top-name */
112  const char *GetTopName() const { return fTopName.Data(); }
113 
114  /** returns request method like GET or POST */
115  const char *GetMethod() const { return fMethod.Data(); }
116 
117  /** returns kTRUE if post method is used */
118  Bool_t IsMethod(const char *name) const { return fMethod.CompareTo(name) == 0; }
119 
120  /** returns kTRUE if post method is used */
121  Bool_t IsPostMethod() const { return IsMethod("POST"); }
122 
123  /** return pointer on posted with request data */
124  void *GetPostData() const { return fPostData; }
125 
126  /** return length of posted with request data */
128 
129  /** returns post data as TString */
130  TString GetPostDataAsString() const { return TString((const char *) GetPostData(), GetPostDataLength()); }
131 
132  /** returns path name from request URL */
133  const char *GetPathName() const { return fPathName.Data(); }
134 
135  /** returns file name from request URL */
136  const char *GetFileName() const { return fFileName.Data(); }
137 
138  /** return authenticated user name (0 - when no authentication) */
139  const char *GetUserName() const { return fUserName.Length() > 0 ? fUserName.Data() : 0; }
140 
141  /** returns request query (string after ? in request URL) */
142  const char *GetQuery() const { return fQuery.Data(); }
143 
144  // these methods used in THttpServer to set results of request processing
145 
146  /** set content type like "text/xml" or "application/json" */
147  void SetContentType(const char *typ) { fContentType = typ; }
148 
149  /** mark reply as 404 error - page/request not exists or refused */
150  void Set404() { SetContentType("_404_"); }
151 
152  /** mark reply as postponed - submitting thread will not be inform */
153  void SetPostponed() { SetContentType("_postponed_"); }
154 
155  /** indicate that http request should response with file content */
156  void SetFile(const char *filename = 0)
157  {
158  SetContentType("_file_");
159  if (filename != 0)
160  fContent = filename;
161  }
162 
163  /** set content type as XML */
164  void SetXml() { SetContentType("text/xml"); }
165 
166  /** set content type as JSON */
167  void SetJson() { SetContentType("application/json"); }
168 
169  void AddHeader(const char *name, const char *value);
170 
171  /** returns number of fields in header */
172  Int_t NumHeader() const { return CountHeader(fHeader).Atoi(); }
173 
174  /** returns field name in header */
175  TString GetHeaderName(Int_t number) const { return CountHeader(fHeader, number); }
176 
177  TString GetHeader(const char *name);
178 
179  /** Set Content-Encoding header like gzip */
180  void SetEncoding(const char *typ) { AccessHeader(fHeader, "Content-Encoding", typ, kTRUE); }
181 
182  /** Set content directly */
183  void SetContent(const char *c) { fContent = c; }
184 
186 
187  /** Set kind of content zipping
188  * 0 - none
189  * 1 - only when supported in request header
190  * 2 - if supported and content size bigger than 10K
191  * 3 - always */
192  void SetZipping(Int_t kind) { fZipping = kind; }
193 
194  /** return kind of content zipping */
195  Int_t GetZipping() const { return fZipping; }
196 
197  /** add extra http header value to the reply */
198  void SetExtraHeader(const char *name, const char *value) { AddHeader(name, value); }
199 
200  // Fill http header
201  void FillHttpHeader(TString &buf, const char *header = 0);
202 
203  // these methods used to return results of http request processing
204 
205  Bool_t IsContentType(const char *typ) const { return fContentType == typ; }
206  Bool_t Is404() const { return IsContentType("_404_"); }
207  Bool_t IsFile() const { return IsContentType("_file_"); }
208  Bool_t IsPostponed() const { return IsContentType("_postponed_"); }
209  const char *GetContentType() const { return fContentType.Data(); }
210 
211  void SetBinData(void *data, Long_t length);
212 
213  Long_t GetContentLength() const { return IsBinData() ? fBinDataLength : fContent.Length(); }
214 
215  const void *GetContent() const { return IsBinData() ? fBinData : fContent.Data(); }
216 
217  void NotifyCondition();
218 
219  virtual void HttpReplied();
220 
221  ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
222 };
223 
224 #endif
const char * GetFileName() const
returns file name from request URL
Definition: THttpCallArg.h:136
void SetZipping(Int_t kind)
Set kind of content zipping 0 - none 1 - only when supported in request header 2 - if supported and c...
Definition: THttpCallArg.h:192
void SetRequestHeader(const char *h)
set full set of request header
Definition: THttpCallArg.h:100
Bool_t IsMethod(const char *name) const
returns kTRUE if post method is used
Definition: THttpCallArg.h:118
UInt_t fWSId
! websocket identifier, used in web-socket related operations
Definition: THttpCallArg.h:40
Bool_t IsBinData() const
Definition: THttpCallArg.h:55
Bool_t IsPostMethod() const
returns kTRUE if post method is used
Definition: THttpCallArg.h:121
void SetPathName(const char *p)
set request path name
Definition: THttpCallArg.h:76
UInt_t GetWSId() const
get web-socket id
Definition: THttpCallArg.h:97
void SetWSHandle(TNamed *handle)
assign websocket handle with HTTP call
Int_t NumHeader() const
returns number of fields in header
Definition: THttpCallArg.h:172
TH1 * h
Definition: legend2.C:5
TString fQuery
! additional arguments
Definition: THttpCallArg.h:34
Basic string class.
Definition: TString.h:125
const char * GetPathName() const
returns path name from request URL
Definition: THttpCallArg.h:133
void SetQuery(const char *q)
set request query
Definition: THttpCallArg.h:85
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void * fBinData
! binary data, assigned with http call
Definition: THttpCallArg.h:50
void * GetPostData() const
return pointer on posted with request data
Definition: THttpCallArg.h:124
const char * GetQuery() const
returns request query (string after ? in request URL)
Definition: THttpCallArg.h:142
void SetContentType(const char *typ)
set content type like "text/xml" or "application/json"
Definition: THttpCallArg.h:147
void SetPostponed()
mark reply as postponed - submitting thread will not be inform
Definition: THttpCallArg.h:153
void SetUserName(const char *n)
set name of authenticated user
Definition: THttpCallArg.h:82
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)
Set content directly.
Definition: THttpCallArg.h:183
void SetPostData(void *data, Long_t length, Bool_t make_copy=kFALSE)
set data, posted with the request buffer should be allocated with malloc(length+1) call...
void SetTopName(const char *topname)
set engine-specific top-name
Definition: THttpCallArg.h:71
#define ClassDef(name, id)
Definition: Rtypes.h:320
void SetEncoding(const char *typ)
Set Content-Encoding header like gzip.
Definition: THttpCallArg.h:180
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
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:44
TString fPathName
! item path
Definition: THttpCallArg.h:31
void Set404()
mark reply as 404 error - page/request not exists or refused
Definition: THttpCallArg.h:150
TNamed * fWSHandle
! web-socket handle, derived from TNamed class
Definition: THttpCallArg.h:39
Int_t NumRequestHeader() const
returns number of fields in request header
Definition: THttpCallArg.h:103
const char * GetUserName() const
return authenticated user name (0 - when no authentication)
Definition: THttpCallArg.h:139
XFontStruct * id
Definition: TGX11.cxx:108
virtual void HttpReplied()
virtual method to inform object that http request is processed Normally condition is notified and wai...
TString fRequestHeader
! complete header, provided with request
Definition: THttpCallArg.h:45
Long_t fPostDataLength
! length of binary data
Definition: THttpCallArg.h:37
Long_t fBinDataLength
! length of binary data
Definition: THttpCallArg.h:51
TString fTopName
! top item name
Definition: THttpCallArg.h:29
void SetBinData(void *data, Long_t length)
set binary data, which will be returned as reply body
void SetJson()
set content type as JSON
Definition: THttpCallArg.h:167
Bool_t fNotifyFlag
! indicate that notification called
Definition: THttpCallArg.h:53
void SetExtraHeader(const char *name, const char *value)
add extra http header value to the reply
Definition: THttpCallArg.h:198
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...
unsigned int UInt_t
Definition: RtypesCore.h:42
Ssiz_t Length() const
Definition: TString.h:386
const char * GetTopName() const
returns engine-specific top-name
Definition: THttpCallArg.h:112
void * fPostData
! binary data received with post request
Definition: THttpCallArg.h:36
void SetFileName(const char *f)
set request file name
Definition: THttpCallArg.h:79
Bool_t IsPostponed() const
Definition: THttpCallArg.h:208
const Bool_t kFALSE
Definition: RtypesCore.h:88
const void * GetContent() const
Definition: THttpCallArg.h:215
TString fUserName
! authenticated user name (if any)
Definition: THttpCallArg.h:33
long Long_t
Definition: RtypesCore.h:50
std::condition_variable fCond
! condition used to wait for processing
Definition: THttpCallArg.h:42
Int_t fZipping
! indicate if content should be zipped
Definition: THttpCallArg.h:48
void SetWSId(UInt_t id)
set web-socket id
Definition: THttpCallArg.h:94
void SetMethod(const char *method)
set request method kind like GET or POST
Definition: THttpCallArg.h:68
void SetXml()
set content type as XML
Definition: THttpCallArg.h:164
TString fHeader
! response header like ContentEncoding, Cache-Control and so on
Definition: THttpCallArg.h:46
const char * GetContentType() const
Definition: THttpCallArg.h:209
THttpCallArg()
constructor
void NotifyCondition()
method used to notify condition which waiting when operation will complete Condition notified only if...
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:396
TNamed * TakeWSHandle()
takeout websocket handle with HTTP call can be done only once
TString GetPostDataAsString() const
returns post data as TString
Definition: THttpCallArg.h:130
Mother of all ROOT objects.
Definition: TObject.h:37
TString fContent
! text content (if any)
Definition: THttpCallArg.h:47
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:30
Bool_t IsFile() const
Definition: THttpCallArg.h:207
Bool_t Is404() const
Definition: THttpCallArg.h:206
void SetFile(const char *filename=0)
indicate that http request should response with file content
Definition: THttpCallArg.h:156
Bool_t IsContentType(const char *typ) const
Definition: THttpCallArg.h:205
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1975
TString GetRequestHeaderName(Int_t number) const
returns field name in request header
Definition: THttpCallArg.h:106
Int_t GetZipping() const
return kind of content zipping
Definition: THttpCallArg.h:195
float * q
Definition: THbookFile.cxx:87
TString GetRequestHeader(const char *name)
get named field from request header
Definition: THttpCallArg.h:109
Long_t GetPostDataLength() const
return length of posted with request data
Definition: THttpCallArg.h:127
const Bool_t kTRUE
Definition: RtypesCore.h:87
~THttpCallArg()
destructor
const Int_t n
Definition: legend1.C:16
TString fFileName
! file name
Definition: THttpCallArg.h:32
const char * GetMethod() const
returns request method like GET or POST
Definition: THttpCallArg.h:115
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
returns field name in header
Definition: THttpCallArg.h:175
const char * Data() const
Definition: TString.h:345
Long_t GetContentLength() const
Definition: THttpCallArg.h:213