// File: trisolaris233[P]httpc/src/http_router.hpp
// Function: httpc::HttpRouter::Route(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, httpc::Request&, httpc::Response&)
void Route(
            const std::string& http_method,
            const std::string& route_directory, 
            Request& request, 
            Response& response
        ) {
            
            if (route_directory.find('.') != std::string::npos) {
                // std::cout << "Render local file" << std::endl;
                this->InvokeLocalFileRenderer(route_directory, response);
                // response.RenderFromStaticFile(this->server_->GetDocumentRoot() + route_directory);
                return;
            }

            bool end_with_slash = (route_directory.back() == '/');
            // try to search directory
            std::string key(http_method + route_directory);
            auto res_itr = directly_router_map_.find(key);
            if (res_itr != directly_router_map_.end()) {
                return res_itr->second(request, response);
            } 


            res_itr = directly_router_map_.find(key + ((end_with_slash) ? "?" : "/?"));            
            if (res_itr != directly_router_map_.end()) {
                res_itr->second(request, response);
            }

            // try fezzy match
            std::size_t num_of_slash{ 1 };
            for (
                auto pos_of_slash = route_directory.rfind('/');
                pos_of_slash != std::string::npos;
                pos_of_slash = route_directory.rfind('/', pos_of_slash - 1)
            ) {
                std::string tmp(http_method + route_directory.substr(0, pos_of_slash + 1));
                for (auto i = 0; i < num_of_slash; ++i) {
                    tmp.push_back('*');
                    if (i + 1 < num_of_slash) {
                        tmp.push_back('/');
                    }
                }
                
                auto wildcard_itr = wildcard_router_map_.find(tmp);
                if (wildcard_itr != wildcard_router_map_.end()) {
                    return wildcard_itr->second(request, response);
                }

                num_of_slash++;
                
                if (pos_of_slash == 0) 
                    break;
            }
            
            // try regex match
            auto method_regex_list = regex_router_map_[http_method];
            
            for (const auto& reg : method_regex_list) {
                if (std::regex_match(route_directory, reg.first)) {
                    return reg.second(request, response);
                }   
            }

            response.SetDefault(HTTPStatusCodeEnum::kNotFound);
        }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x18(%rbp), %rdi
