ROOT logo
// $Id$
// Author: Sergey Linev   21/05/2015

#ifndef ROOT_THttpCallArg
#define ROOT_THttpCallArg

#ifndef ROOT_TObject
#include "TObject.h"
#endif

#ifndef ROOT_TString
#include "TString.h"
#endif

#ifndef ROOT_TCondition
#include "TCondition.h"
#endif


class THttpServer;

class THttpCallArg : public TObject {

protected:
   friend class THttpServer;

   TString fTopName;            //! top item name
   TString fMethod;             //! request method like GET or POST
   TString fPathName;           //! item path
   TString fFileName;           //! file name
   TString fUserName;           //! authenticated user name (if any)
   TString fQuery;              //! additional arguments

   void *fPostData;              //! binary data received with post request
   Long_t fPostDataLength;       //! length of binary data

   TCondition fCond;            //! condition used to wait for processing

   TString fContentType;        //! type of content
   TString fRequestHeader;      //! complete header, provided with request
   TString fHeader;             //! response header like ContentEncoding, Cache-Control and so on
   TString fContent;            //! text content (if any)
   Int_t   fZipping;            //! indicate if content should be zipped

   void *fBinData;              //! binary data, assigned with http call
   Long_t fBinDataLength;       //! length of binary data

   Bool_t IsBinData() const
   {
      return fBinData && fBinDataLength > 0;
   }

   TString AccessHeader(TString& buf, const char* name, const char* value = 0, Bool_t doing_set = kFALSE);

   TString CountHeader(const TString& buf, Int_t number = -1111) const;

public:

   THttpCallArg();
   ~THttpCallArg();

   // these methods used to set http request arguments

   void SetMethod(const char *method)
   {
      // set request method kind like GET or POST

      fMethod = method;
   }

   void SetTopName(const char *topname)
   {
      // set engine-specific top-name

      fTopName = topname;
   }

   void SetPathAndFileName(const char *fullpath);

   void SetPathName(const char *p)
   {
      // set request path name

      fPathName = p;
   }

   void SetFileName(const char *f)
   {
      // set request file name

      fFileName = f;
   }

   void SetUserName(const char *n)
   {
      // set name of authenticated user

      fUserName = n;
   }

   void SetQuery(const char *q)
   {
      // set request query

      fQuery = q;
   }

   void SetPostData(void *data, Long_t length);

   void SetRequestHeader(const char* h)
   {
      // set full set of request header

      fRequestHeader = h ? h : "";
   }

   Int_t NumRequestHeader() const
   {
      // returns number of fields in request header

      return CountHeader(fRequestHeader).Atoi();
   }

   TString GetRequestHeaderName(Int_t number) const
   {
      // returns field name in request header

      return CountHeader(fRequestHeader, number);
   }

   TString GetRequestHeader(const char* name)
   {
      // get named field from request header

      return AccessHeader(fRequestHeader, name);
   }

   const char *GetTopName() const
   {
      // returns engine-specific top-name

      return fTopName.Data();
   }

   const char *GetMethod() const
   {
      // returns request method like GET or POST

      return fMethod.Data();
   }

   Bool_t IsPostMethod() const
   {
      // returns kTRUE if post method is used

      return fMethod.CompareTo("POST")==0;
   }

   void* GetPostData() const
   {
      // return pointer on posted with request data

      return fPostData;
   }

   Long_t GetPostDataLength() const
   {
      // return length of posted with request data

      return fPostDataLength;
   }

   const char *GetPathName() const
   {
      // returns path name from request URL

      return fPathName.Data();
   }

   const char *GetFileName() const
   {
      // returns file name from request URL

      return fFileName.Data();
   }

   const char *GetUserName() const
   {
      // return authenticated user name (0 - when no authentication)

      return fUserName.Length() > 0 ? fUserName.Data() : 0;
   }

   const char *GetQuery() const
   {
      // returns request query (string after ? in request URL)

      return fQuery.Data();
   }

   // these methods used in THttpServer to set results of request processing

   void SetContentType(const char *typ)
   {
      // set content type like "text/xml" or "application/json"

      fContentType = typ;
   }

   void Set404()
   {
      // mark reply as 404 error - page/request not exists

      SetContentType("_404_");
   }

   void SetFile(const char *filename = 0)
   {
      // indicate that http request should response with file content

      SetContentType("_file_");
      if (filename != 0) fContent = filename;
   }

