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