Ejemplo n.º 1
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);
  }
}
void TMongSrv::OnHttpRq(const int& SockId, const PHttpRq& HttpRq) {
	// check http-request correctness - return if error
	if (!HttpRq->IsOk()) {
		TNotify::OnNotify(Notify, ntInfo, "Web-Server: Bad Http Request.");
		return;
	}
	// check url correctness - return if error
	PUrl RqUrl = HttpRq->GetUrl();
	if (!RqUrl->IsOk()) {
		TNotify::OnNotify(Notify, ntInfo, "Web-Server: Bad Url Requested.");
		return;
	}

	// construct http-response
	PHttpResp HttpResp;
	if (!RqUrl->GetPathStr().Empty()) {
		// get request-file-name
		TStr ExeFPath = TSysProc::GetExeFNm().GetFPath();
		TStr RqFNm = RqUrl->GetPathStr();
		if (RqFNm.LastCh() == '/') {
			RqFNm = RqFNm + "default.htm";
		}
		if ((RqFNm[0] == '/') || (RqFNm[0] == '\\')) {
			RqFNm.DelSubStr(0, 0);
		}
		RqFNm = ExeFPath + RqFNm;
		// open file
		bool RqFOpened = false;
		PSIn RqSIn = TFIn::New(RqFNm, RqFOpened);
		if (!RqFOpened) {
			// prepare default html with time
			TChA HtmlChA;
			HtmlChA += "<html><title>Error - Not Found</title><body>";
			HtmlChA += "File: ";
			HtmlChA += RqUrl->GetPathStr();
			HtmlChA += " not found.";
			HtmlChA += "</body></html>";
			PSIn BodySIn = TMIn::New(HtmlChA);
			HttpResp = PHttpResp(
					new THttpResp(THttp::ErrNotFoundStatusCd,
							THttp::TextHtmlFldVal, false, BodySIn, ""));
		} else {
			// file successfully opened
			PSIn BodySIn = RqSIn;
			if (THttp::IsHtmlFExt(RqFNm.GetFExt())) {
				// send text/html mime type if Html filemg_callback_t
				HttpResp = PHttpResp(
						new THttpResp(THttp::OkStatusCd, THttp::TextHtmlFldVal,
								false, BodySIn, ""));
			} else if (THttp::IsGifFExt(RqFNm.GetFExt())) {
				// send image/gif mime type if Gif file
				HttpResp = PHttpResp(
						new THttpResp(THttp::OkStatusCd, THttp::ImageGifFldVal,
								false, BodySIn, ""));
			} else {
				// send application/octet mime type
				HttpResp = PHttpResp(
						new THttpResp(THttp::OkStatusCd, THttp::AppOctetFldVal,
								false, BodySIn, ""));
			}
		}
	} else {
		// prepare default html with time
		TChA HtmlChA;
		HtmlChA += "<html><title>Welcome to TWebSrv (powered by mongoose 3.1)</title><body>";
		HtmlChA += TSecTm::GetCurTm().GetStr();
		HtmlChA += "</body></html>";
		PSIn BodySIn = TMIn::New(HtmlChA);
		HttpResp = THttpResp::New(THttp::OkStatusCd, THttp::TextHtmlFldVal,
				false, BodySIn);
	}

	// construct & send response
	SendHttpResp(SockId, HttpResp);
	// notify
	if (RqUrl->IsOk()) {
		TChA MsgChA;
		MsgChA += "Web-Server: Request for '";
		MsgChA += RqUrl->GetUrlStr();
		MsgChA += "'.";
		TNotify::OnNotify(Notify, ntInfo, MsgChA);
	}
}