Go to the documentation of this file.00001
00002
00003 #include "ace/Get_Opt.h"
00004 #include "ace/Auto_Ptr.h"
00005 #include "ace/OS_NS_errno.h"
00006 #include "FTP_URL.h"
00007 #include "FTP_ClientRequestHandler.h"
00008 #include <iostream>
00009 #include <fstream>
00010
00011 ACE_CString url;
00012 ACE_CString outfile;
00013 bool do_login = false;
00014 ACE_CString password;
00015 bool do_active = false;
00016
00017 void
00018 usage (void)
00019 {
00020 std::cout << "usage: ftp_simple_wget [options] <url>\n";
00021 std::cout << "Executes an FTP download request and sends the result to STDOUT or file\n";
00022 std::cout << "\t-a\t\tuse active mode for data connections\n";
00023 std::cout << "\t-o <filename>\t\tfile to write output to\n";
00024 std::cout << "\t-l\t\task for password at login\n";
00025 std::cout << "\t-p <password>\t\tuse <password> for login\n";
00026 }
00027
00028 bool
00029 parse_args (int argc, ACE_TCHAR *argv [])
00030 {
00031 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("ao:lp:h"), 0, 0, ACE_Get_Opt::RETURN_IN_ORDER);
00032
00033 int c;
00034 ACE_CString s;
00035 while ((c = get_opt ()) != EOF)
00036 {
00037 switch (c)
00038 {
00039 case 1:
00040 url = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ());
00041 break;
00042
00043 case 'a':
00044 do_active = true;
00045 break;
00046
00047 case 'o':
00048 outfile = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ());
00049 break;
00050
00051 case 'l':
00052 do_login = true;
00053 break;
00054
00055 case 'p':
00056 password = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ());
00057 break;
00058
00059 case 'h':
00060 default:
00061 usage ();
00062 return false;
00063 }
00064 }
00065
00066 return true;
00067 }
00068
00069 class My_Authenticator
00070 : public ACE::INet::AuthenticatorBase
00071 {
00072 public:
00073 My_Authenticator () {}
00074 ~My_Authenticator () {}
00075
00076 virtual bool authenticate(ACE::INet::AuthenticationBase& auth) const
00077 {
00078 if (!password.empty ()) auth.password (password);
00079 if (do_login)
00080 {
00081 std::cout << "Authentication (" << auth.scheme() << ") required." << std::endl;
00082 std::cout << "Realm : " << auth.realm () << std::endl
00083 << "User: " << auth.user() << std::endl
00084 << "Password: \t\t\t (Enter password or press enter for default)\rPassword: ";
00085 char buf[80] = {0};
00086 u_int n = 0;
00087 int ch = 0;
00088 while (n < (sizeof(buf)-1) && (ch = std::cin.get ()) != '\n' && ch != std::char_traits<char>::eof ())
00089 {
00090 buf[n++] = char(ch);
00091 }
00092 if (n>0)
00093 auth.password (buf);
00094 }
00095 return true;
00096 }
00097 };
00098
00099 class My_FTP_RequestHandler
00100 : public ACE::FTP::ClientRequestHandler
00101 {
00102 public:
00103 My_FTP_RequestHandler () : read_length_ (0) {}
00104 virtual ~My_FTP_RequestHandler () {}
00105
00106 protected:
00107 virtual void handle_request_error (const ACE::FTP::URL& url)
00108 {
00109 std::cout << "ERROR" << std::endl;
00110 std::cerr << "Failed to handle download for " << url.to_string ().c_str () << std::endl;
00111 }
00112
00113 virtual void handle_connection_error (const ACE::FTP::URL& url)
00114 {
00115 std::cout << "ERROR" << std::endl;
00116 std::cerr << "Failed to set up connection for " << url.to_string ().c_str () << std::endl;
00117 }
00118
00119 virtual void after_read (const char_type* , int length_read)
00120 {
00121 this->read_length_ += length_read;
00122 std::cout << "\r [" << this->read_length_ << "/???]";
00123 }
00124
00125 virtual void on_eof ()
00126 {
00127 ACE::FTP::ClientRequestHandler::on_eof ();
00128 std::cout << std::endl;
00129 }
00130
00131 private:
00132 int read_length_;
00133 };
00134
00135 int
00136 ACE_TMAIN (int argc, ACE_TCHAR *argv [])
00137 {
00138 ACE_Auto_Ptr<std::ofstream> fout;
00139 std::ostream* sout = &std::cout;
00140
00141 if (!parse_args (argc, argv))
00142 {
00143 return 1;
00144 }
00145
00146 ACE::INet::URL_INetAuthBase::add_authenticator ("my_auth",
00147 new My_Authenticator);
00148
00149 std::cout << "Starting..." << std::endl;
00150
00151 if (!url.empty ())
00152 {
00153 if (!outfile.empty ())
00154 {
00155 fout.reset (new std::ofstream (outfile.c_str (), std::ios_base::binary|std::ios_base::out));
00156
00157 if (!*fout)
00158 {
00159 std::cerr << "Failed to open output file : " << outfile.c_str () << std::endl;
00160 return 1;
00161 }
00162
00163 sout = fout.get ();
00164 }
00165
00166 ACE::FTP::URL ftp_url;
00167
00168 std::cout << "Parsing url [" << url.c_str () << "]" << std::endl;
00169
00170 if (!ftp_url.parse (url))
00171 {
00172 std::cerr << "Failed parsing url [" << url << "]" << std::endl;
00173 std::cerr << "\tresult = " << ftp_url.to_string ().c_str ();
00174 return 1;
00175 }
00176
00177 std::cout << "Opening url..." << std::endl;
00178 My_FTP_RequestHandler my_rh;
00179 if (do_active) my_rh.use_active_mode ();
00180 ACE::INet::URLStream urlin = ftp_url.open (my_rh);
00181 if (urlin)
00182 {
00183 std::cout << "Saving to: ";
00184 if (!outfile.empty ())
00185 std::cout << '\'' << outfile.c_str () << '\'' << std::endl;
00186 else
00187 std::cout << "(stdout)" << std::endl;
00188
00189 (*sout) << urlin->rdbuf ();
00190 sout->flush ();
00191 }
00192 }
00193 else
00194 {
00195 std::cerr << "ERROR: No URL specified!" << std::endl;
00196 usage ();
00197 return 1;
00198 }
00199
00200 std::cout << "Done" << std::endl;
00201
00202 return 0;
00203 }