Download Source Code

CNetFile Class Reference

Implements HTTP GET and POST. Wraps the windows Inet API. More...

#include <NetFile.h>

Inheritance diagram for CNetFile:

Inheritance graph
[legend]
Collaboration diagram for CNetFile:

Collaboration graph
[legend]
List of all members.

Public Member Functions

BOOL Download (LPCTSTR pUrl, LPCTSTR pLocal=NULL, BOOL bCloseFileAfterDownload=TRUE, HWND hWndParent=NULL)
 Downloads data from the specified URL.
BOOL Open (LPCTSTR pUrl, LPCTSTR pLocal=NULL)
 Opens the specified URL for data transfer.
BOOL HttpRequest (LPCTSTR pUrl, CRKey *pHeaders, CRKey *pData, LPCTSTR pMethod="POST", LPCTSTR pLocal=NULL, BOOL bCloseFileAfterDownload=TRUE, HWND hWndParent=NULL, DWORD dwUrlEncoding=1)
 Builds an HTTP request.
void Release ()
 Releases all resources including downloaded data.
BOOL Upload (LPCTSTR pUrl, LPBYTE buf, DWORD size, BOOL bPassive)
 Uploads data to the specified URL.
BOOL SetDataPtr (LPBYTE buf, DWORD size)
 Sets the buffer that is used for data transfers.
BOOL SetOption (DWORD option, LPVOID buf, DWORD size)
 Sets internet transfer options.
BOOL Upload (LPCTSTR pUrl, LPCTSTR pLocal, BOOL bPassive)
 Uploads data to the specified URL.
void Close ()
 Closes network connection and cancels any ongoing transfer.
virtual BOOL EndThread (LPVOID pData)
 Transfer thread cleanup.
virtual BOOL InitThread (LPVOID pData)
 Transfer thread initialization.
virtual BOOL DoThread (LPVOID pData)
 Transfer thread main body. Handles the actual data transfer.
void Destroy ()
 Closes network connection and cancels any ongoing transfer.
 CNetFile (LPCTSTR pUserAgent="WheresJames Software (www_wheresjames_com)")
 Default constructor.
 ~CNetFile ()
 Destructor.
CWinFileGetFile ()
 Returns the local transfer file.
 operator CWinFile ()
 Returns the local transfer file.
LPBYTE GetMem ()
 Returns a pointer to the transfer data.
DWORD GetTransferStatus ()
 Returns the current status of the file transfer.
LPCTSTR GetStatus ()
 Returns a human readable string defining the current transfer status code.
DWORD GetTransferError ()
 Returns the last transfer error code.
DWORD GetDataRead ()
 Returns the number of bytes read from the remote host.
DWORD GetDataWritten ()
 Returns the number of bytes written to the remote host.
void CloseFileAfterDownload (BOOL b)
 Sets whether the file should be closed after downloading.
void SetBlockSize (DWORD bs)
 Sets the transfer block size.
LPCTSTR GetUrl ()
 Returns the current target URL.
LPCTSTR GetLocalFile ()
 Returns the path of the current transfer disk file.
void SetOpenFlags (DWORD f)
 Sets URL open flags.
void SetInternetFlags (DWORD f)
 Sets internet open flags.
void SetUserAgent (LPCTSTR str)
 Sets the user agent name.
void SetUsernamePassword (LPCTSTR un, LPCTSTR pw)
 Sets the username and password for URL connections.
void SetMessageTarget (HWND hWnd, UINT uMsg)
 Sets the window handle that will receive status messages.
void SetCallback (NetFileCallback f, DWORD user)
 Sets the address of a callback function that receives status messages.
BOOL DoCallback (WPARAM wParam, LPARAM lParam)
 Executes a callback.
void EnableCache (BOOL b)
 Enables fetching data from the local internet cache.
BOOL IsWorking ()
 Returns non-zero if a transfer is currently in progress.
void SetAccessFlags (DWORD f)
 Sets data access flags.
DWORD GetTotalSize ()
 Gets the length, in bytes, of the data being transfered.

Static Public Member Functions

static BOOL GetErrorMsg (LPSTR msg, DWORD err)
 Returns a string describing the error.
static LPCTSTR GetStatus (DWORD status)
 Returns a human readable string defining the transfer status code.

Detailed Description

