// File: 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:
subq $0xb8, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x98(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x32120
movq 0x10(%rsp), %rdi
callq 0x22180
movq 0x18(%rsp), %rdi
movq %rax, 0x20(%rsp)
callq 0x321f0
movq %rax, 0x28(%rsp)
leaq 0x77(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xcca0
movq 0x30(%rsp), %rdx
leaq 0x4798d(%rip), %rsi     # 0x799d1
leaq 0x78(%rsp), %rdi
callq 0xcae0
jmp 0x32050
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x221c0
leaq 0x50(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x32220
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
leaq 0x78(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x32190
jmp 0x32096
leaq 0x38(%rsp), %rdi
callq 0x32250
leaq 0x78(%rsp), %rdi
callq 0xc9e0
leaq 0x77(%rsp), %rdi
callq 0xcfa0
leaq 0x98(%rsp), %rdi
callq 0x32280
addq $0xb8, %rsp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
jmp 0x320fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x32250
leaq 0x78(%rsp), %rdi
callq 0xc9e0
leaq 0x77(%rsp), %rdi
callq 0xcfa0
leaq 0x98(%rsp), %rdi
callq 0x32280
movq 0x68(%rsp), %rdi
callq 0xc900
nopl (%rax)