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