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

Function do_session

example/http/server/sync/http_server_sync.cpp:211–255  ·  view source on GitHub ↗

Handles an HTTP server connection

Source from the content-addressed store, hash-verified

209
210// Handles an HTTP server connection
211void
212do_session(
213 tcp::socket& socket,
214 std::shared_ptr<std::string const> const& doc_root)
215{
216 beast::error_code ec;
217
218 // This buffer is required to persist across reads
219 beast::flat_buffer buffer;
220
221 for(;;)
222 {
223 // Read a request
224 http::request<http::string_body> req;
225 http::read(socket, buffer, req, ec);
226 if(ec == http::error::end_of_stream)
227 break;
228 if(ec)
229 return fail(ec, "read");
230
231 // Handle request
232 http::message_generator msg =
233 handle_request(*doc_root, std::move(req));
234
235 // Determine if we should close the connection
236 bool keep_alive = msg.keep_alive();
237
238 // Send the response
239 beast::write(socket, std::move(msg), ec);
240
241 if(ec)
242 return fail(ec, "write");
243 if(! keep_alive)
244 {
245 // This means we should close the connection, usually because
246 // the response indicated the "Connection: close" semantic.
247 break;
248 }
249 }
250
251 // Send a TCP shutdown
252 socket.shutdown(tcp::socket::shutdown_send, ec);
253
254 // At this point the connection is closed gracefully
255}
256
257//------------------------------------------------------------------------------
258

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected