SNAP Library, Developer Reference
2012-10-15 15:06:59
SNAP, a general purpose network analysis and graph mining library
|
00001 00002 // Http-General 00003 class THttp{ 00004 public: 00005 // general strings 00006 static const TStr HttpStr; 00007 static const TStr SlashStr; 00008 static const TStr ColonStr; 00009 // field names 00010 static const TStr ContTypeFldNm; 00011 static const TStr ContLenFldNm; 00012 static const TStr HostFldNm; 00013 static const TStr AcceptRangesFldNm; 00014 static const TStr CacheCtrlFldNm; 00015 static const TStr AcceptFldNm; 00016 static const TStr SrvFldNm; 00017 static const TStr ConnFldNm; 00018 static const TStr FetchIdFldNm; 00019 static const TStr LocFldNm; 00020 static const TStr SetCookieFldNm; 00021 static const TStr CookieFldNm; 00022 // content-type field-values 00023 static const TStr TextFldVal; 00024 static const TStr TextPlainFldVal; 00025 static const TStr TextHtmlFldVal; 00026 static const TStr TextXmlFldVal; 00027 static const TStr TextWmlFldVal; 00028 static const TStr TextJavaScriptFldVal; 00029 static const TStr TextCssFldVal; 00030 static const TStr ImagePngFldVal; 00031 static const TStr ImageGifFldVal; 00032 static const TStr ImageJpgFldVal; 00033 static const TStr AppOctetFldVal; 00034 static const TStr AppSoapXmlFldVal; 00035 static const TStr AppW3FormFldVal; 00036 static const TStr AppJSonFldVal; 00037 static const TStr ConnKeepAliveFldVal; 00038 // file extensions 00039 static bool IsHtmlFExt(const TStr& FExt); 00040 static bool IsGifFExt(const TStr& FExt); 00041 00042 // port number 00043 static const int DfPortN; 00044 // status codes 00045 static const int OkStatusCd; 00046 static const int RedirStatusCd; 00047 static const int BadRqStatusCd; 00048 static const int ErrStatusCd; 00049 static const int ErrNotFoundStatusCd; 00050 static const int InternalErrStatusCd; 00051 static TStr GetReasonPhrase(const int& StatusCd); 00052 // method names 00053 static const TStr GetMethodNm; 00054 static const TStr HeadMethodNm; 00055 static const TStr PostMethodNm; 00056 static const TStr UndefMethodNm; 00057 }; 00058 00060 // Http-Request 00061 typedef enum { 00062 hrmUndef, hrmGet, hrmHead, hrmPost} THttpRqMethod; 00063 00064 ClassTP(THttpRq, PHttpRq)//{ 00065 private: 00066 bool Ok; 00067 bool CompleteP; 00068 int MajorVerN, MinorVerN; 00069 THttpRqMethod Method; 00070 PUrl Url; 00071 TStrStrH FldNmToValH; 00072 PUrlEnv UrlEnv; 00073 TStr HdStr; 00074 TMem BodyMem; 00075 void ParseSearch(const TStr& SearchStr); 00076 void ParseHttpRq(const PSIn& SIn); 00077 public: 00078 THttpRq(const PSIn& SIn); 00079 static PHttpRq New(const PSIn& SIn){ 00080 return PHttpRq(new THttpRq(SIn));} 00081 THttpRq( 00082 const THttpRqMethod& _Method, const PUrl& _Url, 00083 const TStr& ContTypeFldVal, const TMem& _BodyMem, const int& FetchId=-1); 00084 static PHttpRq New( 00085 const THttpRqMethod& Method, const PUrl& Url, 00086 const TStr& ContTypeFldVal, const TMem& BodyMem){ 00087 return PHttpRq(new THttpRq(Method, Url, ContTypeFldVal, BodyMem));} 00088 static PHttpRq New(const PUrl& Url, const int& FetchId=-1){ 00089 return PHttpRq(new THttpRq(hrmGet, Url, "", TMem(), FetchId));} 00090 ~THttpRq(){} 00091 THttpRq(TSIn&){Fail;} 00092 static PHttpRq Load(TSIn&){Fail; return NULL;} 00093 void Save(TSOut&){Fail;} 00094 00095 THttpRq& operator=(const THttpRq&){Fail; return *this;} 00096 00097 // component-retrieval 00098 bool IsOk() const {return Ok;} 00099 bool IsComplete() const {return CompleteP;} 00100 THttpRqMethod GetMethod() const {return Method;} 00101 const TStr& GetMethodNm() const; 00102 PUrl GetUrl() const {return Url;} 00103 PUrlEnv GetUrlEnv() const {return UrlEnv;} 00104 bool IsFldNm(const TStr& FldNm) const; 00105 TStr GetFldVal(const TStr& FldNm) const; 00106 bool IsFldVal(const TStr& FldNm, const TStr& FldVal) const; 00107 void AddFldVal(const TStr& FldNm, const TStr& FldVal); 00108 const TStrStrH& GetFldValH() const; 00109 00110 // header & body 00111 TStr GetHdStr() const {return HdStr;} 00112 int GetBodyLen() const { return BodyMem.Len(); } 00113 TStr GetBodyAsStr() const { return BodyMem.GetAsStr(' ');} 00114 PSIn GetBodyAsSIn() const { return TMemIn::New(BodyMem); } 00115 void GetBodyAsMem(TMem& Mem) const {Mem.Clr(); Mem += BodyMem;} 00116 void GetAsMem(TMem& Mem) const {Mem.Clr(); Mem+=HdStr; Mem+=BodyMem;} 00117 00118 // content-type 00119 bool IsContType(const TStr& ContTypeStr) const { 00120 return GetFldVal(THttp::ContTypeFldNm).IsStrIn(ContTypeStr);} 00121 bool IsContLen(int& ContLen) const { 00122 return GetFldVal(THttp::ContLenFldNm).IsInt(ContLen);} 00123 00124 // string representation 00125 TStr GetStr() const; 00126 }; 00127 00129 // Http-Response 00130 ClassTP(THttpResp, PHttpResp)//{ 00131 private: 00132 bool Ok; 00133 int MajorVerN, MinorVerN; 00134 int StatusCd; 00135 TStr ReasonPhrase; 00136 TStrStrVH FldNmToValVH; 00137 TStr HdStr; 00138 TMem BodyMem; 00139 void AddHdFld(const TStr& FldNm, const TStr& FldVal, TChA& HdChA); 00140 void ParseHttpResp(const PSIn& SIn); 00141 public: 00142 THttpResp(const int& _StatusCd, const TStr& ContTypeVal, 00143 const bool& CacheCtrlP, const PSIn& BodySIn, const TStr LocStr); 00144 static PHttpResp New(const int& StatusCd, const TStr& ContTypeVal, 00145 const bool& CacheCtrlP, const PSIn& BodySIn, const TStr LocStr=TStr()){ 00146 return PHttpResp(new 00147 THttpResp(StatusCd, ContTypeVal, CacheCtrlP, BodySIn, LocStr));} 00148 THttpResp(const PSIn& SIn); 00149 static PHttpResp New(const PSIn& SIn){ 00150 return PHttpResp(new THttpResp(SIn));} 00151 ~THttpResp(){} 00152 THttpResp(TSIn&){Fail;} 00153 static PHttpResp Load(TSIn&){Fail; return NULL;} 00154 void Save(TSOut&){Fail;} 00155 00156 THttpResp& operator=(const THttpResp&){Fail; return *this;} 00157 00158 bool IsOk() const {return Ok;} 00159 int Len() const {return HdStr.Len()+BodyMem.Len();} 00160 bool IsContLenOk() const {int ContLen; 00161 return IsOk()&&IsContLen(ContLen)&&(ContLen==BodyMem.Len());} 00162 void GetAsMem(TMem& Mem) const { 00163 Mem.Clr(); Mem+=HdStr; Mem+=BodyMem;} 00164 TStr GetHdStr() const {return HdStr;} 00165 const TMem& GetBodyAsMem() const {return BodyMem;} 00166 TStr GetBodyAsStr() const {return BodyMem.GetAsStr(' ');} 00167 int GetStatusCd() const {return StatusCd;} 00168 TStr GetReasonPhrase() const {return THttp::GetReasonPhrase(StatusCd);} 00169 int GetFlds() const {return FldNmToValVH.Len();} 00170 int GetFldVals(const int& FldN) const { 00171 return FldNmToValVH[FldN].Len();} 00172 void GetFldNmVal(const int& FldN, TStr& FldNm, TStr& FldVal){ 00173 FldNm=FldNmToValVH.GetKey(FldN); FldVal=FldNmToValVH[FldN][0];} 00174 void GetFldNmVal(const int& FldN, const int& ValN, TStr& FldNm, TStr& FldVal){ 00175 FldNm=FldNmToValVH.GetKey(FldN); FldVal=FldNmToValVH[FldN][ValN];} 00176 bool IsFldNm(const TStr& FldNm) const; 00177 TStr GetFldVal(const TStr& FldNm, const int& ValN=0) const; 00178 void GetFldValV(const TStr& FldNm, TStrV& FldValV) const; 00179 bool IsFldVal(const TStr& FldNm, const TStr& FldVal) const; 00180 void AddFldVal(const TStr& FldNm, const TStr& FldVal); 00181 00182 bool IsStatusCd_Ok() const { 00183 return IsOk() && (GetStatusCd()/100==THttp::OkStatusCd/100);} 00184 bool IsStatusCd_Redir() const { 00185 return IsOk() && (GetStatusCd()/100==THttp::RedirStatusCd/100);} 00186 00187 bool IsContType() const { 00188 return IsFldNm(THttp::ContTypeFldNm);} 00189 bool IsContType(const TStr& ContTypeStr) const { 00190 return GetFldVal(THttp::ContTypeFldNm).IsStrIn(ContTypeStr);} 00191 bool IsContLen(int& ContLen) const { 00192 return GetFldVal(THttp::ContLenFldNm).IsInt(ContLen);} 00193 TStr GetSrvNm() const { 00194 return GetFldVal(THttp::SrvFldNm);} 00195 void GetCookieKeyValDmPathQuV(TStrQuV& CookieKeyValDmPathQuV); 00196 00197 int GetTxtLen() const {return HdStr.Len()+BodyMem.Len();} 00198 static PHttpResp LoadTxt(PSIn& SIn){ 00199 return new THttpResp(SIn);} 00200 void SaveTxt(const PSOut& SOut) const { 00201 HdStr.SaveTxt(SOut); BodyMem.SaveMem(SOut);} 00202 void SaveBody(const PSOut& SOut) const { 00203 BodyMem.SaveMem(SOut);} 00204 00205 PSIn GetSIn() const; 00206 }; 00207