/* private */
void VVirtualFolder::_NormalizeFolder()
{
	assert (NULL != fFolder);

	if (!fFolder->Exists())
		fFolder->CreateRecursive();

	if (fFolder->Exists())
	{
		XBOX::VFilePath folderPath (fFolder->GetPath());
		XBOX::VFilePath indexPath;

		indexPath = folderPath.ToSubFile (fIndexFileName);

		if (indexPath.IsFile())
		{
			XBOX::VFile indexFile (indexPath);

			if (!indexFile.Exists())
			{
				XBOX::VFolder *componentFolder = VHTTPServer::RetainComponentFolder (kBF_RESOURCES_FOLDER);

				if (NULL != componentFolder)
				{
					XBOX::VFilePath		defaultIndexPath = componentFolder->GetPath();
					XBOX::DialectCode	dialectCode = XBOX::VIntlMgr::GetDefaultMgr()->GetCurrentDialectCode();
					XBOX::VString		languageCode;
					XBOX::VString		fileName;

					XBOX::VIntlMgr::GetDefaultMgr()->GetISO6391LanguageCode (dialectCode, languageCode);

					fileName.AppendCString ("index_");
					fileName.AppendString (languageCode);
					fileName.AppendCString (".html");

					defaultIndexPath.ToSubFolder (CVSTR ("Default Page")).ToSubFile (fileName);

					if (defaultIndexPath.IsFile())
					{
						XBOX::VFile defaultIndexFile (defaultIndexPath);

						if (defaultIndexFile.Exists())
							indexFile.CopyFrom (defaultIndexFile);
					}

					XBOX::QuickReleaseRefCountable (componentFolder);
				}
			}
		}
	}
}
static
XBOX::VError GetFileCreationTime (const XBOX::VFilePath& inFilePath, XBOX::VTime *outCreationTime)
{
	if (NULL == outCreationTime)
		return VE_INVALID_PARAMETER;

	XBOX::VError error = XBOX::VE_MEMORY_FULL;
	XBOX::VFile *file = new XBOX::VFile (inFilePath);
	
	if (NULL != file)
	{
		if (file->Exists())
			error = file->GetTimeAttributes (NULL, outCreationTime);
		else
			error = XBOX::VE_FILE_NOT_FOUND;
		
		XBOX::QuickReleaseRefCountable (file);
	}
	
	return error;
}
XBOX::VJSObject VJSTextStream::_Construct (VJSParms_withArguments &ioParms)
{
	XBOX::VJSObject	constructedObject(ioParms.GetContext());
	XBOX::VFile		*file;
	
	constructedObject.SetUndefined();
	if ((file = ioParms.RetainFileParam(1)) == NULL) {

		XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_FILE, "1");
		return constructedObject;

	}
	
	XBOX::VString	mode;
	bool			forwrite, overwrite	= false;
	CharSet			defaultCharSet		= VTC_UTF_8;
	sLONG			xcharset			= 0;

	if (ioParms.GetStringParam(2, mode) && mode == L"Overwrite") {

		forwrite = true;
		overwrite = true;

	} else

		forwrite = ioParms.GetBoolParam(2, L"Write", L"Read");
	
	if (ioParms.GetLongParam(3, &xcharset) && xcharset != 0)

		defaultCharSet = (CharSet) xcharset;
	
	XBOX::VError	err = XBOX::VE_OK;
	
	if (forwrite) {

		if (!file->Exists())

			err = file->Create();

		else if (overwrite) {

			if ((err = file->Delete()) == XBOX::VE_OK)

				err = file->Create();

		}

	}
	
	VFileStream	*stream = NULL;

	if (err == XBOX::VE_OK) {

		if ((stream = new VFileStream(file)) == NULL) 
		
			XBOX::vThrowError(XBOX::VE_MEMORY_FULL);

		else if (forwrite) {

			if (err == XBOX::VE_OK)

				err = stream->OpenWriting();

			if (err == XBOX::VE_OK)

				stream->SetCharSet(defaultCharSet);

			stream->SetPos(stream->GetSize());

		} else {

			err = stream->OpenReading();
			if (err == XBOX::VE_OK) {

				stream->GuessCharSetFromLeadingBytes(defaultCharSet);
				stream->SetCarriageReturnMode(eCRM_NATIVE);

			}

		}

	}

	if (stream != NULL && err == XBOX::VE_OK) {

		VJSTextStreamState	*textStreamState;
	
		if ((textStreamState = new VJSTextStreamState()) == NULL) {

			delete stream;
			XBOX::vThrowError(XBOX::VE_MEMORY_FULL);

		} else {

			textStreamState->fStream = stream;
			textStreamState->fBuffer.Clear();
			textStreamState->fIndex = 0;
			textStreamState->fPosition = 0;

			constructedObject = VJSTextStream::CreateInstance(ioParms.GetContextRef(), textStreamState);

		}
	
	} 
				
	ReleaseRefCountable(&file);

	return constructedObject;
}
XBOX::VError VHTTPConnectionListener::StartListening()
{
	XBOX::VError error = XBOX::VE_OK;

	if (NULL == fServerListener)
	{
		fServerListener = new VSockListener  (fRequestLogger);

		if (fSSLEnabled || fSSLMandatory)
		{
			if (fCertificatesFolderPath.IsFolder() && fCertificatesFolderPath.IsValid())
			{
				XBOX::VFile keyFile (fCertificatesFolderPath, STRING_KEY_FILE_NAME);
				XBOX::VFile certFile (fCertificatesFolderPath, STRING_CERT_FILE_NAME);

				if (keyFile.Exists() && certFile.Exists())
					error = fServerListener->SetCertificateFolder (fCertificatesFolderPath, STRING_KEY_FILE_NAME, STRING_CERT_FILE_NAME);
			}

			if (XBOX::VE_OK != error)
			{
				/*
				 *	If SSL publication is enabled but not mandatory: Silently disable SSL Publishing 
				 *	other else, if SSL is Mandatory: no Way... Throw an error and prevent StartListening to run
				 */

				if (fSSLEnabled)
				{
					XBOX::VErrorTaskContext *	errorTaskContext = XBOX::VTask::GetCurrent()->GetErrorContext (false);
					XBOX::VErrorContext *		errorContext = (errorTaskContext != NULL) ? errorTaskContext->GetLastContext() : NULL;

					errorContext->Flush();

					error = XBOX::VE_OK;
					fSSLEnabled = false;
				}
			}
		}

		if (XBOX::VE_OK == error)
		{
			/* We just here need to add 2 listeners (one on for http and another one for https)*/
			bool isOK = true;

			if (!fSSLMandatory)
				isOK = fServerListener->AddListeningPort (fListeningIP, fPort, false, fSocketDescriptor, fReuseAddressSocketOption);

			if (isOK && fSSLEnabled)
				fServerListener->AddListeningPort (fListeningIP, fSSLPort, true, fSSLSocketDescriptor, fReuseAddressSocketOption);

			/* Add a VConnectionHandlerFactory for HTTP requests */
			fConnectionHandlerFactory = new VHTTPConnectionHandlerFactory (fHTTPServer);
			if (NULL == fConnectionHandlerFactory)
				error = VE_HTTP_MEMORY_ERROR;

			if (XBOX::VE_OK == error)
			{
				fAbortTask = false;

				if (fServerListener->StartListening())
					Run();
				else
					error = XBOX::ServerNetTools::ThrowNetError (VE_SRVR_FAILED_TO_START_LISTENER);
			}
		}

		if  (XBOX::VE_OK != error)
			DeInit();
	}

	return error;
}