Ejemplo n.º 1
0
// replay a particular http request
void TReplaySrv::ReplayHttpRq(const PHttpRq& HttpRq)
{
	PUrl HttpRqUrl = HttpRq->GetUrl();
	// extract function name
	TStr FunNm = HttpRqUrl->GetPathSeg(0);
	// extract parameters
	TStrKdV FldNmValPrV;
	PUrlEnv HttpRqUrlEnv = HttpRq->GetUrlEnv();
	const int Keys = HttpRqUrlEnv->GetKeys();
	for (int KeyN = 0; KeyN < Keys; KeyN++) {
		TStr KeyNm = HttpRqUrlEnv->GetKeyNm(KeyN);
		const int Vals = HttpRqUrlEnv->GetVals(KeyN);
		for (int ValN = 0; ValN < Vals; ValN++) {
			TStr Val = HttpRqUrlEnv->GetVal(KeyN, ValN);
			FldNmValPrV.Add(TStrKd(KeyNm, Val));
		}
	}
	// processed requested function
	if (!FunNm.Empty()) {
		// prepare request environment
		PSAppSrvRqEnv RqEnv = TSAppSrvRqEnv::New(this, TUInt64::Mx, HttpRq, FunNmToFunH);
		// retrieve function
		PSAppSrvFun SrvFun = FunNmToFunH.GetDat(FunNm);
		// call function
		SrvFun->Exec(FldNmValPrV, RqEnv);
	}
}
Ejemplo n.º 2
0
// a http request is received. process it using the standard TSAppSrv method 
// and then optionally also save the request to the log file
void TReplaySrv::OnHttpRq(const uint64& SockId, const PHttpRq& HttpRq)
{
	// if we are logging requests then log it
	if (!SOut.Empty()) {
		try {
			PUrl HttpRqUrl = HttpRq->GetUrl();
			TStr FunNm = HttpRqUrl->GetPathSeg(0);
			if (LoggingFunNmH.IsKey(FunNm)) {
				THttpReqSerInfo ReqInfo(HttpRq);
				ReqInfo.Save(*SOut);
				if (FlushEachRequest)
					SOut->Flush();
			}
		}
		catch (PExcept Except) {
			TFileNotify Notify("ReplaySrvError.txt");
			Notify.OnNotifyFmt(ntErr, "TReplaySrv::OnHttpRq. Exception %s.", Except->GetMsgStr().CStr());
			Notify.OnNotifyFmt(ntErr, "TReplaySrv::OnHttpRq. Location info: \n%s.", Except->GetLocStr().CStr());
		}
		catch (...) {
			TFileNotify Notify("ReplaySrvError.txt");
			Notify.OnNotify(ntErr, "TReplaySrv::OnHttpRq. Unrecognized exception.");
		}
	}

	// only after we've logged the request we also execute it - otherwise we could process the request and crash before saving the data
	TSAppSrv::OnHttpRq(SockId, HttpRq);
}
Ejemplo n.º 3
0
//////////////////////////////////////
// File-Download-Function
PSIn TSASFunFPath::ExecSIn(const TStrKdV& FldNmValPrV, 
        const PSAppSrvRqEnv& RqEnv, TStr& ContTypeStr) {

	// construct file name
    TStr FNm = FPath;
    PUrl Url = RqEnv->GetHttpRq()->GetUrl();
    const int PathSegs = Url->GetPathSegs();
    if ((PathSegs == 1) || (PathSegs == 2 && Url->GetPathSeg(1).Empty())) {
        // nothing specified, do the default
        TStr PathSeg = Url->GetPathSeg(0);
        if (PathSeg.LastCh() != '/') { FNm += "/"; }
        FNm += DefaultFNm; 
    } else {
        // extract file name
        for (int PathSegN = 1; PathSegN < PathSegs; PathSegN++) {
            FNm += "/"; FNm += Url->GetPathSeg(PathSegN);
        }
    }

    // get mime-type
	TStr FExt = FNm.GetFExt();
	if (FExt == ".htm") { ContTypeStr = THttp::TextHtmlFldVal; }
	else if (FExt == ".html") { ContTypeStr = THttp::TextHtmlFldVal; }
	else if (FExt == ".js") { ContTypeStr = THttp::TextJavaScriptFldVal; }
	else if (FExt == ".css") { ContTypeStr = THttp::TextCssFldVal; }
	else if (FExt == ".ico") { ContTypeStr = THttp::ImageIcoFldVal; }
	else if (FExt == ".png") { ContTypeStr = THttp::ImagePngFldVal; }
	else if (FExt == ".jpg") { ContTypeStr = THttp::ImageJpgFldVal; }
	else if (FExt == ".jpeg") { ContTypeStr = THttp::ImageJpgFldVal; }
	else if (FExt == ".gif") { ContTypeStr = THttp::ImageGifFldVal; }
	else {
		printf("Unknown MIME type for extension '%s' for file '%s'", FExt.CStr(), FNm.CStr());
		ContTypeStr = THttp::AppOctetFldVal; 
	}

    // return stream to the file
    return TFIn::New(FNm);
}
Ejemplo n.º 4
0
void TSASFunRedirect::Exec(const TStrKdV& FldNmValPrV, const PSAppSrvRqEnv& RqEnv) {
    PUrl Url = RqEnv->GetHttpRq()->GetUrl();
    TStr FunNm = (Url->GetPathSegs() > 1) ? Url->GetPathSeg(1) : "";
	EAssert(MapH.IsKey(FunNm));
	const TRedirect& Redirect =  MapH.GetDat(FunNm);
	TStrKdV _FldNmValPrV = Redirect.FldNmValPrV;
	for (int FldN = 0; FldN < FldNmValPrV.Len(); FldN++) {
		TStr OrgFldNm = FldNmValPrV[FldN].Key;
		if (Redirect.FldNmMapH.IsKey(OrgFldNm)) {
			_FldNmValPrV.Add(TStrKd(Redirect.FldNmMapH.GetDat(OrgFldNm), FldNmValPrV[FldN].Dat));
		}
	}
	RqEnv->ExecFun(Redirect.FunNm, _FldNmValPrV);
}
Ejemplo n.º 5
0
void TSAppSrv::OnHttpRq(const uint64& SockId, const PHttpRq& HttpRq) {
	// last appropriate error code, start with bad request
	int ErrStatusCd = THttp::BadRqStatusCd;
    try {
        // check http-request correctness - return if error
        EAssertR(HttpRq->IsOk(), "Bad HTTP request!");
        // check url correctness - return if error
		PUrl HttpRqUrl = HttpRq->GetUrl();
		EAssertR(HttpRqUrl->IsOk(), "Bad request URL!");
        // extract function name
		TStr FunNm = HttpRqUrl->GetPathSeg(0);
		// check if we have the function registered
		if (FunNm == "favicon.ico") {
			PHttpResp HttpResp = THttpResp::New(THttp::OkStatusCd,
				THttp::ImageIcoFldVal, false, Favicon.GetSIn());
			SendHttpResp(SockId, HttpResp); 
			return;
		} else if (!FunNm.Empty() && !FunNmToFunH.IsKey(FunNm)) { 
			ErrStatusCd = THttp::ErrNotFoundStatusCd;
			GetNotify()->OnStatusFmt("[AppSrv] Unknown function '%s'!", FunNm.CStr());
			TExcept::Throw("Unknown function '" + FunNm + "'!");
		}
        // extract parameters
        PUrlEnv HttpRqUrlEnv = HttpRq->GetUrlEnv();
		TStrKdV FldNmValPrV; HttpRqUrlEnv->GetKeyValPrV(FldNmValPrV);
        
		// report call
		if (ShowParamP) {  GetNotify()->OnStatus(HttpRq->GetUrl()->GetUrlStr()); }
		// request parsed well, from now on it's internal error
		ErrStatusCd = THttp::InternalErrStatusCd;
		// processed requested function
		if (!FunNm.Empty()) {
			// prepare request environment
			PSAppSrvRqEnv RqEnv = TSAppSrvRqEnv::New(this, SockId, HttpRq, FunNmToFunH);
			// retrieve function
			PSAppSrvFun SrvFun = FunNmToFunH.GetDat(FunNm);
			// call function
			SrvFun->Exec(FldNmValPrV, RqEnv);
		} else {
			// internal SAppSrv call
			if (!ListFunP) {
				// we are not allowed to list functions
				ErrStatusCd = THttp::ErrNotFoundStatusCd;
				TExcept::Throw("Unknown page");
			}
			// prepare a list of registered functions
            PJsonVal FunArrVal = TJsonVal::NewArr();
			int KeyId = FunNmToFunH.FFirstKeyId();
			while (FunNmToFunH.FNextKeyId(KeyId)) {
                FunArrVal->AddToArr(TJsonVal::NewObj("name", FunNmToFunH.GetKey(KeyId)));
			}
            PJsonVal ResVal = TJsonVal::NewObj();
            ResVal->AddToObj("port", GetPortN());
            ResVal->AddToObj("connections", GetConns());            
            ResVal->AddToObj("functions", FunArrVal);
			TStr ResStr = ResVal->SaveStr();
			// prepare response
			PHttpResp HttpResp = THttpResp::New(THttp::OkStatusCd, 
				THttp::AppJSonFldVal, false, TMIn::New(ResStr));
			// send response
			SendHttpResp(SockId, HttpResp); 
		}
    } catch (PExcept Except) {
		// known internal error
		TNotify::StdNotify->OnNotifyFmt(ntErr, "Error: %s", Except->GetMsgStr().CStr());
		TNotify::StdNotify->OnNotifyFmt(ntErr, "Error location info: %s", Except->GetLocStr().CStr());

        PJsonVal ErrorVal = TJsonVal::NewObj();
        ErrorVal->AddToObj("message", Except->GetMsgStr());
        ErrorVal->AddToObj("location", Except->GetLocStr());
        PJsonVal ResVal = TJsonVal::NewObj("error", ErrorVal);
        TStr ResStr = ResVal->SaveStr();
        // prepare response
		PHttpResp HttpResp = THttpResp::New(ErrStatusCd,
            THttp::AppJSonFldVal, false, TMIn::New(ResStr));
        // send response
	    SendHttpResp(SockId, HttpResp);
    } catch (...) {
		TNotify::StdNotify->OnNotify(ntErr, "Unknown internal error");
		// unknown internal error
        PJsonVal ResVal = TJsonVal::NewObj("error", "Unknown internal error");
        TStr ResStr = ResVal->SaveStr();
        // prepare response
		PHttpResp HttpResp = THttpResp::New(ErrStatusCd,
            THttp::AppJSonFldVal, false, TMIn::New(ResStr));
        // send response
	    SendHttpResp(SockId, HttpResp);
    }
}
Ejemplo n.º 6
0
void TSAppSrv::OnHttpRq(const uint64& SockId, const PHttpRq& HttpRq) {
	// last appropriate error code, start with bad request
	int ErrStatusCd = THttp::BadRqStatusCd;
    try {
        // check http-request correctness - return if error
        EAssertR(HttpRq->IsOk(), "Bad HTTP request!");
        // check url correctness - return if error
        PUrl RqUrl = HttpRq->GetUrl();
        EAssertR(RqUrl->IsOk(), "Bad request URL!");
        // extract function name
        PUrl HttpRqUrl = HttpRq->GetUrl();
        TStr FunNm = HttpRqUrl->GetPathSeg(0);
		// check if we have the function registered
		if (FunNm == "favicon.ico") {
			PHttpResp HttpResp = THttpResp::New(THttp::OkStatusCd,
				THttp::ImageIcoFldVal, false, Favicon.GetSIn());
			SendHttpResp(SockId, HttpResp); 
			return;
		} else if (!FunNm.Empty() && !FunNmToFunH.IsKey(FunNm)) { 
			ErrStatusCd = THttp::ErrNotFoundStatusCd;
			GetNotify()->OnStatusFmt("[AppSrv] Unknown function '%s'!", FunNm.CStr());
			TExcept::Throw("Unknown function '" + FunNm + "'!");
		}
        // extract parameters
        TStrKdV FldNmValPrV;
        PUrlEnv HttpRqUrlEnv = HttpRq->GetUrlEnv();
        const int Keys = HttpRqUrlEnv->GetKeys();
        for (int KeyN = 0; KeyN < Keys; KeyN++) {
            TStr KeyNm = HttpRqUrlEnv->GetKeyNm(KeyN);
            const int Vals = HttpRqUrlEnv->GetVals(KeyN);
            for (int ValN = 0; ValN < Vals; ValN++) {
                TStr Val = HttpRqUrlEnv->GetVal(KeyN, ValN);
                FldNmValPrV.Add(TStrKd(KeyNm, Val));
            }
        }
		// report call
		if (ShowParamP) {  GetNotify()->OnStatus(" " + HttpRq->GetUrl()->GetUrlStr()); }
		// request parsed well, from now on it's internal error
		ErrStatusCd = THttp::InternalErrStatusCd;
		// processed requested function
		if (!FunNm.Empty()) {
			// prepare request environment
			PSAppSrvRqEnv RqEnv = TSAppSrvRqEnv::New(this, SockId, HttpRq, FunNmToFunH);
			// retrieve function
			PSAppSrvFun SrvFun = FunNmToFunH.GetDat(FunNm);
			// call function
			SrvFun->Exec(FldNmValPrV, RqEnv);
		} else {
			// internal SAppSrv call
			if (!ListFunP) {
				// we are not allowed to list functions
				ErrStatusCd = THttp::ErrNotFoundStatusCd;
				TExcept::Throw("Unknown page");
			}
			// prepare a list of registered functions
			PXmlTok TopTok = TXmlTok::New("registered-functions");
			int KeyId = FunNmToFunH.FFirstKeyId();
			while (FunNmToFunH.FNextKeyId(KeyId)) {
				PXmlTok FunTok = TXmlTok::New("function");
				FunTok->AddArg("name", FunNmToFunH.GetKey(KeyId));
				TopTok->AddSubTok(FunTok);
			}
			TStr ResXmlStr; TXmlDoc::New(TopTok)->SaveStr(ResXmlStr);
			PSIn BodySIn = TMIn::New(TSAppSrvFun::XmlHdStr + ResXmlStr);
			// prepare response
			PHttpResp HttpResp = THttpResp::New(THttp::OkStatusCd, 
				THttp::TextXmlFldVal, false, BodySIn);
			// send response
			SendHttpResp(SockId, HttpResp); 
		}
    } catch (PExcept Except) {
		// known internal error
		PXmlTok TopTok = TXmlTok::New("error");
		TopTok->AddSubTok(TXmlTok::New("message", Except->GetMsgStr()));
		TopTok->AddSubTok(TXmlTok::New("location", Except->GetLocStr()));
		PXmlDoc ErrorXmlDoc = TXmlDoc::New(TopTok); 
        TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr);
        // prepare response
		PHttpResp HttpResp = THttpResp::New(ErrStatusCd, 
            THttp::TextHtmlFldVal, false, 
			TMIn::New(TSAppSrvFun::XmlHdStr + ResXmlStr));
        // send response
	    SendHttpResp(SockId, HttpResp); 
    } catch (...) {
		// unknown internal error
		PXmlDoc ErrorXmlDoc = TXmlDoc::New(TXmlTok::New("error")); 
        TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr);
        // prepare response
        PHttpResp HttpResp = THttpResp::New(ErrStatusCd, 
            THttp::TextHtmlFldVal, false, 
			TMIn::New(TSAppSrvFun::XmlHdStr + ResXmlStr));
        // send response
	    SendHttpResp(SockId, HttpResp); 
    }
}
Ejemplo n.º 7
0
void TSAppSrv::OnHttpRq(const int& SockId, const PHttpRq& HttpRq) {
    PHttpResp HttpResp;
    try {
        // check http-request correctness - return if error
        EAssertR(HttpRq->IsOk(), "Bad HTTP request!");
        // check url correctness - return if error
        PUrl RqUrl = HttpRq->GetUrl();
        EAssertR(RqUrl->IsOk(), "Bad request URL!");
        // extract function name
        PUrl HttpRqUrl = HttpRq->GetUrl();
        TStr FunNm = HttpRqUrl->GetPathSeg(0);
        EAssertR(FunNmToFunH.IsKey(FunNm) || FunNm.Empty(), "Unknown function '" + FunNm + "' !");
        // extract parameters
        TStrKdV FldNmValPrV;
        PUrlEnv HttpRqUrlEnv = HttpRq->GetUrlEnv();
        const int Keys = HttpRqUrlEnv->GetKeys();
        for (int KeyN = 0; KeyN < Keys; KeyN++) {
            TStr KeyNm = HttpRqUrlEnv->GetKeyNm(KeyN);
            const int Vals = HttpRqUrlEnv->GetVals(KeyN);
            for (int ValN = 0; ValN < Vals; ValN++) {
                TStr Val = HttpRqUrlEnv->GetVal(KeyN, ValN);
                FldNmValPrV.Add(TStrKd(KeyNm, Val));
            }
        }
        // log the call
        TStr TimeNow = TTm::GetCurLocTm().GetWebLogDateTimeStr(true);
        GetNotify()->OnStatus(TStr::Fmt("[%s] Request %s", TimeNow.CStr(), FunNm.CStr()));
		// prepare request environment
		PSAppSrvRqEnv RqEnv = TSAppSrvRqEnv::New(this, SockId, HttpRq);
		PSIn BodySIn; TStr ContTypeVal;
		if (!FunNm.Empty()) {
			// call function
			PSAppSrvFun SrvFun = FunNmToFunH.GetDat(FunNm);
			if (SrvFun->GetFunOutType() == saotXml) {
				PXmlDoc ResXmlDoc = SrvFun->Exec(FldNmValPrV, RqEnv);        
				TStr ResXmlStr; ResXmlDoc->SaveStr(ResXmlStr);
				//ResXmlDoc->SaveTxt(TFile::GetUniqueFNm("test.xml"));
				BodySIn = TMIn::New(XmlHdStr + ResXmlStr);
				ContTypeVal = THttp::TextXmlFldVal;
			} else if (SrvFun->GetFunOutType() == saotJSon) {
				TStr ResStr = SrvFun->ExecJSon(FldNmValPrV, RqEnv);
				BodySIn = TMIn::New(ResStr);
				//ContTypeVal = THttp::TextHtmlFldVal;
				ContTypeVal = THttp::AppJSonFldVal;
			} else {
				BodySIn = SrvFun->ExecCustom(FldNmValPrV, RqEnv, ContTypeVal);
			}
		} else {
			PXmlTok TopTok = TXmlTok::New("registered-functions");
			int KeyId = FunNmToFunH.FFirstKeyId();
			while (FunNmToFunH.FNextKeyId(KeyId)) {
				PXmlTok FunTok = TXmlTok::New("function");
				FunTok->AddArg("name", FunNmToFunH.GetKey(KeyId));
				TopTok->AddSubTok(FunTok);
			}
			PXmlDoc ResXmlDoc = TXmlDoc::New(TopTok);
			TStr ResXmlStr; ResXmlDoc->SaveStr(ResXmlStr);
			BodySIn = TMIn::New(XmlHdStr + ResXmlStr);
			ContTypeVal = THttp::TextXmlFldVal;
		}
        // prepare response
        HttpResp = THttpResp::New(THttp::OkStatusCd, 
            ContTypeVal, false, BodySIn);
        // send response
    } catch (PExcept Except) {
		PXmlTok TopTok = TXmlTok::New("error");
		TopTok->AddSubTok(TXmlTok::New("message", Except->GetMsgStr()));
		TopTok->AddSubTok(TXmlTok::New("location", Except->GetLocStr()));
		PXmlDoc ErrorXmlDoc = TXmlDoc::New(TopTok); 
        TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr);
        HttpResp = THttpResp::New(THttp::OkStatusCd, 
            THttp::TextHtmlFldVal, false, 
			TMIn::New(XmlHdStr + ResXmlStr));
    } catch (...) {
		PXmlDoc ErrorXmlDoc = TXmlDoc::New(TXmlTok::New("error")); 
        TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr);
        HttpResp = THttpResp::New(THttp::OkStatusCd, 
            THttp::TextHtmlFldVal, false, 
			TMIn::New(XmlHdStr + ResXmlStr));
    }
    SendHttpResp(SockId, HttpResp); 
}