Implements HTTP GET and POST. Wraps the windows Inet API.

Wraps the Inet API to provide HTTP file downloading and uploading. This class supports FTP through the Inet API as well.


Constructor & Destructor Documentation

CNetFile::CNetFile LPCTSTR  pUserAgent = "WheresJames Software (www_wheresjames_com)"  ) 
 

Default constructor.

Parameters:
[in] pUserAgent - User agent string to pass on to server.


Member Function Documentation

void CNetFile::CloseFileAfterDownload BOOL  b  )  [inline]
 

Sets whether the file should be closed after downloading.

Parameters:
[in] b - Non-zero to close file after download

BOOL CNetFile::DoCallback WPARAM  wParam,
LPARAM  lParam
[inline]
 

Executes a callback.

Parameters:
[in] wParam - WPARAM value passed to callback
[in] lParam - LPARAM value passed to callback
Returns:
Non-zero if callback was executed.
See also:

BOOL CNetFile::DoThread LPVOID  pData  )  [virtual]
 

Transfer thread main body. Handles the actual data transfer.

Parameters:
[in] pData - Reserved
Returns:
Non-zero if transfer is still in progress
See also:

Reimplemented from CThread.

BOOL CNetFile::Download LPCTSTR  pUrl,
LPCTSTR  pLocal = NULL,
BOOL  bCloseFileAfterDownload = TRUE,
HWND  hWndParent = NULL
 

Downloads data from the specified URL.

Parameters:
[in] pUrl - URL
[in] pLocal - File in which to save data. If this is NULL, the data is saved to a memory buffer.
[in] bCloseFileAfterDownload - Non-zero to close file in pLocal after downloading. If zero, the file will remain open.
[in] hWndParent - Parent window.
Returns:
Non-zero if transfer is successfully started.
See also:

void CNetFile::EnableCache BOOL  b  )  [inline]
 

Enables fetching data from the local internet cache.

Parameters:
[in] b - Non-zero to enable use of the data cache.

BOOL CNetFile::EndThread LPVOID  pData  )  [virtual]
 

Transfer thread cleanup.

Parameters:
[in] pData - Reserved
Returns:
Non-zero if success
See also:

Reimplemented from CThread.

BOOL CNetFile::GetErrorMsg LPSTR  msg,
DWORD  err
[static]
 

Returns a string describing the error.

Parameters:
[out] msg - Receives the error string
[in] err - Error code
Returns:
Non-zero if success
See also:

CWinFile* CNetFile::GetFile  )  [inline]
 

Returns the local transfer file.

Returns:
Pointer to CWinFile object

LPBYTE CNetFile::GetMem  )  [inline]
 

Returns a pointer to the transfer data.

This function only returns a valid pointer if a local disk file was not specified as the target for data transfer.

Returns:
Pointer to transfered data or NULL if none.
See also:

LPCTSTR CNetFile::GetStatus  )  [inline]
 

Returns a human readable string defining the current transfer status code.

Returns:
Human readable string describing current transfer status code.
See also:

LPCTSTR CNetFile::GetStatus DWORD  status  )  [static]
 

Returns a human readable string defining the transfer status code.

Parameters:
[in] status - Transfer status code
Returns:
Human readable string describing transfer status code.
See also:

DWORD CNetFile::GetTransferStatus  )  [inline]
 

Returns the current status of the file transfer.

  • NETFILE_DS_NONE - Connection is idle
  • NETFILE_DS_INITIALIZING - Network is initializing
  • NETFILE_DS_CONNECTING - Connecting to remote host
  • NETFILE_DS_DOWNLOADING - Downloading data
  • NETFILE_DS_UPLOADING - Uploading data
  • NETFILE_DS_DONE - Transfer completed successfully
  • NETFILE_DS_ERROR - Transfer aborted with error(s)

Returns:
Transfer status code
See also:

BOOL CNetFile::HttpRequest LPCTSTR  pUrl,
CRKey pHeaders,
CRKey pData,
LPCTSTR  pMethod = "POST",
LPCTSTR  pLocal = NULL,
BOOL  bCloseFileAfterDownload = TRUE,
HWND  hWndParent = NULL,
DWORD  dwUrlEncoding = 1
 

Builds an HTTP request.

