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
THttpRq Class Reference

#include <http.h>

Collaboration diagram for THttpRq:

List of all members.

Public Member Functions

 THttpRq (const PSIn &SIn)
 THttpRq (const THttpRqMethod &_Method, const PUrl &_Url, const TStr &ContTypeFldVal, const TMem &_BodyMem, const int &FetchId=-1)
 ~THttpRq ()
 THttpRq (TSIn &)
void Save (TSOut &)
THttpRqoperator= (const THttpRq &)
bool IsOk () const
bool IsComplete () const
THttpRqMethod GetMethod () const
const TStrGetMethodNm () const
PUrl GetUrl () const
PUrlEnv GetUrlEnv () const
bool IsFldNm (const TStr &FldNm) const
TStr GetFldVal (const TStr &FldNm) const
bool IsFldVal (const TStr &FldNm, const TStr &FldVal) const
void AddFldVal (const TStr &FldNm, const TStr &FldVal)
const TStrStrHGetFldValH () const
TStr GetHdStr () const
bool IsBody () const
int GetBodyLen () const
TStr GetBodyAsStr () const
PSIn GetBodyAsSIn () const
void GetBodyAsMem (TMem &Mem) const
void GetAsMem (TMem &Mem) const
bool IsContType (const TStr &ContTypeStr) const
bool IsContLen (int &ContLen) const
TStr GetStr () const

Static Public Member Functions

static PHttpRq New (const PSIn &SIn)
static PHttpRq New (const THttpRqMethod &Method, const PUrl &Url, const TStr &ContTypeFldVal, const TMem &BodyMem)
static PHttpRq New (const PUrl &Url, const int &FetchId=-1)
static PHttpRq Load (TSIn &)

Private Member Functions

void ParseSearch (const TStr &SearchStr)
void ParseHttpRq (const PSIn &SIn)

Private Attributes

TCRef CRef
bool Ok
bool CompleteP
int MajorVerN
int MinorVerN
THttpRqMethod Method
PUrl Url
TStrStrH FldNmToValH
PUrlEnv UrlEnv
TStr HdStr
TMem BodyMem

Friends

class TPt< THttpRq >

Detailed Description

Definition at line 67 of file http.h.


Constructor & Destructor Documentation

THttpRq::THttpRq ( const PSIn SIn)

Definition at line 581 of file http.cpp.

References Ok, and ParseHttpRq().

                               :
  Ok(false), MajorVerN(0), MinorVerN(0), Method(hrmUndef),
  FldNmToValH(), UrlEnv(TUrlEnv::New()),
  HdStr(), BodyMem(){
  try {
    ParseHttpRq(SIn);
  }
  catch (const THttpEx&){Ok=false;}
}

Here is the call graph for this function:

THttpRq::THttpRq ( const THttpRqMethod _Method,
const PUrl _Url,
const TStr ContTypeFldVal,
const TMem _BodyMem,
const int &  FetchId = -1 
)

