Logo ROOT   6.10/09
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);
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 IsPostMethod() const { return fMethod.CompareTo("POST") == 0; }
119 
120  /** return pointer on posted with request data */
121  void *GetPostData() const { return fPostData; }
122 
123  /** return length of posted with request data */
125 
126  /** returns path name from request URL */
127  const char *GetPathName() const { return fPathName.Data(); }
128 
129  /** returns file name from request URL */
130  const char *GetFileName() const { return fFileName.Data(); }
131 
132  /** return authenticated user name (0 - when no authentication) */
133  const char *GetUserName() const { return fUserName.Length() > 0 ? fUserName.Data() : 0; }
134 
135  /** returns request query (string after ? in request URL) */
136  const char *GetQuery() const { return fQuery.Data(); }
137 
138  // these methods used in THttpServer to set results of request processing
139 
140  /** set content type like "text/xml" or "application/json" */
141  void SetContentType(const char *typ) { fContentType = typ; }
142 
143  /** mark reply as 404 error - page/request not exists or refused */
144  void Set404() { SetContentType("_404_"); }
145 
146  /** mark reply as postponed - submitting thread will not be inform */
147  void SetPostponed() { SetContentType("_postponed_"); }
148 
149  /** indicate that http request should response with file content */
150  void SetFile(const char *filename = 0)
151  {
152  SetContentType("_file_");
153  if (filename != 0) fContent = filename;
154  }
155 
156  /** set content type as XML */
157  void SetXml() { SetContentType("text/xml"); }
158 
159  /** set content type as JSON */
160  void SetJson() { SetContentType("application/json"); }
161 
162  void AddHeader(const char *name, const char *value);
163 
164  /** returns number of fields in header */
165  Int_t NumHeader() const { return CountHeader(fHeader).Atoi(); }
166 
167  /** returns field name in header */
168  TString GetHeaderName(Int_t number) const { return CountHeader(fHeader, number); }
169 
170  TString GetHeader(const char *name);
171 
172  /** Set Content-Encoding header like gzip */
173  void SetEncoding(const char *typ) { AccessHeader(fHeader, "Content-Encoding", typ, kTRUE); }
174 
175  /** Set content directly */
176  void SetContent(const char *c) { fContent = c; }
177 
179 
180  /** Set kind of content zipping
181  * 0 - none
182  * 1 - only when supported in request header
183  * 2 - if supported and content size bigger than 10K
184  * 3 - always */
185  void SetZipping(Int_t kind) { fZipping = kind; }
186 
187  /** return kind of content zipping */
188  Int_t GetZipping() const { return fZipping; }
189 
190  /** add extra http header value to the reply */
191  void SetExtraHeader(const char *name, const char *value) { AddHeader(name, value); }
192 
193  // Fill http header
194  void FillHttpHeader(TString &buf, const char *header = 0);
195 
196  // these methods used to return results of http request processing
197 
198  Bool_t IsContentType(const char *typ) const { return fContentType == typ; }
199  Bool_t Is404() const { return IsContentType("_404_"); }
200  Bool_t IsFile() const { return IsContentType("_file_"); }
201  Bool_t IsPostponed() const { return IsContentType("_postponed_"); }
202  const char *GetContentType() const { return fContentType.Data(); }
203 
204  void SetBinData(void *data, Long_t length);
205 
206  Long_t GetContentLength() const { return IsBinData() ? fBinDataLength : fContent.Length(); }
207 
208  const void *GetContent() const { return IsBinData() ? fBinData : fContent.Data(); }
209 
210  void NotifyCondition();
211 
212  virtual void HttpReplied();
213 
214  ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
215 };
216 
217 #endif
const char * GetFileName() const
returns file name from request URL
Definition: THttpCallArg.h:130
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:185
void SetRequestHeader(const char *h)
set full set of request header
Definition: THttpCallArg.h:100
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:118
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:165
TH1 * h
Definition: legend2.C:5
TString fQuery
! additional arguments
Definition: THttpCallArg.h:34
Basic string class.
Definition: TString.h:129
const char * GetPathName() const
returns path name from request URL
Definition: THttpCallArg.h:127
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:121
const char * GetQuery() const
returns request query (string after ? in request URL)
Definition: THttpCallArg.h:136
void SetContentType(const char *typ)
set content type like "text/xml" or "application/json"
Definition: THttpCallArg.h:141
void SetPostponed()
mark reply as postponed - submitting thread will not be inform
Definition: THttpCallArg.h:147
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:176
void SetTopName(const char *topname)
set engine-specific top-name
Definition: THttpCallArg.h:71
#define ClassDef(name, id)
Definition: Rtypes.h:297
void SetEncoding(const char *typ)
Set Content-Encoding header like gzip.
Definition: THttpCallArg.h:173
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: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:144
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:133
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:160
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:191
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:388
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:201
const Bool_t kFALSE
Definition: RtypesCore.h:92
const void * GetContent() const
Definition: THttpCallArg.h:208
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
double f(double x)
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:157
TString fHeader
! response header like ContentEncoding, Cache-Control and so on
Definition: THttpCallArg.h:46
const char * GetContentType() const
Definition: THttpCallArg.h:202
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
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:200
Bool_t Is404() const
Definition: THttpCallArg.h:199
void SetFile(const char *filename=0)
indicate that http request should response with file content
Definition: THttpCallArg.h:150
Bool_t IsContentType(const char *typ) const
Definition: THttpCallArg.h:198
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:188
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:124
const Bool_t kTRUE
Definition: RtypesCore.h:91
~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
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:168
const char * Data() const
Definition: TString.h:347
Long_t GetContentLength() const
Definition: THttpCallArg.h:206