// File: mbourand[P]webserv/srcs/Request.cpp
// Function: Request::dechunk(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
std::string		Request::dechunk(const std::string& str)
{
	if (!_chunked)
		return (str);
	size_t		start = 0;
	size_t		end = 0;
	size_t		len;
	size_t		total_len = 0;
	std::string	output;

	while ((end = str.find("\r\n", start)) != std::string::npos)
	{
		len = ft::hexToInt(str.substr(start, (end - start)));
		end += 2;
		if (!len)
		{
			for (Request::HeadersVector::iterator it = _headers.begin(); it != _headers.end(); it++)
				if ((*it)->getType() == TransferEncodingHeader().getType())
				{
					delete (*it);
					_headers.erase(it);
					Header* header = g_webserv.headers.createByType_ignore_case(ContentLengthHeader().getType());
					std::stringstream	ss;
					std::string			converted;
					ss << total_len;
					ss >> converted;
					header->setValue(converted);
					_headers.push_back(header);
					return (output);
				}
			return (output);
		}
		else if (str.find("\r\n", (end + len)) != (end + len))
		{
			_error_code = 400;
			return ("");
		}
		total_len += len;
		if (total_len > _max_body_size)
		{
			_error_code = 413;
			return ("");
		}
		output += str.substr(end, len);
		start = end + len + 2;
	}
	_error_code = 400;
	return ("");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3a0, %rsp            # imm = 0x3A0
movq %rdi, -0x360(%rbp)
movq %rdi, %rax
movq %rax, -0x358(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x350(%rbp)
testb $0x1, 0x193(%rax)
jne 0x60221
movq -0x360(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x92c0
jmp 0x60871
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x98c0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0xda5f(%rip), %rsi      # 0x6dcb0
callq 0x9c60
movq %rax, -0x28(%rbp)
cmpq $-0x1, %rax
je 0x607e6
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
subq %rdx, %rcx
leaq -0x78(%rbp), %rdi
callq 0x96b0
jmp 0x6027e
leaq -0x78(%rbp), %rdi
callq 0x2f860
movl %eax, -0x364(%rbp)
jmp 0x6028f
movl -0x364(%rbp), %eax
cltq
movq %rax, -0x30(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x9c98
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x6063c
movq -0x350(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
callq 0x614d0
movq %rax, -0x90(%rbp)
movq -0x350(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
callq 0x61540
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x61500
testb $0x1, %al
jne 0x6030b
jmp 0x6061d
leaq -0x90(%rbp), %rdi
callq 0x61570
movq (%rax), %rsi
movq (%rsi), %rax
movq 0x10(%rax), %rax
leaq -0xb8(%rbp), %rdi
callq *%rax
jmp 0x6032c
leaq -0x100(%rbp), %rdi
callq 0xe4c0
jmp 0x6033a
leaq -0xd8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xe5b0
jmp 0x6034f
leaq -0xb8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x144e0
movb %al, -0x365(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x9c98
leaq -0x100(%rbp), %rdi
callq 0xe540
leaq -0xb8(%rbp), %rdi
callq 0x9c98
movb -0x365(%rbp), %al
testb $0x1, %al
jne 0x6039b
jmp 0x60601
leaq -0x90(%rbp), %rdi
callq 0x61570
movq (%rax), %rax
movq %rax, -0x370(%rbp)
cmpq $0x0, %rax
je 0x603c4
movq -0x370(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x350(%rbp), %rax
addq $0x140, %rax            # imm = 0x140
movq %rax, -0x380(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x61680
movq -0x380(%rbp), %rdi
movq -0x108(%rbp), %rsi
callq 0x61610
movq %rax, -0x378(%rbp)
jmp 0x60407
movq -0x378(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0x160(%rbp), %rdi
callq 0xb3f0
jmp 0x60423
leaq -0x138(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0xb4e0
jmp 0x60438
leaq 0x2f2e1(%rip), %rdi     # 0x8f720
addq $0x88, %rdi
leaq -0x138(%rbp), %rsi
callq 0x616c0
movq %rax, -0x388(%rbp)
jmp 0x6045b
leaq -0x138(%rbp), %rdi
callq 0x9c98
leaq -0x160(%rbp), %rdi
callq 0xb470
movq -0x388(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x9490
jmp 0x6048f
leaq -0x308(%rbp), %rdi
callq 0x98c0
leaq -0x2d8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x9190
jmp 0x604ad
leaq -0x2e8(%rbp), %rdi
leaq -0x308(%rbp), %rsi
callq 0x9820
jmp 0x604c2
movq -0x118(%rbp), %rdi
leaq -0x308(%rbp), %rsi
callq 0xbf10
jmp 0x604d7
movq -0x350(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
leaq -0x118(%rbp), %rsi
callq 0x61800
jmp 0x604f3
movq -0x360(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x9410
movl $0x1, -0x30c(%rbp)
leaq -0x308(%rbp), %rdi
callq 0x9c98
leaq -0x2e8(%rbp), %rdi
callq 0x94e0
jmp 0x6085b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x60866
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x9c98
jmp 0x60866
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x60587
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x100(%rbp), %rdi
callq 0xe540
leaq -0xb8(%rbp), %rdi
callq 0x9c98
jmp 0x60866
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x605c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x9c98
leaq -0x160(%rbp), %rdi
callq 0xb470
jmp 0x60866
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x308(%rbp), %rdi
callq 0x9c98
leaq -0x2e8(%rbp), %rdi
callq 0x94e0
jmp 0x60866
jmp 0x60603
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x61590
movq %rax, -0x318(%rbp)
jmp 0x602d5
movq -0x360(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x9410
movl $0x1, -0x30c(%rbp)
jmp 0x6085b
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rdx
addq -0x30(%rbp), %rdx
leaq 0xd661(%rip), %rsi      # 0x6dcb0
callq 0x9c60
movq -0x28(%rbp), %rcx
addq -0x30(%rbp), %rcx
cmpq %rcx, %rax
je 0x606dc
movq -0x350(%rbp), %rax
movl $0x190, 0x188(%rax)     # imm = 0x190
leaq -0x319(%rbp), %rdi
movq %rdi, -0x390(%rbp)
callq 0x9b10
movq -0x360(%rbp), %rdi
movq -0x390(%rbp), %rdx
leaq 0xa6d6(%rip), %rsi      # 0x6ad70
callq 0xa780
jmp 0x606a1
leaq -0x319(%rbp), %rdi
callq 0x9700
movl $0x1, -0x30c(%rbp)
jmp 0x6085b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x319(%rbp), %rdi
callq 0x9700
jmp 0x60866
jmp 0x606de
movq -0x350(%rbp), %rcx
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq 0x180(%rcx), %rax
jbe 0x60779
movq -0x350(%rbp), %rax
movl $0x19d, 0x188(%rax)     # imm = 0x19D
leaq -0x31a(%rbp), %rdi
movq %rdi, -0x398(%rbp)
callq 0x9b10
movq -0x360(%rbp), %rdi
movq -0x398(%rbp), %rdx
leaq 0xa639(%rip), %rsi      # 0x6ad70
callq 0xa780
jmp 0x6073e
leaq -0x31a(%rbp), %rdi
callq 0x9700
movl $0x1, -0x30c(%rbp)
jmp 0x6085b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x31a(%rbp), %rdi
callq 0x9700
jmp 0x60866
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x340(%rbp), %rdi
callq 0x96b0
jmp 0x60793
leaq -0x58(%rbp), %rdi
leaq -0x340(%rbp), %rsi
callq 0x9830
jmp 0x607a5
leaq -0x340(%rbp), %rdi
callq 0x9c98
movq -0x28(%rbp), %rax
addq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
jmp 0x60242
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x9c98
jmp 0x60866
movq -0x350(%rbp), %rax
movl $0x190, 0x188(%rax)     # imm = 0x190
leaq -0x341(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
callq 0x9b10
movq -0x360(%rbp), %rdi
movq -0x3a0(%rbp), %rdx
leaq 0xa551(%rip), %rsi      # 0x6ad70
callq 0xa780
jmp 0x60826
leaq -0x341(%rbp), %rdi
callq 0x9700
movl $0x1, -0x30c(%rbp)
jmp 0x6085b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x341(%rbp), %rdi
callq 0x9700
jmp 0x60866
leaq -0x58(%rbp), %rdi
callq 0x9c98
jmp 0x60871
leaq -0x58(%rbp), %rdi
callq 0x9c98
jmp 0x60881
movq -0x358(%rbp), %rax
addq $0x3a0, %rsp            # imm = 0x3A0
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x9af0
nopw (%rax,%rax)