// File: /workspace/github_cpp_makefile_star2_O3/mbourand[P]webserv/srcs/Request.cpp
// Function: Request::parse()
void Request::parse()
{
	if (_finished_parsing)
		return;
	if (_method == NULL)
		parse_method();
	if (_method == NULL)
		return;

	if (!_url_finished)
	{
		if (_parse_start + 1 >= _raw.size())
			return;
		if (_raw[_parse_start + 1] == ' ')
		{
			_error_code = 400;
			throw std::invalid_argument("Bad whitespace after method.");
		}

		parse_uri();
	}
	if (!_url_finished)
		return;

	if (_protocolVersion.empty())
	{
		if (_parse_start + 1 >= _raw.size())
			return;
		if (_raw[_parse_start + 1] == ' ')
		{
			_error_code = 400;
			throw std::invalid_argument("Bad whitespace after uri.");
		}
		parse_protocol_version();
	}
	if (_protocolVersion.empty())
		return;

	while (!_header_section_finished && parse_headers())
		;

	if (_header_section_finished)
	{
		size_t cnt = 0;
		for (size_t i = 0; i < _headers.size(); i++)
		{
			if (_headers[i]->getType() == HostHeader().getType())
			{
				if (++cnt > 1)
					break ;
				if (_port && !_max_body_size)
				{
					try
					{
						VirtualHost vhost = VirtualHost::getServerByName(_headers[i]->getValue(), _port, g_webserv.vhosts);
						std::stringstream	convert;
						convert << vhost.getConfig().getConfigPath(_url._path).getParam("max_client_body_size").front();
						convert >> _max_body_size;
					}
					catch(const std::exception& e)
					{}
				}
			}
		}
		if (cnt > 1)
		{
			_error_code = 400;
			throw std::invalid_argument("Multiple Host header in request");
		}
		if (cnt == 0 && _protocolVersion != "HTTP/1.0")
		{
			_error_code = 400;
			throw std::invalid_argument("No Host header in request");
		}

	}
	if (_header_section_finished && !_chunked && !_content_length && _method->getType() != "PUT" && _method->requestHasBody())
	{
		_body = "";
		_finished_parsing = true;
	}
	else if (_header_section_finished && ((_raw.find("\r\n\r\n", _parse_start) != std::string::npos)
		|| (_content_length && ((_raw.size() - _parse_start) >= _content_length))))
	{
		if (_method->requestHasBody())
		{
			if (!_chunked && !_content_length)
			{
				_error_code = 411;
				throw std::invalid_argument("Body Length Required.");
			}
			if (!_chunked && ((_raw.size() - _parse_start) > _max_body_size)
				&& _raw.find("\r\n", _parse_start + _max_body_size) != (_parse_start + _max_body_size))
			{
				_error_code = 413;
				throw std::invalid_argument("Request Entity too large.");
			}
			if ((_raw.size() - _parse_start) == _content_length)
				_body = dechunk(_raw.substr(_parse_start, _raw.substr(_parse_start).size()));
			else
				_body = dechunk(_raw.substr(_parse_start, _raw.substr(_parse_start).size() - 2));
			if (_body.size() < 100)
				Logger::print("Request body is \"" + _body + "\"", NULL, INFO, VERBOSE);
			else
				Logger::print("Request body is <<<BODY TOO LONG>>>", NULL, INFO, VERBOSE);
		}
		_finished_parsing = true;
	}
	else if (_header_section_finished && _method->requestHasBody() && !_chunked && (_raw.size() - _parse_start) > _max_body_size
		&& _raw.find("\r\n", _parse_start + _max_body_size) != (_parse_start + _max_body_size))
	{
		_error_code = 413;
		throw std::invalid_argument("Request Entity too large.");
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
cmpb $0x0, 0x192(%rdi)
jne 0x33dce
movq %rdi, %rbx
cmpq $0x0, 0x28(%rdi)
jne 0x335e3
movq %rbx, %rdi
callq 0x33fe8
cmpq $0x0, 0x28(%rbx)
je 0x33dce
movb 0x191(%rbx), %al
testb %al, %al
jne 0x3361d
movq 0x198(%rbx), %rax
incq %rax
cmpq 0x10(%rbx), %rax
jae 0x33dce
movq 0x8(%rbx), %rcx
cmpb $0x20, (%rcx,%rax)
je 0x33e1c
movq %rbx, %rdi
callq 0x34234
movb 0x191(%rbx), %al
testb $0x1, %al
je 0x33dce
cmpq $0x0, 0x128(%rbx)
je 0x339a7
leaq 0x120(%rbx), %r14
movb 0x190(%rbx), %al
testb %al, %al
jne 0x33656
movq %rbx, %rdi
callq 0x34848
testb %al, %al
jne 0x3363a
movb 0x190(%rbx), %al
testb $0x1, %al
je 0x33948
movq 0x140(%rbx), %rax
cmpq %rax, 0x148(%rbx)
je 0x33931
movq %r14, 0x28(%rsp)
leaq 0x180(%rbx), %rcx
movq %rcx, 0x30(%rsp)
leaq 0xb8(%rbx), %rcx
movq %rcx, 0x38(%rsp)
xorl %r15d, %r15d
leaq 0x1c0(%rsp), %r12
leaq 0x8(%rsp), %r14
movl $0xa8, %ecx
addq 0x1a90d(%rip), %rcx     # 0x4dfb8
movq %rcx, 0x40(%rsp)
xorl %ebp, %ebp
movq (%rax,%r15,8), %rsi
movq (%rsi), %rax
leaq 0x88(%rsp), %rdi
callq *0x10(%rax)
movq %r12, %rdi
callq 0x8e7c
movq %r14, %rdi
movq %r12, %rsi
callq 0x8f22
movq 0x90(%rsp), %rdx
cmpq 0x10(%rsp), %rdx
jne 0x33708
movq 0x8(%rsp), %r13
testq %rdx, %rdx
je 0x33712
movq 0x88(%rsp), %rdi
movq %r13, %rsi
callq 0x74c0
testl %eax, %eax
sete %r14b
jmp 0x33715
movq 0x8(%rsp), %r13
xorl %r14d, %r14d
jmp 0x33715
movb $0x1, %r14b
leaq 0x18(%rsp), %rax
cmpq %rax, %r13
je 0x3372f
movq 0x18(%rsp), %rsi
incq %rsi
movq %r13, %rdi
callq 0x74b0
movq %r12, %rdi
callq 0x8ebc
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3375c
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %r14b, %r14b
je 0x33901
testq %rbp, %rbp
leaq 0x8(%rsp), %r14
jne 0x33de0
cmpl $0x0, 0x18c(%rbx)
movl $0x1, %ebp
je 0x33906
cmpq $0x0, 0x180(%rbx)
jne 0x33906
movq 0x140(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x8e50
movl 0x18c(%rbx), %esi
movq %rax, %rdi
movq 0x40(%rsp), %rdx
callq 0x390ea
leaq 0x88(%rsp), %rdi
movq %rax, %rsi
callq 0x38dba
movq %r12, %rdi
callq 0x7360
leaq 0x88(%rsp), %rdi
callq 0x390dc
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x2bdbc
movq %rax, %r13
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x95b0(%rip), %rsi      # 0x3cdaf
leaq 0x95bd(%rip), %rdx      # 0x3cdc3
callq 0x80f4
movq %r13, %rdi
movq %r14, %rsi
callq 0x2b17e
movq (%rax), %rax
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
leaq 0x1d0(%rsp), %rdi
callq 0x7510
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3384a
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq 0x72d0
movq %r12, %rdi
movq 0x1a70f(%rip), %rsi     # 0x4df70
callq 0x73b0
leaq 0x240(%rsp), %rdi
callq 0x71c0
leaq 0x88(%rsp), %rdi
callq 0x39078
jmp 0x33906
jmp 0x338b3
movq %rdx, %r14
movq %rax, %r13
jmp 0x338d5
movq %rdx, %r14
movq %rax, %r13
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x338b9
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x338b9
movq %rdx, %r14
movq %rax, %r13
movq %r12, %rdi
movq 0x1a6ad(%rip), %rsi     # 0x4df70
callq 0x73b0
leaq 0x240(%rsp), %rdi
callq 0x71c0
leaq 0x88(%rsp), %rdi
callq 0x39078
jmp 0x338ea
movq %rdx, %r14
movq %rax, %r13
cmpl $0x1, %r14d
jne 0x33fdf
movq %r13, %rdi
callq 0x7210
callq 0x7780
leaq 0x8(%rsp), %r14
incq %r15
movq 0x140(%rbx), %rax
movq 0x148(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r15
jb 0x336b2
testq %rbp, %rbp
movq 0x28(%rsp), %r14
jne 0x33948
leaq 0x9861(%rip), %rsi      # 0x3d199
movq %r14, %rdi
callq 0x7180
testl %eax, %eax
jne 0x33e44
movb 0x190(%rbx), %al
cmpb $0x1, %al
jne 0x33a36
cmpb $0x0, 0x193(%rbx)
jne 0x33a36
cmpq $0x0, 0x178(%rbx)
jne 0x33a36
movq 0x28(%rbx), %rsi
movq (%rsi), %rax
leaq 0x1c0(%rsp), %r14
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x6ee3(%rip), %rsi      # 0x3a870
movq %r14, %rdi
callq 0x7180
testl %eax, %eax
je 0x339e4
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movl %eax, %ebp
jmp 0x339e6
movq 0x198(%rbx), %rax
incq %rax
cmpq 0x10(%rbx), %rax
jae 0x33dce
movq 0x8(%rbx), %rcx
cmpb $0x20, (%rcx,%rax)
je 0x33e6c
movq %rbx, %rdi
callq 0x343aa
cmpq $0x0, 0x128(%rbx)
jne 0x33633
jmp 0x33dce
xorl %ebp, %ebp
leaq 0x1d0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33a07
movq 0x1d0(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %bpl, %bpl
je 0x33a30
leaq 0x158(%rbx), %rdi
movq 0x160(%rbx), %rdx
leaq 0x737a(%rip), %rcx      # 0x3ad9b
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x76f0
jmp 0x33dc7
movb 0x190(%rbx), %al
testb $0x1, %al
je 0x33b69
leaq 0x8(%rbx), %r14
movq 0x198(%rbx), %rdx
leaq 0x9e9e(%rip), %rsi      # 0x3d8ee
movl $0x4, %ecx
movq %r14, %rdi
callq 0x77f0
cmpq $-0x1, %rax
je 0x33b49
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
testb %al, %al
je 0x33dc7
movb 0x193(%rbx), %cl
movq 0x178(%rbx), %rax
testq %rax, %rax
setne %dl
orb %cl, %dl
je 0x33e97
movq 0x198(%rbx), %r15
testb %cl, %cl
jne 0x33aea
movq 0x10(%rbx), %rcx
movq 0x180(%rbx), %rdx
subq %r15, %rcx
cmpq %rdx, %rcx
jbe 0x33aea
addq %r15, %rdx
leaq 0x9e34(%rip), %rsi      # 0x3d8f0
movl $0x2, %ecx
movq %r14, %rdi
callq 0x77f0
movq 0x198(%rbx), %r15
movq 0x180(%rbx), %rcx
addq %r15, %rcx
cmpq %rcx, %rax
jne 0x33ec2
movq 0x178(%rbx), %rax
movq 0x10(%rbx), %rcx
subq %r15, %rcx
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
cmpq %rax, %rcx
jne 0x33c0d
movq $-0x1, %rcx
callq 0x74d0
movq 0x8(%r12), %rcx
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x74d0
leaq 0x1c0(%rsp), %rdi
leaq 0x88(%rsp), %rdx
movq %rbx, %rsi
callq 0x35240
jmp 0x33c4d
movq 0x178(%rbx), %rax
testq %rax, %rax
je 0x33b69
movq 0x10(%rbx), %rcx
subq 0x198(%rbx), %rcx
cmpq %rax, %rcx
jae 0x33a67
cmpb $0x1, 0x190(%rbx)
jne 0x33dce
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
testb %al, %al
je 0x33dce
cmpb $0x0, 0x193(%rbx)
jne 0x33dce
movq 0x198(%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x180(%rbx), %rdx
subq %rax, %rcx
cmpq %rdx, %rcx
jbe 0x33dce
leaq 0x8(%rbx), %rdi
addq %rax, %rdx
leaq 0x9d2f(%rip), %rsi      # 0x3d8f0
movl $0x2, %ecx
callq 0x77f0
movq 0x180(%rbx), %rcx
addq 0x198(%rbx), %rcx
cmpq %rcx, %rax
je 0x33dce
movl $0x19d, 0x188(%rbx)     # imm = 0x19D
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x9aef(%rip), %rsi      # 0x3d6ef
movq %rax, %rdi
callq 0x78c0
jmp 0x33e06
movq $-0x1, %rcx
callq 0x74d0
movq 0x8(%r12), %rcx
addq $-0x2, %rcx
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x74d0
leaq 0x1c0(%rsp), %rdi
leaq 0x88(%rsp), %rdx
movq %rbx, %rsi
callq 0x35240
leaq 0x158(%rbx), %rdi
leaq 0x1c0(%rsp), %r14
movq %r14, %rsi
callq 0x7590
movq (%r14), %rdi
leaq 0x1d0(%rsp), %rax
cmpq %rax, %rdi
je 0x33c84
movq 0x1d0(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33ca5
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33cc0
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
cmpq $0x63, 0x160(%rbx)
ja 0x33d1e
leaq 0x158(%rbx), %rdx
leaq 0x9a31(%rip), %rsi      # 0x3d709
leaq 0x1c0(%rsp), %r14
movq %r14, %rdi
callq 0xe2cb
leaq 0x9bf3(%rip), %rsi      # 0x3d8e2
movq %r14, %rdi
callq 0x78d0
leaq 0x58(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x33d61
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x33d68
leaq 0x78(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x99ed(%rip), %rsi      # 0x3d71b
leaq 0x9a09(%rip), %rdx      # 0x3d73e
leaq 0x68(%rsp), %r14
movq %r14, %rdi
callq 0x80f4
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xdea4
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x33dc7
movq 0x78(%rsp), %rsi
jmp 0x33dbf
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
leaq 0x48(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xdea4
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x33da6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x1d0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33dc7
movq 0x1d0(%rsp), %rsi
incq %rsi
callq 0x74b0
movb $0x1, 0x192(%rbx)
addq $0x348, %rsp            # imm = 0x348
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x98a1(%rip), %rsi      # 0x3d69f
movq %rax, %rdi
callq 0x78c0
movq 0x1a1bb(%rip), %rsi     # 0x4dfc8
movq 0x1a19c(%rip), %rdx     # 0x4dfb0
movq %rbx, %rdi
callq 0x77d0
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x982e(%rip), %rsi      # 0x3d668
movq %rax, %rdi
callq 0x78c0
jmp 0x33e06
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x985d(%rip), %rsi      # 0x3d6bf
movq %rax, %rdi
callq 0x78c0
jmp 0x33e06
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x97fb(%rip), %rsi      # 0x3d685
movq %rax, %rdi
callq 0x78c0
jmp 0x33e06
movl $0x19b, 0x188(%rbx)     # imm = 0x19B
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x9824(%rip), %rsi      # 0x3d6d9
movq %rax, %rdi
callq 0x78c0
jmp 0x33e06
movl $0x19d, 0x188(%rbx)     # imm = 0x19D
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x980f(%rip), %rsi      # 0x3d6ef
movq %rax, %rdi
callq 0x78c0
jmp 0x33e06
jmp 0x33f98
jmp 0x33f98
movq %rax, %r13
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x33f7b
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x33f7b
movq %rax, %r13
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x33fdf
movq 0x78(%rsp), %rsi
jmp 0x33fd7
jmp 0x33f78
jmp 0x33f32
movq %rax, %r13
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33f5d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x33f5d
jmp 0x33f5a
movq %rax, %r13
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33fdf
movq 0x18(%rsp), %rsi
jmp 0x33fd7
jmp 0x33f98
jmp 0x33f98
jmp 0x33f98
movq %rax, %r13
leaq 0x1d0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33fdf
movq 0x1d0(%rsp), %rsi
jmp 0x33fd7
jmp 0x33f98
movq %rax, %r13
movq %rbx, %rdi
callq 0x7390
jmp 0x33fdf
movq %rax, %r13
jmp 0x33fba
movq %rax, %r13
leaq 0x1c0(%rsp), %rdi
callq 0x8ebc
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x33fdf
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %r13, %rdi
callq 0x7810
nop