11
11
*/
12
12
#include < boost/program_options.hpp>
13
13
#include < boost/network/protocol/http.hpp>
14
- #include < boost/function_output_iterator.hpp>
15
14
#include < string>
15
+ #include < utility>
16
16
#include < iostream>
17
17
18
18
namespace po = boost::program_options;
19
- using namespace std ;
20
-
21
- struct header_printer {
22
- std::ostream & os;
23
- header_printer (std::ostream & os_) : os(os_) {}
24
- template <class Pair >
25
- void operator ()(Pair const & p) {
26
- os << p.first << " : " << p.second << endl;
27
- }
28
- };
29
19
30
20
int main (int argc, char * argv[]) {
21
+ using namespace boost ::network;
31
22
po::options_description options (" Allowed options" );
32
- string output_filename, source;
23
+ std:: string output_filename, source;
33
24
bool show_headers;
34
25
options.add_options ()
35
26
(" help,h" , " produce help message" )
36
27
(" headers,H" , " print headers" )
37
- (" source,s" , po::value<string>(&source), " source URL" )
28
+ (" source,s" , po::value<std:: string>(&source), " source URL" )
38
29
;
39
30
40
31
po::positional_options_description positional_options;
@@ -44,47 +35,47 @@ int main(int argc, char * argv[]) {
44
35
po::store (po::command_line_parser (argc, argv).options (options).positional (positional_options).run (),
45
36
vm);
46
37
po::notify (vm);
47
- } catch (exception & e) {
48
- cout << " Error: " << e.what () << endl;
49
- cout << options << endl;
38
+ } catch (std:: exception & e) {
39
+ std:: cout << " Error: " << e.what () << std:: endl;
40
+ std:: cout << options << std:: endl;
50
41
return EXIT_FAILURE;
51
42
};
52
43
53
44
if (vm.count (" help" )) {
54
- cout << options << endl;
45
+ std:: cout << options << std:: endl;
55
46
return EXIT_SUCCESS;
56
47
};
57
48
58
49
if (vm.count (" source" ) < 1 ) {
59
- cout << " Error: Source URL required." << endl;
60
- cout << options << endl;
50
+ std:: cout << " Error: Source URL required." << std:: endl;
51
+ std:: cout << options << std:: endl;
61
52
return EXIT_FAILURE;
62
53
};
63
54
64
55
show_headers = vm.count (" headers" ) ? true : false ;
65
56
66
- using namespace boost ::network;
67
57
68
58
typedef http::basic_client<http::tags::http_async_8bit_tcp_resolve, 1 , 0 >
69
59
http_client;
70
-
60
+
71
61
http_client::request request (source);
72
62
http_client::string_type destination_ = host (request);
73
-
63
+
74
64
request << ::boost::network::header (" Connection" , " close" );
75
65
http_client client (http::_follow_redirects=true );
76
66
http_client::response response = client.get (request);
77
67
78
68
if (show_headers) {
79
69
headers_range<http_client::response>::type headers_ = response.headers ();
80
- std::copy (headers_.begin (), headers_.end (),
81
- boost::make_function_output_iterator (header_printer (cout)));
82
- cout << endl;
70
+ typedef std::pair<std::string, std::string> header_type;
71
+ BOOST_FOREACH (header_type const & header, headers_) {
72
+ std::cout << header.first << " : " << header.second << std::endl;
73
+ }
74
+ std::cout << std::endl;
83
75
};
84
-
76
+
85
77
body_range<http_client::response>::type body_ = body (response).range ();
86
- boost::copy (body_, std::ostream_iterator<char_<http_client::request::tag>::type>(cout));
87
-
78
+ boost::copy (body_, std::ostream_iterator<char_<http_client::request::tag>::type>(std::cout));
88
79
return EXIT_SUCCESS;
89
80
}
90
81
// ]
0 commit comments