   void SetXml()
   {
      // set content type as JSON

      SetContentType("text/xml");
   }

   void SetJson()
   {
      // set content type as JSON

      SetContentType("application/json");
   }

   void AddHeader(const char *name, const char *value);

   Int_t NumHeader() const
   {
      // returns number of fields in header

      return CountHeader(fHeader).Atoi();
   }

   TString GetHeaderName(Int_t number) const
   {
      // returns field name in header

      return CountHeader(fHeader, number);
   }

   TString GetHeader(const char* name);

   void SetEncoding(const char *typ)
   {
      // Set Content-Encoding header like gzip

      AccessHeader(fHeader, "Content-Encoding", typ, kTRUE);
   }

   void SetContent(const char *c)
   {
      // Set content directly

      fContent = c;
   }

   Bool_t CompressWithGzip();

   void SetZipping(Int_t kind)
   {
      // Set kind of content zipping
      // 0 - none
      // 1 - only when supported in request header
      // 2 - if supported and content size bigger than 10K
      // 3 - always

      fZipping = kind;
   }

   Int_t GetZipping() const
   {
      // return kind of content zipping

      return fZipping;
   }

   void SetExtraHeader(const char *name, const char *value)
   {
      AddHeader(name, value);
   }

   // Fill http header
   void FillHttpHeader(TString &buf, const char *header = 0);

   // these methods used to return results of http request processing

   Bool_t IsContentType(const char *typ) const
   {
      return fContentType == typ;
   }

   Bool_t Is404() const
   {
      return IsContentType("_404_");
   }

   Bool_t IsFile() const
   {
      return IsContentType("_file_");
   }

   const char *GetContentType() const
   {
      return fContentType.Data();
   }

   void SetBinData(void *data, Long_t length);

   Long_t GetContentLength() const
   {
      return IsBinData() ? fBinDataLength : fContent.Length();
   }

   const void *GetContent() const
   {
      return IsBinData() ? fBinData : fContent.Data();
   }

   ClassDef(THttpCallArg, 0) // Arguments for single HTTP call
};

