Example #1
0
void TWebPgFetchEvent::OnFetchEnd(const PHttpResp& HttpResp){
  EAssert(HttpResp->IsOk());
  EndMSecs=TTm::GetCurUniMSecs();
  int StatusCd=HttpResp->GetStatusCd();
  if (StatusCd/100==2){ // codes 2XX - ok
    ChangeLastUrlToLc(HttpResp);
    PWebPg WebPg=TWebPg::New(UrlStrV, IpNumV, HttpResp);
    WebPg->PutFetchMSecs(GetFetchMSecs());
    CloseConn(); Fetch->DisconnUrl(FId);
    Fetch->OnFetch(FId, WebPg);
  } else
  if (StatusCd/100==3){ // codes 3XX - redirection
    ChangeLastUrlToLc(HttpResp);
    if (UrlStrV.Len()<5){
      TStr RedirUrlStr=HttpResp->GetFldVal("Location");
      PUrl RedirUrl=TUrl::New(RedirUrlStr, CurUrl->GetUrlStr());
      if (RedirUrl->IsOk(usHttp)){
        TStrQuV CookieQuV; HttpResp->GetCookieKeyValDmPathQuV(CookieQuV);
        AddCookieKeyValDmPathQuV(CookieQuV);
        CurUrl=RedirUrl; Retries=0; CloseConn();
        TSockHost::GetAsyncSockHost(CurUrl->GetIpNumOrHostNm(), this);
      } else {
        TStr MsgStr=TStr("Invalid Redirection URL (")+RedirUrlStr+")";
        OnFetchError(MsgStr);
      }
    } else {
      TStr MsgStr=TStr("Cycling Redirection [")+TopUrl->GetUrlStr()+"]";
      OnFetchError(MsgStr);
    }
  } else { // all other codes - error
    TStr MsgStr=TStr("Http Error (")+
     TInt::GetStr(StatusCd)+"/"+HttpResp->GetReasonPhrase()+")";
    OnFetchError(MsgStr);
  }
}
void TWebNetClt::OnHttpResp(const PHttpResp& HttpResp){
  IAssert(HttpResp->IsOk());
  // get fetch-id
  TStr FetchIdStr=HttpResp->GetFldVal(THttp::FetchIdFldNm);
  int FetchId=FetchIdStr.GetInt(-1);

  // check if response has correct fetch-id
  if (IsSentQEmpty()){return;} // queue can be emptied by timeout...
  int TopFetchId; TTm TopCreateTm; TopFromSentQ(TopFetchId, TopCreateTm);
  IAssert(FetchId==TopFetchId);
  PopFromSentQ();

  // get status-code
  int StatusCd=HttpResp->GetStatusCd();
  // process http-response
  if (StatusCd/100==2){ // codes 2XX - ok
    WebNetCltV->OnHttpResp(FetchId, HttpResp);
  } else
  if (StatusCd/100==3){ // codes 3XX - redirection
    TStr RedirUrlStr=HttpResp->GetFldVal("Location");
    PUrl RedirUrl=TUrl::New(RedirUrlStr);
    if (RedirUrl->IsOk(usHttp)){
      //SaveToErrLog((TStr("Redirect: ")+HttpResp->GetHdStr()).CStr());
      TStr Host=HttpResp->GetFldVal(THttp::HostFldNm);
      WebNetCltV->FetchUrl(RedirUrl, FetchId, Host);
    } else {
      TStr MsgStr=TStr("Invalid Redirection URL (")+RedirUrlStr+")";
      WebNetCltV->OnHttpRespError(FetchId, MsgStr);
    }
  } else { // all other codes - error
    TStr MsgStr=TStr("Http Error (")+
     TInt::GetStr(StatusCd)+"/"+HttpResp->GetReasonPhrase()+")";
    WebNetCltV->OnHttpRespError(FetchId, MsgStr);
  }
}