ftp.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/protocol/ftp.h
  3. // Purpose: FTP protocol
  4. // Author: Vadim Zeitlin
  5. // Modified by: Mark Johnson, wxWindows@mj10777.de
  6. // 20000917 : RmDir, GetLastResult, GetList
  7. // Created: 07/07/1997
  8. // Copyright: (c) 1997, 1998 Guilhem Lavaux
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef __WX_FTP_H__
  12. #define __WX_FTP_H__
  13. #include "wx/defs.h"
  14. #if wxUSE_PROTOCOL_FTP
  15. #include "wx/sckaddr.h"
  16. #include "wx/protocol/protocol.h"
  17. #include "wx/url.h"
  18. class WXDLLIMPEXP_NET wxFTP : public wxProtocol
  19. {
  20. public:
  21. enum TransferMode
  22. {
  23. NONE, // not set by user explicitly
  24. ASCII,
  25. BINARY
  26. };
  27. wxFTP();
  28. virtual ~wxFTP();
  29. // Connecting and disconnecting
  30. virtual bool Connect(const wxSockAddress& addr, bool wait = true);
  31. virtual bool Connect(const wxString& host) { return Connect(host, 0); }
  32. virtual bool Connect(const wxString& host, unsigned short port);
  33. // disconnect
  34. virtual bool Close();
  35. // Parameters set up
  36. // set transfer mode now
  37. void SetPassive(bool pasv) { m_bPassive = pasv; }
  38. bool SetBinary() { return SetTransferMode(BINARY); }
  39. bool SetAscii() { return SetTransferMode(ASCII); }
  40. bool SetTransferMode(TransferMode mode);
  41. // Generic FTP interface
  42. // FTP doesn't know the MIME type of the last downloaded/uploaded file
  43. virtual wxString GetContentType() const { return wxEmptyString; }
  44. // the last FTP server reply
  45. const wxString& GetLastResult() const { return m_lastResult; }
  46. // send any FTP command (should be full FTP command line but without
  47. // trailing "\r\n") and return its return code
  48. char SendCommand(const wxString& command);
  49. // check that the command returned the given code
  50. bool CheckCommand(const wxString& command, char expectedReturn)
  51. {
  52. // SendCommand() does updates m_lastError
  53. return SendCommand(command) == expectedReturn;
  54. }
  55. // Filesystem commands
  56. bool ChDir(const wxString& dir);
  57. bool MkDir(const wxString& dir);
  58. bool RmDir(const wxString& dir);
  59. wxString Pwd();
  60. bool Rename(const wxString& src, const wxString& dst);
  61. bool RmFile(const wxString& path);
  62. // Get the size of a file in the current dir.
  63. // this function tries its best to deliver the size in bytes using BINARY
  64. // (the SIZE command reports different sizes depending on whether
  65. // type is set to ASCII or BINARY)
  66. // returns -1 if file is non-existent or size could not be found
  67. int GetFileSize(const wxString& fileName);
  68. // Check to see if a file exists in the current dir
  69. bool FileExists(const wxString& fileName);
  70. // Download methods
  71. bool Abort();
  72. virtual wxInputStream *GetInputStream(const wxString& path);
  73. virtual wxOutputStream *GetOutputStream(const wxString& path);
  74. // Directory listing
  75. // get the list of full filenames, the format is fixed: one file name per
  76. // line
  77. bool GetFilesList(wxArrayString& files,
  78. const wxString& wildcard = wxEmptyString)
  79. {
  80. return GetList(files, wildcard, false);
  81. }
  82. // get a directory list in server dependent format - this can be shown
  83. // directly to the user
  84. bool GetDirList(wxArrayString& files,
  85. const wxString& wildcard = wxEmptyString)
  86. {
  87. return GetList(files, wildcard, true);
  88. }
  89. // equivalent to either GetFilesList() (default) or GetDirList()
  90. bool GetList(wxArrayString& files,
  91. const wxString& wildcard = wxEmptyString,
  92. bool details = false);
  93. protected:
  94. // this executes a simple ftp command with the given argument and returns
  95. // true if it its return code starts with '2'
  96. bool DoSimpleCommand(const wxChar *command,
  97. const wxString& arg = wxEmptyString);
  98. // get the server reply, return the first character of the reply code,
  99. // '1'..'5' for normal FTP replies, 0 (*not* '0') if an error occurred
  100. char GetResult();
  101. // check that the result is equal to expected value
  102. bool CheckResult(char ch) { return GetResult() == ch; }
  103. // return the socket to be used, Passive/Active versions are used only by
  104. // GetPort()
  105. wxSocketBase *GetPort();
  106. wxSocketBase *GetPassivePort();
  107. wxSocketBase *GetActivePort();
  108. // helper for GetPort()
  109. wxString GetPortCmdArgument(const wxIPV4address& Local, const wxIPV4address& New);
  110. // accept connection from server in active mode, returns the same socket as
  111. // passed in passive mode
  112. wxSocketBase *AcceptIfActive(wxSocketBase *sock);
  113. // internal variables:
  114. wxString m_lastResult;
  115. // true if there is an FTP transfer going on
  116. bool m_streaming;
  117. // although this should be set to ASCII by default according to STD9,
  118. // we will use BINARY transfer mode by default for backwards compatibility
  119. TransferMode m_currentTransfermode;
  120. bool m_bPassive;
  121. // following is true when a read or write times out, we then assume
  122. // the connection is dead and abort. we avoid additional delays this way
  123. bool m_bEncounteredError;
  124. friend class wxInputFTPStream;
  125. friend class wxOutputFTPStream;
  126. DECLARE_DYNAMIC_CLASS_NO_COPY(wxFTP)
  127. DECLARE_PROTOCOL(wxFTP)
  128. };
  129. // the trace mask used by assorted wxLogTrace() in ftp code, do
  130. // wxLog::AddTraceMask(FTP_TRACE_MASK) to see them in output
  131. #define FTP_TRACE_MASK wxT("ftp")
  132. #endif // wxUSE_PROTOCOL_FTP
  133. #endif // __WX_FTP_H__