Skip to content

Commit d5bf9ad

Browse files
author
glynos
committed
Merged branches/http_integration-r72 and branches/docs-r78.
1 parent c810667 commit d5bf9ad

23 files changed

+1143
-332
lines changed

boost/network/protocol/http/client.hpp

+221-133
Large diffs are not rendered by default.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
// Copyright Dean Michael Berris 2007-2008.
3+
// 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)
6+
7+
#ifndef __NETWORK_PROTOCOL_HTTP_CLIENT_20080923_1_HPP__
8+
#define __NETWORK_PROTOCOL_HTTP_CLIENT_20080923_1_HPP__
9+
10+
#ifndef BOOST_NETLIB_VERSION
11+
#define BOOST_NETLIB_VERSION "0.3"
12+
#endif
13+
14+
#include <boost/network/protocol/http/tags.hpp>
15+
16+
namespace boost { namespace network { namespace http {
17+
18+
//! Forward declaration of basic_client template.
19+
template <class tag, unsigned version_major, unsigned version_minor>
20+
class basic_client;
21+
22+
typedef basic_client<http::message_tag, 1, 0> client;
23+
24+
} // namespace http
25+
26+
} // namespace network
27+
28+
} // namespace boost
29+
30+
#endif // __NETWORK_PROTOCOL_HTTP_CLIENT_20080923_1_HPP__
31+

