int TWebNetCltV::FetchUrl(const PUrl& Url, const int& _FetchId, TStr ConnUid){
  IAssert(Url->IsOk(usHttp));
  //FetchingP=true;
  // prepare fetch-id & http-request
  int FetchId=_FetchId;
  if (_FetchId==-1){
    FetchId=GetNextFetchId();}
  PHttpRq HttpRq=THttpRq::New(Url, FetchId);
  // prepare host-port-string
  TStr HostNm=Url->GetHostNm();
  int PortN=Url->GetPortN();
  TStr ConnIdStr=TWebNetClt::GetConnIdStr(HostNm, PortN, ConnUid);
  // check if already connected
  PWebNetClt WebNetClt;
  if (IsGetWebNetClt(ConnIdStr, WebNetClt)){
    if (WebNetClt->IsConnected()){
      // already connected; send http-request
      WebNetClt->SendHttpRq(HttpRq);
    } else {
      // not connected; process of connecting
      WebNetClt->PushToWaitQ(FetchId, HttpRq);
    }
  } else {
    // not connected; connect
    //SaveToErrLog((TStr("NetCltConnect: ")+ConnIdStr).CStr());
    WebNetClt=TWebNetClt::New(HostNm, PortN, this, ConnIdStr, Notify);
    AddWebNetClt(ConnIdStr, WebNetClt);
    WebNetClt->PushToWaitQ(FetchId, HttpRq);
  }
  //FetchingP=false;
  return FetchId;
}
Ejemplo n.º 2
0
void TWebPgFetchEvent::OnConnect(const uint64& SockId){
  TChA RqChA;
  if (CurUrl->IsHttpRqStr()){
    RqChA=CurUrl->GetHttpRqStr();
  } else {
    // get http components
    TStr HostNm=CurUrl->GetHostNm();
    TStr AbsPath=CurUrl->GetPathStr()+CurUrl->GetSearchStr();
    // compose http request
    RqChA+="GET "; RqChA+=AbsPath; RqChA+=" HTTP/1.0\r\n";
    RqChA+="Host: "; RqChA+=HostNm; RqChA+="\r\n";
    if (!UserAgentStr.Empty()){ 
      RqChA+="User-Agent: "; RqChA+=UserAgentStr; RqChA+="\r\n";}
    // add cookies
    if (GetCookies()>0){
      RqChA+="Cookie: ";
      for (int CookieN=0; CookieN<GetCookies(); CookieN++){
        TStr KeyNm; TStr ValStr; TStr DmNm; TStr PathStr;
        GetCookie(CookieN, KeyNm, ValStr, DmNm, PathStr);
        if (HostNm.EndsWith(DmNm)){
          if (CookieN>0){RqChA+="; ";}
          RqChA+=KeyNm; RqChA+='='; RqChA+=ValStr; 
        }
      }
      RqChA+="\r\n";
    }
    // finish request
    RqChA+="\r\n";
  }
  // send http request
  PSIn RqSIn=TMIn::New(RqChA);
  bool Ok; TStr ErrMsg; Sock->Send(RqSIn, Ok, ErrMsg);
  if (Ok){
    Sock->PutTimeOut(TimeOutMSecs);
  } else {
    OnFetchError("Unable to send the data: " + ErrMsg);
  }
}