1 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_errno.h"
4 #include "ace/INet/FTP_URL.h"
5 #include "ace/INet/FTP_ClientRequestHandler.h"
11 bool do_login
= false;
13 bool do_active
= false;
18 std::cout
<< "usage: ftp_simple_wget [options] <url>\n";
19 std::cout
<< "Executes an FTP download request and sends the result to STDOUT or file\n";
20 std::cout
<< "\t-a\t\tuse active mode for data connections\n";
21 std::cout
<< "\t-o <filename>\t\tfile to write output to\n";
22 std::cout
<< "\t-l\t\task for password at login\n";
23 std::cout
<< "\t-p <password>\t\tuse <password> for login\n";
27 parse_args (int argc
, ACE_TCHAR
*argv
[])
29 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("ao:lp:h"), 0, 0, ACE_Get_Opt::RETURN_IN_ORDER
);
33 while ((c
= get_opt ()) != EOF
)
38 url
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg ());
46 outfile
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg ());
54 password
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg ());
67 class My_Authenticator
68 : public ACE::INet::AuthenticatorBase
71 My_Authenticator () {}
72 ~My_Authenticator () {}
74 virtual bool authenticate(ACE::INet::AuthenticationBase
& auth
) const
76 if (!password
.empty ()) auth
.password (password
);
79 std::cout
<< "Authentication (" << auth
.scheme() << ") required." << std::endl
;
80 std::cout
<< "Realm : " << auth
.realm () << std::endl
81 << "User: " << auth
.user() << std::endl
82 << "Password: \t\t\t (Enter password or press enter for default)\rPassword: ";
86 while (n
< (sizeof(buf
)-1) && (ch
= std::cin
.get ()) != '\n' && ch
!= std::char_traits
<char>::eof ())
97 class My_FTP_RequestHandler
98 : public ACE::FTP::ClientRequestHandler
101 My_FTP_RequestHandler () : read_length_ (0) {}
102 virtual ~My_FTP_RequestHandler () {}
105 virtual void handle_request_error (const ACE::FTP::URL
& url
)
107 std::cout
<< "ERROR" << std::endl
;
108 std::cerr
<< "Failed to handle download for " << url
.to_string ().c_str () << std::endl
;
111 virtual void handle_connection_error (const ACE::FTP::URL
& url
)
113 std::cout
<< "ERROR" << std::endl
;
114 std::cerr
<< "Failed to set up connection for " << url
.to_string ().c_str () << std::endl
;
117 virtual void after_read (const char_type
* /*buffer*/, int length_read
)
119 this->read_length_
+= length_read
;
120 std::cout
<< "\r [" << this->read_length_
<< "/???]";
123 virtual void on_eof ()
125 ACE::FTP::ClientRequestHandler::on_eof ();
126 std::cout
<< std::endl
;
134 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
136 std::unique_ptr
<std::ofstream
> fout
;
137 std::ostream
* sout
= &std::cout
;
139 if (!parse_args (argc
, argv
))
144 ACE::INet::URL_INetAuthBase::add_authenticator ("my_auth",
145 new My_Authenticator
);
147 std::cout
<< "Starting..." << std::endl
;
151 if (!outfile
.empty ())
153 fout
.reset (new std::ofstream (outfile
.c_str (), std::ios_base::binary
|std::ios_base::out
));
157 std::cerr
<< "Failed to open output file : " << outfile
.c_str () << std::endl
;
164 ACE::FTP::URL ftp_url
;
166 std::cout
<< "Parsing url [" << url
.c_str () << "]" << std::endl
;
168 if (!ftp_url
.parse (url
))
170 std::cerr
<< "Failed parsing url [" << url
<< "]" << std::endl
;
171 std::cerr
<< "\tresult = " << ftp_url
.to_string ().c_str ();
175 std::cout
<< "Opening url..." << std::endl
;
176 My_FTP_RequestHandler my_rh
;
177 if (do_active
) my_rh
.use_active_mode ();
178 ACE::INet::URLStream urlin
= ftp_url
.open (my_rh
);
181 std::cout
<< "Saving to: ";
182 if (!outfile
.empty ())
183 std::cout
<< '\'' << outfile
.c_str () << '\'' << std::endl
;
185 std::cout
<< "(stdout)" << std::endl
;
187 (*sout
) << urlin
->rdbuf ();
193 std::cerr
<< "ERROR: No URL specified!" << std::endl
;
198 std::cout
<< "Done" << std::endl
;