Exemplo n.º 1
0
bool WebIO::UploadFileData(std::string file, std::string data)
{
	WebIO::m_hFile = FtpOpenFileA(WebIO::m_hConnect, file.c_str(), GENERIC_WRITE, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD, 0);

	if (WebIO::m_hFile)
	{
		DWORD size = 0;
		InternetWriteFile(WebIO::m_hFile, data.c_str(), data.size(), &size);
		InternetCloseHandle(WebIO::m_hFile);

		return (size == data.size());
	}

	return false;
}
Exemplo n.º 2
0
bool WebIO::DownloadFileData(std::string file, std::string &data)
{
	data.clear();

	WebIO::m_hFile = FtpOpenFileA(WebIO::m_hConnect, file.c_str(), GENERIC_READ, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD, 0);

	if (WebIO::m_hFile)
	{
		DWORD size = 0;
		char buffer[0x2001] = { 0 };

		while (InternetReadFile(WebIO::m_hFile, buffer, 0x2000, &size))
		{
			data.append(buffer, size);
			if (!size) break;
		}

		InternetCloseHandle(WebIO::m_hFile);
		return true;
	}

	return false;
}
Exemplo n.º 3
0
Arquivo: ftp.c Projeto: bilboed/wine
static void test_find_first_file(HINTERNET hFtp, HINTERNET hConnect)
{
    WIN32_FIND_DATA findData;
    HINTERNET hSearch;
    HINTERNET hSearch2;
    HINTERNET hOpenFile;
    DWORD error;

    /* NULL as the search file ought to return the first file in the directory */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hFtp, NULL, &findData, 0, 0);
    ok ( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );

    /* This should fail as the previous handle wasn't closed */
    SetLastError(0xdeadbeef);
    hSearch2 = FtpFindFirstFileA(hFtp, "welcome.msg", &findData, 0, 0);
    todo_wine ok ( hSearch2 == NULL, "Expected FtpFindFirstFileA to fail\n" );
    todo_wine ok ( GetLastError() == ERROR_FTP_TRANSFER_IN_PROGRESS,
        "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", GetLastError() );
    InternetCloseHandle(hSearch2); /* Just in case */

    InternetCloseHandle(hSearch);

    /* Try a valid filename in a subdirectory search */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hFtp, "pub/wine", &findData, 0, 0);
    todo_wine ok ( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );
    InternetCloseHandle(hSearch);

    /* Try a valid filename in a subdirectory wildcard search */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hFtp, "pub/w*", &findData, 0, 0);
    todo_wine ok ( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );
    InternetCloseHandle(hSearch);

    /* Try an invalid wildcard search */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hFtp, "*/w*", &findData, 0, 0);
    ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
    InternetCloseHandle(hSearch); /* Just in case */

    /* Try FindFirstFile between FtpOpenFile and InternetCloseHandle */
    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
    ok ( hOpenFile != NULL, "Expected FtpOpenFileA to succeed\n" );
    ok ( GetLastError() == ERROR_SUCCESS ||
        broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
        "Expected ERROR_SUCCESS, got %u\n", GetLastError() );

    /* This should fail as the OpenFile handle wasn't closed */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hFtp, "welcome.msg", &findData, 0, 0);
    error = GetLastError();
    ok ( hSearch == NULL || broken(hSearch != NULL), /* win2k */
         "Expected FtpFindFirstFileA to fail\n" );
    if (!hSearch)
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
             "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error );
    else
    {
        ok( error == ERROR_SUCCESS, "wrong error %u on success\n", GetLastError() );
        InternetCloseHandle(hSearch);
    }

    InternetCloseHandle(hOpenFile);

    /* Test using a nonexistent filename */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hFtp, "this_file_should_not_exist", &findData, 0, 0);
    ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
    todo_wine ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
        "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError() );
    InternetCloseHandle(hSearch); /* Just in case */

    /* Test using a nonexistent filename and a wildcard */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hFtp, "this_file_should_not_exist*", &findData, 0, 0);
    ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
    todo_wine ok ( GetLastError() == ERROR_NO_MORE_FILES,
        "Expected ERROR_NO_MORE_FILES, got %d\n", GetLastError() );
    InternetCloseHandle(hSearch); /* Just in case */

    /* Test using an invalid handle type */
    SetLastError(0xdeadbeef);
    hSearch = FtpFindFirstFileA(hConnect, "welcome.msg", &findData, 0, 0);
    ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
    ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
        "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError() );
    InternetCloseHandle(hSearch); /* Just in case */
}
Exemplo n.º 4
0
Arquivo: ftp.c Projeto: bilboed/wine
static void test_openfile(HINTERNET hFtp, HINTERNET hConnect)
{
    HINTERNET hOpenFile;

    /* Invalid internet handle, the rest are valid parameters */
    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(NULL, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
    ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
    ok ( GetLastError() == ERROR_INVALID_HANDLE,
        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
    InternetCloseHandle(hOpenFile); /* Just in case */

    /* No filename */
    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hFtp, NULL, GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
    ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
    ok ( GetLastError() == ERROR_INVALID_PARAMETER,
        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    InternetCloseHandle(hOpenFile); /* Just in case */

    /* Illegal access flags */
    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", 0, FTP_TRANSFER_TYPE_ASCII, 0);
    ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
    ok ( GetLastError() == ERROR_INVALID_PARAMETER,
        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    InternetCloseHandle(hOpenFile); /* Just in case */

    /* Illegal combination of access flags */
    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ|GENERIC_WRITE, FTP_TRANSFER_TYPE_ASCII, 0);
    ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
    ok ( GetLastError() == ERROR_INVALID_PARAMETER,
        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    InternetCloseHandle(hOpenFile); /* Just in case */

    /* Illegal condition flags */
    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, 0xffffffff, 0);
    ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
    ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR || GetLastError() == ERROR_INVALID_PARAMETER,
        "Expected ERROR_INTERNET_EXTENDED_ERROR or ERROR_INVALID_PARAMETER (win98), got %d\n", GetLastError());
    InternetCloseHandle(hOpenFile); /* Just in case */

    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
    ok ( hOpenFile != NULL, "Expected FtpOpenFileA to succeed\n");
    ok ( GetLastError() == ERROR_SUCCESS ||
        broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
        "Expected ERROR_SUCCESS, got %u\n", GetLastError());

    if (hOpenFile)
    {
        BOOL bRet;
        DWORD error;
        HINTERNET hOpenFile2;
        HANDLE    hFile;

        /* We have a handle so all ftp calls should fail (TODO: Put all ftp-calls in here) */
        SetLastError(0xdeadbeef);
        bRet = FtpCreateDirectoryA(hFtp, "new_directory_deadbeef");
        error = GetLastError();
        ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
            "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
        trace_extended_error(error);

        SetLastError(0xdeadbeef);
        bRet = FtpDeleteFileA(hFtp, "non_existent_file_deadbeef");
        error = GetLastError();
        ok ( bRet == FALSE, "Expected FtpDeleteFileA to fail\n");
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
            "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
        trace_extended_error(error);

        SetLastError(0xdeadbeef);
        bRet = FtpGetFileA(hFtp, "welcome.msg", "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
        error = GetLastError();
        ok ( bRet == FALSE || broken(bRet == TRUE), "Expected FtpGetFileA to fail\n");
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_SUCCESS),
            "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
        DeleteFileA("should_be_non_existing_deadbeef"); /* Just in case */

        SetLastError(0xdeadbeef);
        hOpenFile2 = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
        error = GetLastError();
        ok ( bRet == FALSE || broken(bRet == TRUE), "Expected FtpOpenFileA to fail\n");
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_SUCCESS),
            "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
        InternetCloseHandle(hOpenFile2); /* Just in case */

        /* Create a temporary local file */
        SetLastError(0xdeadbeef);
        hFile = CreateFileA("now_existing_local", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
        ok ( hFile != NULL, "Error creating a local file : %d\n", GetLastError());
        CloseHandle(hFile);
        SetLastError(0xdeadbeef);
        bRet = FtpPutFileA(hFtp, "now_existing_local", "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
        error = GetLastError();
        ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
            "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
        DeleteFileA("now_existing_local");

        SetLastError(0xdeadbeef);
        bRet = FtpRemoveDirectoryA(hFtp, "should_be_non_existing_deadbeef_dir");
        error = GetLastError();
        ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
            "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);

        SetLastError(0xdeadbeef);
        bRet = FtpRenameFileA(hFtp , "should_be_non_existing_deadbeef", "new");
        error = GetLastError();
        ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
        ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
            "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
    }

    InternetCloseHandle(hOpenFile);

    /* One small test to show that handle type is checked before parameters */
    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hConnect, "welcome.msg", GENERIC_READ, 5, 0);
    ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
    ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
        "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
    InternetCloseHandle(hOpenFile); /* Just in case */

    SetLastError(0xdeadbeef);
    hOpenFile = FtpOpenFileA(hConnect, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
    ok ( hOpenFile == NULL, "Expected FtpOpenFileA to fail\n");
    ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
        "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());

    InternetCloseHandle(hOpenFile); /* Just in case */
}
Exemplo n.º 5
0
// Upload file
bool FtpClient::upload(const string & source, const string & destination)
{

	// Check connection
	if (mFtpHnd == NULL)
	{

		// Report error and break
		OutputDebugStringA("Not connected to FTP server:\n");
		showError();
		return false;

	}

	// Check if file exists
	if (!boost::filesystem::exists(boost::filesystem::path(source)))
	{

		// Report error and break
		OutputDebugStringA((string() + "\"" + source + "\" does not exist").c_str());
		return false;

	}

	// Split destination to vector
	vector<string> mPath;
	boost::split(mPath, destination, boost::is_any_of("/"));

	// Iterate through directories
	LPCSTR mFilename;
	for (vector<string>::const_iterator mDirectory = mPath.cbegin(); mDirectory != mPath.cend(); ++mDirectory)
	{

		// End of the line
		if (mDirectory == mPath.end() - 1)
		{

			// Convert to character array
			mFilename = (LPCSTR)mDirectory->c_str();

		}
		else
		{

			// Convert to character array
			LPCSTR nDirectory = (LPCSTR)mDirectory->c_str();

			// Open directory
			if (!FtpSetCurrentDirectoryA(mFtpHnd, nDirectory))
			{

				// Create directory
				if (!FtpCreateDirectoryA(mFtpHnd, nDirectory))
				{

					// Report error and break
					OutputDebugStringA("Unable to create directory:\n");
					showError();
					return false;

				}
				else
				{

					// Open new directory
					if (!FtpSetCurrentDirectoryA(mFtpHnd, nDirectory))
					{

						// Report error and break
						OutputDebugStringA("Unable to open new directory:\n");
						showError();
						return false;

					}

				}

			}

		}

	}

	// Check for existing file
	LPWIN32_FIND_DATAA mInfo = LPWIN32_FIND_DATAA();
	HINTERNET mRemoteFileHnd = FtpFindFirstFileA(mFtpHnd, mFilename, mInfo, INTERNET_FLAG_RELOAD, 0);

	// Clean up memory
	LocalFree(mInfo);
	delete mInfo;

	// File exists
	if (mRemoteFileHnd != NULL)
	{
		
		// Warn
		OutputDebugStringA("File already exists. Overwriting.\n");

		// Close remote file
		InternetCloseHandle(mRemoteFileHnd);

		// Delete file
		if (!FtpDeleteFileA(mFtpHnd, mFilename))
		{

			// Report error and break
			OutputDebugStringA("Unable to delete file from server:\n");
			showError();
			mPath.clear();
			return false;

		}

	}

	// Open remote file for upload
	HINTERNET mFileHnd = FtpOpenFileA(mFtpHnd, mFilename, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0);
	if (mFileHnd == NULL)
	{

		// Report error and break
		OutputDebugStringA("Unable to open remote file:\n");
		showError();
		return false;

	}

	// Open local file
	mFileIO.open(source.c_str(), ios::in|ios::binary|ios::ate);
	mFileSize = (DWORD)mFileIO.tellg();
	mFileIO.seekg(0, ios::beg);

	// Return flag
	bool mUploadSuccess = false;

	// Check file size
	if (mFileSize > 0)
	{

		// Allocate memory for buffer and read file
		mBuffer = new int_fast8_t[mFileSize];
		mFileIO.read(mBuffer, mFileSize);
		
		// Write buffer to remote file
		mBytesWritten = 0;
		mUploadSuccess = InternetWriteFile(mFileHnd, mBuffer, mFileSize, &mBytesWritten) == TRUE;
		if (!mUploadSuccess)
			showError();

		// Clean up
		delete [] mBuffer;

	}

	// Close file, clean up
	mFileIO.close();
	InternetCloseHandle(mFileHnd);
	mFileHnd = NULL;
	mPath.clear();

	// Write result
	OutputDebugStringA(mUploadSuccess ? "File uploaded.\n" : "Upload did not complete.\n");

	// Return flag
	return mUploadSuccess;

}