forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFTPSStreamFactory.cpp
More file actions
161 lines (125 loc) · 2.69 KB
/
FTPSStreamFactory.cpp
File metadata and controls
161 lines (125 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// 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