Skip to content

Commit d7d01be

Browse files
committed
Merge pull request #284 from domoticz/ConnectionCleaned
Cleaned up connection.cpp.
2 parents 81cc5d8 + 842caeb commit d7d01be

File tree

3 files changed

+50
-120
lines changed

3 files changed

+50
-120
lines changed

msbuild/domoticz.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<PrecompiledHeader>Use</PrecompiledHeader>
5656
<WarningLevel>Level3</WarningLevel>
5757
<Optimization>Disabled</Optimization>
58-
<PreprocessorDefinitions>WIN32;_DEBUG;PTW32_STATIC_LIB;WITH_OPENZWAVE;OPENZWAVE_USEDLL;NS_ENABLE_SSL;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
58+
<PreprocessorDefinitions>WIN32;_DEBUG;PTW32_STATIC_LIB;WITH_OPENZWAVE;OPENZWAVE_USEDLL;_CONSOLE;NS_ENABLE_SSL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
5959
<AdditionalIncludeDirectories>./Windows Libraries/Boost/boost_1_59_0;./libusb;..\hardware\openzwave;./Windows Libraries/openssl;./Windows Libraries/Curl;./Windows Libraries/pthread;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
6060
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
6161
</ClCompile>

webserver/connection.cpp

Lines changed: 46 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void connection::start()
8686
}
8787
else {
8888
// start reading data
89-
read_more_plain();
89+
read_more();
9090
}
9191
m_lastresponse=mytime(NULL);
9292
}
@@ -110,16 +110,17 @@ void connection::handle_handshake(const boost::system::error_code& error)
110110
if (!error)
111111
{
112112
// handshake completed, start reading
113-
read_more_secure();
113+
read_more();
114114
}
115115
else
116116
{
117117
connection_manager_.stop(shared_from_this());
118118
}
119119
}
120120
}
121+
#endif
121122

