SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
http.h
Go to the documentation of this file.
1 #include "bd.h"
2 
4 // Http-General
5 class THttp{
6 public:
7  // general strings
8  static const TStr HttpStr;
9  static const TStr SlashStr;
10  static const TStr ColonStr;
11  // field names
12  static const TStr ContTypeFldNm;
13  static const TStr ContLenFldNm;
14  static const TStr HostFldNm;
15  static const TStr AcceptRangesFldNm;
16  static const TStr CacheCtrlFldNm;
17  static const TStr AcceptFldNm;
18  static const TStr SrvFldNm;
19  static const TStr ConnFldNm;
20  static const TStr FetchIdFldNm;
21  static const TStr LocFldNm;
22  static const TStr SetCookieFldNm;
23  static const TStr CookieFldNm;
24  // content-type field-values
25  static const TStr TextFldVal;
26  static const TStr TextPlainFldVal;
27  static const TStr TextHtmlFldVal;
28  static const TStr TextXmlFldVal;
29  static const TStr TextWmlFldVal;
30  static const TStr TextJavaScriptFldVal;
31  static const TStr TextCssFldVal;
32  static const TStr ImageIcoFldVal;
33  static const TStr ImagePngFldVal;
34  static const TStr ImageGifFldVal;
35  static const TStr ImageJpgFldVal;
36  static const TStr AppOctetFldVal;
37  static const TStr AppSoapXmlFldVal;
38  static const TStr AppW3FormFldVal;
39  static const TStr AppJSonFldVal;
40  static const TStr ConnKeepAliveFldVal;
41  // file extensions
42  static bool IsHtmlFExt(const TStr& FExt);
43  static bool IsGifFExt(const TStr& FExt);
44 
45  // port number
46  static const int DfPortN;
47  // status codes
48  static const int OkStatusCd;
49  static const int RedirStatusCd;
50  static const int BadRqStatusCd;
51  static const int ErrStatusCd;
52  static const int ErrNotFoundStatusCd;
53  static const int InternalErrStatusCd;
54  static TStr GetReasonPhrase(const int& StatusCd);
55  // method names
56  static const TStr GetMethodNm;
57  static const TStr HeadMethodNm;
58  static const TStr PostMethodNm;
59  static const TStr UndefMethodNm;
60 };
61 
63 // Http-Request
64 typedef enum {
66 
68 private:
69  bool Ok;
70  bool CompleteP;
71  int MajorVerN, MinorVerN;
72  THttpRqMethod Method;
73  PUrl Url;
74  TStrStrH FldNmToValH;
75  PUrlEnv UrlEnv;
76  TStr HdStr;
77  TMem BodyMem;
78  void ParseSearch(const TStr& SearchStr);
79  void ParseHttpRq(const PSIn& SIn);
80 public:
81  THttpRq(const PSIn& SIn);
82  static PHttpRq New(const PSIn& SIn){
83  return PHttpRq(new THttpRq(SIn));}
84  THttpRq(
85  const THttpRqMethod& _Method, const PUrl& _Url,
86  const TStr& ContTypeFldVal, const TMem& _BodyMem, const int& FetchId=-1);
87  static PHttpRq New(
88  const THttpRqMethod& Method, const PUrl& Url,
89  const TStr& ContTypeFldVal, const TMem& BodyMem){
90  return PHttpRq(new THttpRq(Method, Url, ContTypeFldVal, BodyMem));}
91  static PHttpRq New(const PUrl& Url, const int& FetchId=-1){
92  return PHttpRq(new THttpRq(hrmGet, Url, "", TMem(), FetchId));}
95  static PHttpRq Load(TSIn&){Fail; return NULL;}
96  void Save(TSOut&){Fail;}
97 
98  THttpRq& operator=(const THttpRq&){Fail; return *this;}
99 
100  // component-retrieval
101  bool IsOk() const {return Ok;}
102  bool IsComplete() const {return CompleteP;}
103  THttpRqMethod GetMethod() const {return Method;}
104  const TStr& GetMethodNm() const;
105  PUrl GetUrl() const {return Url;}
106  PUrlEnv GetUrlEnv() const {return UrlEnv;}
107  bool IsFldNm(const TStr& FldNm) const;
108  TStr GetFldVal(const TStr& FldNm) const;
109  bool IsFldVal(const TStr& FldNm, const TStr& FldVal) const;
110  void AddFldVal(const TStr& FldNm, const TStr& FldVal);
111  const TStrStrH& GetFldValH() const;
112 
113  // header & body
114  TStr GetHdStr() const {return HdStr;}
115  bool IsBody() const { return !BodyMem.Empty(); }
116  int GetBodyLen() const { return BodyMem.Len(); }
117  TStr GetBodyAsStr() const { return BodyMem.GetAsStr(' ');}
118  PSIn GetBodyAsSIn() const { return TMemIn::New(BodyMem); }
119  void GetBodyAsMem(TMem& Mem) const {Mem.Clr(); Mem += BodyMem;}
120  void GetAsMem(TMem& Mem) const {Mem.Clr(); Mem+=HdStr; Mem+=BodyMem;}
121 
122  // content-type
123  bool IsContType(const TStr& ContTypeStr) const {
124  return GetFldVal(THttp::ContTypeFldNm).IsStrIn(ContTypeStr);}
125  bool IsContLen(int& ContLen) const {
126  return GetFldVal(THttp::ContLenFldNm).IsInt(ContLen);}
127 
128  // string representation
129  TStr GetStr() const;
130 };
131 
133 // Http-Response
135 private:
136  bool Ok;
137  int MajorVerN, MinorVerN;
138  int StatusCd;
139  TStr ReasonPhrase;
140  TStrStrVH FldNmToValVH;
141  TStr HdStr;
142  TMem BodyMem;
143  void AddHdFld(const TStr& FldNm, const TStr& FldVal, TChA& HdChA);
144  void ParseHttpResp(const PSIn& SIn);
145 public:
146  THttpResp(const int& _StatusCd, const TStr& ContTypeVal,
147  const bool& CacheCtrlP, const PSIn& BodySIn, const TStr LocStr);
148  static PHttpResp New(const int& StatusCd, const TStr& ContTypeVal,
149  const bool& CacheCtrlP, const PSIn& BodySIn, const TStr LocStr=TStr()){
150  return PHttpResp(new
151  THttpResp(StatusCd, ContTypeVal, CacheCtrlP, BodySIn, LocStr));}
152  THttpResp(const PSIn& SIn);
153  static PHttpResp New(const PSIn& SIn){
154  return PHttpResp(new THttpResp(SIn));}
157  static PHttpResp Load(TSIn&){Fail; return NULL;}
158  void Save(TSOut&){Fail;}
159 
160  THttpResp& operator=(const THttpResp&){Fail; return *this;}
161 
162  bool IsOk() const {return Ok;}
163  int Len() const {return HdStr.Len()+BodyMem.Len();}
164  bool IsContLenOk() const {int ContLen;
165  return IsOk()&&IsContLen(ContLen)&&(ContLen==BodyMem.Len());}
166  void GetAsMem(TMem& Mem) const {
167  Mem.Clr(); Mem+=HdStr; Mem+=BodyMem;}
168  TStr GetHdStr() const {return HdStr;}
169  const TMem& GetBodyAsMem() const {return BodyMem;}
170  TStr GetBodyAsStr() const {return BodyMem.GetAsStr(' ');}
171  PSIn GetBodyAsSIn() const {return BodyMem.GetSIn(); }
172  int GetStatusCd() const {return StatusCd;}
173  TStr GetReasonPhrase() const {return THttp::GetReasonPhrase(StatusCd);}
174  int GetFlds() const {return FldNmToValVH.Len();}
175  int GetFldVals(const int& FldN) const {
176  return FldNmToValVH[FldN].Len();}
177  void GetFldNmVal(const int& FldN, TStr& FldNm, TStr& FldVal){
178  FldNm=FldNmToValVH.GetKey(FldN); FldVal=FldNmToValVH[FldN][0];}
179  void GetFldNmVal(const int& FldN, const int& ValN, TStr& FldNm, TStr& FldVal){
180  FldNm=FldNmToValVH.GetKey(FldN); FldVal=FldNmToValVH[FldN][ValN];}
181  bool IsFldNm(const TStr& FldNm) const;
182  TStr GetFldVal(const TStr& FldNm, const int& ValN=0) const;
183  void GetFldValV(const TStr& FldNm, TStrV& FldValV) const;
184  bool IsFldVal(const TStr& FldNm, const TStr& FldVal) const;
185  void AddFldVal(const TStr& FldNm, const TStr& FldVal);
186 
187  bool IsStatusCd_Ok() const {
188  return IsOk() && (GetStatusCd()/100==THttp::OkStatusCd/100);}
189  bool IsStatusCd_Redir() const {
190  return IsOk() && (GetStatusCd()/100==THttp::RedirStatusCd/100);}
191 
192  bool IsContType() const {
193  return IsFldNm(THttp::ContTypeFldNm);}
194  bool IsContType(const TStr& ContTypeStr) const {
195  return GetFldVal(THttp::ContTypeFldNm).IsStrIn(ContTypeStr);}
196  bool IsContLen(int& ContLen) const {
197  return GetFldVal(THttp::ContLenFldNm).IsInt(ContLen);}
198  TStr GetSrvNm() const {
199  return GetFldVal(THttp::SrvFldNm);}
200  void GetCookieKeyValDmPathQuV(TStrQuV& CookieKeyValDmPathQuV);
201 
202  int GetTxtLen() const {return HdStr.Len()+BodyMem.Len();}
203  static PHttpResp LoadTxt(PSIn& SIn){
204  return new THttpResp(SIn);}
205  void SaveTxt(const PSOut& SOut) const {
206  HdStr.SaveTxt(SOut); BodyMem.SaveMem(SOut);}
207  void SaveBody(const PSOut& SOut) const {
208  BodyMem.SaveMem(SOut);}
209 
210  PSIn GetSIn() const;
211 };
212 
THttpResp(TSIn &)
Definition: http.h:156
static const TStr ContTypeFldNm
Definition: http.h:12
const TMem & GetBodyAsMem() const
Definition: http.h:169
static const TStr ImageIcoFldVal
Definition: http.h:32
void GetAsMem(TMem &Mem) const
Definition: http.h:120
static const int DfPortN
Definition: http.h:46
TStr GetSrvNm() const
Definition: http.h:198
static PHttpRq New(const PUrl &Url, const int &FetchId=-1)
Definition: http.h:91
static const int BadRqStatusCd
Definition: http.h:50
bool IsContType(const TStr &ContTypeStr) const
Definition: http.h:123
THttpRqMethod GetMethod() const
Definition: http.h:103
static const TStr GetMethodNm
Definition: http.h:56
static PSIn New(const TMem &Mem)
Definition: dt.h:165
void GetAsMem(TMem &Mem) const
Definition: http.h:166
static const TStr HttpStr
Definition: http.h:8
~THttpRq()
Definition: http.h:93
static const TStr HostFldNm
Definition: http.h:14
Definition: http.h:5
#define Fail
Definition: bd.h:238
static const int RedirStatusCd
Definition: http.h:49
static PHttpResp Load(TSIn &)
Definition: http.h:157
TStr GetReasonPhrase() const
Definition: http.h:173
bool IsOk() const
Definition: http.h:101
void SaveTxt(const PSOut &SOut) const
Definition: http.h:205
Definition: http.h:65
THttpRqMethod
Definition: http.h:64
PSIn GetBodyAsSIn() const
Definition: http.h:118
static const TStr AcceptRangesFldNm
Definition: http.h:15
THttpRq(TSIn &)
Definition: http.h:94
static const TStr ConnFldNm
Definition: http.h:19
TStr GetHdStr() const
Definition: http.h:168
static const TStr TextCssFldVal
Definition: http.h:31
static TStr GetReasonPhrase(const int &StatusCd)
Definition: http.cpp:62
Definition: http.h:65
static const TStr AcceptFldNm
Definition: http.h:17
static const int ErrStatusCd
Definition: http.h:51
static bool IsHtmlFExt(const TStr &FExt)
Definition: http.cpp:42
Definition: http.h:67
static PHttpResp New(const PSIn &SIn)
Definition: http.h:153
static bool IsGifFExt(const TStr &FExt)
Definition: http.cpp:47
Definition: fl.h:58
static const TStr TextPlainFldVal
Definition: http.h:26
static const TStr CookieFldNm
Definition: http.h:23
Definition: dt.h:77
#define ClassTP(TNm, PNm)
Definition: bd.h:126
bool IsStatusCd_Redir() const
Definition: http.h:189
static const TStr HeadMethodNm
Definition: http.h:57
static const TStr PostMethodNm
Definition: http.h:58
static const TStr ColonStr
Definition: http.h:10
bool IsContLen(int &ContLen) const
Definition: http.h:196
static const TStr ImageGifFldVal
Definition: http.h:34
void Save(TSOut &)
Definition: http.h:158
TStr GetBodyAsStr() const
Definition: http.h:117
static const TStr AppOctetFldVal
Definition: http.h:36
PUrl GetUrl() const
Definition: http.h:105
static const int ErrNotFoundStatusCd
Definition: http.h:52
THttpResp & operator=(const THttpResp &)
Definition: http.h:160
static const TStr UndefMethodNm
Definition: http.h:59
static PHttpResp LoadTxt(PSIn &SIn)
Definition: http.h:203
static const TStr CacheCtrlFldNm
Definition: http.h:16
static const TStr LocFldNm
Definition: http.h:21
void Save(TSOut &)
Definition: http.h:96
Definition: http.h:65
int Len() const
Definition: http.h:163
TStr GetBodyAsStr() const
Definition: http.h:170
static const TStr TextHtmlFldVal
Definition: http.h:27
bool IsStatusCd_Ok() const
Definition: http.h:187
bool IsContType(const TStr &ContTypeStr) const
Definition: http.h:194
int GetFldVals(const int &FldN) const
Definition: http.h:175
PSIn GetBodyAsSIn() const
Definition: http.h:171
static const TStr AppJSonFldVal
Definition: http.h:39
bool IsOk() const
Definition: http.h:162
static const int OkStatusCd
Definition: http.h:48
static PHttpRq Load(TSIn &)
Definition: http.h:95
int GetFlds() const
Definition: http.h:174
int GetTxtLen() const
Definition: http.h:202
static const TStr ContLenFldNm
Definition: http.h:13
Definition: fl.h:128
static const TStr SlashStr
Definition: http.h:9
bool IsContLen(int &ContLen) const
Definition: http.h:125
static const TStr TextJavaScriptFldVal
Definition: http.h:30
void GetBodyAsMem(TMem &Mem) const
Definition: http.h:119
static const TStr TextWmlFldVal
Definition: http.h:29
Definition: http.h:65
static const TStr TextXmlFldVal
Definition: http.h:28
Definition: dt.h:201
static const TStr ConnKeepAliveFldVal
Definition: http.h:40
void GetFldNmVal(const int &FldN, const int &ValN, TStr &FldNm, TStr &FldVal)
Definition: http.h:179
Definition: dt.h:412
bool IsBody() const
Definition: http.h:115
TStr GetHdStr() const
Definition: http.h:114
static PHttpRq New(const THttpRqMethod &Method, const PUrl &Url, const TStr &ContTypeFldVal, const TMem &BodyMem)
Definition: http.h:87
bool IsComplete() const
Definition: http.h:102
bool IsContType() const
Definition: http.h:192
static const int InternalErrStatusCd
Definition: http.h:53
static const TStr TextFldVal
Definition: http.h:25
static const TStr FetchIdFldNm
Definition: http.h:20
TPt< THttpResp > PHttpResp
Definition: http.h:134
Definition: bd.h:196
static const TStr AppW3FormFldVal
Definition: http.h:38
~THttpResp()
Definition: http.h:155
static const TStr ImagePngFldVal
Definition: http.h:33
void SaveBody(const PSOut &SOut) const
Definition: http.h:207
THttpRq & operator=(const THttpRq &)
Definition: http.h:98
TPt< THttpRq > PHttpRq
Definition: http.h:67
static const TStr SrvFldNm
Definition: http.h:18
bool IsContLenOk() const
Definition: http.h:164
void GetFldNmVal(const int &FldN, TStr &FldNm, TStr &FldVal)
Definition: http.h:177
static const TStr ImageJpgFldVal
Definition: http.h:35
PUrlEnv GetUrlEnv() const
Definition: http.h:106
int GetBodyLen() const
Definition: http.h:116
int GetStatusCd() const
Definition: http.h:172
static const TStr AppSoapXmlFldVal
Definition: http.h:37
void Clr(const bool &DoDel=true)
Definition: dt.h:131
static const TStr SetCookieFldNm
Definition: http.h:22