| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //
- // FTPSStreamFactory.cpp
- //
- // Library: NetSSL_OpenSSL
- // Package: FTPS
- // Module: FTPSStreamFactory
- //
- // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // SPDX-License-Identifier: BSL-1.0
- //
- #include "Poco/Net/FTPSStreamFactory.h"
- #include "Poco/Net/FTPSClientSession.h"
- #include "Poco/Net/NetException.h"
- #include "Poco/URI.h"
- #include "Poco/URIStreamOpener.h"
- #include "Poco/UnbufferedStreamBuf.h"
- #include "Poco/Path.h"
- using Poco::URIStreamFactory;
- using Poco::URI;
- using Poco::URIStreamOpener;
- using Poco::UnbufferedStreamBuf;
- using Poco::Path;
- namespace Poco {
- namespace Net {
- class FTPSStreamBuf: public UnbufferedStreamBuf
- {
- public:
- FTPSStreamBuf(std::istream& istr):
- _istr(istr)
- {
- // make sure exceptions from underlying string propagate
- _istr.exceptions(std::ios::badbit);
- }
- ~FTPSStreamBuf()
- {
- }
- private:
- int readFromDevice()
- {
- return _istr.get();
- }
- std::istream& _istr;
- };
- class FTPSIOS: public virtual std::ios
- {
- public:
- FTPSIOS(std::istream& istr):
- _buf(istr)
- {
- poco_ios_init(&_buf);
- }
- ~FTPSIOS()
- {
- }
- FTPSStreamBuf* rdbuf()
- {
- return &_buf;
- }
- protected:
- FTPSStreamBuf _buf;
- };
- class FTPSStream: public FTPSIOS, public std::istream
- {
- public:
- FTPSStream(std::istream& istr, FTPSClientSession* pSession):
- FTPSIOS(istr),
- std::istream(&_buf),
- _pSession(pSession)
- {
- }
- ~FTPSStream()
- {
- delete _pSession;
- }
- private:
- FTPSClientSession* _pSession;
- };
- FTPSStreamFactory::FTPSStreamFactory()
- {
- }
- FTPSStreamFactory::~FTPSStreamFactory()
- {
- }
- std::istream* FTPSStreamFactory::open(const URI& uri)
- {
- poco_assert (uri.getScheme() == "ftps");
- Poco::UInt16 port = uri.getPort();
- if (port == 0) port = FTPClientSession::FTP_PORT;
- FTPSClientSession* pSession = new FTPSClientSession(uri.getHost(), port);
- try
- {
- std::string username;
- std::string password;
- getUserInfo(uri, username, password);
- std::string path;
- char type;
- getPathAndType(uri, path, type);
- pSession->login(username, password);
- if (type == 'a')
- pSession->setFileType(FTPClientSession::TYPE_TEXT);
- Path p(path, Path::PATH_UNIX);
- p.makeFile();
- for (int i = 0; i < p.depth(); ++i)
- pSession->setWorkingDirectory(p[i]);
- std::string file(p.getFileName());
- std::istream& istr = (type == 'd' ? pSession->beginList(file) : pSession->beginDownload(file));
- return new FTPSStream(istr, pSession);
- }
- catch (...)
- {
- delete pSession;
- throw;
- }
- }
- void FTPSStreamFactory::registerFactory()
- {
- URIStreamOpener::defaultOpener().registerStreamFactory("ftps", new FTPSStreamFactory);
- }
- void FTPSStreamFactory::unregisterFactory()
- {
- URIStreamOpener::defaultOpener().unregisterStreamFactory("ftps");
- }
- } } // namespace Poco::Net
|