Definition at line 591 of file http.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), TStr::Empty(), FldNmToValH, TUrl::GetHostNm(), GetMethodNm(), THttpLx::GetNrStr(), TUrl::GetPathStr(), TUrl::GetSearchStr(), GetStr(), HdStr, TUrl::IsOk(), Ok, ParseSearch(), and Url.

                                                                      :
  Ok(false),
  MajorVerN(1), MinorVerN(0),
  Method(_Method),
  Url(_Url),
  FldNmToValH(),
  UrlEnv(TUrlEnv::New()),
  HdStr(), BodyMem(_BodyMem){
  // compose head-http-request
  TChA HdChA;
  if (Url->IsOk()){
    TStr AbsPath=Url->GetPathStr()+Url->GetSearchStr();
    HdChA+=GetMethodNm(); HdChA+=' '; HdChA+=AbsPath; HdChA+=" HTTP/1.0\r\n";
  }
  // add content-type
  if (!ContTypeFldVal.Empty()){
    FldNmToValH.AddDat(THttpLx::GetNrStr(THttp::ContTypeFldNm), ContTypeFldVal);
    HdChA+=THttpLx::GetNrStr(THttp::ContTypeFldNm); HdChA+=": ";
    HdChA+=ContTypeFldVal; HdChA+="\r\n";
  }
  // add host
  if (Url->IsOk()){
    TStr HostNm=Url->GetHostNm();
    FldNmToValH.AddDat(THttpLx::GetNrStr(THttp::HostFldNm), HostNm);
    HdChA+=THttpLx::GetNrStr(THttp::HostFldNm); HdChA+=": ";
    HdChA+=HostNm; HdChA+="\r\n";
    ParseSearch(Url->GetSearchStr());
  }
  // add fetch-id
  if (Url->IsOk()&&(FetchId!=-1)){
    TStr FetchIdStr=TInt::GetStr(FetchId);
    FldNmToValH.AddDat(THttpLx::GetNrStr(THttp::FetchIdFldNm), FetchIdStr);
    HdChA+=THttpLx::GetNrStr(THttp::FetchIdFldNm); HdChA+=": ";
    HdChA+=FetchIdStr; HdChA+="\r\n";
  }
  // finish head-http-request
  if (Url->IsOk()){
    HdChA+="\r\n";
    HdStr=HdChA;
  }
  // set http-request ok
  Ok=true;
}

Here is the call graph for this function:

THttpRq::~THttpRq ( ) [inline]

Definition at line 93 of file http.h.

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

Definition at line 94 of file http.h.

References Fail.

{Fail;}

Member Function Documentation

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

Definition at line 664 of file http.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), FldNmToValH, and THttpLx::GetNrStr().

                                                            {
  TStr NrFldNm=THttpLx::GetNrStr(FldNm);
  FldNmToValH.AddDat(NrFldNm, FldVal);
}

Here is the call graph for this function:

void THttpRq::GetAsMem ( TMem Mem) const [inline]

Definition at line 120 of file http.h.

References TMem::Clr().

{Mem.Clr(); Mem+=HdStr; Mem+=BodyMem;}

Here is the call graph for this function:

void THttpRq::GetBodyAsMem ( TMem Mem) const [inline]

Definition at line 119 of file http.h.

References TMem::Clr().

{Mem.Clr(); Mem += BodyMem;}

Here is the call graph for this function:

PSIn THttpRq::GetBodyAsSIn ( ) const [inline]

Definition at line 118 of file http.h.

References TMemIn::New().

{ return TMemIn::New(BodyMem); }

Here is the call graph for this function:

TStr THttpRq::GetBodyAsStr ( ) const [inline]

Definition at line 117 of file http.h.

{ return BodyMem.GetAsStr(' ');}
int THttpRq::GetBodyLen ( ) const [inline]

Definition at line 116 of file http.h.

{ return BodyMem.Len(); }
TStr THttpRq::GetFldVal ( const TStr FldNm) const

Definition at line 650 of file http.cpp.

