// File: mbourand[P]webserv/srcs/Request.cpp
// Function: Request::parse_headers()
bool Request::parse_headers()
{
	size_t header_len = is_header_field_finished(_raw.substr(_parse_start));

	if (header_len == size_t(-1))
		return false;
	else if (header_len == 0)
	{
		if (_method->requestHasBody())
			_parse_start += 2;
		else
			_parse_start -= 2;
		Logger::print("Header section is finished", false, INFO, VERBOSE);
		return (_header_section_finished = true);
	}
	if (_raw.find(':', _parse_start) == std::string::npos)
	{
		_error_code = 400;
		throw std::invalid_argument("Invalid header field in request");
	}
	std::string header_name = _raw.substr(_parse_start, _raw.find(':', _parse_start) - _parse_start);
	if ((header_name.find("X-") == 0) || g_webserv.headers.contains_ignore_case(header_name))
	{
		Header* header;
		if (header_name.find("X-") == 0)
			header = CustomHeader(header_name).clone();
		else
			header = g_webserv.headers.createByType_ignore_case(header_name);
		if (header == NULL)
		{
			_error_code = 500;
			throw std::invalid_argument("Out of memory");
		}
		try
		{
			_parse_start += header_name.size() + 1;
			_parse_start += header->parse(_raw.substr(_parse_start));
			Logger::print("Request header : " + header->getType() + " -> " + header->getValue(), true, INFO, VERBOSE);
			_headers.push_back(header);
			if (header->getType() == ContentLengthHeader().getType())
				_content_length = ft::toInt(header->getValue());
			if (header->getType() == TransferEncodingHeader().getType() && header->getValue() == "chunked")
				_chunked = true;
		}
		catch(const std::exception& e)
		{
			delete header;
			_error_code = 400;
			throw std::invalid_argument("Invalid header field in request");
		}
	}
	else
		_parse_start += header_len;
	if (_raw[_parse_start] == '\r' && _raw[_parse_start + 1] == '\n')
	{
		_parse_start -= 2;
		if (_method->requestHasBody())
			_parse_start += 4;
		Logger::print("Header section is finished", false, INFO, VERBOSE);
		return (_header_section_finished = true);
	}
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x298(%rbp)
movq %rax, %rsi
addq $0x8, %rsi
movq 0x198(%rax), %rdx
leaq -0x38(%rbp), %rdi
movq %rdi, -0x290(%rbp)
movq $-0x1, %rcx
callq 0x96b0
movq -0x290(%rbp), %rdi
callq 0x60a10
movq %rax, -0x288(%rbp)
jmp 0x5f714
leaq -0x38(%rbp), %rdi
callq 0x9c98
movq -0x288(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $-0x1, -0x18(%rbp)
jne 0x5f752
movb $0x0, -0x1(%rbp)
jmp 0x601a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x9c98
jmp 0x601b1
cmpq $0x0, -0x18(%rbp)
jne 0x5f83c
movq -0x298(%rbp), %rax
movq 0x28(%rax), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
testb $0x1, %al
jne 0x5f774
jmp 0x5f78f
movq -0x298(%rbp), %rax
movq 0x198(%rax), %rcx
addq $0x2, %rcx
movq %rcx, 0x198(%rax)
jmp 0x5f7a8
movq -0x298(%rbp), %rax
movq 0x198(%rax), %rcx
subq $0x2, %rcx
movq %rcx, 0x198(%rax)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x9b10
movq -0x2a0(%rbp), %rdx
leaq 0xe43b(%rip), %rsi      # 0x6dc01
leaq -0x68(%rbp), %rdi
callq 0xa780
jmp 0x5f7d1
leaq -0x68(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x5bdc0
jmp 0x5f7e2
leaq -0x68(%rbp), %rdi
callq 0x9c98
leaq -0x69(%rbp), %rdi
callq 0x9700
movq -0x298(%rbp), %rax
movb $0x1, 0x190(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x601a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5f82e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x9c98
leaq -0x69(%rbp), %rdi
callq 0x9700
jmp 0x601b1
jmp 0x5f83e
movq -0x298(%rbp), %rax
movq %rax, %rdi
addq $0x8, %rdi
movq 0x198(%rax), %rdx
movl $0x3a, %esi
callq 0x9860
cmpq $-0x1, %rax
jne 0x5f8d0
movq -0x298(%rbp), %rax
movl $0x190, 0x188(%rax)     # imm = 0x190
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2a8(%rbp)
leaq 0xe38a(%rip), %rsi      # 0x6dc1c
callq 0x9be0
jmp 0x5f899
movq -0x2a8(%rbp), %rdi
movq 0x2f721(%rip), %rsi     # 0x8efc8
movq 0x2f70a(%rip), %rdx     # 0x8efb8
callq 0x9aa0
movq -0x2a8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x94c0
jmp 0x601b1
movq -0x298(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x2b8(%rbp)
movq 0x198(%rax), %rcx
movq %rcx, -0x2b0(%rbp)
movq %rax, %rdi
addq $0x8, %rdi
movq 0x198(%rax), %rdx
movl $0x3a, %esi
callq 0x9860
movq -0x2b8(%rbp), %rsi
movq -0x2b0(%rbp), %rdx
movq %rax, %rcx
movq -0x298(%rbp), %rax
subq 0x198(%rax), %rcx
leaq -0x90(%rbp), %rdi
callq 0x96b0
leaq -0x90(%rbp), %rdi
leaq 0xb288(%rip), %rsi      # 0x6abcc
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
cmpq $0x0, %rax
je 0x5f984
leaq 0x2fdc6(%rip), %rdi     # 0x8f720
addq $0x88, %rdi
leaq -0x90(%rbp), %rsi
callq 0x61a20
movb %al, -0x2b9(%rbp)
jmp 0x5f975
movb -0x2b9(%rbp), %al
testb $0x1, %al
jne 0x5f984
jmp 0x5fff1
leaq -0x90(%rbp), %rdi
leaq 0xb23a(%rip), %rsi      # 0x6abcc
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
cmpq $0x0, %rax
jne 0x5fa15
leaq -0xe0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xb930
jmp 0x5f9b6
leaq -0xe0(%rbp), %rdi
callq 0xbac0
movq %rax, -0x2c8(%rbp)
jmp 0x5f9cb
movq -0x2c8(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xba00
jmp 0x5fa46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x60195
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xba00
jmp 0x60195
leaq 0x2fd04(%rip), %rdi     # 0x8f720
addq $0x88, %rdi
leaq -0x90(%rbp), %rsi
callq 0x616c0
movq %rax, -0x2d0(%rbp)
jmp 0x5fa38
movq -0x2d0(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x5fac2
movq -0x298(%rbp), %rax
movl $0x1f4, 0x188(%rax)     # imm = 0x1F4
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2d8(%rbp)
leaq 0xe1bd(%rip), %rsi      # 0x6dc3c
callq 0x9be0
jmp 0x5fa86
movq -0x2d8(%rbp), %rdi
movq 0x2f534(%rip), %rsi     # 0x8efc8
movq 0x2f51d(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x601c2
movq -0x2d8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x94c0
jmp 0x60195
leaq -0x90(%rbp), %rdi
callq 0x9480
movq %rax, %rcx
movq -0x298(%rbp), %rax
movq 0x198(%rax), %rdx
leaq 0x1(%rcx,%rdx), %rcx
movq %rcx, 0x198(%rax)
movq -0x98(%rbp), %rcx
movq %rcx, -0x2e0(%rbp)
movq %rax, %rsi
addq $0x8, %rsi
movq 0x198(%rax), %rdx
leaq -0x100(%rbp), %rdi
movq $-0x1, %rcx
callq 0x96b0
jmp 0x5fb1c
movq -0x2e0(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
leaq -0x100(%rbp), %rsi
callq *%rax
movq %rax, -0x2e8(%rbp)
jmp 0x5fb3c
movq -0x298(%rbp), %rax
movq -0x2e8(%rbp), %rdx
movq 0x198(%rax), %rcx
addq %rdx, %rcx
movq %rcx, 0x198(%rax)
leaq -0x100(%rbp), %rdi
callq 0x9c98
movq -0x98(%rbp), %rsi
movq (%rsi), %rax
movq 0x10(%rax), %rax
leaq -0x180(%rbp), %rdi
callq *%rax
jmp 0x5fb80
leaq 0xe0c3(%rip), %rsi      # 0x6dc4a
leaq -0x160(%rbp), %rdi
leaq -0x180(%rbp), %rdx
callq 0x14790
jmp 0x5fb9c
leaq 0xdd84(%rip), %rdx      # 0x6d927
leaq -0x140(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x13970
jmp 0x5fbb8
movq -0x98(%rbp), %rdi
callq 0xbf40
movq %rax, -0x2f0(%rbp)
jmp 0x5fbcd
movq -0x2f0(%rbp), %rdx
leaq -0x120(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x14140
jmp 0x5fbe9
leaq -0x120(%rbp), %rdi
movl $0x1, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x5bdc0
jmp 0x5fc00
leaq -0x120(%rbp), %rdi
callq 0x9c98
leaq -0x140(%rbp), %rdi
callq 0x9c98
leaq -0x160(%rbp), %rdi
callq 0x9c98
leaq -0x180(%rbp), %rdi
callq 0x9c98
movq -0x298(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
leaq -0x98(%rbp), %rsi
callq 0x61800
jmp 0x5fc4c
movq -0x98(%rbp), %rsi
movq (%rsi), %rax
movq 0x10(%rax), %rax
leaq -0x1a0(%rbp), %rdi
callq *%rax
jmp 0x5fc65
leaq -0x1e8(%rbp), %rdi
callq 0xb3f0
jmp 0x5fc73
leaq -0x1c0(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0xb4e0
jmp 0x5fc88
leaq -0x1a0(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x144e0
movb %al, -0x2f1(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x9c98
leaq -0x1e8(%rbp), %rdi
callq 0xb470
leaq -0x1a0(%rbp), %rdi
callq 0x9c98
movb -0x2f1(%rbp), %al
testb $0x1, %al
jne 0x5fcd4
jmp 0x5fde9
movq -0x98(%rbp), %rdi
callq 0xbf40
movq %rax, -0x300(%rbp)
jmp 0x5fce9
movq -0x300(%rbp), %rdi
callq 0x2f2a0
movl %eax, -0x304(%rbp)
jmp 0x5fcfd
movq -0x298(%rbp), %rax
movl -0x304(%rbp), %ecx
movslq %ecx, %rcx
movq %rcx, 0x178(%rax)
jmp 0x5fde9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5ff25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x9c98
jmp 0x5ff25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5fda1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5fd95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5fd89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x9c98
leaq -0x140(%rbp), %rdi
callq 0x9c98
leaq -0x160(%rbp), %rdi
callq 0x9c98
leaq -0x180(%rbp), %rdi
callq 0x9c98
jmp 0x5ff25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5fdd8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0xb470
leaq -0x1a0(%rbp), %rdi
callq 0x9c98
jmp 0x5ff25
movq -0x98(%rbp), %rsi
movq (%rsi), %rax
movq 0x10(%rax), %rax
leaq -0x208(%rbp), %rdi
callq *%rax
jmp 0x5fe02
leaq -0x250(%rbp), %rdi
callq 0xe4c0
jmp 0x5fe10
leaq -0x228(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0xe5b0
jmp 0x5fe25
leaq -0x208(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x144e0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x305(%rbp)
jne 0x5fe49
jmp 0x5fe87
movq -0x98(%rbp), %rdi
callq 0xbf40
movq %rax, -0x310(%rbp)
jmp 0x5fe5e
movq -0x310(%rbp), %rdi
leaq 0xddf0(%rip), %rsi      # 0x6dc5c
callq 0xdf50
movb %al, -0x311(%rbp)
jmp 0x5fe79
movb -0x311(%rbp), %al
movb %al, -0x305(%rbp)
jmp 0x5fe87
movb -0x305(%rbp), %al
movb %al, -0x312(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x9c98
leaq -0x250(%rbp), %rdi
callq 0xe540
leaq -0x208(%rbp), %rdi
callq 0x9c98
movb -0x312(%rbp), %al
testb $0x1, %al
jne 0x5fec6
jmp 0x5ffbb
movq -0x298(%rbp), %rax
movb $0x1, 0x193(%rax)
jmp 0x5ffbb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5ff19
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5ff0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x9c98
leaq -0x250(%rbp), %rdi
callq 0xe540
leaq -0x208(%rbp), %rdi
callq 0x9c98
movl -0x44(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x60195
movq -0x40(%rbp), %rdi
callq 0x92a0
movq %rax, -0x258(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x320(%rbp)
cmpq $0x0, %rax
je 0x5ff66
movq -0x320(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x298(%rbp), %rax
movl $0x190, 0x188(%rax)     # imm = 0x190
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x328(%rbp)
leaq 0xdc87(%rip), %rsi      # 0x6dc1c
callq 0x9be0
jmp 0x5ff9c
movq -0x328(%rbp), %rdi
movq 0x2f01e(%rip), %rsi     # 0x8efc8
movq 0x2f007(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x601c2
jmp 0x5ffef
movq -0x328(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x94c0
jmp 0x5ffe3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x9a40
jmp 0x5ffea
jmp 0x60195
jmp 0x6000a
movq -0x298(%rbp), %rax
movq -0x18(%rbp), %rcx
addq 0x198(%rax), %rcx
movq %rcx, 0x198(%rax)
movq -0x298(%rbp), %rax
movq %rax, %rdi
addq $0x8, %rdi
movq 0x198(%rax), %rsi
callq 0x9c70
movq %rax, -0x330(%rbp)
jmp 0x6002d
movq -0x330(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x60179
movq -0x298(%rbp), %rax
movq %rax, %rdi
addq $0x8, %rdi
movq 0x198(%rax), %rsi
incq %rsi
callq 0x9c70
movq %rax, -0x338(%rbp)
jmp 0x60066
movq -0x338(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x60179
movq -0x298(%rbp), %rax
movq 0x198(%rax), %rcx
addq $-0x2, %rcx
movq %rcx, 0x198(%rax)
movq 0x28(%rax), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
callq *%rax
movb %al, -0x339(%rbp)
jmp 0x600a7
movb -0x339(%rbp), %al
testb $0x1, %al
jne 0x600b3
jmp 0x600cc
movq -0x298(%rbp), %rax
movq 0x198(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x198(%rax)
leaq -0x279(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x9b10
movq -0x348(%rbp), %rdx
leaq 0xdb14(%rip), %rsi      # 0x6dc01
leaq -0x278(%rbp), %rdi
callq 0xa780
jmp 0x600fb
leaq -0x278(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x5bdc0
jmp 0x6010f
leaq -0x278(%rbp), %rdi
callq 0x9c98
leaq -0x279(%rbp), %rdi
callq 0x9700
movq -0x298(%rbp), %rax
movb $0x1, 0x190(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0x280(%rbp)
jmp 0x60187
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6016b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x9c98
leaq -0x279(%rbp), %rdi
callq 0x9700
jmp 0x60195
movb $0x1, -0x1(%rbp)
movl $0x1, -0x280(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x9c98
jmp 0x601a3
leaq -0x90(%rbp), %rdi
callq 0x9c98
jmp 0x601b1
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x9af0
movq %rax, %rdi
callq 0xaa90
nopw %cs:(%rax,%rax)