File indexing completed on 2025-01-18 10:12:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_THttpCallArg
0013 #define ROOT_THttpCallArg
0014
0015 #include "TObject.h"
0016
0017 #include "TString.h"
0018
0019 #include <condition_variable>
0020 #include <string>
0021 #include <memory>
0022
0023 class THttpServer;
0024 class THttpWSEngine;
0025 class THttpWSHandler;
0026
0027 class THttpCallArg : public TObject {
0028
0029 friend class THttpServer;
0030 friend class THttpWSEngine;
0031 friend class THttpWSHandler;
0032
0033 public:
0034 enum {
0035 kNoZip = 0,
0036 kZip = 1,
0037 kZipLarge = 2,
0038 kZipAlways = 3
0039 };
0040
0041 protected:
0042 TString fTopName;
0043 TString fMethod;
0044 TString fPathName;
0045 TString fFileName;
0046 TString fUserName;
0047 TString fQuery;
0048
0049 UInt_t fWSId{0};
0050
0051 std::condition_variable fCond;
0052
0053 TString fContentType;
0054 TString fRequestHeader;
0055 TString fHeader;
0056 Int_t fZipping{kNoZip};
0057
0058 Bool_t fNotifyFlag{kFALSE};
0059
0060 TString AccessHeader(TString &buf, const char *name, const char *value = nullptr, Bool_t doing_set = kFALSE);
0061
0062 TString CountHeader(const TString &buf, Int_t number = -1111) const;
0063
0064 private:
0065 std::shared_ptr<THttpWSEngine> fWSEngine;
0066
0067 std::string fContent;
0068 std::string fPostData;
0069
0070 void AssignWSId();
0071 std::shared_ptr<THttpWSEngine> TakeWSEngine();
0072
0073 public:
0074 explicit THttpCallArg() {}
0075 virtual ~THttpCallArg();
0076
0077
0078
0079
0080 void SetMethod(const char *method) { fMethod = method; }
0081
0082
0083 void SetTopName(const char *topname) { fTopName = topname; }
0084
0085 void SetPathAndFileName(const char *fullpath);
0086
0087
0088 void SetPathName(const char *p) { fPathName = p; }
0089
0090
0091 void SetFileName(const char *f) { fFileName = f; }
0092
0093
0094 void SetUserName(const char *n) { fUserName = n; }
0095
0096
0097 void SetQuery(const char *q) { fQuery = q; }
0098
0099 void SetPostData(void *data, Long_t length, Bool_t make_copy = kFALSE);
0100
0101 void SetPostData(std::string &&data);
0102
0103
0104 void SetWSId(UInt_t id) { fWSId = id; }
0105
0106
0107 UInt_t GetWSId() const { return fWSId; }
0108
0109
0110 virtual const char *GetWSKind() const { return "websocket"; }
0111
0112
0113 virtual const char *GetWSPlatform() const { return "http"; }
0114
0115
0116 void SetRequestHeader(const char *h) { fRequestHeader = (h ? h : ""); }
0117
0118
0119 Int_t NumRequestHeader() const { return CountHeader(fRequestHeader).Atoi(); }
0120
0121
0122 TString GetRequestHeaderName(Int_t number) const { return CountHeader(fRequestHeader, number); }
0123
0124
0125 TString GetRequestHeader(const char *name) { return AccessHeader(fRequestHeader, name); }
0126
0127
0128 const char *GetTopName() const { return fTopName.Data(); }
0129
0130
0131 const char *GetMethod() const { return fMethod.Data(); }
0132
0133
0134 Bool_t IsMethod(const char *name) const { return fMethod.CompareTo(name) == 0; }
0135
0136
0137 Bool_t IsPostMethod() const { return IsMethod("POST"); }
0138
0139
0140 const void *GetPostData() const { return fPostData.data(); }
0141
0142
0143 Long_t GetPostDataLength() const { return (Long_t) fPostData.length(); }
0144
0145
0146 const char *GetPathName() const { return fPathName.Data(); }
0147
0148
0149 const char *GetFileName() const { return fFileName.Data(); }
0150
0151
0152 const char *GetUserName() const { return fUserName.Length() > 0 ? fUserName.Data() : nullptr; }
0153
0154
0155 const char *GetQuery() const { return fQuery.Data(); }
0156
0157
0158
0159
0160 void SetContentType(const char *typ) { fContentType = typ; }
0161
0162
0163 void Set404() { SetContentType("_404_"); }
0164
0165
0166 virtual Bool_t CanPostpone() const { return kTRUE; }
0167
0168
0169 void SetPostponed()
0170 {
0171 if (CanPostpone())
0172 SetContentType("_postponed_");
0173 else
0174 Set404();
0175 }
0176
0177
0178 void SetFile(const char *filename = nullptr)
0179 {
0180 SetContentType("_file_");
0181 if (filename)
0182 fContent = filename;
0183 }
0184
0185 void SetText();
0186 void SetTextContent(std::string &&txt);
0187
0188 void SetXml();
0189 void SetXmlContent(std::string &&xml);
0190
0191 void SetJson();
0192 void SetJsonContent(std::string &&json);
0193
0194 void SetBinary();
0195 void SetBinaryContent(std::string &&bin);
0196
0197 void AddHeader(const char *name, const char *value);
0198
0199 void AddNoCacheHeader();
0200
0201
0202 Int_t NumHeader() const { return CountHeader(fHeader).Atoi(); }
0203
0204
0205 TString GetHeaderName(Int_t number) const { return CountHeader(fHeader, number); }
0206
0207 TString GetHeader(const char *name);
0208
0209
0210 void SetEncoding(const char *typ) { AccessHeader(fHeader, "Content-Encoding", typ, kTRUE); }
0211
0212 void SetContent(const char *cont);
0213 void SetContent(std::string &&cont);
0214 void ReplaceAllinContent(const std::string &from, const std::string &to, bool once = false);
0215
0216 Bool_t CompressWithGzip();
0217
0218 void SetZipping(Int_t mode = kZipLarge) { fZipping = mode; }
0219 Int_t GetZipping() const { return fZipping; }
0220
0221
0222 void SetExtraHeader(const char *name, const char *value) { AddHeader(name, value); }
0223
0224 std::string FillHttpHeader(const char *header = nullptr);
0225
0226
0227
0228 Bool_t IsContentType(const char *typ) const { return fContentType == typ; }
0229 const char *GetContentType() const { return fContentType.Data(); }
0230
0231 Bool_t Is404() const { return IsContentType("_404_"); }
0232 Bool_t IsFile() const { return IsContentType("_file_"); }
0233 Bool_t IsPostponed() const { return IsContentType("_postponed_"); }
0234 Bool_t IsText() const { return IsContentType("text/plain"); }
0235 Bool_t IsXml() const { return IsContentType("text/xml"); }
0236 Bool_t IsJson() const { return IsContentType("application/json"); }
0237 Bool_t IsBinary() const { return IsContentType("application/x-binary"); }
0238
0239 Long_t GetContentLength() const { return (Long_t) fContent.length(); }
0240 const void *GetContent() const { return fContent.data(); }
0241
0242 void NotifyCondition();
0243
0244 virtual void HttpReplied();
0245
0246 template <class T, typename... Args>
0247 void CreateWSEngine(Args... args)
0248 {
0249 fWSEngine = std::make_shared<T>(args...);
0250 AssignWSId();
0251 }
0252
0253 ClassDefOverride(THttpCallArg, 0)
0254 };
0255
0256 #endif