Beispiel #1
0
void Engine::createPredefinedObjects(JSContext *cx,JSObject *obj,HttpServerRequest &httpRequest)
{
	JSObject *predefinedObj = NULL;

	predefinedObj = JS_DefineObject(cx,obj,"response",JsHttpServerResponse::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE);
	if ( predefinedObj==NULL ) {
		LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"response\" object");
	}

	predefinedObj = JS_DefineObject(cx,obj,"request",JsHttpServerRequest::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE);
	if ( predefinedObj==NULL ) {
		LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"request\" object");
	}

	predefinedObj = JS_DefineObject(cx,obj,"server",JsServer::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE);
	if ( predefinedObj==NULL ) {
		LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"server\" object");
	}

	if ( httpRequest.getSession()!=NULL )
	{
		predefinedObj = JS_DefineObject(cx,obj,"session",JsHttpSession::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE);
		if ( predefinedObj==NULL ) {
			LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"session\" object");
		}
		else {
			JS_SetPrivate(cx,predefinedObj,new HttpSession::Ptr(httpRequest.getSession()));
		}
	}
	else
	{
		if ( JS_DefineProperty(cx,obj,"session",JSVAL_NULL,NULL,NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE)==JS_FALSE ) {
			LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"session\" object");
		}
	}

	if ( httpRequest.getSite()!=NULL )
	{
		predefinedObj = JS_DefineObject(cx,obj,"site",JsSite::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE);
		if ( predefinedObj==NULL ) {
			LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"site\" object");
		}
		else {
			JS_SetPrivate(cx,predefinedObj,httpRequest.getSite());
		}
	}
	else
	{
		if ( JS_DefineProperty(cx,obj,"site",JSVAL_NULL,NULL,NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE)==JS_FALSE ) {
			LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"site\" object");
		}
	}
}
Beispiel #2
0
bool Engine::executeDirective(std::string directive,HttpServerRequest &httpRequest,
	std::string &output)
{
	std::string name;

	std::map<std::string,std::string> attributes;

	if ( !parseDirective(directive,name,attributes) ) {
		return false;
	}

	if ( name=="include" )
	{
		std::string fileUri = attributes["file"];

		if ( httpRequest.getSite()!=NULL && Util::UriUtil::isValid(fileUri) )
		{
			if ( !Util::UriUtil::isAbsolute(fileUri) ) {
				fileUri = Util::UriUtil::getParentSegment(httpRequest.getUri()) + "/" + fileUri;
			}

			std::string filePath = httpRequest.getSite()->getRealPath(fileUri);
			FILE *file = fopen(filePath.c_str(),"rb");
			if ( file!=NULL )
			{
				std::string script;
				if ( parseFile(file,httpRequest,script) ) {
					output = script;
				}

				fclose(file);
			}

			return true;
		}
	}

	return false;
}