Parameters:
[in] pUrl - URL
[in] pHeaders - HTTP headers
[in] pData - GET or POST data
[in] pMethod - GET or POST
[in] pLocal - Local file that receives any data transferred. Set to NULL to have the data copied to a memory buffer instead.
[in] bCloseFileAfterDownload - Non-zero to close the file after download is complete.
[in] hWndParent - Handle of parent window.
[in] dwUrlEncoding - URL encoding. See CRKey::EncodeUrl()
Returns:
Non-zero if transfer is successfully started.
See also:

BOOL CNetFile::InitThread LPVOID  pData  )  [virtual]
 

Transfer thread initialization.

Parameters:
[in] pData - Reserved
Returns:
Non-zero if success
See also:

Reimplemented from CThread.

BOOL CNetFile::Open LPCTSTR  pUrl,
LPCTSTR  pLocal = NULL
 

Opens the specified URL for data transfer.

Parameters:
[in] pUrl - URL
[in] pLocal - Local file that will receive any transfer data. If NULL, a memory buffer will receive the data instead.
Returns:
See also:

CNetFile::operator CWinFile  )  [inline]
 

Returns the local transfer file.

Returns:
Pointer to CWinFile object

void CNetFile::SetAccessFlags DWORD  f  )  [inline]
 

Sets data access flags.

Parameters:
[in] f - Access flags

void CNetFile::SetBlockSize DWORD  bs  )  [inline]
 

Sets the transfer block size.

Parameters:
[in] bs - Transfer block size in bytes.

void CNetFile::SetCallback NetFileCallback  f,
DWORD  user
[inline]
 

Sets the address of a callback function that receives status messages.

Parameters:
[in] f - Pointer to callback function
[in] user - User data, usually a class pointer

BOOL CNetFile::SetDataPtr LPBYTE  buf,
DWORD  size
 

Sets the buffer that is used for data transfers.

Parameters:
[in] buf - Pointer to buffer.
[in] size - Size of the buffer in buf.
Returns:
Non-zero if success
See also:

void CNetFile::SetInternetFlags DWORD  f  )  [inline]
 

Sets internet open flags.

Parameters:
[in] f - Internet open flags

void CNetFile::SetMessageTarget HWND  hWnd,
UINT  uMsg
[inline]
 

Sets the window handle that will receive status messages.

Parameters:
[in] hWnd - Window handle receiving status messages
[in] uMsg - Message id for callbacks

void CNetFile::SetOpenFlags DWORD  f  )  [inline]
 

Sets URL open flags.

Parameters:
[in] f - Open flags

BOOL CNetFile::SetOption DWORD  option,
LPVOID  buf,
DWORD  size
 

Sets internet transfer options.

Parameters:
[in] option - Option flags
[in] buf - Option data buffer
[in] size - Number of bytes in buf
Returns:
Non-zero if success
See also:

void CNetFile::SetUserAgent LPCTSTR  str  )  [inline]
 

Sets the user agent name.

Parameters:
[in] str - User-agent name sent to remote host.

void CNetFile::SetUsernamePassword LPCTSTR  un,
LPCTSTR  pw
[inline]
 

Sets the username and password for URL connections.

Parameters:
[in] un - Username
[in] pw - Password

BOOL CNetFile::Upload LPCTSTR  pUrl,
LPCTSTR  pLocal,
BOOL  bPassive
 

Uploads data to the specified URL.

Parameters:
[in] pUrl - URL
[in] pLocal - Disk filename to upload
[in] bPassive - Non-zero to use Passive FTP
This function uploads data to the specified URL using FTP. Normal or passive FTP can be used.

Returns:
Non-zero if upload is successfully started.
See also:

BOOL CNetFile::Upload LPCTSTR  pUrl,
LPBYTE  buf,
DWORD  size,
BOOL  bPassive
 

Uploads data to the specified URL.

Parameters:
[in] pUrl - URL
[in] buf - Data to upload
[in] size - Number of bytes in buf
[in] bPassive - Non-zero to use Passive FTP
This function uploads data to the specified URL using FTP. Normal or passive FTP can be used.

Returns:
Non-zero if upload is successfully started.
See also:


The documentation for this class was generated from the following files:
  • inc/NetFile.h
  • NetFile.cpp
Copyright Robert Umbehant
This documentation is covered by the LGPL