1
1
2
- // Copyright Dean Michael Berris 2008.
2
+ // Copyright 2008, 2014 Dean Michael Berris <[email protected] >
3
+ // Copyright 2014 Google, Inc.
3
4
// Distributed under the Boost Software License, Version 1.0.
4
- // (See accompanying file LICENSE_1_0.txt or copy at
5
- // http://www.boost.org/LICENSE_1_0.txt)
5
+ // (See accompanying file LICENSE_1_0.txt or copy at
6
+ // http://www.boost.org/LICENSE_1_0.txt)
6
7
7
8
// [ http_client_main
8
9
/* `
@@ -25,6 +26,7 @@ int main(int argc, char * argv[]) {
25
26
options.add_options ()
26
27
(" help,h" , " produce help message" )
27
28
(" headers,H" , " print headers" )
29
+ (" status,S" , " print status and message" )
28
30
(" source,s" , po::value<std::string>(&source), " source URL" )
29
31
;
30
32
@@ -39,44 +41,44 @@ int main(int argc, char * argv[]) {
39
41
std::cout << " Error: " << e.what () << std::endl;
40
42
std::cout << options << std::endl;
41
43
return EXIT_FAILURE;
42
- };
44
+ }
43
45
44
46
if (vm.count (" help" )) {
45
47
std::cout << options << std::endl;
46
48
return EXIT_SUCCESS;
47
- };
49
+ }
48
50
49
51
if (vm.count (" source" ) < 1 ) {
50
52
std::cout << " Error: Source URL required." << std::endl;
51
53
std::cout << options << std::endl;
52
54
return EXIT_FAILURE;
53
- };
55
+ }
54
56
55
- show_headers = vm.count (" headers" ) ? true : false ;
57
+ show_headers = vm.count (" headers" ) ? true : false ;
58
+ bool show_status = vm.count (" status" ) ? true : false ;
56
59
57
-
58
- typedef http::basic_client<http::tags::http_async_8bit_tcp_resolve, 1 , 0 >
59
- http_client;
60
-
61
- http_client::request request (source);
62
- http_client::string_type destination_ = host (request);
60
+ http::client::request request (source);
61
+ http::client::string_type destination_ = host (request);
63
62
64
63
request << ::boost::network::header (" Connection" , " close" );
65
- http_client::options client_options;
66
- http_client client (client_options.follow_redirects (true ));
67
- http_client::response response = client.get (request);
64
+ http::client::options client_options;
65
+ client_options.follow_redirects (true );
66
+ http::client client (client_options);
67
+ http::client::response response = client.get (request);
68
+
69
+ if (show_status)
70
+ std::cout << status (response) << " " << status_message (response) << std::endl;
68
71
69
72
if (show_headers) {
70
- headers_range<http_client ::response>::type headers_ = response.headers ();
73
+ headers_range<http::client ::response>::type headers_ = response.headers ();
71
74
typedef std::pair<std::string, std::string> header_type;
72
75
BOOST_FOREACH (header_type const & header, headers_) {
73
76
std::cout << header.first << " : " << header.second << std::endl;
74
77
}
75
78
std::cout << std::endl;
76
- };
79
+ }
77
80
78
- body_range<http_client::response>::type body_ = body (response).range ();
79
- boost::copy (body_, std::ostream_iterator<char_<http_client::request::tag>::type>(std::cout));
81
+ std::cout << body (response);
80
82
return EXIT_SUCCESS;
81
83
}
82
84
// ]
0 commit comments