Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
23 class THttpServer;
24 class THttpWSEngine;
25 class THttpWSHandler;
26 
27 class THttpCallArg : public TObject {
28 
29  friend class THttpServer;
30  friend class THttpWSEngine;
31  friend class THttpWSHandler;
32 
33 public:
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 
41 protected:
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 
64  /** Method used to modify content of web page used by web socket handler */
65  virtual void CheckWSPageContent(THttpWSHandler *) {}
66 
67 private:
68  std::shared_ptr<THttpWSEngine> fWSEngine; ///<! web-socket engine, which supplied to run created web socket
69 
70  std::string fContent; ///<! content - text or binary
71  std::string fPostData; ///<! data received with post request - text - or binary
72 
73  void AssignWSId();
74  std::shared_ptr<THttpWSEngine> TakeWSEngine();
75 
76 public:
77  explicit THttpCallArg() {} // NOLINT: not allowed to use = default because of TObject::kIsOnHeap detection, see ROOT-10300
78  virtual ~THttpCallArg();
79 
80  // these methods used to set http request arguments
81 
82  /** set request method kind like GET or POST */
83  void SetMethod(const char *method) { fMethod = method; }
84 
85  /** set engine-specific top-name */
86  void SetTopName(const char *topname) { fTopName = topname; }
87 
88  void SetPathAndFileName(const char *fullpath);
89 
90  /** set request path name */
91  void SetPathName(const char *p) { fPathName = p; }
92 
93  /** set request file name */
94  void SetFileName(const char *f) { fFileName = f; }
95 
96  /** set name of authenticated user */
97  void SetUserName(const char *n) { fUserName = n; }
98 
99  /** set request query */
100  void SetQuery(const char *q) { fQuery = q; }
101 
102  void SetPostData(void *data, Long_t length, Bool_t make_copy = kFALSE);
103 
104  void SetPostData(std::string &&data);
105 
106  /** set web-socket id */
107  void SetWSId(UInt_t id) { fWSId = id; }
108 
109  /** get web-socket id */
110  UInt_t GetWSId() const { return fWSId; }
111 
112  /** set full set of request header */
113  void SetRequestHeader(const char *h) { fRequestHeader = (h ? h : ""); }
114 
115  /** returns number of fields in request header */
116  Int_t NumRequestHeader() const { return CountHeader(fRequestHeader).Atoi(); }
117 
118  /** returns field name in request header */
119  TString GetRequestHeaderName(Int_t number) const { return CountHeader(fRequestHeader, number); }
120 
121  /** get named field from request header */
122  TString GetRequestHeader(const char *name) { return AccessHeader(fRequestHeader, name); }
123 
124  /** returns engine-specific top-name */
125  const char *GetTopName() const { return fTopName.Data(); }
126 
127  /** returns request method like GET or POST */
128  const char *GetMethod() const { return fMethod.Data(); }
129 
130  /** returns kTRUE if post method is used */
131  Bool_t IsMethod(const char *name) const { return fMethod.CompareTo(name) == 0; }
132 
133  /** returns kTRUE if post method is used */
134  Bool_t IsPostMethod() const { return IsMethod("POST"); }
135 
136  /** return pointer on posted with request data */
137  const void *GetPostData() const { return fPostData.data(); }
138 
139  /** return length of posted with request data */
140  Long_t GetPostDataLength() const { return (Long_t) fPostData.length(); }
141 
142  /** returns path name from request URL */
143  const char *GetPathName() const { return fPathName.Data(); }
144 
145  /** returns file name from request URL */
146  const char *GetFileName() const { return fFileName.Data(); }
147 
148  /** return authenticated user name (0 - when no authentication) */
149  const char *GetUserName() const { return fUserName.Length() > 0 ? fUserName.Data() : nullptr; }
150 
151  /** returns request query (string after ? in request URL) */
152  const char *GetQuery() const { return fQuery.Data(); }
153 
154  // these methods used in THttpServer to set results of request processing
155 
156  /** set content type like "text/xml" or "application/json" */
157  void SetContentType(const char *typ) { fContentType = typ; }
158 
159  /** mark reply as 404 error - page/request not exists or refused */
160  void Set404() { SetContentType("_404_"); }
161 
162  /** mark as postponed - reply will not be send to client immediately */
163  void SetPostponed() { SetContentType("_postponed_"); }
164 
165  /** indicate that http request should response with file content */
166  void SetFile(const char *filename = nullptr)
167  {
168  SetContentType("_file_");
169  if (filename)
170  fContent = filename;
171  }
172 
173  void SetText();
174  void SetTextContent(std::string &&txt);
175 
176  void SetXml();
177  void SetXmlContent(std::string &&xml);
178 
179  void SetJson();
180  void SetJsonContent(std::string &&json);
181 
182  void SetBinary();
183  void SetBinaryContent(std::string &&bin);
184 
185  void AddHeader(const char *name, const char *value);
186 
187  void AddNoCacheHeader();
188 
189  /** returns number of fields in header */
190  Int_t NumHeader() const { return CountHeader(fHeader).Atoi(); }
191 
192  /** returns field name in header */
193  TString GetHeaderName(Int_t number) const { return CountHeader(fHeader, number); }
194 
195  TString GetHeader(const char *name);
196 
197  /** Set Content-Encoding header like gzip */
198  void SetEncoding(const char *typ) { AccessHeader(fHeader, "Content-Encoding", typ, kTRUE); }
199 
200  void SetContent(const char *cont);
201  void SetContent(std::string &&cont);
202  void ReplaceAllinContent(const std::string &from, const std::string &to, bool once = false);
203 
204  Bool_t CompressWithGzip();
205 
206  void SetZipping(Int_t mode = kZipLarge) { fZipping = mode; }
207  Int_t GetZipping() const { return fZipping; }
208 
209  /** add extra http header value to the reply */
210  void SetExtraHeader(const char *name, const char *value) { AddHeader(name, value); }
211 
212  std::string FillHttpHeader(const char *header = nullptr);
213 
214  // these methods used to return results of http request processing
215 
216  Bool_t IsContentType(const char *typ) const { return fContentType == typ; }
217  const char *GetContentType() const { return fContentType.Data(); }
218 
219  Bool_t Is404() const { return IsContentType("_404_"); }
220  Bool_t IsFile() const { return IsContentType("_file_"); }
221  Bool_t IsPostponed() const { return IsContentType("_postponed_"); }
222  Bool_t IsText() const { return IsContentType("text/plain"); }
223  Bool_t IsXml() const { return IsContentType("text/xml"); }
224  Bool_t IsJson() const { return IsContentType("application/json"); }
225  Bool_t IsBinary() const { return IsContentType("application/x-binary"); }
226 
227  Long_t GetContentLength() const { return (Long_t) fContent.length(); }
228  const void *GetContent() const { return fContent.data(); }
229 
230  void NotifyCondition();
231 
232  virtual void HttpReplied();
233 
234  template <class T, typename... Args>
235  void CreateWSEngine(Args... args)
236  {
237  fWSEngine = std::make_shared<T>(args...);
238  AssignWSId();
239  }
240 
241  ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
242 };
243 
244 #endif