Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include <string>
21#include <memory>
22
23class THttpServer;
24class THttpWSEngine;
25class THttpWSHandler;
26
27class THttpCallArg : public TObject {
28
29 friend class THttpServer;
30 friend class THttpWSEngine;
31 friend class THttpWSHandler;
32
33public:
34 enum {
35 kNoZip = 0, // no zipping
36 kZip = 1, // zip content if "Accept-Encoding" header contains "gzip"
37 kZipLarge = 2, // zip if content larger than 10K and "Accept-Encoding" contains "gzip"
38 kZipAlways = 3 // zip always
39 };
40
41protected:
42 TString fTopName; ///<! top item name
43 TString fMethod; ///<! request method like GET or POST
44 TString fPathName; ///<! item path
45 TString fFileName; ///<! file name
46 TString fUserName; ///<! authenticated user name (if any)
47 TString fQuery; ///<! additional arguments
48
49 UInt_t fWSId{0}; ///<! websocket identifier, used in web-socket related operations
50
51 std::condition_variable fCond; ///<! condition used to wait for processing
52
53 TString fContentType; ///<! type of content
54 TString fRequestHeader; ///<! complete header, provided with request
55 TString fHeader; ///<! response header like ContentEncoding, Cache-Control and so on
56 Int_t fZipping{kNoZip}; ///<! indicate if and when content should be compressed
57
58 Bool_t fNotifyFlag{kFALSE}; ///<! indicate that notification called
59
60 TString AccessHeader(TString &buf, const char *name, const char *value = nullptr, Bool_t doing_set = kFALSE);
61
62 TString CountHeader(const TString &buf, Int_t number = -1111) const;
63
64private:
65 std::shared_ptr<THttpWSEngine> fWSEngine; ///<! web-socket engine, which supplied to run created web socket
66
67 std::string fContent; ///<! content - text or binary
68 std::string fPostData; ///<! data received with post request - text - or binary
69
70 void AssignWSId();
71 std::shared_ptr<THttpWSEngine> TakeWSEngine();
72
73public:
74 explicit THttpCallArg() {} // NOLINT: not allowed to use = default because of TObject::kIsOnHeap detection, see ROOT-10300
75 virtual ~THttpCallArg();
76
77 // these methods used to set http request arguments
78
79 /** set request method kind like GET or POST */
80 void SetMethod(const char *method) { fMethod = method; }
81
82 /** set engine-specific top-name */
83 void SetTopName(const char *topname) { fTopName = topname; }
84
85 void SetPathAndFileName(const char *fullpath);
86
87 /** set request path name */
88 void SetPathName(const char *p) { fPathName = p; }
89
90 /** set request file name */
91 void SetFileName(const char *f) { fFileName = f; }
92
93 /** set name of authenticated user */
94 void SetUserName(const char *n) { fUserName = n; }
95
96 /** set request query */
97 void SetQuery(const char *q) { fQuery = q; }
98
99 void SetPostData(void *data, Long_t length, Bool_t make_copy = kFALSE);
100
101 void SetPostData(std::string &&data);
102
103 /** set web-socket id */
104 void SetWSId(UInt_t id) { fWSId = id; }
105
106 /** get web-socket id */
107 UInt_t GetWSId() const { return fWSId; }
108
109 /** provide WS kind - websocket, longpoll, rawlongpoll */
110 virtual const char *GetWSKind() const { return "websocket"; }
111
112 /** provide WS platform - http, fastcgi, cef3, qt5 */
113 virtual const char *GetWSPlatform() const { return "http"; }
114
115 /** set full set of request header */
116 void SetRequestHeader(const char *h) { fRequestHeader = (h ? h : ""); }
117
118 /** returns number of fields in request header */
120
121 /** returns field name in request header */
123
124 /** get named field from request header */
126
127 /** returns engine-specific top-name */
128 const char *GetTopName() const { return fTopName.Data(); }
129
130 /** returns request method like GET or POST */
131 const char *GetMethod() const { return fMethod.Data(); }
132
133 /** returns kTRUE if post method is used */
134 Bool_t IsMethod(const char *name) const { return fMethod.CompareTo(name) == 0; }
135
136 /** returns kTRUE if post method is used */
137 Bool_t IsPostMethod() const { return IsMethod("POST"); }
138
139 /** return pointer on posted with request data */
140 const void *GetPostData() const { return fPostData.data(); }
141
142 /** return length of posted with request data */
143 Long_t GetPostDataLength() const { return (Long_t) fPostData.length(); }
144
145 /** returns path name from request URL */
146 const char *GetPathName() const { return fPathName.Data(); }
147
148 /** returns file name from request URL */
149 const char *GetFileName() const { return fFileName.Data(); }
150
151 /** return authenticated user name (0 - when no authentication) */
152 const char *GetUserName() const { return fUserName.Length() > 0 ? fUserName.Data() : nullptr; }
153
154 /** returns request query (string after ? in request URL) */
155 const char *GetQuery() const { return fQuery.Data(); }
156
157 // these methods used in THttpServer to set results of request processing
158
159 /** set content type like "text/xml" or "application/json" */
160 void SetContentType(const char *typ) { fContentType = typ; }
161
162 /** mark reply as 404 error - page/request not exists or refused */
163 void Set404() { SetContentType("_404_"); }
164
165 /** Return true if reply can be postponed by server */
166 virtual Bool_t CanPostpone() const { return kTRUE; }
167
168 /** mark as postponed - reply will not be send to client immediately */
170 {
171 if (CanPostpone())
172 SetContentType("_postponed_");
173 else
174 Set404();
175 }
176
177 /** indicate that http request should response with file content */
178 void SetFile(const char *filename = nullptr)
179 {
180 SetContentType("_file_");
181 if (filename)
182 fContent = filename;
183 }
184
185 void SetText();
186 void SetTextContent(std::string &&txt);
187
188 void SetXml();
189 void SetXmlContent(std::string &&xml);
190
191 void SetJson();
192 void SetJsonContent(std::string &&json);
193
194 void SetBinary();
195 void SetBinaryContent(std::string &&bin);
196
197 void AddHeader(const char *name, const char *value);
198
199 void AddNoCacheHeader();
200
201 /** returns number of fields in header */
202 Int_t NumHeader() const { return CountHeader(fHeader).Atoi(); }
203
204 /** returns field name in header */
205 TString GetHeaderName(Int_t number) const { return CountHeader(fHeader, number); }
206
207 TString GetHeader(const char *name);
208
209 /** Set Content-Encoding header like gzip */
210 void SetEncoding(const char *typ) { AccessHeader(fHeader, "Content-Encoding", typ, kTRUE); }
211
212 void SetContent(const char *cont);
213 void SetContent(std::string &&cont);
214 void ReplaceAllinContent(const std::string &from, const std::string &to, bool once = false);
215
217
218 void SetZipping(Int_t mode = kZipLarge) { fZipping = mode; }
219 Int_t GetZipping() const { return fZipping; }
220
221 /** add extra http header value to the reply */
222 void SetExtraHeader(const char *name, const char *value) { AddHeader(name, value); }
223
224 std::string FillHttpHeader(const char *header = nullptr);
225
226 // these methods used to return results of http request processing
227
228 Bool_t IsContentType(const char *typ) const { return fContentType == typ; }
229 const char *GetContentType() const { return fContentType.Data(); }
230
231 Bool_t Is404() const { return IsContentType("_404_"); }
232 Bool_t IsFile() const { return IsContentType("_file_"); }
233 Bool_t IsPostponed() const { return IsContentType("_postponed_"); }
234 Bool_t IsText() const { return IsContentType("text/plain"); }
235 Bool_t IsXml() const { return IsContentType("text/xml"); }
236 Bool_t IsJson() const { return IsContentType("application/json"); }
237 Bool_t IsBinary() const { return IsContentType("application/x-binary"); }
238
239 Long_t GetContentLength() const { return (Long_t) fContent.length(); }
240 const void *GetContent() const { return fContent.data(); }
241
242 void NotifyCondition();
243
244 virtual void HttpReplied();
245
246 template <class T, typename... Args>
247 void CreateWSEngine(Args... args)
248 {
249 fWSEngine = std::make_shared<T>(args...);
250 AssignWSId();
251 }
252
253 ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
254};
255
256#endif
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:101
long Long_t
Definition RtypesCore.h:54
bool Bool_t
Definition RtypesCore.h:63
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassDef(name, id)
Definition Rtypes.h:325
XFontStruct * id
Definition TGX11.cxx:109
char name[80]
Definition TGX11.cxx:110
float * q
std::string fPostData
! data received with post request - text - or binary
Bool_t fNotifyFlag
! indicate that notification called
Bool_t CompressWithGzip()
compress reply data with gzip compression
void Set404()
mark reply as 404 error - page/request not exists or refused
Bool_t IsXml() const
void SetRequestHeader(const char *h)
set full set of request header
void SetJson()
Set content type as "application/json".
UInt_t GetWSId() const
get web-socket id
void SetFileName(const char *f)
set request file name
TString GetHeader(const char *name)
return specified header
void SetFile(const char *filename=nullptr)
indicate that http request should response with file content
std::condition_variable fCond
! condition used to wait for processing
virtual Bool_t CanPostpone() const
Return true if reply can be postponed by server
TString fTopName
! top item name
TString GetRequestHeader(const char *name)
get named field from request header
void SetPostData(void *data, Long_t length, Bool_t make_copy=kFALSE)
const char * GetUserName() const
return authenticated user name (0 - when no authentication)
std::shared_ptr< THttpWSEngine > TakeWSEngine()
takeout websocket handle with HTTP call can be done only once
void AddHeader(const char *name, const char *value)
Set name: value pair to reply header Content-Type field handled separately - one should use SetConten...
void SetText()
Set content type as "text/plain".
void SetTextContent(std::string &&txt)
Set content type as "text/plain" and also assigns content After method call argument.
TString GetHeaderName(Int_t number) const
returns field name in header
virtual void HttpReplied()
virtual method to inform object that http request is processed Normally condition is notified and wai...
TString fUserName
! authenticated user name (if any)
void SetUserName(const char *n)
set name of authenticated user
void SetPathName(const char *p)
set request path name
const char * GetTopName() const
returns engine-specific top-name
void ReplaceAllinContent(const std::string &from, const std::string &to, bool once=false)
Replace all occurrences of.
Bool_t IsPostMethod() const
returns kTRUE if post method is used
const void * GetPostData() const
return pointer on posted with request data
virtual const char * GetWSKind() const
provide WS kind - websocket, longpoll, rawlongpoll
void SetTopName(const char *topname)
set engine-specific top-name
const char * GetQuery() const
returns request query (string after ? in request URL)
TString fPathName
! item path
Bool_t IsBinary() const
std::shared_ptr< THttpWSEngine > fWSEngine
! web-socket engine, which supplied to run created web socket
void NotifyCondition()
method used to notify condition which waiting when operation will complete Condition notified only if...
void CreateWSEngine(Args... args)
Bool_t IsText() const
Int_t NumRequestHeader() const
returns number of fields in request header
void SetPathAndFileName(const char *fullpath)
set complete path of requested http element For instance, it could be "/folder/subfolder/get....
TString fContentType
! type of content
Long_t GetContentLength() const
Int_t GetZipping() const
Bool_t Is404() const
TString fQuery
! additional arguments
void SetMethod(const char *method)
set request method kind like GET or POST
virtual ~THttpCallArg()
destructor
TString CountHeader(const TString &buf, Int_t number=-1111) const
method used to counter number of headers or returns name of specified header
const void * GetContent() const
void SetPostponed()
mark as postponed - reply will not be send to client immediately
void SetBinary()
Set content type as "application/x-binary".
void AddNoCacheHeader()
Set CacheControl http header to disable browser caching.
TString AccessHeader(TString &buf, const char *name, const char *value=nullptr, Bool_t doing_set=kFALSE)
method used to get or set http header in the string buffer Header has following format: field1 : valu...
Bool_t IsContentType(const char *typ) const
void SetXml()
Set content type as "text/xml".
void SetExtraHeader(const char *name, const char *value)
add extra http header value to the reply
Int_t fZipping
! indicate if and when content should be compressed
void SetContent(const char *cont)
Set content as text.
Bool_t IsPostponed() const
void SetWSId(UInt_t id)
set web-socket id
TString fFileName
! file name
UInt_t fWSId
! websocket identifier, used in web-socket related operations
void SetQuery(const char *q)
set request query
TString GetRequestHeaderName(Int_t number) const
returns field name in request header
Int_t NumHeader() const
returns number of fields in header
std::string fContent
! content - text or binary
void SetBinaryContent(std::string &&bin)
Set content type as "application/x-binary" and also assigns content After method call argument.
Bool_t IsJson() const
void SetContentType(const char *typ)
set content type like "text/xml" or "application/json"
virtual const char * GetWSPlatform() const
provide WS platform - http, fastcgi, cef3, qt5
const char * GetPathName() const
returns path name from request URL
TString fMethod
! request method like GET or POST
void SetEncoding(const char *typ)
Set Content-Encoding header like gzip.
Long_t GetPostDataLength() const
return length of posted with request data
void SetJsonContent(std::string &&json)
Set content type as "application/json" and also assigns content After method call argument.
const char * GetMethod() const
returns request method like GET or POST
Bool_t IsMethod(const char *name) const
returns kTRUE if post method is used
const char * GetContentType() const
const char * GetFileName() const
returns file name from request URL
void AssignWSId()
Assign websocket identifier from the engine.
void SetZipping(Int_t mode=kZipLarge)
void SetXmlContent(std::string &&xml)
Set content type as "text/xml" and also assigns content After method call argument.
std::string FillHttpHeader(const char *header=nullptr)
Fills HTTP header, which can be send at the beggining of reply on the http request.
TString fRequestHeader
! complete header, provided with request
TString fHeader
! response header like ContentEncoding, Cache-Control and so on
Bool_t IsFile() const
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition TString.cxx:442
Int_t Atoi() const
Return integer value of string.
Definition TString.cxx:1946
const char * Data() const
Definition TString.h:369
const Int_t n
Definition legend1.C:16