Skip to content

Commit f45f21a

Browse files
committed
Added an atom feed example.
1 parent 064b0bb commit f45f21a

File tree

12 files changed

+4096
-15
lines changed

12 files changed

+4096
-15
lines changed

boost/network/uri/accessors.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Map &query_map(const uri &uri_, Map &map) {
5151
return map;
5252
}
5353

54+
inline
5455
std::string username(const uri &uri_) {
5556
const std::string user_info = uri_.user_info();
5657
uri::const_iterator it(boost::begin(user_info)), end(boost::end(user_info));
@@ -62,6 +63,7 @@ std::string username(const uri &uri_) {
6263
return std::string(boost::begin(user_info), it);
6364
}
6465

66+
inline
6567
std::string password(const uri &uri_) {
6668
const std::string user_info = uri_.user_info();
6769
uri::const_iterator it(boost::begin(user_info)), end(boost::end(user_info));
@@ -74,20 +76,23 @@ std::string password(const uri &uri_) {
7476
return std::string(it, boost::end(user_info));
7577
}
7678

79+
inline
7780
std::string decoded_path(const uri &uri_) {
7881
const std::string path = uri_.path();
7982
std::string decoded_path;
8083
decode(path, std::back_inserter(decoded_path));
8184
return decoded_path;
8285
}
8386

87+
inline
8488
std::string decoded_query(const uri &uri_) {
8589
const std::string query = uri_.query();
8690
std::string decoded_query;
8791
decode(query, std::back_inserter(decoded_query));
8892
return decoded_query;
8993
}
9094

95+
inline
9196
std::string decoded_fragment(const uri &uri_) {
9297
const std::string fragment = uri_.fragment();
9398
std::string decoded_fragment;

boost/network/uri/uri.hpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,6 @@ bool is_valid(const uri &uri_) {
238238
return valid(uri_);
239239
}
240240

241-
inline
242-
bool is_hierarchical(const uri &uri_) {
243-
//uri::const_range_type scheme = uri_.scheme_range();
244-
//uri::const_range_type user_info = uri_.user_info_range();
245-
//return is_valid(uri_) &&
246-
// boost::equal(std::make_pair(boost::end(scheme),
247-
// boost::begin(user_info)),
248-
// boost::as_literal("://"));
249-
return false;
250-
}
251-
252-
bool is_opaque(const uri &uri_) {
253-
return false;
254-
}
255-
256241
inline
257242
bool operator == (const uri &lhs, const uri &rhs) {
258243
return std::equal(lhs.begin(), lhs.end(), rhs.begin());

libs/network/example/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ endif (OPENSSL_FOUND)
1010

1111
add_executable(http_client http_client.cpp)
1212
add_executable(simple_wget simple_wget.cpp)
13+
add_executable(atom_reader atom/atom.cpp atom/main.cpp)
1314
add_executable(hello_world_server http/hello_world_server.cpp)
1415
if (UNIX)
1516
add_executable(fileserver http/fileserver.cpp)
1617
endif (UNIX)
1718
add_executable(uri uri.cpp)
1819
add_dependencies(http_client cppnetlib-uri cppnetlib-client-connections)
1920
add_dependencies(simple_wget cppnetlib-uri cppnetlib-client-connections)
21+
add_dependencies(atom_reader cppnetlib-uri cppnetlib-client-connections)
2022
add_dependencies(uri cppnetlib-uri)
2123
set(BOOST_CLIENT_LIBS
2224
${Boost_PROGRAM_OPTIONS_LIBRARY}
@@ -43,13 +45,20 @@ target_link_libraries(simple_wget
4345
cppnetlib-uri
4446
cppnetlib-client-connections)
4547

48+
target_link_libraries(atom_reader
49+
${BOOST_CLIENT_LIBS}
50+
${CMAKE_THREAD_LIBS_INIT}
51+
cppnetlib-uri
52+
cppnetlib-client-connections)
53+
4654
target_link_libraries(hello_world_server
4755
${BOOST_SERVER_LIBS}
4856
${CMAKE_THREAD_LIBS_INIT})
4957

5058
if (OPENSSL_FOUND)
5159
target_link_libraries(http_client ${OPENSSL_LIBRARIES})
5260
target_link_libraries(simple_wget ${OPENSSL_LIBRARIES})
61+
target_link_libraries(atom_reader ${OPENSSL_LIBRARIES})
5362
target_link_libraries(hello_world_server ${OPENSSL_LIBRARIES})
5463
endif (OPENSSL_FOUND)
5564

@@ -64,6 +73,7 @@ endif (UNIX)
6473
target_link_libraries(uri cppnetlib-uri)
6574
set_target_properties(http_client PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
6675
set_target_properties(simple_wget PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
76+
set_target_properties(atom_reader PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
6777
set_target_properties(hello_world_server PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
6878
if (UNIX)
6979
set_target_properties(fileserver PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)

libs/network/example/atom/atom.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 "atom.hpp"
8+
#include "../rapidxml/rapidxml.hpp"
9+
#include <stdexcept>
10+
#include <cassert>
11+
12+
13+
namespace boost {
14+
namespace network {
15+
namespace atom {
16+
feed::feed(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<> *feed = doc.first_node("feed");
22+
if (!feed) {
23+
throw std::runtime_error("Invalid atom feed.");
24+
}
25+
26+
rapidxml::xml_node<> *title = feed->first_node("title");
27+
if (title) {
28+
title_ = title->first_node()->value();
29+
}
30+
31+
rapidxml::xml_node<> *subtitle = feed->first_node("subtitle");
32+
if (subtitle) {
33+
subtitle_ = subtitle->first_node()->value();
34+
}
35+
36+
rapidxml::xml_node<> *id = feed->first_node("id");
37+
if (id) {
38+
id_ = id->first_node()->value();
39+
}
40+
41+
rapidxml::xml_node<> *updated = feed->first_node("updated");
42+
if (updated) {
43+
updated_ = updated->first_node()->value();
44+
}
45+
46+
rapidxml::xml_node<> *author = feed->first_node("author");
47+
if (author) {
48+
rapidxml::xml_node<> *name = author->first_node("name");
49+
rapidxml::xml_node<> *email = author->first_node("email");
50+
if (name && email) {
51+
author_ = atom::author(name->first_node()->value(), email->first_node()->value());
52+
}
53+
else if (name) {
54+
author_ = atom::author(name->first_node()->value());
55+
}
56+
}
57+
58+
rapidxml::xml_node<> *entry = feed->first_node("entry");
59+
while (entry) {
60+
entries_.push_back(atom::entry());
61+
62+
rapidxml::xml_node<> *title = entry->first_node("title");
63+
if (title) {
64+
entries_.back().set_title(title->first_node()->value());
65+
}
66+
67+
rapidxml::xml_node<> *id = entry->first_node("id");
68+
if (id) {
69+
entries_.back().set_id(id->first_node()->value());
70+
}
71+
72+
rapidxml::xml_node<> *published = entry->first_node("published");
73+
if (published) {
74+
entries_.back().set_published(published->first_node()->value());
75+
}
76+
77+
rapidxml::xml_node<> *updated = entry->first_node("updated");
78+
if (updated) {
79+
entries_.back().set_updated(updated->first_node()->value());
80+
}
81+
82+
rapidxml::xml_node<> *summary = entry->first_node("summary");
83+
if (summary) {
84+
entries_.back().set_summary(summary->first_node()->value());
85+
}
86+
87+
rapidxml::xml_node<> *content = entry->first_node("content");
88+
if (content) {
89+
entries_.back().set_content(content->first_node()->value());
90+
}
91+
92+
entry = entry->next_sibling();
93+
}
94+
}
95+
} // namespace atom
96+
} // namespace network
97+
} // namespace boost

libs/network/example/atom/atom.hpp

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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 ___ATOM_INC__
7+
# define ___ATOM_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 atom {
18+
class entry {
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_id(const std::string &id) {
31+
id_ = id;
32+
}
33+
34+
std::string id() const {
35+
return id_;
36+
}
37+
38+
void set_published(const std::string &published) {
39+
published_ = published;
40+
}
41+
42+
std::string published() const {
43+
return published_;
44+
}
45+
46+
void set_updated(const std::string &updated) {
47+
updated_ = updated;
48+
}
49+
50+
std::string updated() const {
51+
return updated_;
52+
}
53+
54+
void set_summary(const std::string &summary) {
55+
summary_ = summary;
56+
}
57+
58+
std::string summary() const {
59+
return summary_;
60+
}
61+
62+
void set_content(const std::string &content) {
63+
content_ = content;
64+
}
65+
66+
std::string content() const {
67+
return content_;
68+
}
69+
70+
private:
71+
72+
std::string title_;
73+
std::string id_;
74+
std::string published_;
75+
std::string updated_;
76+
std::string summary_;
77+
std::string content_;
78+
79+
};
80+
81+
class author {
82+
83+
public:
84+
85+
author() {
86+
87+
}
88+
89+
author(const std::string &name)
90+
: name_(name) {
91+
92+
}
93+
94+
author(const std::string &name, const std::string &email)
95+
: name_(name), email_(email) {
96+
97+
}
98+
99+
std::string name() const {
100+
return name_;
101+
}
102+
103+
std::string email() const {
104+
return email_;
105+
}
106+
107+
private:
108+
109+
std::string name_;
110+
std::string email_;
111+
112+
};
113+
114+
class feed {
115+
116+
public:
117+
typedef entry value_type;
118+
typedef std::vector<entry>::iterator iterator;
119+
typedef std::vector<entry>::const_iterator const_iterator;
120+
121+
feed(const http::client::response &response);
122+
123+
std::string title() const {
124+
return title_;
125+
}
126+
127+
std::string subtitle() const {
128+
return subtitle_;
129+
}
130+
131+
std::string id() const {
132+
return id_;
133+
}
134+
135+
std::string updated() const {
136+
return updated_;
137+
}
138+
139+
atom::author author() const {
140+
return author_;
141+
}
142+
143+
unsigned int entry_count() const {
144+
return entries_.size();
145+
}
146+
147+
iterator begin() {
148+
return entries_.begin();
149+
}
150+
151+
iterator end() {
152+
return entries_.end();
153+
}
154+
155+
const_iterator begin() const {
156+
return entries_.begin();
157+
}
158+
159+
const_iterator end() const {
160+
return entries_.end();
161+
}
162+
163+
private:
164+
165+
std::string title_;
166+
std::string subtitle_;
167+
std::string id_;
168+
std::string updated_;
169+
atom::author author_;
170+
std::vector<entry> entries_;
171+
172+
};
173+
} // namespace atom
174+
} // namespace network
175+
} // namespace boost
176+
177+
#endif // ___ATOM_INC__

0 commit comments

Comments
 (0)