movl $0x2e, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0xa8f0
cmpq $-0x1, %rax
je 0x30ddb
movq -0x150(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0xb1e0
jmp 0x312d4
movq -0x18(%rbp), %rdi
callq 0xa310
movb (%rax), %al
subb $0x2f, %al
sete -0x29(%rbp)
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x50(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x32dd0
movq -0x150(%rbp), %rdi
movq -0x160(%rbp), %rsi
callq 0x32e50
movq %rax, -0x158(%rbp)
jmp 0x30e20
movq -0x150(%rbp), %rdi
movq -0x158(%rbp), %rax
movq %rax, -0x58(%rbp)
callq 0x32eb0
movq %rax, -0x70(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x32e80
testb $0x1, %al
jne 0x30e4e
jmp 0x30e8a
leaq -0x58(%rbp), %rdi
callq 0x32ee0
movq %rax, %rdi
addq $0x20, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x32f00
jmp 0x30e6d
movl $0x1, -0x74(%rbp)
jmp 0x312cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x312dd
movq -0x150(%rbp), %rax
movq %rax, -0x168(%rbp)
movb -0x29(%rbp), %cl
leaq 0x28dec(%rip), %rax     # 0x59c8e
leaq 0x28de4(%rip), %rdx     # 0x59c8d
testb $0x1, %cl
cmovneq %rax, %rdx
leaq -0xa0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x32f50
jmp 0x30ec2
movq -0x168(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x32e50
movq %rax, -0x170(%rbp)
jmp 0x30ede
movq -0x170(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xa450
movq -0x150(%rbp), %rdi
callq 0x32eb0
movq %rax, -0xa8(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x32e80
testb $0x1, %al
jne 0x30f26
jmp 0x30f64
leaq -0x58(%rbp), %rdi
callq 0x32ee0
movq %rax, %rdi
addq $0x20, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x32f00
jmp 0x30f45
jmp 0x30f64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xa450
jmp 0x312dd
movq $0x1, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0xac50
movq %rax, -0xb8(%rbp)
cmpq $-0x1, -0xb8(%rbp)
je 0x3119d
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x18(%rbp), %rsi
movq -0xb8(%rbp), %rcx
incq %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0xf8(%rbp), %rdi
callq 0xa700
jmp 0x30fc4
movq -0x178(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
leaq -0xf8(%rbp), %rdx
callq 0x32fd0
jmp 0x30fe0
leaq -0xf8(%rbp), %rdi
callq 0xa450
movl $0x0, -0xfc(%rbp)
movslq -0xfc(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jae 0x31096
leaq -0xd8(%rbp), %rdi
movl $0x2a, %esi
callq 0xa5d0
jmp 0x3101d
movl -0xfc(%rbp), %eax
addl $0x1, %eax
cltq
cmpq -0xb0(%rbp), %rax
jae 0x31080
leaq -0xd8(%rbp), %rdi
movl $0x2f, %esi
callq 0xa5d0
jmp 0x31044
jmp 0x31080
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xa450
jmp 0x312dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0xa450
jmp 0x312dd
jmp 0x31082
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x30ff6
movq -0x150(%rbp), %rdi
addq $0x30, %rdi
leaq -0xd8(%rbp), %rsi
callq 0x32e50
movq %rax, -0x180(%rbp)
jmp 0x310b6
movq -0x150(%rbp), %rdi
movq -0x180(%rbp), %rax
movq %rax, -0x108(%rbp)
addq $0x30, %rdi
callq 0x32eb0
movq %rax, -0x110(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x32e80
testb $0x1, %al
jne 0x310f4
jmp 0x3111f
leaq -0x108(%rbp), %rdi
callq 0x32ee0
movq %rax, %rdi
addq $0x20, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x32f00
jmp 0x31116
movl $0x1, -0x74(%rbp)
jmp 0x3114b
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb8(%rbp)
jne 0x31144
movl $0x2, -0x74(%rbp)
jmp 0x3114b
movl $0x0, -0x74(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0xa450
movl -0x74(%rbp), %eax
movl %eax, -0x184(%rbp)
testl %eax, %eax
je 0x31176
jmp 0x31166
movl -0x184(%rbp), %eax
subl $0x2, %eax
je 0x3119d
jmp 0x312cb
jmp 0x31178
movq -0x18(%rbp), %rdi
movq -0xb8(%rbp), %rdx
subq $0x1, %rdx
movl $0x2f, %esi
callq 0xac50
movq %rax, -0xb8(%rbp)
jmp 0x30f8b
movq -0x150(%rbp), %rdi
addq $0x60, %rdi
movq -0x10(%rbp), %rsi
callq 0x33020
movq %rax, -0x190(%rbp)
jmp 0x311ba
movq -0x190(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0x33100
jmp 0x311cf
leaq -0x128(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
callq 0x331f0
movq %rax, -0x138(%rbp)
movq -0x130(%rbp), %rdi
callq 0x33220
movq %rax, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x33250
testb $0x1, %al
jne 0x3121f
jmp 0x312a8
leaq -0x138(%rbp), %rdi
callq 0x33290
movq %rax, -0x148(%rbp)
movq -0x18(%rbp), %rdi
movq -0x148(%rbp), %rsi
xorl %edx, %edx
callq 0x332b0
movb %al, -0x191(%rbp)
jmp 0x3124c
movb -0x191(%rbp), %al
testb $0x1, %al
jne 0x31258
jmp 0x31295
movq -0x148(%rbp), %rdi
addq $0x20, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x32f00
jmp 0x31272
movl $0x1, -0x74(%rbp)
jmp 0x312bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x27080
jmp 0x312dd
jmp 0x31297
leaq -0x138(%rbp), %rdi
callq 0x33300
jmp 0x31203
movq -0x28(%rbp), %rdi
movl $0x194, %esi            # imm = 0x194
callq 0xf680
jmp 0x312b8
movl $0x0, -0x74(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x27080
leaq -0x50(%rbp), %rdi
callq 0xa450
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0xa450
movq -0x60(%rbp), %rdi
callq 0xab10
nop