1 | // |
---|
2 | // FTPStreamFactory.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/FTPStreamFactory.h#1 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: FTP |
---|
8 | // Module: FTPStreamFactory |
---|
9 | // |
---|
10 | // Definition of the FTPStreamFactory class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_FTPStreamFactory_INCLUDED |
---|
40 | #define Net_FTPStreamFactory_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/HTTPSession.h" |
---|
45 | #include "Poco/URIStreamFactory.h" |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | namespace Net { |
---|
50 | |
---|
51 | |
---|
52 | class Net_API FTPPasswordProvider |
---|
53 | /// The base class for all password providers. |
---|
54 | /// An instance of a subclass of this class can be |
---|
55 | /// registered with the FTPStreamFactory to |
---|
56 | /// provide a password |
---|
57 | { |
---|
58 | public: |
---|
59 | virtual std::string password(const std::string& username, const std::string& host) = 0; |
---|
60 | /// Provide the password for the given user on the given host. |
---|
61 | |
---|
62 | protected: |
---|
63 | FTPPasswordProvider(); |
---|
64 | virtual ~FTPPasswordProvider(); |
---|
65 | }; |
---|
66 | |
---|
67 | |
---|
68 | class Net_API FTPStreamFactory: public Poco::URIStreamFactory |
---|
69 | /// An implementation of the URIStreamFactory interface |
---|
70 | /// that handles File Transfer Protocol (ftp) URIs. |
---|
71 | /// |
---|
72 | /// The URI's path may end with an optional type specification |
---|
73 | /// in the form (;type=<typecode>), where <typecode> is |
---|
74 | /// one of a, i or d. If type=a, the file identified by the path |
---|
75 | /// is transferred in ASCII (text) mode. If type=i, the file |
---|
76 | /// is transferred in Image (binary) mode. If type=d, a directory |
---|
77 | /// listing (in NLST format) is returned. This corresponds with |
---|
78 | /// the FTP URL format specified in RFC 1738. |
---|
79 | /// |
---|
80 | /// If the URI does not contain a username and password, the |
---|
81 | /// username "anonymous" and the password " |
---|
82 | { |
---|
83 | public: |
---|
84 | FTPStreamFactory(); |
---|
85 | /// Creates the FTPStreamFactory. |
---|
86 | |
---|
87 | ~FTPStreamFactory(); |
---|
88 | /// Destroys the FTPStreamFactory. |
---|
89 | |
---|
90 | std::istream* open(const Poco::URI& uri); |
---|
91 | /// Creates and opens a HTTP stream for the given URI. |
---|
92 | /// The URI must be a ftp://... URI. |
---|
93 | /// |
---|
94 | /// Throws a NetException if anything goes wrong. |
---|
95 | |
---|
96 | static void setAnonymousPassword(const std::string& password); |
---|
97 | /// Sets the password used for anonymous FTP. |
---|
98 | /// |
---|
99 | /// WARNING: Setting the anonymous password is not |
---|
100 | /// thread-safe, so it's best to call this method |
---|
101 | /// during application initialization, before the |
---|
102 | /// FTPStreamFactory is used for the first time. |
---|
103 | |
---|
104 | static const std::string& getAnonymousPassword(); |
---|
105 | /// Returns the password used for anonymous FTP. |
---|
106 | |
---|
107 | static void setPasswordProvider(FTPPasswordProvider* pProvider); |
---|
108 | /// Sets the FTPPasswordProvider. If NULL is given, |
---|
109 | /// no password provider is used. |
---|
110 | /// |
---|
111 | /// WARNING: Setting the password provider is not |
---|
112 | /// thread-safe, so it's best to call this method |
---|
113 | /// during application initialization, before the |
---|
114 | /// FTPStreamFactory is used for the first time. |
---|
115 | |
---|
116 | static FTPPasswordProvider* getPasswordProvider(); |
---|
117 | /// Returns the FTPPasswordProvider currently in use, |
---|
118 | /// or NULL if no one has been set. |
---|
119 | |
---|
120 | static void registerFactory(); |
---|
121 | /// Registers the FTPStreamFactory with the |
---|
122 | /// default URIStreamOpener instance. |
---|
123 | |
---|
124 | protected: |
---|
125 | static void splitUserInfo(const std::string& userInfo, std::string& username, std::string& password); |
---|
126 | static void getUserInfo(const Poco::URI& uri, std::string& username, std::string& password); |
---|
127 | static void getPathAndType(const Poco::URI& uri, std::string& path, char& type); |
---|
128 | |
---|
129 | private: |
---|
130 | static std::string _anonymousPassword; |
---|
131 | static FTPPasswordProvider* _pPasswordProvider; |
---|
132 | }; |
---|
133 | |
---|
134 | |
---|
135 | } } // namespace Poco::Net |
---|
136 | |
---|
137 | |
---|
138 | #endif // Net_FTPStreamFactory_INCLUDED |
---|