MCPcopy Create free account
hub / github.com/boostorg/beast / do_session

Function do_session

example/http/server/sync-ssl/http_server_sync_ssl.cpp:214–270  ·  view source on GitHub ↗

Handles an HTTP server connection

Source from the content-addressed store, hash-verified

212
213// Handles an HTTP server connection
214void
215do_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

Callers

nothing calls this directly

Calls 8

moveClass · 0.85
shutdownMethod · 0.80
failFunction · 0.70
handle_requestFunction · 0.70
readFunction · 0.50
writeFunction · 0.50
handshakeMethod · 0.45
keep_aliveMethod · 0.45

Tested by

no test coverage detected