Esempio n. 1
0
void TWebTxtBsSrv::OnHttpRq(const int& SockId, const PHttpRq& HttpRq){
  // request parameters
  TStr RqContTypeStr=THttp::TextHtmlFldVal;
  PUrlEnv UrlEnv;
  TStr QueryStr; TStr EQueryStr;
  TStr HitSetStr; TStr AcceptStr;
  // prepare & extract search-environment
  if (HttpRq->IsOk()){
    // prepare search-environment
    PUrl Url=HttpRq->GetUrl();
    UrlEnv=HttpRq->GetUrlEnv();
    // if empty search-environment and url-path is not empty
    if (UrlEnv->Empty()&&
     (Url->GetPathSegs()>0)&&(!Url->GetPathSeg(0).Empty())){
      // get document name
      TStr DocNm=Url->GetPathSeg(Url->GetPathSegs()-1);
      if (WebTxtBs->GetTxtBs()->IsDoc(DocNm)){
        // document exists in text-base
        TStr DocStr=WebTxtBs->GetTxtBs()->GetDocStr(DocNm);
        PSIn HttpBodySIn=TMIn::New(DocStr);
        PHttpResp HttpResp=
         THttpResp::New(THttp::OkStatusCd, RqContTypeStr, false, HttpBodySIn);
        SendHttpResp(SockId, HttpResp);
      } else {
        // ordinary http request
        TWebSrv::OnHttpRq(SockId, HttpRq);
      }
      // end if no search request
      return;
    }
    // extract fields from search-environment
    QueryStr=UrlEnv->GetVal(QueryUrlFldNm).GetTrunc();
    EQueryStr=THtmlLx::GetEscapedStr(QueryStr);
    HitSetStr=UrlEnv->GetVal(HitSetUrlFldNm).GetTrunc();
    AcceptStr=UrlEnv->GetVal(AcceptUrlFldNm).GetTrunc();
    if (AcceptStr.Empty()){RqContTypeStr=GetRqContType(HttpRq);}
    else {RqContTypeStr=AcceptStr;}
  }

  // hit-set
  int HitSetN=1;
  HitSetStr.IsInt(true, 1, TInt::Mx, HitSetN);
  int HitSetDocs=GetVarVal(RqContTypeStr, "HitSetDocs").GetInt();
  int StrHitSets=GetVarVal(RqContTypeStr, "StrHitSets").GetInt();

  // output buffer
  TChA OutChA(10000);
  // header
  TStr HdTpl=GetTplVal(RqContTypeStr, "Header");
  HdTpl.ChangeStrAll(QueryMacro, EQueryStr);
  OutChA+=HdTpl;
  // html body
  if (HttpRq->IsOk()){
    if (!QueryStr.Empty()){
      // execute query
      PTxtBsRes TxtBsRes=WebTxtBs->Search(QueryStr);
      TStr EWixExpStr=THtmlLx::GetEscapedStr(TxtBsRes->GetWixExpStr());
      // log string
      TChA QueryInfoChA;
      QueryInfoChA+="Query: "+QueryStr;
      //QueryInfoChA+=" ["+GetPeerNm(SockId)+"]";
      QueryInfoChA+=" ["+TSecTm::GetCurTm().GetStr()+"]";
      TNotify::OnNotify(Notify, ntInfo, QueryInfoChA);
      SLog->PutStr(QueryInfoChA); SLog->PutLn(); SLog->Flush();
      // query-results processing
      if (TxtBsRes->IsOk()){
        // result header
        TStr ResultHdTpl=GetTplVal(RqContTypeStr, "ResultHd");
        ResultHdTpl.ChangeStrAll(QueryMacro, EWixExpStr);
        ResultHdTpl.ChangeStrAll(HitsMacro, TInt::GetStr(TxtBsRes->GetDocs()));
        OutChA+=ResultHdTpl;
        // result records
        int MnDocN; int MxDocN;
        TxtBsRes->GetHitSetMnMxDocN(HitSetN, HitSetDocs, MnDocN, MxDocN);
        for (int DocN=MnDocN; DocN<=MxDocN; DocN++){
          // get result document data
          int MxDocTitleLen=GetVarVal(RqContTypeStr, "MxDocTitleLen").GetInt();
          int MxDocCtxLen=GetVarVal(RqContTypeStr, "MxDocCtxLen").GetInt();
          TStr DocNm; TStr DocTitleStr; TStr DocStr; TStr DocCtxStr;
          TxtBsRes->GetDocInfo(DocN, MxDocTitleLen, MxDocCtxLen,
           DocNm, DocTitleStr, DocStr, DocCtxStr);
          if (DocTitleStr.Empty()){DocTitleStr=DocNm;}
          // result record
          TStr ResultRecTpl=GetTplVal(RqContTypeStr, "ResultRec");
          ResultRecTpl.ChangeStrAll(HitNumMacro, TInt::GetStr(DocN+1));
          ResultRecTpl.ChangeStrAll(DocAddrMacro, DocNm);
          ResultRecTpl.ChangeStrAll(DocTitleMacro, DocTitleStr);
          ResultRecTpl.ChangeStrAll(DocCtxMacro, DocCtxStr);
          OutChA+=ResultRecTpl;
        }
        // result footer
        TStr ResultFtTpl=GetTplVal(RqContTypeStr, "ResultFt");
        OutChA+=ResultFtTpl;
        // hit-set
        AddHitSetChA(TxtBsRes, RqContTypeStr, HitSetN, HitSetDocs, StrHitSets,
         UrlEnv, OutChA);
      } else {
        // bad query
        TStr BadQueryTpl=GetTplVal(RqContTypeStr, "BadQuery");
        BadQueryTpl.ChangeStrAll(QueryMacro, EWixExpStr);
        OutChA+=BadQueryTpl;
      }
    }
  } else {
    // bad http-request
    TStr BadHttpRqTpl=GetTplVal(RqContTypeStr, "BadHttpRq");
    OutChA+=BadHttpRqTpl;
  }

  // footer
  TStr FtTpl=GetTplVal(RqContTypeStr, "Footer");
  FtTpl.ChangeStrAll(QueryMacro, EQueryStr);
  OutChA+=FtTpl;

  // construct & send response
  PSIn HttpBodySIn=TMIn::New(OutChA);
  PHttpResp HttpResp=
   THttpResp::New(THttp::OkStatusCd, RqContTypeStr, false, HttpBodySIn);
  SendHttpResp(SockId, HttpResp);
}
Esempio n. 2
0
short DisplayVar(short handle,short varNo,TDataKind varKind,unsigned short
                                                                  dataSection)
