Handles an HTTP server connection
| 212 | |
| 213 | // Handles an HTTP server connection |
| 214 | void |
| 215 | do_session( |
| 216 | tcp::socket& socket, |
| 217 | ssl::context& ctx, |
| 218 | std::shared_ptr<std::string const> const& doc_root) |
| 219 | { |
| 220 | beast::error_code ec; |
| 221 | |
| 222 | // Construct the stream around the socket |
| 223 | ssl::stream<tcp::socket&> stream{socket, ctx}; |
| 224 | |
| 225 | // Perform the SSL handshake |
| 226 | stream.handshake(ssl::stream_base::server, ec); |
| 227 | if(ec) |
| 228 | return fail(ec, "handshake"); |
| 229 | |
| 230 | // This buffer is required to persist across reads |
| 231 | beast::flat_buffer buffer; |
| 232 | |
| 233 | for(;;) |
| 234 | { |
| 235 | // Read a request |
| 236 | http::request<http::string_body> req; |
| 237 | http::read(stream, buffer, req, ec); |
| 238 | if(ec == http::error::end_of_stream) |
| 239 | break; |
| 240 | if(ec) |
| 241 | return fail(ec, "read"); |
| 242 | |
| 243 | // Handle request |
| 244 | http::message_generator msg = |
| 245 | handle_request(*doc_root, std::move(req)); |
| 246 | |
| 247 | // Determine if we should close the connection |
| 248 | bool keep_alive = msg.keep_alive(); |
| 249 | |
| 250 | // Send the response |
| 251 | beast::write(stream, std::move(msg), ec); |
| 252 | |
| 253 | if(ec) |
| 254 | return fail(ec, "write"); |
| 255 | |
| 256 | if(! keep_alive) |
| 257 | { |
| 258 | // This means we should close the connection, usually because |
| 259 | // the response indicated the "Connection: close" semantic. |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Perform the SSL shutdown |
| 265 | stream.shutdown(ec); |
| 266 | if(ec) |
| 267 | return fail(ec, "shutdown"); |
| 268 | |
| 269 | // At this point the connection is closed gracefully |
| 270 | } |
| 271 |
nothing calls this directly
no test coverage detected