SNAP Library 2.1, User Reference  2013-09-25 10:47:25
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
THttpResp Class Reference

#include <http.h>

List of all members.

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 &)
THttpRespoperator= (const THttpResp &)
bool IsOk () const
int Len () const
bool IsContLenOk () const
void GetAsMem (TMem &Mem) const
TStr GetHdStr () const
const TMemGetBodyAsMem () const
TStr GetBodyAsStr () const
PSIn GetBodyAsSIn () 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 >

Detailed Description

Definition at line 134 of file http.h.


Constructor & Destructor Documentation

THttpResp::THttpResp ( const int &  _StatusCd,
const TStr ContTypeVal,
const bool &  CacheCtrlP,
const PSIn BodySIn,
const TStr  LocStr 
)

Definition at line 740 of file http.cpp.

                                                                :
  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 777 of file http.cpp.

                                   :
  Ok(false), MajorVerN(0), MinorVerN(0), StatusCd(-1), ReasonPhrase(),
  FldNmToValVH(20), HdStr(), BodyMem(){
  try {
    ParseHttpResp(SIn);
  }
  catch (const THttpEx&){Ok=false;}
}
THttpResp::~THttpResp ( ) [inline]

Definition at line 155 of file http.h.

{}
THttpResp::THttpResp ( TSIn ) [inline]

Definition at line 156 of file http.h.

{Fail;}

Member Function Documentation

void THttpResp::AddFldVal ( const TStr FldNm,
const TStr FldVal 
)

