@@ -86,7 +86,7 @@ void connection::start()
86
86
}
87
87
else {
88
88
// start reading data
89
- read_more_plain ();
89
+ read_more ();
90
90
}
91
91
m_lastresponse=mytime (NULL );
92
92
}
@@ -110,16 +110,17 @@ void connection::handle_handshake(const boost::system::error_code& error)
110
110
if (!error)
111
111
{
112
112
// handshake completed, start reading
113
- read_more_secure ();
113
+ read_more ();
114
114
}
115
115
else
116
116
{
117
117
connection_manager_.stop (shared_from_this ());
118
118
}
119
119
}
120
120
}
121
+ #endif
121
122
122
- void connection::read_more_secure ()
123
+ void connection::read_more ()
123
124
{
124
125
// read chunks of max 4 KB
125
126
boost::asio::streambuf::mutable_buffers_type buf = _buf.prepare (4096 );
@@ -128,107 +129,25 @@ void connection::read_more_secure()
128
129
timer_.expires_from_now (boost::posix_time::seconds (timeout_));
129
130
timer_.async_wait (boost::bind (&connection::handle_timeout, shared_from_this (), boost::asio::placeholders::error));
130
131
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
190
140
}
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));
210
147
}
211
- m_lastresponse=mytime (NULL );
212
148
}
213
- #endif
214
149
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)
232
151
{
233
152
// data read, no need for timeouts (RK, note: race condition)
234
153
timer_.cancel ();
@@ -264,21 +183,39 @@ void connection::handle_read_plain(const boost::system::error_code& error, std::
264
183
request_.host = request_.host .substr (7 );
265
184
}
266
185
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
+ }
270
198
}
271
199
else if (!result)
272
200
{
273
201
keepalive_ = false ;
274
202
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
+ }
278
215
}
279
216
else
280
217
{
281
- read_more_plain ();
218
+ read_more ();
282
219
}
283
220
}
284
221
else if (error != boost::asio::error::operation_aborted)
@@ -287,12 +224,12 @@ void connection::handle_read_plain(const boost::system::error_code& error, std::
287
224
}
288
225
}
289
226
290
- void connection::handle_write_plain (const boost::system::error_code& error)
227
+ void connection::handle_write (const boost::system::error_code& error)
291
228
{
292
229
if (!error) {
293
230
if (keepalive_) {
294
231
// if a keep-alive connection is requested, we read the next request
295
- read_more_plain ();
232
+ read_more ();
296
233
}
297
234
else {
298
235
// Initiate graceful connection closure.
0 commit comments