// File: /workspace/github_cpp_makefile_star2_O2/DMLfor[P]TinyWebServer/./ServerBase.hpp
// Function: WebServer::ServerBase<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<boost::asio::execution_context&>, boost::asio::execution::detail::blocking::never_t<0>, boost::asio::execution::prefer_only<boost::asio::execution::detail::blocking::possibly_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::tracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::untracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::fork_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::continuation_t<0>>>>>>::process_request_and_respond(std::shared_ptr<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<boost::asio::execution_context&>, boost::asio::execution::detail::blocking::never_t<0>, boost::asio::execution::prefer_only<boost::asio::execution::detail::blocking::possibly_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::tracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::untracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::fork_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::continuation_t<0>>>>>>) const
void WebServer::ServerBase<socket_type>::process_request_and_respond(std::shared_ptr<socket_type> socket) const
{
	// 为 async_read_untile() 创建新的读缓存
	// shared_ptr 用于传递临时对象给匿名函数
	// 会被推导为 std::shared_ptr<boost::asio::streambuf>
	auto read_buffer = std::make_shared<boost::asio::streambuf>();

	boost::asio::async_read_until(*socket, *read_buffer, "\r\n\r\n",
	[this, socket, read_buffer](const boost::system::error_code& ec, size_t bytes_transferred)
	{
		if(!ec)
		{
			// 注意: read_buffer->size() 的大小一定是和 bytes_transferred 相等, Boost的文档中指出:
			// 在 async_read_until 操作成功后, streambuf 在界定符之外可能包含额外的数据
			// 所以较好的做法是直接从流中提取并且解析当前 read_buffer 左边的报头，在拼接async_read_untile
			// 后面的内容
			size_t total = read_buffer->size();
			// 转换到 istream
			std::istream stream(read_buffer.get());
			// 被推导为 std::shared_ptr<Request> 类型
			auto request = std::make_shared<Request>();
			*request = parse_request(stream);

			size_t num_additional_bytes = total - bytes_transferred;

			// 接下来要将 stream 中的请求信息进行解析, 然后保存到 request 对象中
			// 如果满足，同样读取

			if(request->header.count("Content-Length")>0)
			{
				boost::asio::async_read(*socket, *read_buffer,
				boost::asio::transfer_exactly(stoull(request->header["Content-Length"]) - num_additional_bytes),
				[this, socket, read_buffer, request](const boost::system::error_code &ec, size_t bytes_transferred)
				{
					if(!ec)
					{
						// 将指针作为 istream 对象存储到 read_buffer 中
						request->content = std::shared_ptr<std::istream>(new std::istream(read_buffer.get()));
						respond(socket, request);
					}
				});
			}
			else
			{
				respond(socket, request);
			}
		}

	});
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r15
movq %rdi, %r12
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x15caf
movq (%r15), %rbx
movq (%r14), %r14
leaq 0x15ce3(%rip), %rsi     # 0x2b8f6
leaq 0x48(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0xa2f0
leaq 0x28(%rsp), %rdi
movq %r12, -0x8(%rdi)
movq %r15, %rsi
callq 0x28de6
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2895e
leaq 0x48(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15cca
leaq 0x20(%rsp), %rdi
callq 0x15cde
leaq 0x48(%rsp), %rdi
callq 0xa370
leaq 0x18(%rsp), %rdi
callq 0x18f4e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x15cde
leaq 0x48(%rsp), %rdi
callq 0xa370
jmp 0x15c9d
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x18f4e
movq %rbx, %rdi
callq 0xaac0