Definition at line 813 of file http.cpp.

                                                              {
  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 693 of file http.cpp.

                                                                          {
  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]

Definition at line 166 of file http.h.

                                 {
    Mem.Clr(); Mem+=HdStr; Mem+=BodyMem;}
const TMem& THttpResp::GetBodyAsMem ( ) const [inline]

Definition at line 169 of file http.h.

{return BodyMem;}
PSIn THttpResp::GetBodyAsSIn ( ) const [inline]

Definition at line 171 of file http.h.

{return BodyMem.GetSIn(); }
TStr THttpResp::GetBodyAsStr ( ) const [inline]

Definition at line 170 of file http.h.

{return BodyMem.GetAsStr(' ');}
void THttpResp::GetCookieKeyValDmPathQuV ( TStrQuV CookieKeyValDmPathQuV)

Definition at line 825 of file http.cpp.

                                                                      {
  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 177 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 179 of file http.h.

                                                                               {
    FldNm=FldNmToValVH.GetKey(FldN); FldVal=FldNmToValVH[FldN][ValN];}
int THttpResp::GetFlds ( ) const [inline]

Definition at line 174 of file http.h.

{return FldNmToValVH.Len();}
TStr THttpResp::GetFldVal ( const TStr FldNm,
const int &  ValN = 0 
) const

Definition at line 790 of file http.cpp.

                                                                  {
  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 175 of file http.h.

                                        {
    return FldNmToValVH[FldN].Len();}
void THttpResp::GetFldValV ( const TStr FldNm,
TStrV FldValV 
) const

Definition at line 800 of file http.cpp.

                                                                  {
  TStr NrFldNm=THttpLx::GetNrStr(FldNm);
  if (FldNmToValVH.IsKey(NrFldNm)){
    FldValV=FldNmToValVH.GetDat(NrFldNm);
  } else {
    FldValV.Clr();
  }
}
TStr THttpResp::GetHdStr ( ) const [inline]

Definition at line 168 of file http.h.

{return HdStr;}
TStr THttpResp::GetReasonPhrase ( ) const [inline]

Definition at line 173 of file http.h.

Definition at line 860 of file http.cpp.

                             {
  TMOut MOut(HdStr.Len()+BodyMem.Len());
  MOut.PutStr(HdStr); MOut.PutMem(BodyMem);
  return MOut.GetSIn();
}
TStr THttpResp::GetSrvNm ( ) const [inline]

Definition at line 198 of file http.h.

int THttpResp::GetStatusCd ( ) const [inline]

Definition at line 172 of file http.h.

{return StatusCd;}
int THttpResp::GetTxtLen ( ) const [inline]

Definition at line 202 of file http.h.

{return HdStr.Len()+BodyMem.Len();}
bool THttpResp::IsContLen ( int &  ContLen) const [inline]

Definition at line 196 of file http.h.

                                     {
    return GetFldVal(THttp::ContLenFldNm).IsInt(ContLen);}
bool THttpResp::IsContLenOk ( ) const [inline]

Definition at line 164 of file http.h.

                           {int ContLen;
    return IsOk()&&IsContLen(ContLen)&&(ContLen==BodyMem.Len());}
bool THttpResp::IsContType ( ) const [inline]

Definition at line 192 of file http.h.

bool THttpResp::IsContType ( const TStr ContTypeStr) const [inline]

Definition at line 194 of file http.h.

                                                 {
    return GetFldVal(THttp::ContTypeFldNm).IsStrIn(ContTypeStr);}
bool THttpResp::IsFldNm ( const TStr FldNm) const

Definition at line 786 of file http.cpp.

                                               {
  return FldNmToValVH.IsKey(THttpLx::GetNrStr(FldNm));
}
bool THttpResp::IsFldVal ( const TStr FldNm,
const TStr FldVal 
) const

Definition at line 809 of file http.cpp.

                                                                    {
  return THttpLx::GetNrStr(FldVal)==THttpLx::GetNrStr(GetFldVal(FldNm));
}
bool THttpResp::IsOk ( ) const [inline]

Definition at line 162 of file http.h.

{return Ok;}
bool THttpResp::IsStatusCd_Ok ( ) const [inline]

Definition at line 187 of file http.h.

                             {
    return IsOk() && (GetStatusCd()/100==THttp::OkStatusCd/100);}
bool THttpResp::IsStatusCd_Redir ( ) const [inline]

Definition at line 189 of file http.h.

                                {
    return IsOk() && (GetStatusCd()/100==THttp::RedirStatusCd/100);}
int THttpResp::Len ( ) const [inline]

Definition at line 163 of file http.h.

{return HdStr.Len()+BodyMem.Len();}
static PHttpResp THttpResp::Load ( TSIn ) [inline, static]

Definition at line 157 of file http.h.

{Fail; return NULL;}
static PHttpResp THttpResp::LoadTxt ( PSIn SIn) [inline, static]

Definition at line 203 of file http.h.

                                     {
    return new THttpResp(SIn);}
static PHttpResp THttpResp::New ( const int &  StatusCd,
const TStr ContTypeVal,
const bool &  CacheCtrlP,
const PSIn BodySIn,
const TStr  LocStr = TStr() 
) [inline, static]

Definition at line 148 of file http.h.

                                                                         {
    return PHttpResp(new
     THttpResp(StatusCd, ContTypeVal, CacheCtrlP, BodySIn, LocStr));}
static PHttpResp THttpResp::New ( const PSIn SIn) [inline, static]

Definition at line 153 of file http.h.

                                       {
    return PHttpResp(new THttpResp(SIn));}
THttpResp& THttpResp::operator= ( const THttpResp ) [inline]

Definition at line 160 of file http.h.

{Fail; return *this;}
void THttpResp::ParseHttpResp ( const PSIn SIn) [private]

Definition at line 699 of file http.cpp.

                                            {
  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]

Definition at line 158 of file http.h.

{Fail;}
void THttpResp::SaveBody ( const PSOut SOut) const [inline]

Definition at line 207 of file http.h.

                                         {
    BodyMem.SaveMem(SOut);}
void THttpResp::SaveTxt ( const PSOut SOut) const [inline]

Definition at line 205 of file http.h.

                                        {
    HdStr.SaveTxt(SOut); BodyMem.SaveMem(SOut);}

Friends And Related Function Documentation

friend class TPt< THttpResp > [friend]

Definition at line 134 of file http.h.


Member Data Documentation

Definition at line 142 of file http.h.

Definition at line 134 of file http.h.

Definition at line 140 of file http.h.

Definition at line 141 of file http.h.

int THttpResp::MajorVerN [private]

Definition at line 137 of file http.h.

int THttpResp::MinorVerN [private]

Definition at line 137 of file http.h.

bool THttpResp::Ok [private]

Definition at line 136 of file http.h.

Definition at line 139 of file http.h.

int THttpResp::StatusCd [private]

Definition at line 138 of file http.h.


The documentation for this class was generated from the following files: