#include "URL.hpp" #include "SimpleURLNormalizer.hpp" #include "RewindInputStream.hpp" #ifdef USE_MODULELOADER #include "Fetcher.hpp" #include "ModuleLoader.hpp" #else #ifndef _WIN32 #include "LibFetchFetcher.hpp" #else #include "WinHttpFetcher.hpp" #endif #endif #include #include #include #include using namespace std; static void copy_stream( istream& in, ostream &out ) { enum { BUFSIZE = 4096 }; char buf[BUFSIZE]; while( in.good( ) && !in.eof( ) ) { in.read( buf, BUFSIZE ); out.write( buf, in.gcount( ) ); } } int main( int argc, char *argv[] ) { try { if( argc < 3 ) { cerr << "usage: test1 \n" << endl; return 1; } char *method = argv[1]; char *urlString = argv[2]; #ifdef USE_MODULELOADER vector modules; #ifndef _WIN32 modules.push_back( "../../src/modules/fetcher/libfetch/mod_fetcher_libfetch.so" ); #else modules.push_back( "..\\..\\src\\modules\\fetcher\\winhttp\\mod_fetcher_winhttp.dll" ); #endif ModuleLoader fetchers( modules ); Fetcher *fetcher = fetchers.create( method ); #else Fetcher *fetcher; if( strcmp( method, "libfetch_fetcher" ) == 0 ) { #ifndef _WIN32 fetcher = new LibFetchFetcher( ); #endif } else if( strcmp( method, "winhttp_fetcher" ) == 0 ) { #ifdef _WIN32 fetcher = new WinHttpFetcher( ); #endif } else { cerr << "Unknown fetcher method '" << method << "'" << endl; return 1; } #endif SimpleURLNormalizer normalizer; URL url = normalizer.parseUrl( urlString ); RewindInputStream *s = fetcher->fetch( url ); if( !s->good( ) ) { cerr << "Failed to fetch '" << url << "': " << s->lastErrMsg( ) << endl; delete s; #ifdef USE_MODULELOADER fetchers.destroy( fetcher ); #else delete fetcher; #endif return 1; } copy_stream( *s, cout ); // s->rewind( ); // copy_stream( *s, cout ); delete s; #ifdef USE_MODULELOADER fetchers.destroy( fetcher ); #else delete fetcher; #endif return 0; } catch( exception &e ) { cerr << e.what( ) << endl; return 1; } }