boost/network/protocol/http/impl/request.hpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,30 @@ namespace boost { namespace network { namespace http {
5050
fusion::pair<typename tags::query,typename string_traits<tag>::type>,
5151
fusion::pair<typename tags::anchor,typename string_traits<tag>::type>
5252
> uri_parts_type;
53-
53+
5454
mutable uri_parts_type uri_parts;
5555

5656
public:
57-
explicit basic_request(typename string_traits<tag>::type const & uri) {
57+
explicit basic_request(typename string_traits<tag>::type const & uri_) {
58+
uri(uri_);
59+
}
60+
61+
void uri(typename string_traits<tag>::type const & uri_) {
5862
using namespace boost::spirit;
5963
using namespace phoenix;
6064

6165
fusion::at_key<typename tags::port>(uri_parts) = 80u;
6266

6367
parse(
64-
uri.begin(), uri.end(),
68+
uri_.begin(), uri_.end(),
6569
// the parser
66-
str_p("http")[
67-
var(fusion::at_key<typename tags::protocol>(uri_parts))
68-
= construct_<typename string_traits<tag>::type>(arg1, arg2)
69-
]
70-
>> str_p("://")
70+
!(
71+
str_p("http")[
72+
var(fusion::at_key<typename tags::protocol>(uri_parts))
73+
= construct_<typename string_traits<tag>::type>(arg1, arg2)
74+
]
75+
>> str_p("://")
76+
)
7177
>> (+(alnum_p | '.' | '-' | '_'))[
7278
var(fusion::at_key<typename tags::host>(uri_parts))
7379
= construct_<typename string_traits<tag>::type>(arg1, arg2)
@@ -79,7 +85,7 @@ namespace boost { namespace network { namespace http {
7985
= arg1
8086
]
8187
>> !ch_p('/')
82-
)
88+
)
8389
>> (+(alnum_p | '/' | '%' | '_' | '-' | '.'))[
8490
var(fusion::at_key<typename tags::path>(uri_parts))
8591
= construct_<typename string_traits<tag>::type>(arg1, arg2)
@@ -111,7 +117,7 @@ namespace boost { namespace network { namespace http {
111117
basic_message<tag>(), uri_parts()
112118
{ }
113119

114-
basic_request(basic_request const & other) :
120+
basic_request(basic_request const & other) :
115121
basic_message<tag>(other), uri_parts(other.uri_parts)
116122
{ }
117123

boost/network/protocol/http/response.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace boost { namespace network { namespace http {
1515
template <class Tag>
1616
struct basic_response : public message_impl<Tag> {
1717
private:
18+
typedef message_impl<Tag> base_type;
1819
typedef typename string<Tag>::type string_type;
1920

2021
string_type version_;
@@ -25,7 +26,11 @@ namespace boost { namespace network { namespace http {
2526
typedef Tag tag;
2627

2728
basic_response()
28-
: version_(), status_(0u), status_message_()
29+
: base_type(), version_(), status_(0u), status_message_()
30+
{ };
31+
32+
basic_response(basic_response const & other)
33+
: base_type(other), version_(other.version_), status_(other.status_), status_message_(other.status_message_)
2934
{ };
3035

3136
string_type & version() {
@@ -64,7 +69,7 @@ namespace boost { namespace network { namespace http {
6469

6570
} // namespace http
6671

67-
} // namespace network
72+
} // namespace network
6873

6974
} // namespace boost
7075

boost/network/protocol/mime.hpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright Allister Levi Sanchez 2008.
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+
#ifndef __NETWORK_PROTOCOL_MIME__
8+
#define __NETWORK_PROTOCOL_MIME__
9+
10+
#include <string>
11+
#include <boost/network/message/tags.hpp>
12+
#include <boost/network/message/traits.hpp>
13+
14+
namespace boost { namespace network { namespace mime {
15+
16+
template <typename Tag>
17+
class mime {
18+
19+
friend typename ostringstream<Tag> &
20+
operator << (typename ostringstream<Tag> & out, const typename mime<Tag> & m);
21+
22+
public:
23+
24+
typedef typename headers_container<Tag>::type parameter_map;
25+
typedef typename string<Tag>::type string_type;
26+
27+
std::string type() { return _type; }
28+
string_type body() { return _body; }
29+
std::string encoding() { return _encoding; }
30+
std::string boundary() { return _boundary; }
31+
std::string disposition() { return _disposition; }
32+
const parameter_map & type_parameters() { return _type_params; }
33+
const parameter_map & disposition_parameters() { return _disposition_params; }
34+
35+
void set_disposition(std::string d) { _disposition = d; }
36+
void set_disposition_parameter(std::string param, std::string value) { _disposition_params[param] = value; }
37+
void set_type(std::string t) { _type = t; }
38+
void set_type_parameter(std::string param, std::string value) { _type_params[param] = value; }
39+
void set_body(std::string b) { _body = b; }
40+
void set_encoding(encoding_type e) { _encoding = e; }
41+
void set_boundary(std::string b) { _boundary = b; }
42+
43+
private:
44+
45+
std::string _disposition;
46+
parameter_map _disposition_params;
47+
std::string _type;
48+
parameter_map _type_params
49+
string_type _body;
50+
std::string _boundary;
51+
std::string _encoding;
52+
};
53+
54+
55+
template <class Tag>
56+
ostringstream<Tag> & operator << (ostringstream<Tag> & out, const mime & m)
57+
{
58+
std::string CRLF = "\r\n";
59+
out << "--" << m._boundary << CRLF << CRLF;
60+
if (m._type.size() > 0)
61+
out << "Content-Type: " << m._type;
62+
else
63+
out << "Content-Type: text/plain";
64+
65+
typedef typename headers_container<Tag>::type map_t;
66+
typedef typename map_t::const_iterator iter_t;
67+
68+
if (m._type_params.empty())
69+
out << "; charset=us-ascii" << CRLF;
70+
else {
71+
for (iter_t i = m._type_params.begin(); i != m._type_params.end(); ++i)
72+
{
73+
out << "; " << i->first << "=" << i->second;
74+
}
75+
out << CRLF;
76+
}
77+
78+
if (m._encoding.size() > 0)
79+
out << "Content-Type-Encoding: " << m._encoding << CRLF;
80+
else
81+
out << "Content-Type-Encoding: 7bit";
82+
83+
if (m._disposition.size() > 0) {
84+
out << "Content-Disposition: " << m._disposition;
85+
86+
for (iter_t i = m._disposition_params.begin(); i != m._disposition_params.end(); ++i)
87+
{
88+
out << "; " << i->first << "=" << i->second;
89+
}
90+
out << CRLF;
91+
}
92+
out << CRLF;
93+
out << m._body;
94+
out << CRLF;
95+
out << "--" << m._boundary << CRLF << CRLF;
96+
}
97+
} // namespace mime
98+
99+
} // namespace network
100+
101+
} // namespace boost
102+
103+
#endif // __NETWORK_PROTOCOL_MIME__

libs/network/doc/architecture.qbk

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
[section:architecture Architecture]
1010
__cnl__ is built upon the __boost_asio__, for reasons of portability.
1111

12-
The architecture is driven by the requirement to separate requests from the transport mechanism. Additionally, its possible to utilise templates and static mechanisms to make decisions at compile-time, resulting in more efficient and stable client code.
12+
The architecture is driven by the requirement to separate requests from the transport mechanism. Additionally, it's possible to utilise templates and static mechanisms to make decisions at compile-time, resulting in more efficient and stable client code.
13+
14+
There are two main features of the architecture which use modern C++ techniques to allow extensibility without comprimising efficiency: tags and directives. These underly the design of the message.
1315

1416
[include message.qbk]
17+
[include tags_and_directives.qbk]
1518

1619

1720
[endsect]

libs/network/doc/http.qbk

+25-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
[section:http HTTP]
10-
__cnl__ provides direct support for HTTP. A motivating example will show how it's intended to work:
10+
The __cnl__ provides direct support for HTTP. A motivating example will show how it's intended to work:
1111

1212
#include <boost/network/protocol/http.hpp>
1313
#include <iostream>
@@ -24,8 +24,8 @@ __cnl__ provides direct support for HTTP. A motivating example will show how it
2424
headers_range<http::response>::type hdrs = headers(response);
2525
boost::range_iterator<headers_range<http::response>::type>::type
2626
it = boost::begin(hdrs), end = boost::end(hdrs);
27-
for (; it != end; ++it)
28-
std::cout << it->first << ": " << it->second << std::endl;
27+
for (; it != end; ++it) {
28+
std::cout << it->first << ": " << it->second << std::endl;
2929
}
3030

3131
// print response body
@@ -58,7 +58,7 @@ The [^response] encapsulates the data received from the server.
5858

5959
http::request request("http://www.boost.org/");
6060

61-
This line frames the request for the resource __boost_org__. This is slightly different to what one would normally expect from a request type, but there are good reasons for this. Its useful to consider a [^request] object as the base unit of data used to instruct the client what to do.
61+
This line frames the request for the resource __boost_org__. This is slightly different to what one would normally expect from a request type, but there are good reasons for this. Consider a [^request] object as the base unit of data used to instruct the client what to do.
6262

6363
http::client client;
6464

@@ -88,5 +88,25 @@ There are several advantages to this design:
8888
Once the request has been made, and the [^client] returns a [^response] object, the rest is simple. This example outputs all the response headers and body, in this case just the Boost homepage.
8989

9090

91-
[endsect]
91+
[heading Using [^http::client]]
92+
93+
The [^http::client] supports the following operations:
94+
95+
* [^http::client::head]
96+
* [^http::client::get]
97+
* [^http::client::post]
98+
* [^http::client::put]
99+
* [^http::client::delete_]
100+
101+
HTTP features can be enabled by using constructor arguments:
92102

103+
* [^http::client(http::client::cache_resolved)]
104+
* [^http::client(http::client::follow_redirect)]
105+
106+
[h5 [^http::client::cache_resolved]]
107+
This argument enables the caching of resolved endpoints and prevents the client from resolving IP addresses of previously resolved hostnames.
108+
109+
[h5 [^http::client::follow_redirect(s)]]
110+
[^http::client::follow_redirects] / [^http::client::follow_redirect] follow HTTP redirect(s) (300..307) by looking at the "Location" header provided by the response(s); headers present in the original request are preserved in the subsequent request(s).
111+
112+
[endsect]

libs/network/doc/message.qbk

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ The initial concept behind the __cnl__ is the message template. The message temp
1414
The message template interface is presented below:
1515

1616
namespace boost { namespace network {
17-
template <class Tag>
17+
template <
18+
class Tag
19+
>
1820
class basic_message {
1921
public:
20-
typedef implementation_defined headers_container_type;
21-
typedef implementation_defined string_type;
22+
typedef typename headers_container<Tag>::type headers_container_type;
23+
typedef typename string<Tag>::type string_type;
2224

2325
basic_message(const basic_message &);
2426
basic_message &operator = (const basic_message &);
@@ -33,8 +35,15 @@ The message template interface is presented below:
3335
typedef basic_message<tags::default_> message; // default message type
3436
}}
3537

38+
The __message__ template has a single argument (Tag). Tags are useful because:
39+
40+
# It's possible to specialize the message for different storage requirements;
41+
# It's possible to extend the message to support different network protocols.
42+
43+
The use of tags is discussed in further detail in the next section.
44+
3645
[h4 Concepts]
37-
=basic_message= supports the CopyConstructible, Assignable and Swappable concepts, as do all its derivates.
46+
__message__ supports the CopyConstructible, Assignable and Swappable concepts, as do all its derivatives.
3847

3948

4049
[endsect]

libs/network/doc/network.qbk

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
[def __boost_org__ [@http://www.boost.org/]]
2424
[def __boost_asio__ [@http://www.boost.org/libs/asio/index.html Boost.Asio]]
2525
[def __boost_system__ [@http://www.boost.org/libs/system/index.html Boost.System]]
26+
[def __boost_type_traits__ [@http://www.boost.org/libs/type_traits/index.html Boost.TypeTraits]]
2627
[def __pion__ [@http://www.pion.org/projects/pion-network-library the Pion Network Library]]
2728
[def __cnl__ C++ Network Library]
29+
[def __message__ [^basic_message]]
30+
[def __python__ [@http://www.python.org Python]]
2831

2932

3033

3134
[include motivation.qbk]
3235
[include using.qbk]
3336
[include architecture.qbk]
34-
[section:protocol Application Layer Protocols]
35-
[include http.qbk]
36-
[endsect]
37+
[include protocol.qbk]
3738
[include examples.qbk]
3839
[include reference.qbk]
3940
[include acknowledgements.qbk]

libs/network/doc/protocol.qbk

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[/
2+
(C) Copyright 2008 Glyn Matthews.
3+
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).
6+
]
7+
8+
[section:protocol Application Layer Protocols]
9+
[include http.qbk]
10+
11+
The message alone isn't any use. This section describes some of the common network protocols that are implemented in the __cnl__.
12+
[endsect]

0 commit comments

Comments
 (0)