forked from boostorg/beast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_3_decorator.cpp
More file actions
145 lines (110 loc) · 3.05 KB
/
Copy pathwebsocket_3_decorator.cpp
File metadata and controls
145 lines (110 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
#include <boost/beast/_experimental/unit_test/suite.hpp>
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable: 4459) // declaration hides global declaration
#endif
#include <boost/beast.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
namespace {
#include "websocket_common.ipp"
//[code_websocket_3_1
void set_user_agent(request_type& req)
{
// Set the User-Agent on the request
req.set(http::field::user_agent, "My User Agent");
}
//]
void
snippets()
{
{
//[code_websocket_3_2
stream<tcp_stream> ws(ioc);
// The function `set_user_agent` will be invoked with
// every upgrade request before it is sent by the stream.
ws.set_option(stream_base::decorator(&set_user_agent));
//]
}
stream<tcp_stream> ws(ioc);
{
//[code_websocket_3_3
struct set_server
{
void operator()(response_type& res)
{
// Set the Server field on the response
res.set(http::field::user_agent, "My Server");
}
};
ws.set_option(stream_base::decorator(set_server{}));
//]
}
{
//[code_websocket_3_4
ws.set_option(stream_base::decorator(
[](response_type& res)
{
// Set the Server field on the response
res.set(http::field::user_agent, "My Server");
}));
//]
}
{
//[code_websocket_3_5
struct set_message_fields
{
void operator()(request_type& req)
{
// Set the User-Agent on the request
req.set(http::field::user_agent, "My User Agent");
}
void operator()(response_type& res)
{
// Set the Server field on the response
res.set(http::field::user_agent, "My Server");
}
};
ws.set_option(stream_base::decorator(set_message_fields{}));
//]
}
{
//[code_websocket_3_6
struct set_auth
{
std::unique_ptr<std::string> key;
void operator()(request_type& req)
{
// Set the authorization field
req.set(http::field::authorization, *key);
}
};
// The stream takes ownership of the decorator object
ws.set_option(stream_base::decorator(
set_auth{boost::make_unique<std::string>("Basic QWxhZGRpbjpPcGVuU2VzYW1l")}));
//]
}
}
struct websocket_3_test
: public boost::beast::unit_test::suite
{
void
run() override
{
BEAST_EXPECT(&snippets);
}
};
BEAST_DEFINE_TESTSUITE(beast,doc,websocket_3);
} // (anon)
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif