Skip to content

Commit b46df4e

Browse files
committed
Fixes #82 -- simplifying header printing.
1 parent 2d39520 commit b46df4e

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

libs/network/example/http_client.cpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,21 @@
1111
*/
1212
#include <boost/program_options.hpp>
1313
#include <boost/network/protocol/http.hpp>
14-
#include <boost/function_output_iterator.hpp>
1514
#include <string>
15+
#include <utility>
1616
#include <iostream>
1717

1818
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-
};
2919

3020
int main(int argc, char * argv[]) {
21+
using namespace boost::network;
3122
po::options_description options("Allowed options");
32-
string output_filename, source;
23+
std::string output_filename, source;
3324
bool show_headers;
3425
options.add_options()
3526
("help,h", "produce help message")
3627
("headers,H", "print headers")
37-
("source,s", po::value<string>(&source), "source URL")
28+
("source,s", po::value<std::string>(&source), "source URL")
3829
;
3930

4031
po::positional_options_description positional_options;
@@ -44,47 +35,47 @@ int main(int argc, char * argv[]) {
4435
po::store(po::command_line_parser(argc, argv).options(options).positional(positional_options).run(),
4536
vm);
4637
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;
5041
return EXIT_FAILURE;
5142
};
5243

5344
if (vm.count("help")) {
54-
cout << options << endl;
45+
std::cout << options << std::endl;
5546
return EXIT_SUCCESS;
5647
};
5748

5849
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;
6152
return EXIT_FAILURE;
6253
};
6354

6455
show_headers = vm.count("headers") ? true : false ;
6556

66-
using namespace boost::network;
6757

6858
typedef http::basic_client<http::tags::http_async_8bit_tcp_resolve, 1, 0>
6959
http_client;
70-
60+
7161
http_client::request request(source);
7262
http_client::string_type destination_ = host(request);
73-
63+
7464
request << ::boost::network::header("Connection", "close");
7565
http_client client(http::_follow_redirects=true);
7666
http_client::response response = client.get(request);
7767

7868
if (show_headers) {
7969
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;
8375
};
84-
76+
8577
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));
8879
return EXIT_SUCCESS;
8980
}
9081
//]

0 commit comments

Comments
 (0)