Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:10

0001 // $Id$
0002 // Author: Sergey Linev   21/05/2015
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
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,             // no zipping
0036       kZip       = 1,             // zip content if "Accept-Encoding" header contains "gzip"
0037       kZipLarge  = 2,             // zip if content larger than 10K and "Accept-Encoding" contains "gzip"
0038       kZipAlways = 3              // zip always
0039    };
0040 
0041 protected:
0042    TString fTopName;              ///<! top item name
0043    TString fMethod;               ///<! request method like GET or POST
0044    TString fPathName;             ///<! item path
0045    TString fFileName;             ///<! file name
0046    TString fUserName;             ///<! authenticated user name (if any)
0047    TString fQuery;                ///<! additional arguments
0048 
0049    UInt_t fWSId{0};               ///<! websocket identifier, used in web-socket related operations
0050 
0051    std::condition_variable fCond; ///<! condition used to wait for processing
0052 
0053    TString fContentType;          ///<! type of content
0054    TString fRequestHeader;        ///<! complete header, provided with request
0055    TString fHeader;               ///<! response header like ContentEncoding, Cache-Control and so on
0056    Int_t fZipping{kNoZip};        ///<! indicate if and when content should be compressed
0057 
0058    Bool_t fNotifyFlag{kFALSE};    ///<!  indicate that notification called
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; ///<!  web-socket engine, which supplied to run created web socket
0066 
0067    std::string  fContent;  ///<! content - text or binary
0068    std::string  fPostData; ///<! data received with post request - text - or binary
0069 
0070    void AssignWSId();
0071    std::shared_ptr<THttpWSEngine> TakeWSEngine();
0072 
0073 public:
0074    explicit THttpCallArg() {} // NOLINT: not allowed to use = default because of TObject::kIsOnHeap detection, see ROOT-10300
0075    virtual ~THttpCallArg();
0076 
0077    // these methods used to set http request arguments
0078 
0079    /** set request method kind like GET or POST */
0080    void SetMethod(const char *method) { fMethod = method; }
0081 
0082    /** set engine-specific top-name */
0083    void SetTopName(const char *topname) { fTopName = topname; }
0084 
0085    void SetPathAndFileName(const char *fullpath);
0086 
0087    /** set request path name */
0088    void SetPathName(const char *p) { fPathName = p; }
0089 
0090    /** set request file name */
0091    void SetFileName(const char *f) { fFileName = f; }
0092 
0093    /** set name of authenticated user */
0094    void SetUserName(const char *n) { fUserName = n; }
0095 
0096    /** set request query */
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    /** set web-socket id */
0104    void SetWSId(UInt_t id) { fWSId = id; }
0105 
0106    /** get web-socket id */
0107    UInt_t GetWSId() const { return fWSId; }
0108 
0109    /** provide WS kind - websocket, longpoll, rawlongpoll */
0110    virtual const char *GetWSKind() const { return "websocket"; }
0111 
0112    /** provide WS platform - http, fastcgi, cef3, qt5 */
0113    virtual const char *GetWSPlatform() const { return "http"; }
0114 
0115    /** set full set of request header */
0116    void SetRequestHeader(const char *h) { fRequestHeader = (h ? h : ""); }
0117 
0118    /** returns number of fields in request header */
0119    Int_t NumRequestHeader() const { return CountHeader(fRequestHeader).Atoi(); }
0120 
0121    /** returns field name in request header */
0122    TString GetRequestHeaderName(Int_t number) const { return CountHeader(fRequestHeader, number); }
0123 
0124    /** get named field from request header */
0125    TString GetRequestHeader(const char *name) { return AccessHeader(fRequestHeader, name); }
0126 
0127    /** returns engine-specific top-name */
0128    const char *GetTopName() const { return fTopName.Data(); }
0129 
0130    /** returns request method like GET or POST */
0131    const char *GetMethod() const { return fMethod.Data(); }
0132 
0133    /** returns kTRUE if post method is used */
0134    Bool_t IsMethod(const char *name) const { return fMethod.CompareTo(name) == 0; }
0135 
0136    /** returns kTRUE if post method is used */
0137    Bool_t IsPostMethod() const { return IsMethod("POST"); }
0138 
0139    /** return pointer on posted with request data */
0140    const void *GetPostData() const { return fPostData.data(); }
0141 
0142    /** return length of posted with request data */
0143    Long_t GetPostDataLength() const { return (Long_t) fPostData.length(); }
0144 
0145    /** returns path name from request URL */
0146    const char *GetPathName() const { return fPathName.Data(); }
0147 
0148    /** returns file name from request URL */
0149    const char *GetFileName() const { return fFileName.Data(); }
0150 
0151    /** return authenticated user name (0 - when no authentication) */
0152    const char *GetUserName() const { return fUserName.Length() > 0 ? fUserName.Data() : nullptr; }
0153 
0154    /** returns request query (string after ? in request URL) */
0155    const char *GetQuery() const { return fQuery.Data(); }
0156 
0157    // these methods used in THttpServer to set results of request processing
0158 
0159    /** set content type like "text/xml" or "application/json" */
0160    void SetContentType(const char *typ) { fContentType = typ; }
0161 
0162    /** mark reply as 404 error - page/request not exists or refused */
0163    void Set404() { SetContentType("_404_"); }
0164 
0165    /** Return true if reply can be postponed by server  */
0166    virtual Bool_t CanPostpone() const { return kTRUE; }
0167 
0168    /** mark as postponed - reply will not be send to client immediately */
0169    void SetPostponed()
0170    {
0171       if (CanPostpone())
0172          SetContentType("_postponed_");
0173       else
0174          Set404();
0175    }
0176 
0177    /** indicate that http request should response with file content */
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    /** returns number of fields in header */
0202    Int_t NumHeader() const { return CountHeader(fHeader).Atoi(); }
0203 
0204    /** returns field name in header */
0205    TString GetHeaderName(Int_t number) const { return CountHeader(fHeader, number); }
0206 
0207    TString GetHeader(const char *name);
0208 
0209    /** Set Content-Encoding header like gzip */
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    /** add extra http header value to the reply */
0222    void SetExtraHeader(const char *name, const char *value) { AddHeader(name, value); }
0223 
0224    std::string FillHttpHeader(const char *header = nullptr);
0225 
0226    // these methods used to return results of http request processing
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) // Arguments for single HTTP call
0254 };
0255 
0256 #endif