SNAP Library , Developer Reference
2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <http.h>
Public Member Functions | |
THttpResp (const int &_StatusCd, const TStr &ContTypeVal, const bool &CacheCtrlP, const PSIn &BodySIn, const TStr LocStr) | |
THttpResp (const PSIn &SIn) | |
~THttpResp () | |
THttpResp (TSIn &) | |
void | Save (TSOut &) |
THttpResp & | operator= (const THttpResp &) |
bool | IsOk () const |
int | Len () const |
bool | IsContLenOk () const |
void | GetAsMem (TMem &Mem) const |
TStr | GetHdStr () const |
const TMem & | GetBodyAsMem () const |
TStr | GetBodyAsStr () const |
int | GetStatusCd () const |
TStr | GetReasonPhrase () const |
int | GetFlds () const |
int | GetFldVals (const int &FldN) const |
void | GetFldNmVal (const int &FldN, TStr &FldNm, TStr &FldVal) |
void | GetFldNmVal (const int &FldN, const int &ValN, TStr &FldNm, TStr &FldVal) |
bool | IsFldNm (const TStr &FldNm) const |
TStr | GetFldVal (const TStr &FldNm, const int &ValN=0) const |
void | GetFldValV (const TStr &FldNm, TStrV &FldValV) const |
bool | IsFldVal (const TStr &FldNm, const TStr &FldVal) const |
void | AddFldVal (const TStr &FldNm, const TStr &FldVal) |
bool | IsStatusCd_Ok () const |
bool | IsStatusCd_Redir () const |
bool | IsContType () const |
bool | IsContType (const TStr &ContTypeStr) const |
bool | IsContLen (int &ContLen) const |
TStr | GetSrvNm () const |
void | GetCookieKeyValDmPathQuV (TStrQuV &CookieKeyValDmPathQuV) |
int | GetTxtLen () const |
void | SaveTxt (const PSOut &SOut) const |
void | SaveBody (const PSOut &SOut) const |
PSIn | GetSIn () const |
Static Public Member Functions | |
static PHttpResp | New (const int &StatusCd, const TStr &ContTypeVal, const bool &CacheCtrlP, const PSIn &BodySIn, const TStr LocStr=TStr()) |
static PHttpResp | New (const PSIn &SIn) |
static PHttpResp | Load (TSIn &) |
static PHttpResp | LoadTxt (PSIn &SIn) |
Private Member Functions | |
void | AddHdFld (const TStr &FldNm, const TStr &FldVal, TChA &HdChA) |
void | ParseHttpResp (const PSIn &SIn) |
Private Attributes | |
TCRef | CRef |
bool | Ok |
int | MajorVerN |
int | MinorVerN |
int | StatusCd |
TStr | ReasonPhrase |
TStrStrVH | FldNmToValVH |
TStr | HdStr |
TMem | BodyMem |
Friends | |
class | TPt< THttpResp > |
THttpResp::THttpResp | ( | const int & | _StatusCd, |
const TStr & | ContTypeVal, | ||
const bool & | CacheCtrlP, | ||
const PSIn & | BodySIn, | ||
const TStr | LocStr | ||
) |
Definition at line 739 of file http.cpp.
References AddHdFld(), BodyMem, TStr::Empty(), TPt< TRec >::Empty(), GetReasonPhrase(), TInt::GetStr(), HdStr, TSIn::Len(), TMem::LoadMem(), MajorVerN, MinorVerN, ReasonPhrase, and StatusCd.
: Ok(true), MajorVerN(1), MinorVerN(0), StatusCd(_StatusCd), ReasonPhrase(), FldNmToValVH(20), HdStr(), BodyMem(){ ReasonPhrase=THttp::GetReasonPhrase(StatusCd); TChA HdChA; // first line HdChA+="HTTP/"; HdChA+=TInt::GetStr(MajorVerN); HdChA+="."; HdChA+=TInt::GetStr(MinorVerN); HdChA+=' '; HdChA+=TInt::GetStr(StatusCd); HdChA+=' '; HdChA+=ReasonPhrase; HdChA+="\r\n"; // header fields // server //AddHdFld(THttp::SrvFldNm, "Tralala", HdChA); if (!LocStr.Empty()){ AddHdFld("Location", LocStr, HdChA);} if (!BodySIn.Empty()){ // content-type AddHdFld(THttp::ContTypeFldNm, ContTypeVal, HdChA); // accept-ranges AddHdFld(THttp::AcceptRangesFldNm, "bytes", HdChA); // content-length TStr ContLenVal=TInt::GetStr(BodySIn->Len()); AddHdFld(THttp::ContLenFldNm, ContLenVal, HdChA); // cache-control if (!CacheCtrlP){ AddHdFld(THttp::CacheCtrlFldNm, "no-cache", HdChA);} } // header/body separator HdChA+="\r\n"; // header/body data HdStr=HdChA; if (!BodySIn.Empty()){ TMem::LoadMem(BodySIn, BodyMem);} }
THttpResp::THttpResp | ( | const PSIn & | SIn | ) |
Definition at line 776 of file http.cpp.
References Ok, and ParseHttpResp().
: Ok(false), MajorVerN(0), MinorVerN(0), StatusCd(-1), ReasonPhrase(), FldNmToValVH(20), HdStr(), BodyMem(){ try { ParseHttpResp(SIn); } catch (THttpEx){Ok=false;} }
THttpResp::~THttpResp | ( | ) | [inline] |
THttpResp::THttpResp | ( | TSIn & | ) | [inline] |
void THttpResp::AddFldVal | ( | const TStr & | FldNm, |
const TStr & | FldVal | ||
) |
Definition at line 812 of file http.cpp.
References TVec< TVal >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), FldNmToValVH, THttpLx::GetNrStr(), HdStr, TStr::IsSuffix(), and TChA::Pop().
{ TStr NrFldNm=THttpLx::GetNrStr(FldNm); FldNmToValVH.AddDat(NrFldNm).Add(FldVal); if (HdStr.IsSuffix("\r\n\r\n")){ TChA HdChA=HdStr; HdChA.Pop(); HdChA.Pop(); HdChA+=NrFldNm; HdChA+=": "; HdChA+=FldVal; HdChA+="\r\n\r\n"; HdStr=HdChA; } }
void THttpResp::AddHdFld | ( | const TStr & | FldNm, |
const TStr & | FldVal, | ||
TChA & | HdChA | ||
) | [private] |
Definition at line 692 of file http.cpp.
References TVec< TVal >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), FldNmToValVH, and THttpLx::GetNrStr().
Referenced by THttpResp().
{ TStr NrFldNm=THttpLx::GetNrStr(FldNm); FldNmToValVH.AddDat(NrFldNm).Add(FldVal); HdChA+=FldNm; HdChA+=": "; HdChA+=FldVal; HdChA+="\r\n"; }
void THttpResp::GetAsMem | ( | TMem & | Mem | ) | const [inline] |
const TMem& THttpResp::GetBodyAsMem | ( | ) | const [inline] |
TStr THttpResp::GetBodyAsStr | ( | ) | const [inline] |
Definition at line 168 of file http.h.
Referenced by TWebPg::IsTxt().
void THttpResp::GetCookieKeyValDmPathQuV | ( | TStrQuV & | CookieKeyValDmPathQuV | ) |
Definition at line 824 of file http.cpp.
References TVec< TVal >::Add(), TVec< TVal >::Clr(), TStr::Empty(), GetFldValV(), TStr::GetTrunc(), TStr::IsChIn(), TVec< TVal >::Len(), TStr::SplitOnAllCh(), and TStr::ToTrunc().
{ CookieKeyValDmPathQuV.Clr(); TStrV CookieFldValV; GetFldValV(THttp::SetCookieFldNm, CookieFldValV); for (int CookieN=0; CookieN<CookieFldValV.Len(); CookieN++){ TStr CookieFldVal=CookieFldValV[CookieN]; TStrV KeyValStrV; CookieFldVal.SplitOnAllCh(';', KeyValStrV, true); TStrPrV KeyValPrV; TStr DmNm; TStr PathStr; for (int KeyValStrN=0; KeyValStrN<KeyValStrV.Len(); KeyValStrN++){ TStr KeyValStr=KeyValStrV[KeyValStrN]; TStr KeyNm; TStr ValStr; if (KeyValStr.IsChIn('=')){ KeyValStrV[KeyValStrN].SplitOnCh(KeyNm, '=', ValStr); KeyNm.ToTrunc(); ValStr.ToTrunc(); } else { KeyNm=KeyValStr.GetTrunc(); } if (KeyNm=="expires"){} else if (KeyNm=="domain"){DmNm=ValStr;} else if (KeyNm=="path"){PathStr=ValStr;} else if (KeyNm=="expires"){} else if (KeyNm=="secure"){} else if (KeyNm=="httponly"){} else if (!KeyNm.Empty()){ KeyValPrV.Add(TStrPr(KeyNm, ValStr)); } } for (int KeyValPrN=0; KeyValPrN<KeyValPrV.Len(); KeyValPrN++){ TStr KeyNm=KeyValPrV[KeyValPrN].Val1; TStr ValStr=KeyValPrV[KeyValPrN].Val2; CookieKeyValDmPathQuV.Add(TStrQu(KeyNm, ValStr, DmNm, PathStr)); } } }
void THttpResp::GetFldNmVal | ( | const int & | FldN, |
TStr & | FldNm, | ||
TStr & | FldVal | ||
) | [inline] |
Definition at line 174 of file http.h.
{ FldNm=FldNmToValVH.GetKey(FldN); FldVal=FldNmToValVH[FldN][0];}
void THttpResp::GetFldNmVal | ( | const int & | FldN, |
const int & | ValN, | ||
TStr & | FldNm, | ||
TStr & | FldVal | ||
) | [inline] |
Definition at line 176 of file http.h.
{ FldNm=FldNmToValVH.GetKey(FldN); FldVal=FldNmToValVH[FldN][ValN];}
int THttpResp::GetFlds | ( | ) | const [inline] |
Definition at line 171 of file http.h.
{return FldNmToValVH.Len();}
TStr THttpResp::GetFldVal | ( | const TStr & | FldNm, |
const int & | ValN = 0 |
||
) | const |
Definition at line 789 of file http.cpp.
References FldNmToValVH, THash< TKey, TDat, THashFunc >::GetDat(), THttpLx::GetNrStr(), THash< TKey, TDat, THashFunc >::IsKey(), and TVec< TVal >::Len().
Referenced by IsFldVal().
{ TStr NrFldNm=THttpLx::GetNrStr(FldNm); if (FldNmToValVH.IsKey(NrFldNm)){ const TStrV& ValV=FldNmToValVH.GetDat(NrFldNm); if (ValV.Len()>0){return ValV[ValN];} else {return TStr();} } else { return TStr(); } }
int THttpResp::GetFldVals | ( | const int & | FldN | ) | const [inline] |
Definition at line 172 of file http.h.
{ return FldNmToValVH[FldN].Len();}
void THttpResp::GetFldValV | ( | const TStr & | FldNm, |
TStrV & | FldValV | ||
) | const |
Definition at line 799 of file http.cpp.
References TVec< TVal >::Clr(), FldNmToValVH, THash< TKey, TDat, THashFunc >::GetDat(), THttpLx::GetNrStr(), and THash< TKey, TDat, THashFunc >::IsKey().
Referenced by GetCookieKeyValDmPathQuV().
{ TStr NrFldNm=THttpLx::GetNrStr(FldNm); if (FldNmToValVH.IsKey(NrFldNm)){ FldValV=FldNmToValVH.GetDat(NrFldNm); } else { FldValV.Clr(); } }
TStr THttpResp::GetHdStr | ( | ) | const [inline] |
TStr THttpResp::GetReasonPhrase | ( | ) | const [inline] |
Definition at line 170 of file http.h.
References THttp::GetReasonPhrase().
Referenced by THttpResp().
{return THttp::GetReasonPhrase(StatusCd);}
PSIn THttpResp::GetSIn | ( | ) | const |
Definition at line 859 of file http.cpp.
References BodyMem, HdStr, TMem::Len(), TStr::Len(), and TSOut::PutStr().
{ TMOut MOut(HdStr.Len()+BodyMem.Len()); MOut.PutStr(HdStr); MOut.PutMem(BodyMem); return MOut.GetSIn(); }
TStr THttpResp::GetSrvNm | ( | ) | const [inline] |
Definition at line 195 of file http.h.
References THttp::SrvFldNm.
{ return GetFldVal(THttp::SrvFldNm);}
int THttpResp::GetStatusCd | ( | ) | const [inline] |
int THttpResp::GetTxtLen | ( | ) | const [inline] |
bool THttpResp::IsContLen | ( | int & | ContLen | ) | const [inline] |
Definition at line 193 of file http.h.
References THttp::ContLenFldNm.
{ return GetFldVal(THttp::ContLenFldNm).IsInt(ContLen);}
bool THttpResp::IsContLenOk | ( | ) | const [inline] |
bool THttpResp::IsContType | ( | ) | const [inline] |
Definition at line 189 of file http.h.
References THttp::ContTypeFldNm.
Referenced by TWebPg::IsTxt().
{ return IsFldNm(THttp::ContTypeFldNm);}
bool THttpResp::IsContType | ( | const TStr & | ContTypeStr | ) | const [inline] |
Definition at line 191 of file http.h.
References THttp::ContTypeFldNm.
{ return GetFldVal(THttp::ContTypeFldNm).IsStrIn(ContTypeStr);}
bool THttpResp::IsFldNm | ( | const TStr & | FldNm | ) | const |
Definition at line 785 of file http.cpp.
References FldNmToValVH, THttpLx::GetNrStr(), and THash< TKey, TDat, THashFunc >::IsKey().
{ return FldNmToValVH.IsKey(THttpLx::GetNrStr(FldNm)); }
bool THttpResp::IsFldVal | ( | const TStr & | FldNm, |
const TStr & | FldVal | ||
) | const |
Definition at line 808 of file http.cpp.
References GetFldVal(), and THttpLx::GetNrStr().
{ return THttpLx::GetNrStr(FldVal)==THttpLx::GetNrStr(GetFldVal(FldNm)); }
bool THttpResp::IsOk | ( | ) | const [inline] |
bool THttpResp::IsStatusCd_Ok | ( | ) | const [inline] |
Definition at line 184 of file http.h.
References THttp::OkStatusCd.
{ return IsOk() && (GetStatusCd()/100==THttp::OkStatusCd/100);}
bool THttpResp::IsStatusCd_Redir | ( | ) | const [inline] |
Definition at line 186 of file http.h.
References THttp::RedirStatusCd.
{ return IsOk() && (GetStatusCd()/100==THttp::RedirStatusCd/100);}
int THttpResp::Len | ( | ) | const [inline] |
static PHttpResp THttpResp::Load | ( | TSIn & | ) | [inline, static] |
static PHttpResp THttpResp::LoadTxt | ( | PSIn & | SIn | ) | [inline, static] |
static PHttpResp THttpResp::New | ( | const PSIn & | SIn | ) | [inline, static] |
void THttpResp::ParseHttpResp | ( | const PSIn & | SIn | ) | [private] |
Definition at line 698 of file http.cpp.
References TVec< TVal >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), BodyMem, TMem::Clr(), TStr::Clr(), THttpLx::ClrMemSf(), THttpLx::Eof(), FldNmToValVH, TMem::GetAsStr(), THttpLx::GetCrLf(), THttpLx::GetFldVal(), THttpLx::GetInt(), THttpLx::GetMemSf(), THttpLx::GetNrStr(), THttpLx::GetPeriod(), THttpLx::GetRespReasonPhrase(), THttpLx::GetRest(), THttpLx::GetSpec(), THttpLx::GetToken(), HdStr, THttpLx::IsCrLf(), THttpLx::IsRespStatusLn(), MajorVerN, MinorVerN, Ok, ReasonPhrase, and StatusCd.
Referenced by THttpResp().
{ THttpLx Lx(SIn); if (Lx.Eof()){ // no content MajorVerN=0; MinorVerN=9; StatusCd=204; HdStr.Clr(); BodyMem.Clr(); } else { if (Lx.IsRespStatusLn()){ // status-line Lx.GetToken(THttp::HttpStr); Lx.GetSpec(THttp::SlashStr); MajorVerN=Lx.GetInt(1); Lx.GetPeriod(); MinorVerN=Lx.GetInt(1); StatusCd=Lx.GetInt(3); ReasonPhrase=Lx.GetRespReasonPhrase(); Lx.GetCrLf(); // header fields & values while (!Lx.IsCrLf()){ TStr FldNm=Lx.GetToken(); Lx.GetSpec(THttp::ColonStr); TStr FldVal=Lx.GetFldVal(); Lx.GetCrLf(); TStr NrFldNm=THttpLx::GetNrStr(FldNm); FldNmToValVH.AddDat(NrFldNm).Add(FldVal); } // separator CrLf Lx.GetCrLf(); // header & body strings HdStr=Lx.GetMemSf().GetAsStr(); Lx.ClrMemSf(); Lx.GetRest(); BodyMem=Lx.GetMemSf(); } else { // old fashion format MajorVerN=0; MinorVerN=9; StatusCd=200; HdStr.Clr(); Lx.ClrMemSf(); Lx.GetRest(); BodyMem=Lx.GetMemSf(); } } Ok=true; }
void THttpResp::Save | ( | TSOut & | ) | [inline] |
void THttpResp::SaveBody | ( | const PSOut & | SOut | ) | const [inline] |
Definition at line 204 of file http.h.
Referenced by TWebPg::SaveAsHttpBody().
void THttpResp::SaveTxt | ( | const PSOut & | SOut | ) | const [inline] |
TMem THttpResp::BodyMem [private] |
Definition at line 140 of file http.h.
Referenced by GetSIn(), ParseHttpResp(), and THttpResp().
TCRef THttpResp::CRef [private] |
TStrStrVH THttpResp::FldNmToValVH [private] |
Definition at line 138 of file http.h.
Referenced by AddFldVal(), AddHdFld(), GetFldVal(), GetFldValV(), IsFldNm(), and ParseHttpResp().
TStr THttpResp::HdStr [private] |
Definition at line 139 of file http.h.
Referenced by AddFldVal(), GetSIn(), ParseHttpResp(), and THttpResp().
int THttpResp::MajorVerN [private] |
Definition at line 135 of file http.h.
Referenced by ParseHttpResp(), and THttpResp().
int THttpResp::MinorVerN [private] |
Definition at line 135 of file http.h.
Referenced by ParseHttpResp(), and THttpResp().
bool THttpResp::Ok [private] |
Definition at line 134 of file http.h.
Referenced by ParseHttpResp(), and THttpResp().
TStr THttpResp::ReasonPhrase [private] |
Definition at line 137 of file http.h.
Referenced by ParseHttpResp(), and THttpResp().
int THttpResp::StatusCd [private] |
Definition at line 136 of file http.h.
Referenced by ParseHttpResp(), and THttpResp().