Skip to content

Commit 56048ec

Browse files
committed
Added an RSS feed example.
1 parent f45f21a commit 56048ec

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed

libs/network/example/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ endif (OPENSSL_FOUND)
1111
add_executable(http_client http_client.cpp)
1212
add_executable(simple_wget simple_wget.cpp)
1313
add_executable(atom_reader atom/atom.cpp atom/main.cpp)
14+
add_executable(rss_reader rss/rss.cpp rss/main.cpp)
1415
add_executable(hello_world_server http/hello_world_server.cpp)
1516
if (UNIX)
1617
add_executable(fileserver http/fileserver.cpp)
@@ -19,6 +20,7 @@ add_executable(uri uri.cpp)
1920
add_dependencies(http_client cppnetlib-uri cppnetlib-client-connections)
2021
add_dependencies(simple_wget cppnetlib-uri cppnetlib-client-connections)
2122
add_dependencies(atom_reader cppnetlib-uri cppnetlib-client-connections)
23+
add_dependencies(rss_reader cppnetlib-uri cppnetlib-client-connections)
2224
add_dependencies(uri cppnetlib-uri)
2325
set(BOOST_CLIENT_LIBS
2426
${Boost_PROGRAM_OPTIONS_LIBRARY}
@@ -51,6 +53,12 @@ target_link_libraries(atom_reader
5153
cppnetlib-uri
5254
cppnetlib-client-connections)
5355

56+
target_link_libraries(rss_reader
57+
${BOOST_CLIENT_LIBS}
58+
${CMAKE_THREAD_LIBS_INIT}
59+
cppnetlib-uri
60+
cppnetlib-client-connections)
61+
5462
target_link_libraries(hello_world_server
5563
${BOOST_SERVER_LIBS}
5664
${CMAKE_THREAD_LIBS_INIT})
@@ -59,6 +67,7 @@ if (OPENSSL_FOUND)
5967
target_link_libraries(http_client ${OPENSSL_LIBRARIES})
6068
target_link_libraries(simple_wget ${OPENSSL_LIBRARIES})
6169
target_link_libraries(atom_reader ${OPENSSL_LIBRARIES})
70+
target_link_libraries(rss_reader ${OPENSSL_LIBRARIES})
6271
target_link_libraries(hello_world_server ${OPENSSL_LIBRARIES})
6372
endif (OPENSSL_FOUND)
6473

@@ -74,6 +83,7 @@ target_link_libraries(uri cppnetlib-uri)
7483
set_target_properties(http_client PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
7584
set_target_properties(simple_wget PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
7685
set_target_properties(atom_reader PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
86+
set_target_properties(rss_reader PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
7787
set_target_properties(hello_world_server PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
7888
if (UNIX)
7989
set_target_properties(fileserver PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)

libs/network/example/rss/main.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Glyn Matthews 2011.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#include "rss.hpp"
8+
#include <boost/network/protocol/http/client.hpp>
9+
#include <iostream>
10+
11+
int main(int argc, char * argv[]) {
12+
using namespace boost::network;
13+
14+
if (argc != 2) {
15+
std::cout << "Usage: " << argv[0] << " <url>" << std::endl;
16+
return 1;
17+
}
18+
19+
try {
20+
http::client client;
21+
http::client::request request(argv[1]);
22+
request << header("Connection", "close");
23+
http::client::response response = client.get(request);
24+
rss::channel channel(response);
25+
26+
std::cout << "Channel: " << channel.title() << " (" << channel.description() << ")" << std::endl;
27+
BOOST_FOREACH(const rss::item &item, channel) {
28+
std::cout << item.title() << " (" << item.author() << ")" << std::endl;
29+
}
30+
}
31+
catch (std::exception &e) {
32+
std::cerr << e.what() << std::endl;
33+
}
34+
35+
return 0;
36+
}

libs/network/example/rss/rss.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) Glyn Matthews 2011.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#include "rss.hpp"
8+
#include "../rapidxml/rapidxml.hpp"
9+
#include <stdexcept>
10+
#include <cassert>
11+
12+
13+
namespace boost {
14+
namespace network {
15+
namespace rss {
16+
channel::channel(const http::client::response &response) {
17+
std::string response_body = body(response);
18+
rapidxml::xml_document<> doc;
19+
doc.parse<0>(const_cast<char *>(response_body.c_str()));
20+
21+
rapidxml::xml_node<> *rss = doc.first_node("rss");
22+
if (!rss) {
23+
throw std::runtime_error("Invalid RSS feed.");
24+
}
25+
26+
rapidxml::xml_node<> *channel = rss->first_node("channel");
27+
if (!channel) {
28+
throw std::runtime_error("Invalid RSS channel.");
29+
}
30+
31+
rapidxml::xml_node<> *title = channel->first_node("title");
32+
if (title) {
33+
title_ = title->first_node()->value();
34+
}
35+
36+
rapidxml::xml_node<> *description = channel->first_node("description");
37+
if (description) {
38+
description_ = description->first_node()->value();
39+
}
40+
41+
rapidxml::xml_node<> *link = channel->first_node("link");
42+
if (link) {
43+
link_ = link->first_node()->value();
44+
}
45+
46+
rapidxml::xml_node<> *author = channel->first_node("author");
47+
if (author) {
48+
author_ = author->first_node()->value();
49+
}
50+
51+
rapidxml::xml_node<> *item = channel->first_node("item");
52+
while (item) {
53+
items_.push_back(rss::item());
54+
55+
rapidxml::xml_node<> *title = item->first_node("title");
56+
if (title) {
57+
items_.back().set_title(title->first_node()->value());
58+
}
59+
60+
rapidxml::xml_node<> *author = item->first_node("author");
61+
if (author) {
62+
items_.back().set_author(author->first_node()->value());
63+
}
64+
65+
rapidxml::xml_node<> *description = item->first_node("description");
66+
if (description) {
67+
items_.back().set_description(description->first_node()->value());
68+
}
69+
70+
item = item->next_sibling();
71+
}
72+
}
73+
} // namespace rss
74+
} // namespace network
75+
} // namespace boost

libs/network/example/rss/rss.hpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) Glyn Matthews 2011.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef ___RSS_INC__
7+
# define ___RSS_INC__
8+
9+
10+
# include <string>
11+
# include <vector>
12+
# include <boost/network/protocol/http.hpp>
13+
14+
15+
namespace boost {
16+
namespace network {
17+
namespace rss {
18+
class item {
19+
20+
public:
21+
22+
void set_title(const std::string &title) {
23+
title_ = title;
24+
}
25+
26+
std::string title() const {
27+
return title_;
28+
}
29+
30+
void set_author(const std::string &author) {
31+
author_ = author;
32+
}
33+
34+
std::string author() const {
35+
return author_;
36+
}
37+
38+
void set_description(const std::string &description) {
39+
description_ = description;
40+
}
41+
42+
std::string description() const {
43+
return description_;
44+
}
45+
46+
private:
47+
48+
std::string title_;
49+
std::string author_;
50+
std::string description_;
51+
52+
};
53+
54+
class channel {
55+
56+
public:
57+
typedef item value_type;
58+
typedef std::vector<item>::iterator iterator;
59+
typedef std::vector<item>::const_iterator const_iterator;
60+
61+
channel(const http::client::response &response);
62+
63+
std::string title() const {
64+
return title_;
65+
}
66+
67+
std::string description() const {
68+
return description_;
69+
}
70+
71+
std::string link() const {
72+
return link_;
73+
}
74+
75+
std::string author() const {
76+
return author_;
77+
}
78+
79+
unsigned int item_count() const {
80+
return items_.size();
81+
}
82+
83+
iterator begin() {
84+
return items_.begin();
85+
}
86+
87+
iterator end() {
88+
return items_.end();
89+
}
90+
91+
const_iterator begin() const {
92+
return items_.begin();
93+
}
94+
95+
const_iterator end() const {
96+
return items_.end();
97+
}
98+
99+
private:
100+
101+
std::string title_;
102+
std::string description_;
103+
std::string link_;
104+
std::string author_;
105+
std::vector<item> items_;
106+
107+
};
108+
} // namespace rss
109+
} // namespace network
110+
} // namespace boost
111+
112+
#endif // ___RSS_INC__

0 commit comments

Comments
 (0)