前回の Poco::TypeList の記事でも出てきましたが、改めて Poco::URIStreamOpener を紹介します。
Poco::URIStreamFactory の派生クラスの Poco::Net::FTPStreamFactory, Poco::Net::HTTPStreamFactory, Poco::Net::HTTPSStreamFactory を登録しておくと、Poco::URIStreamOpener に対して、それぞれ ftp, http, htts の URI を指定すれば、そのストリーム出力が得られます。
Poco::FileStreamFactory は自動的に登録されるので、file の URI にも対応しています。
URIStreamOpenerTest.cpp
・http のサンプルとして、Poco::HTTPClientSession の記事の時に使った:
http://poco.roundsquare.net/downloads/test.txt にあるテキストファイルを入手。
・https のサンプルは省略。
・ftp のサンプルとして:
ftp://ftp.iij.ad.jp/pub/linux/debian/dists/README にあるテキストファイルを入手。
・file のサンプルとして、Poco::TemporaryFile で作ったテキストファイルを入手。
・TestURIStreamOpener() の中で、ftp の時は user と password を表示。
#include <Poco/URI.h> #include <Poco/Format.h> #include <Poco/URIStreamOpener.h> #include <Poco/Net/HTTPStreamFactory.h> #include <Poco/Net/FTPStreamFactory.h> #include <Poco/StreamCopier.h> #include <Poco/TemporaryFile.h> #include <Poco/Path.h> #include <Poco/Thread.h> #include <sstream> #include <fstream> #include "ScopedLogMessage.h" #include "PrepareConsoleLogger.h" void TestURIStreamOpener(ScopedLogMessage& msg, const Poco::URI& uri) { msg.Message(Poco::format(" --- %s ---", uri.toString())); if("ftp" == uri.getScheme()) { std::string password; std::string user(uri.getUserInfo()); if("" == user) { user = "anonymous"; password = Poco::Net::FTPStreamFactory::getAnonymousPassword(); } else { Poco::Net::FTPPasswordProvider* pProvider = Poco::Net::FTPStreamFactory::getPasswordProvider(); if(NULL != pProvider) { password = pProvider->password(user, uri.getAuthority()); } } msg.Message(Poco::format(" (user=%s, password=%s)", user, password)); } try { std::auto_ptr<std::istream> pStr(Poco::URIStreamOpener::defaultOpener().open(uri)); std::stringstream ss; Poco::StreamCopier::copyStream(*pStr.get(), ss); Poco::Thread* p_thread = Poco::Thread::current(); int threadID = (0 == p_thread) ? 0:p_thread->id(); msg.Message(Poco::format(" <start>\n%s[%d] <end>", ss.str(), threadID)); } catch(Poco::Exception& exc) { msg.Message(Poco::format(" %s", exc.displayText())); } } int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("URIStreamOpenerTest ", "start", "end"); Poco::Net::HTTPStreamFactory::registerFactory(); Poco::Net::FTPStreamFactory::registerFactory(); // HTTP Poco::URI uriHTTP("http://poco.roundsquare.net/downloads/test.txt"); TestURIStreamOpener(msg, uriHTTP); // FTP Poco::URI uriFTP("ftp://ftp.iij.ad.jp/pub/linux/debian/dists/README"); TestURIStreamOpener(msg, uriFTP); // file Poco::TemporaryFile tempFile; std::string path = tempFile.path(); std::ofstream ostr(path.c_str()); ostr << "Hello, world!" << std::endl; ostr.close(); Poco::URI uriFile; uriFile.setScheme("file"); uriFile.setPath(Poco::Path(path).toString(Poco::Path::PATH_UNIX)); TestURIStreamOpener(msg, uriFile); return 0; } |
Results of execution
・anonymous ftp の password に、デフォルトの poco@localhost が使われているが、本来は予め
Poco::Net::FTPStreamFactory::setAnonymousPassword(password) で設定しておくべき。
(ftp.iij.ad.jp の anonymous ftp は password を要求しないので、今回は問題無し)
・firewall の内側からの場合、7行目で停止し、しばらく待つとタイムアウトして次に進むことがある。
残念ながら対策方法が分かっていません。どなたか分かったら、コメントなどでお知らせください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [0] URIStreamOpenerTest start [0] --- http://poco.roundsquare.net/downloads/test.txt --- [0] <start> --- This is a sample text file for HTTPClientSessionTest --- [0] <end> [0] --- ftp://ftp.iij.ad.jp/pub/linux/debian/dists/README --- [0] (user=anonymous, password=poco@localhost) [0] <start> This directory, dists, is the canonical way to access the distributions. Each distribution can be accessed by name or state from here. oldstable, or etch - the released Debian 4.0r9. stable, or lenny - the released Debian 5.0.4. oldstable-proposed-updates - possible updates to Debian 4.0. See the README there. proposed-updates - possible updates to Debian 5.0. See the README there. testing, or squeeze - the development version of the next release. unstable, or sid - untested candidate packages for future releases. [0] <end> [0] --- file:///var/folders/iR/iRcZrcWkGCe6fDFVNkPVjE+++TI/-Tmp-/tmp3929aaaaaa --- [0] <start> Hello, world! [0] <end> [0] URIStreamOpenerTest end |
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年6月4日からのダウンロード数:1097
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
Reference
・http://pocoproject.org にある URIandUUID のプレセンテーション。(PDF)
![]() |
Copyright © 2010 Round Square Inc. All rights reserved. |
---|
0 Comments.