/* Function to display all information for a file or DS variable
                                                    Return 1 if ok 0 if not */
{
    short    handleNo;
    short    procNo;
    short    errNo;                                   /* for error handling */
    TVarDesc var;                        /*to hold the variable description */
    short    ret;                                       /* for return value */
    char     onebyte;                           /* for INT1 and WRD1 values */
    short    twobyte;                           /* for INT2 and WRD2 values */
    long     fourbyte;                                   /* for INT4 values */
    float    realv;                                       /* for RL4 values */
    double   doubv;                                       /* for RL8 values */
    char     s[256];                                   /* for string values */

    ret = 1;                                      /* return value if all ok */
                            /* use function to look at varaible description */
    GetVarDesc(handle,                                   /* CFS file handle */
               varNo,                                    /* variable number */
               varKind,                                 /* FILEVAR or DSVAR */ 
               &var.vSize,                  /* for return of variables size */
               &var.vType,     /* for return of variable type INT1,WRD1 etc.*/
               var.varUnits,                /* for return of variable units */
               var.varDesc); /* for return of users description of variable */
    if (FileError(&handleNo,&procNo,&errNo)!=0)         /* check for errors */
    {
        printf("\nError %d %d %d",handleNo,procNo,errNo);
        ret = 0;                                  /* return value if not ok */
    }
    else               /* report details and get and display variable value */
    {
        if (varKind==DSVAR)
            printf("\nData Section %u data section ",dataSection);
        else
            printf("\nFile ");               /* start with kind of variable */
        printf("variable %d",varNo);                      /* and its number */
        printf("\nUnits %s",var.varUnits);                  /* report units */
        printf("\nDescription %s",var.varDesc); /* report users description */
     /* space needed for return value depends on type so print type, get value
                                         and print value all in case satement.
             NB errors in GetVarVal are not tested until after value report */

        switch (var.vType)
        {
            case INT1 : printf("\nType INT1");
                        GetVarVal(handle,varNo,varKind,dataSection,&onebyte);
                        printf("\nValue %d\n",(int)onebyte);
                        break;
            case WRD1 : printf("\nType WRD1");
                        GetVarVal(handle,varNo,varKind,dataSection,&onebyte);
                        printf("\nValue %u\n",(int)onebyte);
                        break;
            case INT2 : printf("\nType INT2");
                        GetVarVal(handle,varNo,varKind,dataSection,&twobyte);
                        printf("\nValue %d\n",(int)twobyte);
                        break;
            case WRD2 : printf("\nType WRD2");
                        GetVarVal(handle,varNo,varKind,dataSection,&twobyte);
                        printf("\nValue %u\n",(int)twobyte);
                        break;
            case INT4 : printf("\nType INT4");
                        GetVarVal(handle,varNo,varKind,dataSection,&fourbyte);
                        printf("\nValue %ld\n",fourbyte);
                        break;
            case RL4  : printf("\nType RL4");
                        GetVarVal(handle,varNo,varKind,dataSection,&realv);
                        printf("\nValue %f\n",(double)realv);
                        break;
            case RL8  : printf("\nType RL8");
                        GetVarVal(handle,varNo,varKind,dataSection,&doubv);
                        printf("\nValue %f\n",doubv);
                        break;
            case LSTR : printf("\nType LSTR");
                        GetVarVal(handle,varNo,varKind,dataSection,s);
                        printf("\nValue %s\n",s);
                        break;
            default   : printf("\nUndocumented error\n");
                        ret=0;
                        break;
        };
        if (FileError(&handleNo,&procNo,&errNo)!=0)     /* check for errors */
        {
            printf("\nError %d %d %d",handleNo,procNo,errNo);
            ret = 0;                              /* return value if not ok */
        }
    }
   return ret;
};                                                     /* end of DisplayVar */