122-
void connection::read_more_secure()
123+
void connection::read_more()
123124
{
124125
// read chunks of max 4 KB
125126
boost::asio::streambuf::mutable_buffers_type buf = _buf.prepare(4096);
@@ -128,107 +129,25 @@ void connection::read_more_secure()
128129
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
129130
timer_.async_wait(boost::bind(&connection::handle_timeout, shared_from_this(), boost::asio::placeholders::error));
130131

131-
// Perform read
132-
sslsocket_->async_read_some(buf,
133-
boost::bind(&connection::handle_read_secure, shared_from_this(),
134-
boost::asio::placeholders::error,
135-
boost::asio::placeholders::bytes_transferred));
136-
}
137-
138-
void connection::handle_read_secure(const boost::system::error_code& error, std::size_t bytes_transferred)
139-
{
140-
// data read, no need for timeouts (RK, note: race condition)
141-
timer_.cancel();
142-
if (!error && bytes_transferred > 0)
143-
{
144-
// ensure written bytes in the buffer
145-
_buf.commit(bytes_transferred);
146-
m_lastresponse=mytime(NULL);
147-
boost::tribool result;
148-
/// The incoming request.
149-
request request_;
150-
const char *begin = boost::asio::buffer_cast<const char*>(_buf.data());
151-
try
152-
{
153-
request_parser_.reset();
154-
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
155-
request_, begin, begin + _buf.size());
156-
}
157-
catch (...)
158-
{
159-
_log.Log(LOG_ERROR, "Exception parsing http request.");
160-
}
161-
162-
if (result) {
163-
size_t sizeread = begin - boost::asio::buffer_cast<const char*>(_buf.data());
164-
_buf.consume(sizeread);
165-
reply_.reset();
166-
const char *pConnection = request_.get_req_header(&request_, "Connection");
167-
keepalive_ = pConnection != NULL && boost::iequals(pConnection, "Keep-Alive");
168-
request_.keep_alive = keepalive_;
169-
request_.host = host_endpoint_;
170-
if (request_.host.substr(0, 7) == "::ffff:") {
171-
request_.host = request_.host.substr(7);
172-
}
173-
request_handler_.handle_request(request_, reply_);
174-
boost::asio::async_write(*sslsocket_, reply_.to_buffers(request_.method),
175-
boost::bind(&connection::handle_write_secure, shared_from_this(),
176-
boost::asio::placeholders::error));
177-
}
178-
else if (!result)
179-
{
180-
keepalive_ = false;
181-
reply_ = reply::stock_reply(reply::bad_request);
182-
boost::asio::async_write(*sslsocket_, reply_.to_buffers(request_.method),
183-
boost::bind(&connection::handle_write_secure, shared_from_this(),
184-
boost::asio::placeholders::error));
185-
}
186-
else
187-
{
188-
read_more_secure();
189-
}
132+
if (secure_) {
133+
#ifdef NS_ENABLE_SSL
134+
// Perform secure read
135+
sslsocket_->async_read_some(buf,
136+
boost::bind(&connection::handle_read, shared_from_this(),
137+
boost::asio::placeholders::error,
138+
boost::asio::placeholders::bytes_transferred));
139+
#endif
190140
}
191-
else if (error != boost::asio::error::operation_aborted)
192-
{
193-
connection_manager_.stop(shared_from_this());
194-
}
195-
}
196-
197-
void connection::handle_write_secure(const boost::system::error_code& error)
198-
{
199-
if (!error) {
200-
if (keepalive_) {
201-
// if a keep-alive connection is requested, we read the next request
202-
read_more_secure();
203-
}
204-
else {
205-
// Initiate graceful connection closure.
206-
boost::system::error_code ignored_ec;
207-
socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
208-
connection_manager_.stop(shared_from_this());
209-
}
141+
else {
142+
// Perform plain read
143+
socket_->async_read_some(buf,
144+
boost::bind(&connection::handle_read, shared_from_this(),
145+
boost::asio::placeholders::error,
146+
boost::asio::placeholders::bytes_transferred));
210147
}
211-
m_lastresponse=mytime(NULL);
212148
}
213-
#endif
214149

215-
void connection::read_more_plain()
216-
{
217-
// read chunks of max 4 KB
218-
boost::asio::streambuf::mutable_buffers_type buf = _buf.prepare(4096);
219-
220-
// set timeout timer
221-
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
222-
timer_.async_wait(boost::bind(&connection::handle_timeout, shared_from_this(), boost::asio::placeholders::error));
223-
224-
// Perform read
225-
socket_->async_read_some(buf,
226-
boost::bind(&connection::handle_read_plain, shared_from_this(),
227-
boost::asio::placeholders::error,
228-
boost::asio::placeholders::bytes_transferred));
229-
}
230-
231-
void connection::handle_read_plain(const boost::system::error_code& error, std::size_t bytes_transferred)
150+
void connection::handle_read(const boost::system::error_code& error, std::size_t bytes_transferred)
232151
{
233152
// data read, no need for timeouts (RK, note: race condition)
234153
timer_.cancel();
@@ -264,21 +183,39 @@ void connection::handle_read_plain(const boost::system::error_code& error, std::
264183
request_.host = request_.host.substr(7);
265184
}
266185
request_handler_.handle_request(request_, reply_);
267-
boost::asio::async_write(*socket_, reply_.to_buffers(request_.method),
268-
boost::bind(&connection::handle_write_plain, shared_from_this(),
269-
boost::asio::placeholders::error));
186+
if (secure_) {
187+
#ifdef NS_ENABLE_SSL
188+
boost::asio::async_write(*sslsocket_, reply_.to_buffers(request_.method),
189+
boost::bind(&connection::handle_write, shared_from_this(),
190+
boost::asio::placeholders::error));
191+
#endif
192+
}
193+
else {
194+
boost::asio::async_write(*socket_, reply_.to_buffers(request_.method),
195+
boost::bind(&connection::handle_write, shared_from_this(),
196+
boost::asio::placeholders::error));
197+
}
270198
}
271199
else if (!result)
272200
{
273201
keepalive_ = false;
274202
reply_ = reply::stock_reply(reply::bad_request);
275-
boost::asio::async_write(*socket_, reply_.to_buffers(request_.method),
276-
boost::bind(&connection::handle_write_plain, shared_from_this(),
277-
boost::asio::placeholders::error));
203+
if (secure_) {
204+
#ifdef NS_ENABLE_SSL
205+
boost::asio::async_write(*sslsocket_, reply_.to_buffers(request_.method),
206+
boost::bind(&connection::handle_write, shared_from_this(),
207+
boost::asio::placeholders::error));
208+
#endif
209+
}
210+
else {
211+
boost::asio::async_write(*socket_, reply_.to_buffers(request_.method),
212+
boost::bind(&connection::handle_write, shared_from_this(),
213+
boost::asio::placeholders::error));
214+
}
278215
}
279216
else
280217
{
281-
read_more_plain();
218+
read_more();
282219
}
283220
}
284221
else if (error != boost::asio::error::operation_aborted)
@@ -287,12 +224,12 @@ void connection::handle_read_plain(const boost::system::error_code& error, std::
287224
}
288225
}
289226

290-
void connection::handle_write_plain(const boost::system::error_code& error)
227+
void connection::handle_write(const boost::system::error_code& error)
291228
{
292229
if (!error) {
293230
if (keepalive_) {
294231
// if a keep-alive connection is requested, we read the next request
295-
read_more_plain();
232+
read_more();
296233
}
297234
else {
298235
// Initiate graceful connection closure.

webserver/connection.hpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,11 @@ class connection
6767

6868
private:
6969
/// Handle completion of a read operation.
70-
void handle_read_plain(const boost::system::error_code& e, std::size_t bytes_transferred);
71-
void read_more_plain();
70+
void handle_read(const boost::system::error_code& e, std::size_t bytes_transferred);
71+
void read_more();
7272

7373
/// Handle completion of a write operation.
74-
void handle_write_plain(const boost::system::error_code& e);
75-
76-
#ifdef NS_ENABLE_SSL
77-
/// ssl handle functions
78-
void handle_read_secure(const boost::system::error_code& e, std::size_t bytes_transferred);
79-
void read_more_secure();
80-
void handle_write_secure(const boost::system::error_code& e);
81-
#endif
74+
void handle_write(const boost::system::error_code& e);
8275

8376
/// Socket for the (PLAIN) connection.
8477
boost::asio::ip::tcp::socket *socket_;

0 commit comments

Comments
 (0)