// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdi, %r14
leaq 0x10(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x8(%rsp), %r15
leaq 0x18(%rsp), %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x18f66
movq (%rbx), %rdi
movq (%r15), %rax
leaq 0x50(%rsp), %r15
movq %r15, -0x10(%r15)
movl $0xa0d0a0d, (%r15)      # imm = 0xA0D0A0D
movq $0x4, -0x8(%r15)
movb $0x0, 0x4(%r15)
movq %r14, (%r12)
movq %rdi, 0x8(%r12)
movq 0x8(%rbx), %rcx
movq %rcx, 0x10(%r12)
testq %rcx, %rcx
je 0x18e7e
movq 0x31140(%rip), %rdx     # 0x49fb0
cmpb $0x0, (%rdx)
je 0x18e7a
incl 0x8(%rcx)
jmp 0x18e7e
lock
incl 0x8(%rcx)
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %rdx, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
testq %rcx, %rcx
je 0x18eac
movq 0x31112(%rip), %rdx     # 0x49fb0
cmpb $0x0, (%rdx)
je 0x18ea8
incl 0x8(%rcx)
jmp 0x18eac
lock
incl 0x8(%rcx)
leaq 0x60(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x40(%rsp), %rdx
leaq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x19759
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x18ed5
callq 0x1d2a8
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x18ee4
callq 0x1d2a8
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x18ef3
callq 0xa5d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x18f02
callq 0x1d2a8
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x18f42
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x18f2a
callq 0xa5d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x18f39
callq 0x1d2a8
movq %rbx, %rdi
callq 0xaa80
nop