Beispiel #1
0
BOOL CRKey::Replace(LPCTSTR pSrc, LPCTSTR pDst)
{_STTEX();
	// Open files
	CWinFile src, dst;
	if ( !src.OpenExisting( pSrc, GENERIC_READ ) ) return FALSE;
	if ( !dst.OpenNew( pDst, GENERIC_WRITE ) ) return FALSE;

	// Read in data
	TMem< BYTE > in;	
	if ( !in.allocate( src.Size() ) ) return FALSE;
	if ( !src.Read( in.ptr(), in.size() ) ) return FALSE;
	src.Close();

	// Run replace function
	CPipe	outpipe;
	DWORD	op = 0, i = 0;
	char	token[ 256 ];
	while ( Replace(	&outpipe, &op, in.str(), in.size(), 
						NULL, NULL, token, NULL, &i ) )
	{

	} // end while

	// Write out the data
	return dst.Write( outpipe.GetBuffer(), outpipe.GetBufferSize() );
}
Beispiel #2
0
BOOL CReg::EncodeUrl(CPipe *pPipe, char chSepNameVal, char chSepValues )
{
	// Ensure valid pipe
	if ( NULL == pPipe ) return FALSE;

	TMem< char > buf;
	LPREGKEY prk = NULL;
	while( ( prk = (LPREGKEY)GetNext( prk ) ) != NULL )
	{
		// Write separator if needed
		if ( pPipe->GetBufferSize() ) pPipe->Write( &chSepValues, 1 );

		LPCTSTR pName = prk->key->GetName();
		CRKey *pRk = GetKey( pName );
		if ( pRk ) 
		{
			// Write the name
			if ( buf.grow( CCfgFile::GetMinCanonicalizeBufferSize( strlen( pName ) ) ) )
			{	buf.Zero();
				CCfgFile::CanonicalizeBuffer( buf, (LPBYTE)pName, strlen( pName ) );
				pPipe->Write( buf );
			} // end if

			// Separator
			pPipe->Write( &chSepNameVal, 1 );

			// Save the key value
			CPipe tpipe;
			prk->key->EncodeUrl( &tpipe, 1, chSepNameVal, chSepValues );
			if ( buf.grow( CCfgFile::GetMinCanonicalizeBufferSize( tpipe.GetBufferSize() ) ) )
			{	buf.Zero();
				CCfgFile::CanonicalizeBuffer( buf, (LPBYTE)tpipe.GetBuffer(), tpipe.GetBufferSize() );
				pPipe->Write( buf );
			} // end if			

		} // end if

	} // end while

	return TRUE;
}
Beispiel #3
0
BOOL CRKey::Set(LPCTSTR pName, CRKey *pRk)
{_STTEX();

	if ( !pRk || !pRk->Size() )
	{
		// Clear data
		Set( pName, "" );

		return TRUE;

	} // end if

	// Serialize variables
	CPipe pipe;
	pRk->EncodeUrl( &pipe );

	// Write a NULL character
	pipe.Write( "", 1 );

	// Save the serialized variables
	Set( pName, (LPCTSTR)pipe.GetBuffer() );

	return TRUE;
}
Beispiel #4
0
BOOL CNetFile::HttpRequest(LPCTSTR pUrl, CRKey *pHeaders, CRKey *pData, LPCTSTR pMethod, LPCTSTR pLocal, BOOL bCloseFileAfterDownload, HWND hWndParent, DWORD dwUrlEncoding)
{_STT();
	// Lose previous file
	Destroy();

	// Downloading
	m_bUpload = FALSE;

	// Save parent window
	m_hWndParent = hWndParent;

	// Save close file status
	m_bCloseFileAfterDownload = bCloseFileAfterDownload;

	// Sanity check
	if ( pUrl == NULL || *pUrl == NULL ) return FALSE;

	// Are we downloading to file or ram?
	m_bMem = ( pLocal == NULL );

	{ // Copy the url
		
		char buf[ sizeof( m_szUrl ) ] = { 0 };
		DWORD size = sizeof( m_szUrl ) - 1;

		// Is it a local file?
		if ( GetFileAttributes( pUrl ) == MAXDWORD )
		{
			// Fix the url
			if ( InternetCanonicalizeUrl( pUrl, buf, &size, 0 ) )
			{	strcpy_sz( m_szUrl, buf ); }

			// Copy the url name
			else strcpy_sz( m_szUrl, pUrl );
		} // end if
		
		// Copy the local file name
		else strcpy_sz( m_szUrl, pUrl );

	} // end copy url	

	if ( !m_bMem )
	{
		// Where to download the file
		if ( pLocal != DOWNLOADTEMP ) { strcpy_sz( m_szLocal, pLocal ); }
		else CWinFile::CreateTemp( m_szLocal );	

		// Create a file to load data
		if ( !m_local.OpenNew( m_szLocal, GENERIC_READ | GENERIC_WRITE ) )
			return FALSE;
	} // end else
	
	// Set status	
	m_dwTransferStatus = NETFILE_DS_INITIALIZING;

	// Save request method
	if ( pMethod == NULL || *pMethod == 0 ) m_sMethod = "POST";
	else m_sMethod = pMethod;

	// Set data
	DWORD dwContentLength = 0;
	if ( pData != NULL )
	{	CPipe pipe;
		pData->EncodeUrl( &pipe, dwUrlEncoding );
		m_sData.copy( (LPCTSTR)pipe.GetBuffer(), pipe.GetBufferSize() );
		dwContentLength = pipe.GetBufferSize();
	} // end if

	// Set headers
	if ( strcmpi( m_sMethod, "GET" ) )
	{
		CRKey rkHeaders;
		if ( pHeaders != NULL ) rkHeaders.Copy( pHeaders );
		
		rkHeaders.Set( "Content-Type", "application/x-www-form-urlencoded" );
		rkHeaders.Set( "Content-Length", dwContentLength );

		CPipe pipe;
		rkHeaders.EncodeHttpHeaders( &pipe );
		m_sHeaders.copy( (LPCTSTR)pipe.GetBuffer(), pipe.GetBufferSize() );

	} // end if

	// Create a thread to download the file
	if ( !StartThread() )
	{	Destroy();
		m_dwTransferStatus = NETFILE_DS_ERROR;		
		return FALSE;
	} // end if

	return TRUE;
}
Beispiel #5
0
BOOL CNetCmd::vMsg( const GUID *pNode, const GUID *pClass, DWORD dwFunction, CReg *pParams, DWORD dwBuffers, LPVOID *pArgs )
{_STT();
	// Verify we have authority to send this message
	if ( !OnTxAuthenticate( pNode, pClass, dwFunction ) )
		return FALSE;

	// Get destination
	CNetCom *pNc = GetNode( pNode );
	if ( pNc == NULL || !pNc->IsValid() ) 
		return FALSE;

	// Copy message data
	CNetMsg::SMsg msg;
	ZeroMemory( &msg, sizeof( msg ) );
	msg.dwFunction = dwFunction;
	
	// Encode params
	CPipe params;
	if ( pParams ) pParams->SaveRegFile( &params, NULL, "" );

	// Calculate the total size needed
	DWORD dwBlocks = 3; if ( params.GetBufferSize() ) dwBlocks++;
	DWORD dwTotalSize = ( sizeof( CNetMsg::SAddress ) * 2 ) +  
						sizeof( CNetMsg::SMsg ) +
						params.GetBufferSize();

	DWORD i;
	LPVOID *ptrExtra = pArgs;
	for ( i = 0; i < dwBuffers; i++ )
	{	RULIB_TRY 
		{	DWORD	dwType = *(LPDWORD)( ptrExtra );
			LPBYTE	pPtr = *(LPBYTE*)( ptrExtra + 1 );
			DWORD 	dwSize = *(LPDWORD)( ptrExtra + 2 );
			dwBlocks++;

			// Zero means NULL terminated
			if ( dwSize == 0 && pPtr != NULL ) dwSize = strlen( (LPCTSTR)pPtr );

			dwTotalSize += dwSize;

			ptrExtra += 3;
		} // end try
		RULIB_CATCH_ALL { return FALSE; }
	} // end for

	// Lock the tx buffer
	CTlLocalLock ll( *pNc->Tx() );
	if ( !ll.IsLocked() ) return FALSE;

	// Initialze the packet
	pNc->Tx()->InitPacket( NETMSG_PACKETTYPE, dwBlocks, dwTotalSize );

	CNetMsg::SAddress addr;

	// Destination information	
	memcpy( &addr.guidNode, pNc->Rx()->Address(), sizeof( GUID ) );
	if ( pClass ) memcpy( &addr.guidClass, pClass, sizeof( GUID ) );
	else ZeroMemory( &addr.guidClass, sizeof( addr.guidClass ) );
	pNc->Tx()->AddPacketData( NETMSGDT_DSTADDRESS, &addr, sizeof( addr ) );
	
	// Source information	
	memcpy( &addr.guidNode, pNc->Tx()->Address(), sizeof( GUID ) );
	ZeroMemory( &addr.guidClass, sizeof( addr.guidClass ) );
	pNc->Tx()->AddPacketData( NETMSGDT_SRCADDRESS, &addr, sizeof( addr ) );

	// Add message
	pNc->Tx()->AddPacketData( NETMSGDT_MSG, &msg, sizeof( msg ) );

	// Add params if any
	if ( params.GetBufferSize() )
		pNc->Tx()->AddPacketData( NETMSGDT_PARAMS, params.GetBuffer(), params.GetBufferSize() );

	// Add the data to the packet
	ptrExtra = pArgs;
	for ( i = 0; i < dwBuffers; i++ )
	{	RULIB_TRY 
		{	DWORD	dwType = *(LPDWORD)( ptrExtra );
			LPBYTE	pPtr = *(LPBYTE*)( ptrExtra + 1 );
			DWORD 	dwSize = *(LPDWORD)( ptrExtra + 2 );

			// Zero means NULL terminated
			if ( dwSize == 0 && pPtr != NULL ) dwSize = strlen( (LPCTSTR)pPtr );

			// Add packet data block
			pNc->Tx()->AddPacketData( dwType, pPtr, dwSize );

			ptrExtra += 3;
		} // end try
		RULIB_CATCH_ALL { return FALSE; }
	} // end for

	// Make it official
	return pNc->Tx()->EndPacket();
}