References FldNmToValH, THash< TKey, TDat, THashFunc >::GetDat(), THttpLx::GetNrStr(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by IsFldVal(), and ParseHttpRq().

                                               {
  TStr NrFldNm=THttpLx::GetNrStr(FldNm);
  if (FldNmToValH.IsKey(NrFldNm)){
    return FldNmToValH.GetDat(NrFldNm);
  } else {
    return TStr();
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

const TStrStrH & THttpRq::GetFldValH ( ) const

Definition at line 669 of file http.cpp.

References FldNmToValH.

                                          {
        return FldNmToValH;
}
TStr THttpRq::GetHdStr ( ) const [inline]

Definition at line 114 of file http.h.

{return HdStr;}
THttpRqMethod THttpRq::GetMethod ( ) const [inline]

Definition at line 103 of file http.h.

{return Method;}
const TStr & THttpRq::GetMethodNm ( ) const

Definition at line 637 of file http.cpp.

References hrmGet, hrmHead, hrmPost, and Method.

Referenced by GetStr(), and THttpRq().

                                       {
  switch (Method){
    case hrmGet: return THttp::GetMethodNm;
    case hrmHead: return THttp::HeadMethodNm;
    case hrmPost: return THttp::PostMethodNm;
    default: return  THttp::UndefMethodNm;
  }
}

Here is the caller graph for this function:

TStr THttpRq::GetStr ( ) const

Definition at line 673 of file http.cpp.

References BodyMem, TMem::Empty(), FldNmToValH, TMem::GetAsStr(), THash< TKey, TDat, THashFunc >::GetKey(), GetMethodNm(), TUrl::GetUrlStr(), TMem::Len(), THash< TKey, TDat, THashFunc >::Len(), and Url.

Referenced by THttpRq().

                           {
  TChA ChA;
  ChA+=GetMethodNm(); ChA+=' ';
  ChA+=Url->GetUrlStr(); ChA+=' ';
  ChA+="HTTP/1.0\r\n";
  for (int FldN=0; FldN<FldNmToValH.Len(); FldN++){
    ChA+=FldNmToValH.GetKey(FldN); ChA+=": ";
    ChA+=FldNmToValH[FldN]; ChA+="\r\n";
  }
  if (!BodyMem.Empty()) {
    ChA+=THttp::ContLenFldNm; ChA+=": ";
    ChA+=TInt::GetStr(BodyMem.Len()); ChA+="\r\n";
  }
  ChA+="\r\n";
  ChA+=BodyMem.GetAsStr();
  return ChA;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PUrl THttpRq::GetUrl ( ) const [inline]

Definition at line 105 of file http.h.

{return Url;}
PUrlEnv THttpRq::GetUrlEnv ( ) const [inline]

Definition at line 106 of file http.h.

{return UrlEnv;}
bool THttpRq::IsBody ( ) const [inline]

Definition at line 115 of file http.h.

{ return !BodyMem.Empty(); }
bool THttpRq::IsComplete ( ) const [inline]

Definition at line 102 of file http.h.

{return CompleteP;}
bool THttpRq::IsContLen ( int &  ContLen) const [inline]

Definition at line 125 of file http.h.

References THttp::ContLenFldNm.

                                     {
    return GetFldVal(THttp::ContLenFldNm).IsInt(ContLen);}
bool THttpRq::IsContType ( const TStr ContTypeStr) const [inline]

Definition at line 123 of file http.h.

References THttp::ContTypeFldNm.

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

Definition at line 646 of file http.cpp.

References FldNmToValH, THttpLx::GetNrStr(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by ParseHttpRq().

                                             {
  return FldNmToValH.IsKey(THttpLx::GetNrStr(FldNm));
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool THttpRq::IsFldVal ( const TStr FldNm,
const TStr FldVal 
) const

Definition at line 659 of file http.cpp.

References GetFldVal(), and THttpLx::GetNrStr().

                                                                  {
  return THttpLx::GetNrStr(FldVal)==THttpLx::GetNrStr(GetFldVal(FldNm));
}

Here is the call graph for this function:

bool THttpRq::IsOk ( ) const [inline]

Definition at line 101 of file http.h.

{return Ok;}
static PHttpRq THttpRq::Load ( TSIn ) [inline, static]

Definition at line 95 of file http.h.

References Fail.

{Fail; return NULL;}
static PHttpRq THttpRq::New ( const PSIn SIn) [inline, static]

Definition at line 82 of file http.h.

Referenced by ParseHttpRq(), and ParseSearch().

                                     {
    return PHttpRq(new THttpRq(SIn));}

Here is the caller graph for this function:

static PHttpRq THttpRq::New ( const THttpRqMethod Method,
const PUrl Url,
const TStr ContTypeFldVal,
const TMem BodyMem 
) [inline, static]

Definition at line 87 of file http.h.

                                                   {
    return PHttpRq(new THttpRq(Method, Url, ContTypeFldVal, BodyMem));}
static PHttpRq THttpRq::New ( const PUrl Url,
const int &  FetchId = -1 
) [inline, static]

Definition at line 91 of file http.h.

References hrmGet.

                                                            {
    return PHttpRq(new THttpRq(hrmGet, Url, "", TMem(), FetchId));}
THttpRq& THttpRq::operator= ( const THttpRq ) [inline]

Definition at line 98 of file http.h.

References Fail.

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

Definition at line 513 of file http.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), BodyMem, THttpLx::ClrMemSf(), CompleteP, THttpLx::Eof(), FldNmToValH, TMem::GetAsStr(), THttpLx::GetCrLf(), GetFldVal(), THttpLx::GetFldVal(), THttpLx::GetInt(), TStr::GetInt(), THttpLx::GetMemSf(), THttpLx::GetNrStr(), THttpLx::GetPeriod(), THttpLx::GetRest(), THttpLx::GetRqMethod(), TUrl::GetSearchStr(), THttpLx::GetSpec(), THttpLx::GetToken(), THttpLx::GetUrlStr(), THttpLx::GetWs(), HdStr, heBadUrl, hrmGet, hrmPost, THttpLx::IsCrLf(), IsFldNm(), TUrl::IsOk(), TMem::Len(), MajorVerN, Method, MinorVerN, New(), Ok, ParseSearch(), TMem::Trunc(), and Url.

Referenced by THttpRq().

                                        {
  THttpLx Lx(SIn);
  // initial status
  Ok=false;
  CompleteP=false;
  // request-line
  Method=Lx.GetRqMethod();
  Lx.GetWs();
  //Url=Lx.GetUrl();
  TStr UrlStr=Lx.GetUrlStr();
  Lx.GetWs();
  Lx.GetToken(THttp::HttpStr); Lx.GetSpec(THttp::SlashStr);
  MajorVerN=Lx.GetInt(1); Lx.GetPeriod(); MinorVerN=Lx.GetInt(1);
  Lx.GetCrLf();
  // header fields & values
  while ((!Lx.Eof())&&(!Lx.IsCrLf())){
    TStr FldNm=Lx.GetToken(); Lx.GetSpec(THttp::ColonStr);
    TStr FldVal=Lx.GetFldVal();
    Lx.GetCrLf();
    TStr NrFldNm=THttpLx::GetNrStr(FldNm);
    FldNmToValH.AddDat(NrFldNm, FldVal);
  }
  // separator CrLf
  if (!Lx.IsCrLf()){return;} // to avoid exceptions
  Lx.GetCrLf();
  // header & body strings
  HdStr=Lx.GetMemSf().GetAsStr();
  Lx.ClrMemSf();
  Lx.GetRest();
  BodyMem=Lx.GetMemSf();
  // completeness
  int ContLen=GetFldVal(THttp::ContLenFldNm).GetInt(-1);
  if (ContLen==-1){
    // if not content-len is given we assume http-request is ok
    CompleteP=true;
  } else {
    if (ContLen<=BodyMem.Len()){
      // if we read enough data, we claim completeness
      CompleteP=true;
      BodyMem.Trunc(ContLen);
    } else {
      // if we read not enough data we claim incompleteness
      CompleteP=false;
    }
  }
  // url
  if (CompleteP){
    const TStr LocalBaseUrlStr="http://localhost/";
    Url=TUrl::New(UrlStr, LocalBaseUrlStr);
    if (!Url->IsOk()){
      throw THttpEx(heBadUrl);}
  }
  // search string
  TStr SearchStr;
  if (Method==hrmGet){
    SearchStr=Url->GetSearchStr();
  } else
  if ((Method==hrmPost)&&(
   (!IsFldNm(THttp::ContTypeFldNm))||
   (GetFldVal(THttp::ContTypeFldNm)==THttp::TextHtmlFldVal)||
   (GetFldVal(THttp::ContTypeFldNm)==THttp::AppW3FormFldVal))){
    SearchStr=TStr("?")+BodyMem.GetAsStr();
  }
  ParseSearch(SearchStr);
  // at this point ok=true
  Ok=true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void THttpRq::ParseSearch ( const TStr SearchStr) [private]

Definition at line 462 of file http.cpp.

References TChA::AddCh(), TUrlEnv::AddToKeyVal(), TChA::Clr(), THttpChRet::Eof(), THttpChRet::GetCh(), TCh::GetHex(), heBadSearchStr, TCh::IsHex(), New(), Ok, and UrlEnv.

Referenced by ParseHttpRq(), and THttpRq().

                                              {
  PSIn SIn=TStrIn::New(SearchStr);
  THttpChRet ChRet(SIn, heBadSearchStr);
  try {
  // check empty search string
  if (ChRet.Eof()){return;}
  // require '?' at the beginning
  if (ChRet.GetCh()!='?'){
    throw THttpEx(heBadSearchStr);}
  // parse key=val{&...} pairs
  TChA KeyNm; TChA ValStr;
  while (!ChRet.Eof()){
    char Ch; KeyNm.Clr(); ValStr.Clr();
    // key
    while ((Ch=ChRet.GetCh())!='='){
      switch (Ch){
        case '%':{
          char Ch1=ChRet.GetCh();
                  if (!TCh::IsHex(Ch1)) { throw THttpEx(heBadSearchStr); }
                  char Ch2=ChRet.GetCh();
                  if (!TCh::IsHex(Ch2)) { throw THttpEx(heBadSearchStr); }
          KeyNm.AddCh(char(16*TCh::GetHex(Ch1)+TCh::GetHex(Ch2)));} break;
        case '+': KeyNm.AddCh(' '); break;
        case '&': throw THttpEx(heBadSearchStr);
        default: KeyNm.AddCh(Ch);
      }
    }
    // equals
    if (Ch!='='){
      throw THttpEx(heBadSearchStr);}
    // value
    while ((!ChRet.Eof())&&((Ch=ChRet.GetCh())!='&')){
      switch (Ch){
        case '%':{
          char Ch1=ChRet.GetCh();
                  if (!TCh::IsHex(Ch1)) { throw THttpEx(heBadSearchStr); }
          char Ch2=ChRet.GetCh();
                  if (!TCh::IsHex(Ch2)) { throw THttpEx(heBadSearchStr); }
          ValStr.AddCh(char(16*TCh::GetHex(Ch1)+TCh::GetHex(Ch2)));} break;
        case '+': ValStr.AddCh(' '); break;
        case '&': throw THttpEx(heBadSearchStr);
        default: ValStr.AddCh(Ch);
      }
    }
    // save key-value pair
    UrlEnv->AddToKeyVal(KeyNm, ValStr);
    }
  }
  catch (const THttpEx&){Ok=false;}
}

Here is the call graph for this function:

Here is the caller graph for this function:

void THttpRq::Save ( TSOut ) [inline]

Definition at line 96 of file http.h.

References Fail.

{Fail;}

Friends And Related Function Documentation

friend class TPt< THttpRq > [friend]

Definition at line 67 of file http.h.


Member Data Documentation

Definition at line 77 of file http.h.

Referenced by GetStr(), and ParseHttpRq().

bool THttpRq::CompleteP [private]

Definition at line 70 of file http.h.

Referenced by ParseHttpRq().

TCRef THttpRq::CRef [private]

Definition at line 67 of file http.h.

Definition at line 74 of file http.h.

Referenced by AddFldVal(), GetFldVal(), GetFldValH(), GetStr(), IsFldNm(), ParseHttpRq(), and THttpRq().

TStr THttpRq::HdStr [private]

Definition at line 76 of file http.h.

Referenced by ParseHttpRq(), and THttpRq().

int THttpRq::MajorVerN [private]

Definition at line 71 of file http.h.

Referenced by ParseHttpRq().

Definition at line 72 of file http.h.

Referenced by GetMethodNm(), and ParseHttpRq().

int THttpRq::MinorVerN [private]

Definition at line 71 of file http.h.

Referenced by ParseHttpRq().

bool THttpRq::Ok [private]

Definition at line 69 of file http.h.

Referenced by ParseHttpRq(), ParseSearch(), and THttpRq().

PUrl THttpRq::Url [private]

Definition at line 73 of file http.h.

Referenced by GetStr(), ParseHttpRq(), and THttpRq().

Definition at line 75 of file http.h.

Referenced by ParseSearch().


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