// 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>>>>>>::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>>>>>>, std::shared_ptr<WebServer::Request>) const
void WebServer::ServerBase<socket_type>::respond(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) const
{
	for(auto res_it : all_resources)
	{
		// 对请求路径和方法进行匹配查找, 并生成响应
		std::regex e(res_it->first);
		std::smatch sm_res;
		if(std::regex_match(request->path, sm_res, e))
		{
			if(res_it->second.count(request->method) > 0)
				request->path_match = move(sm_res);
			// 会被推导为 std::shared_ptr<boost::asio::streambuf>
			auto write_buffer = std::make_shared<boost::asio::streambuf>();
			std::ostream respondse(write_buffer.get());
			res_it->second[request->method](respondse, *request);

			// 在 lambda 中不糊 write_buffer 使其不会再 async_write 完成前被销毁
			boost::asio::async_write(*socket, *write_buffer,
					[this, socket, request, write_buffer](const boost::system::error_code &ec, size_t bytes_transferred)
					{
						// HTTP 持久连接(HTTP 1.1), 递归调用
						if(!ec && stof(request->http_version) > 1.05)
							process_request_and_respond(socket);
					});
			return ;
		}
	}
}

// Assembly:
subq $0x248, %rsp            # imm = 0x248
movq %rsi, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
movq %rdi, 0x240(%rsp)
movq %rsi, 0x238(%rsp)
movq %rdx, 0x230(%rsp)
movq 0x240(%rsp), %rax
movq %rax, 0x58(%rsp)
addq $0x108, %rax            # imm = 0x108
movq %rax, 0x228(%rsp)
movq 0x228(%rsp), %rdi
callq 0x6b730
movq %rax, 0x220(%rsp)
movq 0x228(%rsp), %rdi
callq 0x6b760
movq %rax, 0x218(%rsp)
leaq 0x220(%rsp), %rdi
leaq 0x218(%rsp), %rsi
callq 0x6b790
testb $0x1, %al
jne 0x3b22c
jmp 0x3b535
leaq 0x220(%rsp), %rdi
callq 0x6b7d0
movq (%rax), %rax
movq %rax, 0x210(%rsp)
leaq 0x210(%rsp), %rdi
callq 0x6b7e0
movq %rax, %rsi
leaq 0x1f0(%rsp), %rdi
movl $0x10, %edx
callq 0x6b800
leaq 0x1d0(%rsp), %rdi
callq 0x3be10
jmp 0x3b275
movq 0x50(%rsp), %rdi
callq 0x3afe0
movq %rax, %rdi
addq $0x20, %rdi
leaq 0x1d0(%rsp), %rsi
leaq 0x1f0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3c970
movb %al, 0x47(%rsp)
jmp 0x3b2a3
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x3b2b0
jmp 0x3b4d7
leaq 0x210(%rsp), %rdi
callq 0x6b7e0
movq 0x50(%rsp), %rdi
addq $0x20, %rax
movq %rax, 0x30(%rsp)
callq 0x3afe0
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x6b8c0
movq %rax, 0x38(%rsp)
jmp 0x3b2e4
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
jbe 0x3b344
movq 0x50(%rsp), %rdi
callq 0x3afe0
movq %rax, %rdi
addq $0xa8, %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x64bb0
jmp 0x3b344
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
jmp 0x3b526
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
jmp 0x3b519
leaq 0x1b0(%rsp), %rdi
callq 0x32120
jmp 0x3b353
leaq 0x1b0(%rsp), %rdi
callq 0x3a9d0
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xced0
jmp 0x3b372
leaq 0x210(%rsp), %rdi
callq 0x6b7e0
movq 0x50(%rsp), %rdi
addq $0x20, %rax
movq %rax, 0x20(%rsp)
callq 0x3afe0
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x6b8f0
movq %rax, 0x28(%rsp)
jmp 0x3b3a6
movq 0x50(%rsp), %rdi
callq 0x3aea0
movq 0x28(%rsp), %rdi
movq %rax, %rdx
leaq 0xa0(%rsp), %rsi
callq 0x6b920
jmp 0x3b3c7
movq 0x48(%rsp), %rdi
callq 0x22180
movq %rax, 0x10(%rsp)
leaq 0x1b0(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x321f0
movq 0x48(%rsp), %rsi
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x221c0
movq 0x50(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x3b130
movq 0x8(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x32220
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x6b970
jmp 0x3b443
leaq 0x68(%rsp), %rdi
callq 0x6b9c0
movl $0x1, 0x64(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xcc20
leaq 0x1b0(%rsp), %rdi
callq 0x32280
jmp 0x3b4df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
jmp 0x3b4c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
jmp 0x3b4bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x6b9c0
leaq 0xa0(%rsp), %rdi
callq 0xcc20
leaq 0x1b0(%rsp), %rdi
callq 0x32280
jmp 0x3b519
movl $0x0, 0x64(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x3caa0
leaq 0x1f0(%rsp), %rdi
callq 0x3cab0
movl 0x64(%rsp), %eax
testl %eax, %eax
je 0x3b505
jmp 0x3b503
jmp 0x3b535
jmp 0x3b507
leaq 0x220(%rsp), %rdi
callq 0x6ba00
jmp 0x3b20e
leaq 0x1d0(%rsp), %rdi
callq 0x3caa0
leaq 0x1f0(%rsp), %rdi
callq 0x3cab0
jmp 0x3b53d
addq $0x248, %rsp            # imm = 0x248
retq
movq 0x1c8(%rsp), %rdi
callq 0xc900
nopw (%rax,%rax)