#endif
 THttpCallArg.h:1
 THttpCallArg.h:2
 THttpCallArg.h:3
 THttpCallArg.h:4
 THttpCallArg.h:5
 THttpCallArg.h:6
 THttpCallArg.h:7
 THttpCallArg.h:8
 THttpCallArg.h:9
 THttpCallArg.h:10
 THttpCallArg.h:11
 THttpCallArg.h:12
 THttpCallArg.h:13
 THttpCallArg.h:14
 THttpCallArg.h:15
 THttpCallArg.h:16
 THttpCallArg.h:17
 THttpCallArg.h:18
 THttpCallArg.h:19
 THttpCallArg.h:20
 THttpCallArg.h:21
 THttpCallArg.h:22
 THttpCallArg.h:23
 THttpCallArg.h:24
 THttpCallArg.h:25
 THttpCallArg.h:26
 THttpCallArg.h:27
 THttpCallArg.h:28
 THttpCallArg.h:29
 THttpCallArg.h:30
 THttpCallArg.h:31
 THttpCallArg.h:32
 THttpCallArg.h:33
 THttpCallArg.h:34
 THttpCallArg.h:35
 THttpCallArg.h:36
 THttpCallArg.h:37
 THttpCallArg.h:38
 THttpCallArg.h:39
 THttpCallArg.h:40
 THttpCallArg.h:41
 THttpCallArg.h:42
 THttpCallArg.h:43
 THttpCallArg.h:44
 THttpCallArg.h:45
 THttpCallArg.h:46
 THttpCallArg.h:47
 THttpCallArg.h:48
 THttpCallArg.h:49
 THttpCallArg.h:50
 THttpCallArg.h:51
 THttpCallArg.h:52
 THttpCallArg.h:53
 THttpCallArg.h:54
 THttpCallArg.h:55
 THttpCallArg.h:56
 THttpCallArg.h:57
 THttpCallArg.h:58
 THttpCallArg.h:59
 THttpCallArg.h:60
 THttpCallArg.h:61
 THttpCallArg.h:62
 THttpCallArg.h:63
 THttpCallArg.h:64
 THttpCallArg.h:65
 THttpCallArg.h:66
 THttpCallArg.h:67
 THttpCallArg.h:68
 THttpCallArg.h:69
 THttpCallArg.h:70
 THttpCallArg.h:71
 THttpCallArg.h:72
 THttpCallArg.h:73
 THttpCallArg.h:74
 THttpCallArg.h:75
 THttpCallArg.h:76
 THttpCallArg.h:77
 THttpCallArg.h:78
 THttpCallArg.h:79
 THttpCallArg.h:80
 THttpCallArg.h:81
 THttpCallArg.h:82
 THttpCallArg.h:83
 THttpCallArg.h:84
 THttpCallArg.h:85
 THttpCallArg.h:86
 THttpCallArg.h:87
 THttpCallArg.h:88
 THttpCallArg.h:89
 THttpCallArg.h:90
 THttpCallArg.h:91
 THttpCallArg.h:92
 THttpCallArg.h:93
 THttpCallArg.h:94
 THttpCallArg.h:95
 THttpCallArg.h:96
 THttpCallArg.h:97
 THttpCallArg.h:98
 THttpCallArg.h:99
 THttpCallArg.h:100
 THttpCallArg.h:101
 THttpCallArg.h:102
 THttpCallArg.h:103
 THttpCallArg.h:104
 THttpCallArg.h:105
 THttpCallArg.h:106
 THttpCallArg.h:107
 THttpCallArg.h:108
 THttpCallArg.h:109
 THttpCallArg.h:110
 THttpCallArg.h:111
 THttpCallArg.h:112
 THttpCallArg.h:113
 THttpCallArg.h:114
 THttpCallArg.h:115
 THttpCallArg.h:116
 THttpCallArg.h:117
 THttpCallArg.h:118
 THttpCallArg.h:119
 THttpCallArg.h:120
 THttpCallArg.h:121
 THttpCallArg.h:122
 THttpCallArg.h:123
 THttpCallArg.h:124
 THttpCallArg.h:125
 THttpCallArg.h:126
 THttpCallArg.h:127
 THttpCallArg.h:128
 THttpCallArg.h:129
 THttpCallArg.h:130
 THttpCallArg.h:131
 THttpCallArg.h:132
 THttpCallArg.h:133
 THttpCallArg.h:134
 THttpCallArg.h:135
 THttpCallArg.h:136
 THttpCallArg.h:137
 THttpCallArg.h:138
 THttpCallArg.h:139
 THttpCallArg.h:140
 THttpCallArg.h:141
 THttpCallArg.h:142
 THttpCallArg.h:143
 THttpCallArg.h:144
 THttpCallArg.h:145
 THttpCallArg.h:146
 THttpCallArg.h:147
 THttpCallArg.h:148
 THttpCallArg.h:149
 THttpCallArg.h:150
 THttpCallArg.h:151
 THttpCallArg.h:152
 THttpCallArg.h:153
 THttpCallArg.h:154
 THttpCallArg.h:155
 THttpCallArg.h:156
 THttpCallArg.h:157
 THttpCallArg.h:158
 THttpCallArg.h:159
 THttpCallArg.h:160
 THttpCallArg.h:161
 THttpCallArg.h:162
 THttpCallArg.h:163
 THttpCallArg.h:164
 THttpCallArg.h:165
 THttpCallArg.h:166
 THttpCallArg.h:167
 THttpCallArg.h:168
 THttpCallArg.h:169
 THttpCallArg.h:170
 THttpCallArg.h:171
 THttpCallArg.h:172
 THttpCallArg.h:173
 THttpCallArg.h:174
 THttpCallArg.h:175
 THttpCallArg.h:176
 THttpCallArg.h:177
 THttpCallArg.h:178
 THttpCallArg.h:179
 THttpCallArg.h:180
 THttpCallArg.h:181
 THttpCallArg.h:182
 THttpCallArg.h:183
 THttpCallArg.h:184
 THttpCallArg.h:185
 THttpCallArg.h:186
 THttpCallArg.h:187
 THttpCallArg.h:188
 THttpCallArg.h:189
 THttpCallArg.h:190
 THttpCallArg.h:191
 THttpCallArg.h:192
 THttpCallArg.h:193
 THttpCallArg.h:194
 THttpCallArg.h:195
 THttpCallArg.h:196
 THttpCallArg.h:197
 THttpCallArg.h:198
 THttpCallArg.h:199
 THttpCallArg.h:200
 THttpCallArg.h:201
 THttpCallArg.h:202
 THttpCallArg.h:203
 THttpCallArg.h:204
 THttpCallArg.h:205
 THttpCallArg.h:206
 THttpCallArg.h:207
 THttpCallArg.h:208
 THttpCallArg.h:209
 THttpCallArg.h:210
 THttpCallArg.h:211
 THttpCallArg.h:212
 THttpCallArg.h:213
 THttpCallArg.h:214
 THttpCallArg.h:215
 THttpCallArg.h:216
 THttpCallArg.h:217
 THttpCallArg.h:218
 THttpCallArg.h:219
 THttpCallArg.h:220
 THttpCallArg.h:221
 THttpCallArg.h:222
 THttpCallArg.h:223
 THttpCallArg.h:224
 THttpCallArg.h:225
 THttpCallArg.h:226
 THttpCallArg.h:227
 THttpCallArg.h:228
 THttpCallArg.h:229
 THttpCallArg.h:230
 THttpCallArg.h:231
 THttpCallArg.h:232
 THttpCallArg.h:233
 THttpCallArg.h:234
 THttpCallArg.h:235
 THttpCallArg.h:236
 THttpCallArg.h:237
 THttpCallArg.h:238
 THttpCallArg.h:239
 THttpCallArg.h:240
 THttpCallArg.h:241
 THttpCallArg.h:242
 THttpCallArg.h:243
 THttpCallArg.h:244
 THttpCallArg.h:245
 THttpCallArg.h:246
 THttpCallArg.h:247
 THttpCallArg.h:248
 THttpCallArg.h:249
 THttpCallArg.h:250
 THttpCallArg.h:251
 THttpCallArg.h:252
 THttpCallArg.h:253
 THttpCallArg.h:254
 THttpCallArg.h:255
 THttpCallArg.h:256
 THttpCallArg.h:257
 THttpCallArg.h:258
 THttpCallArg.h:259
 THttpCallArg.h:260
 THttpCallArg.h:261
 THttpCallArg.h:262
 THttpCallArg.h:263
 THttpCallArg.h:264
 THttpCallArg.h:265
 THttpCallArg.h:266
 THttpCallArg.h:267
 THttpCallArg.h:268
 THttpCallArg.h:269
 THttpCallArg.h:270
 THttpCallArg.h:271
 THttpCallArg.h:272
 THttpCallArg.h:273
 THttpCallArg.h:274
 THttpCallArg.h:275
 THttpCallArg.h:276
 THttpCallArg.h:277
 THttpCallArg.h:278
 THttpCallArg.h:279
 THttpCallArg.h:280
 THttpCallArg.h:281
 THttpCallArg.h:282
 THttpCallArg.h:283
 THttpCallArg.h:284
 THttpCallArg.h:285
 THttpCallArg.h:286
 THttpCallArg.h:287
 THttpCallArg.h:288
 THttpCallArg.h:289
 THttpCallArg.h:290
 THttpCallArg.h:291
 THttpCallArg.h:292
 THttpCallArg.h:293
 THttpCallArg.h:294
 THttpCallArg.h:295
 THttpCallArg.h:296
 THttpCallArg.h:297
 THttpCallArg.h:298
 THttpCallArg.h:299
 THttpCallArg.h:300
 THttpCallArg.h:301
 THttpCallArg.h:302
 THttpCallArg.h:303
 THttpCallArg.h:304
 THttpCallArg.h:305
 THttpCallArg.h:306
 THttpCallArg.h:307
 THttpCallArg.h:308
 THttpCallArg.h:309
 THttpCallArg.h:310
 THttpCallArg.h:311
 THttpCallArg.h:312
 THttpCallArg.h:313
 THttpCallArg.h:314
 THttpCallArg.h:315
 THttpCallArg.h:316
 THttpCallArg.h:317
 THttpCallArg.h:318
 THttpCallArg.h:319
 THttpCallArg.h:320
 THttpCallArg.h:321
 THttpCallArg.h:322
 THttpCallArg.h:323
 THttpCallArg.h:324
 THttpCallArg.h:325
 THttpCallArg.h:326
 THttpCallArg.h:327
 THttpCallArg.h:328
 THttpCallArg.h:329
 THttpCallArg.h:330
 THttpCallArg.h:331
 THttpCallArg.h:332
 THttpCallArg.h:333
 THttpCallArg.h:334
 THttpCallArg.h:335
 THttpCallArg.h:336