FTPSStreamFactory.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // FTPSStreamFactory.cpp
  3. //
  4. // Library: NetSSL_OpenSSL
  5. // Package: FTPS
  6. // Module: FTPSStreamFactory
  7. //
  8. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // SPDX-License-Identifier: BSL-1.0
  12. //
  13. #include "Poco/Net/FTPSStreamFactory.h"
  14. #include "Poco/Net/FTPSClientSession.h"
  15. #include "Poco/Net/NetException.h"
  16. #include "Poco/URI.h"
  17. #include "Poco/URIStreamOpener.h"
  18. #include "Poco/UnbufferedStreamBuf.h"
  19. #include "Poco/Path.h"
  20. using Poco::URIStreamFactory;
  21. using Poco::URI;
  22. using Poco::URIStreamOpener;
  23. using Poco::UnbufferedStreamBuf;
  24. using Poco::Path;
  25. namespace Poco {
  26. namespace Net {
  27. class FTPSStreamBuf: public UnbufferedStreamBuf
  28. {
  29. public:
  30. FTPSStreamBuf(std::istream& istr):
  31. _istr(istr)
  32. {
  33. // make sure exceptions from underlying string propagate
  34. _istr.exceptions(std::ios::badbit);
  35. }
  36. ~FTPSStreamBuf()
  37. {
  38. }
  39. private:
  40. int readFromDevice()
  41. {
  42. return _istr.get();
  43. }
  44. std::istream& _istr;
  45. };
  46. class FTPSIOS: public virtual std::ios
  47. {
  48. public:
  49. FTPSIOS(std::istream& istr):
  50. _buf(istr)
  51. {
  52. poco_ios_init(&_buf);
  53. }
  54. ~FTPSIOS()
  55. {
  56. }
  57. FTPSStreamBuf* rdbuf()
  58. {
  59. return &_buf;
  60. }
  61. protected:
  62. FTPSStreamBuf _buf;
  63. };
  64. class FTPSStream: public FTPSIOS, public std::istream
  65. {
  66. public:
  67. FTPSStream(std::istream& istr, FTPSClientSession* pSession):
  68. FTPSIOS(istr),
  69. std::istream(&_buf),
  70. _pSession(pSession)
  71. {
  72. }
  73. ~FTPSStream()
  74. {
  75. delete _pSession;
  76. }
  77. private:
  78. FTPSClientSession* _pSession;
  79. };
  80. FTPSStreamFactory::FTPSStreamFactory()
  81. {
  82. }
  83. FTPSStreamFactory::~FTPSStreamFactory()
  84. {
  85. }
  86. std::istream* FTPSStreamFactory::open(const URI& uri)
  87. {
  88. poco_assert (uri.getScheme() == "ftps");
  89. Poco::UInt16 port = uri.getPort();
  90. if (port == 0) port = FTPClientSession::FTP_PORT;
  91. FTPSClientSession* pSession = new FTPSClientSession(uri.getHost(), port);
  92. try
  93. {
  94. std::string username;
  95. std::string password;
  96. getUserInfo(uri, username, password);
  97. std::string path;
  98. char type;
  99. getPathAndType(uri, path, type);
  100. pSession->login(username, password);
  101. if (type == 'a')
  102. pSession->setFileType(FTPClientSession::TYPE_TEXT);
  103. Path p(path, Path::PATH_UNIX);
  104. p.makeFile();
  105. for (int i = 0; i < p.depth(); ++i)
  106. pSession->setWorkingDirectory(p[i]);
  107. std::string file(p.getFileName());
  108. std::istream& istr = (type == 'd' ? pSession->beginList(file) : pSession->beginDownload(file));
  109. return new FTPSStream(istr, pSession);
  110. }
  111. catch (...)
  112. {
  113. delete pSession;
  114. throw;
  115. }
  116. }
  117. void FTPSStreamFactory::registerFactory()
  118. {
  119. URIStreamOpener::defaultOpener().registerStreamFactory("ftps", new FTPSStreamFactory);
  120. }
  121. void FTPSStreamFactory::unregisterFactory()
  122. {
  123. URIStreamOpener::defaultOpener().unregisterStreamFactory("ftps");
  124. }
  125. } } // namespace Poco::Net