// File: /workspace/github_cpp_makefile_star2_O2/mbourand[P]webserv/srcs/Methods/GetMethod.cpp
// Function: GetMethod::directory_listing(Request const&, ConfigContext const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Response GetMethod::directory_listing(const Request& request, const ConfigContext&, const std::string &realPath)
{
	const URL& url = request._url;
	std::list<std::string> list;
	try
	{
		list = list_directory(realPath);
	}
	catch (std::exception& e)
	{
		if (errno == ENOENT)
			return Logger::print("Directory not found", Response(404, url._path), ERROR, VERBOSE);
		if (errno == EACCES)
			return Logger::print("Permission denied", Response(403, url._path), ERROR, VERBOSE);
		if (errno != 0)
			return Logger::print("Unknown error while trying to open directory", Response(500, url._path), ERROR, NORMAL);
	}


	std::string body =
"<html>\r\n\
	<head>\r\n\
		<title>Index</title>\r\n\
		<style>\r\n\
			table{\r\n\
				width:100%;\r\n\
			}\r\n\
			table td{\r\n\
				white-space: nowrap;\r\n\
				border: none;\r\n\
				padding-right: 20;\r\n\
			}\r\n\
			table td:last-child{\r\n\
				width:100%;\r\n\
			}\r\n\
		</style>\r\n\
	</head>\r\n\
	<body>\r\n\
		<style>\r\n\
			table, td {\r\n\
				border: 1px solid black;\r\n\
			}\r\n\
\r\n\
			table {\r\n\
				width: 100%;\r\n\
				border-collapse: collapse;\r\n\
			}\r\n\
		</style>\r\n\
\r\n\
		<h1 align=\"center\">Index of " + url._path + "/</h1>\r\n\
		<hr>\r\n\
		<table style=\"border: none;\">\r\n\
			<tbody>\r\n\
				<tr>\r\n\
					<th>Name</th>\r\n\
					<th>Last Modified</th>\r\n\
					<th>Size</th>\r\n\
					<th></th>\r\n\
				</tr>";

	for (std::list<std::string>::iterator it = list.begin(); it != list.end(); it++)
	{
		std::string path = realPath + "/" + *it;
		body +=
"				<tr>\r\n\
					<td><a href=\"" + url._path + "/" + *it + "\">" + *it + "</a></td>\r\n\
					<td>" + (path[path.size() - 1] == '/' ? "-" : get_last_modified_format(path, "%d-%b-%Y %H:%M")) + "</td>\r\n\
					<td>" + (path[path.size() - 1] == '/' ? "-" : get_file_size(path)) + "</td>\r\n\
					<td></td>\r\n\
				</tr>\r\n";
	}
	body +=
"			</tbody>\r\n\
		</table>\r\n\
	</body>\r\n\
</html>";


	Response response(200, url._path.substr(0, url._path.size() - 1));
	std::ostringstream convert;

	convert << body.size();
	response.addHeader("Content-Length", convert.str());
	response.setBody(body);
	convert.str("");
	response.addDateHeader();
	response.addHeader("Server", "Webserv");
	response.addHeader("Content-Type", "text/html; charset=UTF-8");

	return response;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c8, %rsp            # imm = 0x5C8
movq %rdx, 0x18(%rsp)
movq %rdi, %r15
leaq 0x78(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x250(%rsp), %rdi
movq %r8, 0x70(%rsp)
movq %r8, %rdx
movq %r15, 0x10(%rsp)
callq 0x9984
leaq 0x78(%rsp), %rdi
leaq 0x250(%rsp), %rbx
movq %rbx, %rsi
callq 0xc9d8
movq %rbx, %rdi
callq 0x8f64
movq 0x18(%rsp), %rax
leaq 0xb8(%rax), %rdx
leaq 0x22599(%rip), %rsi     # 0x2c3af
leaq 0x250(%rsp), %rdi
movq %rdx, 0x68(%rsp)
callq 0xc154
leaq 0x22730(%rip), %rdx     # 0x2c55f
leaq 0xb0(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0xbe83
leaq 0x250(%rsp), %rdi
callq 0x79b8
leaq 0x20(%rsp), %r14
leaq 0x3c8(%rsp), %rbx
leaq 0x78(%rsp), %r12
movq %r12, 0x40(%rsp)
movq 0x40(%rsp), %rcx
movq (%rcx), %rcx
leaq 0x78(%rsp), %rax
movq %rcx, 0x40(%rsp)
cmpq %rax, %rcx
je 0xa138
movq %r14, %rdi
movq 0x70(%rsp), %rsi
leaq 0x22bb6(%rip), %rdx     # 0x2ca48
callq 0xc1e9
movq 0x40(%rsp), %rax
leaq 0x10(%rax), %r15
leaq 0x250(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xc1ca
movq %r14, %rdi
callq 0x79b8
movq %rbx, %rdi
leaq 0x22740(%rip), %rsi     # 0x2c605
movq 0x68(%rsp), %rdx
callq 0xc154
leaq 0x1d0(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x22b64(%rip), %rdx     # 0x2ca48
callq 0xbe83
leaq 0x1f0(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xc1ca
leaq 0x210(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x2270e(%rip), %rdx     # 0x2c622
callq 0xbe83
leaq 0x230(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xc1ca
leaq 0x130(%rsp), %rdi
movq %rbp, %rsi
leaq 0x226e4(%rip), %rdx     # 0x2c625
callq 0xbe83
movq 0x250(%rsp), %rax
movq 0x258(%rsp), %rcx
movb -0x1(%rax,%rcx), %al
movb %al, 0xc(%rsp)
cmpb $0x2f, %al
jne 0x9f80
leaq 0x110(%rsp), %r15
movq %r15, %rdi
leaq 0x22c38(%rip), %rsi     # 0x2cbac
leaq 0xf(%rsp), %rdx
callq 0x8032
jmp 0x9fb7
leaq 0x1b0(%rsp), %r12
movq %r12, %rdi
leaq 0x226a8(%rip), %rsi     # 0x2c63a
leaq 0xe(%rsp), %rdx
callq 0x8032
leaq 0x110(%rsp), %r15
movq %r15, %rdi
leaq 0x250(%rsp), %rdx
movq %r12, %rcx
callq 0x9b86
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
leaq 0x130(%rsp), %rsi
movq %r15, %rdx
callq 0xc221
leaq 0x250(%rsp), %rbp
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
leaq 0x2263d(%rip), %rdx     # 0x2c629
callq 0xbe83
movq 0x250(%rsp), %rax
movq 0x258(%rsp), %rcx
movb -0x1(%rax,%rcx), %r13b
cmpb $0x2f, %r13b
jne 0xa02a
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
leaq 0x22b8e(%rip), %rsi     # 0x2cbac
leaq 0xd(%rsp), %rdx
callq 0x8032
jmp 0xa03d
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rdx
callq 0x9d0c
leaq 0x48(%rsp), %r12
movq %r12, %rdi
leaq 0x90(%rsp), %rsi
movq %r15, %rdx
callq 0xc221
movq %r14, %r15
movq %r14, %rdi
movq %r12, %rsi
leaq 0x225e4(%rip), %rdx     # 0x2c649
callq 0xbe83
leaq 0xb0(%rsp), %rdi
movq %r15, %rsi
callq 0x7950
movq %r15, %rdi
callq 0x79b8
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0xf0(%rsp), %rdi
callq 0x79b8
cmpb $0x2f, %r13b
leaq 0x90(%rsp), %rdi
callq 0x79b8
leaq 0xd0(%rsp), %rdi
callq 0x79b8
leaq 0x110(%rsp), %rdi
callq 0x79b8
cmpb $0x2f, 0xc(%rsp)
movq 0x10(%rsp), %r15
je 0xa0dd
leaq 0x1b0(%rsp), %rdi
callq 0x79b8
leaq 0x130(%rsp), %rdi
callq 0x79b8
leaq 0x230(%rsp), %rdi
callq 0x79b8
leaq 0x210(%rsp), %rdi
callq 0x79b8
leaq 0x1f0(%rsp), %rdi
callq 0x79b8
leaq 0x1d0(%rsp), %rdi
callq 0x79b8
movq %rbx, %rdi
callq 0x79b8
leaq 0x250(%rsp), %rdi
callq 0x79b8
jmp 0x9e68
leaq 0x2252d(%rip), %rsi     # 0x2c66c
leaq 0xb0(%rsp), %rdi
callq 0x7920
movq 0x18(%rsp), %rax
movq 0xc0(%rax), %rcx
decq %rcx
leaq 0x250(%rsp), %rdi
movq 0x68(%rsp), %rsi
xorl %edx, %edx
callq 0x7540
leaq 0x250(%rsp), %rdx
movq %r15, %rdi
movl $0xc8, %esi
callq 0x2a080
leaq 0x250(%rsp), %rdi
callq 0x79b8
leaq 0x250(%rsp), %rdi
callq 0x7670
movq 0xb8(%rsp), %rsi
leaq 0x250(%rsp), %rdi
callq 0x7370
leaq 0x21f4a(%rip), %rsi     # 0x2c104
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0x8032
leaq 0x258(%rsp), %rbx
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x7780
leaq 0x20(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %r15, %rdi
callq 0x2a124
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x20(%rsp), %rdi
callq 0x79b8
leaq 0xb0(%rsp), %rsi
movq %r15, %rdi
callq 0x2a82e
leaq 0x22b31(%rip), %rsi     # 0x2cd4f
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x8032
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x7720
leaq 0x20(%rsp), %rdi
callq 0x79b8
movq %r15, %rdi
callq 0x2a2f0
leaq 0x21ff9(%rip), %rsi     # 0x2c24c
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0x8032
leaq 0x22924(%rip), %rsi     # 0x2cb90
leaq 0x48(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
callq 0x8032
leaq 0x20(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %r15, %rdi
callq 0x2a124
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x20(%rsp), %rdi
callq 0x79b8
leaq 0x21ea7(%rip), %rsi     # 0x2c152
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0x8032
leaq 0x223d3(%rip), %rsi     # 0x2c697
leaq 0x48(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
callq 0x8032
leaq 0x20(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %r15, %rdi
callq 0x2a124
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x20(%rsp), %rdi
callq 0x79b8
leaq 0x250(%rsp), %rdi
callq 0x7140
leaq 0xb0(%rsp), %rdi
callq 0x79b8
leaq 0x78(%rsp), %rdi
callq 0x8f64
movq %r15, %rax
addq $0x5c8, %rsp            # imm = 0x5C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa34e
jmp 0xa35d
jmp 0xa5d2
jmp 0xa34e
jmp 0xa35d
jmp 0xa5d2
jmp 0xa35d
jmp 0xa5d2
movq %rax, %r12
leaq 0x48(%rsp), %rdi
callq 0x79b8
jmp 0xa360
movq %rax, %r12
leaq 0x20(%rsp), %rdi
callq 0x79b8
jmp 0xa5d5
jmp 0xa5d2
movq %rax, %r12
jmp 0xa5e2
jmp 0xa684
jmp 0xa666
jmp 0xa666
movq %rax, %r12
leaq 0x250(%rsp), %rdi
jmp 0xa722
movq %rax, %r12
jmp 0xa727
movq %rax, %r12
cmpl $0x1, %edx
jne 0xa727
movq %r12, %rdi
callq 0x7220
callq 0x7060
movl (%rax), %eax
testl %eax, %eax
je 0xa44c
cmpl $0xd, %eax
je 0xa453
cmpl $0x2, %eax
jne 0xa4c4
leaq 0x21f7d(%rip), %rsi     # 0x2c35c
leaq 0x190(%rsp), %rdi
leaq 0x250(%rsp), %rdx
callq 0x8032
movq 0x18(%rsp), %rax
leaq 0xb8(%rax), %rdx
leaq 0x528(%rsp), %rdi
movl $0x194, %esi            # imm = 0x194
callq 0x2a080
leaq 0x190(%rsp), %rsi
leaq 0x528(%rsp), %rdx
pushq $0x3
popq %rcx
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
callq 0xc0bc
leaq 0x528(%rsp), %rdi
callq 0x29f10
leaq 0x190(%rsp), %rdi
jmp 0xa534
movb $0x1, %bl
jmp 0xa53b
leaq 0x21f16(%rip), %rsi     # 0x2c370
leaq 0x170(%rsp), %rdi
leaq 0x250(%rsp), %rdx
callq 0x8032
movq 0x18(%rsp), %rax
leaq 0xb8(%rax), %rdx
leaq 0x488(%rsp), %rdi
movl $0x193, %esi            # imm = 0x193
callq 0x2a080
leaq 0x170(%rsp), %rsi
leaq 0x488(%rsp), %rdx
pushq $0x3
popq %rcx
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
callq 0xc0bc
leaq 0x488(%rsp), %rdi
callq 0x29f10
leaq 0x170(%rsp), %rdi
jmp 0xa534
leaq 0x21eb7(%rip), %rsi     # 0x2c382
leaq 0x150(%rsp), %rdi
leaq 0x250(%rsp), %rdx
callq 0x8032
movq 0x18(%rsp), %rax
leaq 0xb8(%rax), %rdx
leaq 0x3e8(%rsp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x2a080
leaq 0x150(%rsp), %rsi
leaq 0x3e8(%rsp), %rdx
pushq $0x3
popq %rcx
pushq $0x1
popq %r8
movq 0x10(%rsp), %rdi
callq 0xc0bc
leaq 0x3e8(%rsp), %rdi
callq 0x29f10
leaq 0x150(%rsp), %rdi
callq 0x79b8
xorl %ebx, %ebx
callq 0x77f0
testb %bl, %bl
movq 0x10(%rsp), %r15
jne 0x9e03
jmp 0xa316
movq %rax, %r12
leaq 0x3e8(%rsp), %rdi
callq 0x29f10
jmp 0xa567
movq %rax, %r12
leaq 0x150(%rsp), %rdi
jmp 0xa5b1
jmp 0xa5b8
movq %rax, %r12
leaq 0x488(%rsp), %rdi
callq 0x29f10
jmp 0xa588
movq %rax, %r12
leaq 0x170(%rsp), %rdi
jmp 0xa5b1
jmp 0xa5b8
movq %rax, %r12
leaq 0x528(%rsp), %rdi
callq 0x29f10
jmp 0xa5a9
movq %rax, %r12
leaq 0x190(%rsp), %rdi
callq 0x79b8
jmp 0xa5bb
movq %rax, %r12
callq 0x77f0
jmp 0xa727
jmp 0xa39b
movq %rax, %rdi
callq 0x8f59
movq %rax, %r12
leaq 0x250(%rsp), %rdi
callq 0x7140
movq 0x10(%rsp), %rdi
callq 0x29f10
jmp 0xa71a
movq %rax, %r12
jmp 0xa6b2
jmp 0xa5fb
movq %rax, %r12
jmp 0xa6bf
movq %rax, %r12
jmp 0xa64e
movq %rax, %r12
jmp 0xa6f3
movq %rax, %r12
jmp 0xa641
movq %rax, %r12
jmp 0xa6e6
movq %rax, %r12
jmp 0xa637
movq %rax, %r12
jmp 0xa6d9
movq %rax, %r12
leaq 0x20(%rsp), %rdi
callq 0x79b8
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0xf0(%rsp), %rdi
callq 0x79b8
cmpb $0x2f, %r13b
leaq 0x90(%rsp), %rdi
callq 0x79b8
jmp 0xa68f
movq %rax, %r12
jmp 0xa6cc
movq %rax, %r12
jmp 0xa71a
movq %rax, %r12
leaq 0x20(%rsp), %rdi
jmp 0xa715
movq %rax, %r12
movb 0xc(%rsp), %bl
jmp 0xa6a0
movq %rax, %r12
jmp 0xa70d
movq %rax, %r12
movb 0xc(%rsp), %bl
leaq 0xd0(%rsp), %rdi
callq 0x79b8
leaq 0x110(%rsp), %rdi
callq 0x79b8
cmpb $0x2f, %bl
je 0xa6bf
leaq 0x1b0(%rsp), %rdi
callq 0x79b8
leaq 0x130(%rsp), %rdi
callq 0x79b8
leaq 0x230(%rsp), %rdi
callq 0x79b8
leaq 0x210(%rsp), %rdi
callq 0x79b8
leaq 0x1f0(%rsp), %rdi
callq 0x79b8
leaq 0x1d0(%rsp), %rdi
callq 0x79b8
leaq 0x3c8(%rsp), %rdi
callq 0x79b8
leaq 0x250(%rsp), %rdi
callq 0x79b8
leaq 0xb0(%rsp), %rdi
callq 0x79b8
leaq 0x78(%rsp), %rdi
callq 0x8f64
movq %r12, %rdi
callq 0x7860
movq %rax, %r12
jmp 0xa700