// File: /workspace/github_cpp_makefile_star2_O1/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 $0x368, %rsp            # imm = 0x368
cmpb $0x0, 0x192(%rdi)
jne 0x33e65
movq %rdi, %rbx
cmpq $0x0, 0x28(%rdi)
jne 0x339fa
movq %rbx, %rdi
callq 0x34430
cmpq $0x0, 0x28(%rbx)
je 0x33e65
cmpb $0x0, 0x191(%rbx)
jne 0x33a38
movq 0x198(%rbx), %rax
incq %rax
cmpq 0x10(%rbx), %rax
jae 0x33e65
movq 0x8(%rbx), %rcx
cmpb $0x20, (%rcx,%rax)
je 0x3421c
movq %rbx, %rdi
callq 0x3467c
cmpb $0x1, 0x191(%rbx)
jne 0x33e65
cmpq $0x0, 0x128(%rbx)
jne 0x33a79
movq 0x198(%rbx), %rax
incq %rax
cmpq 0x10(%rbx), %rax
jae 0x33e65
movq 0x8(%rbx), %rcx
cmpb $0x20, (%rcx,%rax)
je 0x3426f
movq %rbx, %rdi
callq 0x347f2
cmpq $0x0, 0x128(%rbx)
je 0x33e65
leaq 0x120(%rbx), %r14
cmpb $0x0, 0x190(%rbx)
jne 0x33aa3
movq %rbx, %rdi
callq 0x34c7e
testb %al, %al
jne 0x33a8e
cmpb $0x1, 0x190(%rbx)
jne 0x33db2
movq %r14, 0x48(%rsp)
movq 0x140(%rbx), %rax
cmpq %rax, 0x148(%rbx)
je 0x33d7f
leaq 0x180(%rbx), %rcx
movq %rcx, 0x50(%rsp)
leaq 0xb8(%rbx), %rcx
movq %rcx, 0x58(%rsp)
xorl %r15d, %r15d
leaq 0x1e0(%rsp), %rbp
leaq 0x8(%rsp), %r12
movl $0xa8, %ecx
addq 0x1a4bb(%rip), %rcx     # 0x4dfb8
movq %rcx, 0x60(%rsp)
xorl %r14d, %r14d
movq (%rax,%r15,8), %rsi
movq (%rsi), %rax
leaq 0xa8(%rsp), %rdi
callq *0x10(%rax)
movq %rbp, %rdi
callq 0x8e70
movq %r12, %rdi
movq %rbp, %rsi
callq 0x8f16
movq 0xb0(%rsp), %rdx
cmpq 0x10(%rsp), %rdx
jne 0x33b58
testq %rdx, %rdx
je 0x33b5d
movq 0x8(%rsp), %rsi
movq 0xa8(%rsp), %rdi
callq 0x74c0
testl %eax, %eax
sete %r13b
jmp 0x33b60
xorl %r13d, %r13d
jmp 0x33b60
movb $0x1, %r13b
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x33b7c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %rbp, %rdi
callq 0x8eb0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x33ba9
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %r13b, %r13b
je 0x33cd3
testq %r14, %r14
leaq 0x8(%rsp), %r12
jne 0x33d84
cmpl $0x0, 0x18c(%rbx)
movl $0x1, %r14d
je 0x33cd8
cmpq $0x0, 0x180(%rbx)
jne 0x33cd8
movq 0x140(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x8e44
movl 0x18c(%rbx), %esi
movq %rax, %rdi
movq 0x60(%rsp), %rdx
callq 0x39526
leaq 0xa8(%rsp), %rdi
movq %rax, %rsi
callq 0x391ee
movq %rbp, %rdi
callq 0x7360
leaq 0xa8(%rsp), %rdi
callq 0x39518
movq %rax, %rdi
movq 0x58(%rsp), %rsi
callq 0x2c118
movq %rax, %r13
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x8(%rsp), %r12
movq %r12, %rdi
leaq 0x915d(%rip), %rsi      # 0x3cdaf
leaq 0x916a(%rip), %rdx      # 0x3cdc3
callq 0x80f4
movq %r13, %rdi
movq %r12, %rsi
callq 0x2b4c6
movq (%rax), %rax
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
leaq 0x1f0(%rsp), %rdi
callq 0x7510
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x33c9d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %rbp, %rdi
movq 0x50(%rsp), %rsi
callq 0x72d0
movq %rbp, %rdi
movq 0x1a2bc(%rip), %rsi     # 0x4df70
callq 0x73b0
leaq 0x260(%rsp), %rdi
callq 0x71c0
leaq 0xa8(%rsp), %rdi
callq 0x394b4
leaq 0x8(%rsp), %r12
incq %r15
movq 0x140(%rbx), %rax
movq 0x148(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r15
jb 0x33b05
jmp 0x33d8a
jmp 0x33d2c
movq %rdx, %r13
movq %rax, %r12
jmp 0x33d4e
movq %rdx, %r13
movq %rax, %r12
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x33d32
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x33d32
movq %rdx, %r13
movq %rax, %r12
movq %rbp, %rdi
movq 0x1a234(%rip), %rsi     # 0x4df70
callq 0x73b0
leaq 0x260(%rsp), %rdi
callq 0x71c0
leaq 0xa8(%rsp), %rdi
callq 0x394b4
jmp 0x33d63
movq %rdx, %r13
movq %rax, %r12
cmpl $0x1, %r13d
jne 0x34427
movq %r12, %rdi
callq 0x7210
callq 0x7780
jmp 0x33cd3
xorl %r14d, %r14d
jmp 0x33d8a
movl $0x2, %r14d
cmpq $0x2, %r14
jae 0x34247
testq %r14, %r14
movq 0x48(%rsp), %rdi
jne 0x33db2
leaq 0x93f4(%rip), %rsi      # 0x3d199
callq 0x7180
testl %eax, %eax
jne 0x34297
cmpb $0x1, 0x190(%rbx)
jne 0x33e0f
xorl %r14d, %r14d
cmpb $0x0, 0x193(%rbx)
jne 0x33e12
cmpq $0x0, 0x178(%rbx)
movl $0x0, %ebp
jne 0x33e14
movq 0x28(%rbx), %rsi
movq (%rsi), %rax
leaq 0x1e0(%rsp), %r14
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x6a7e(%rip), %rsi      # 0x3a870
movq %r14, %rdi
callq 0x7180
movb $0x1, %r14b
testl %eax, %eax
je 0x33e12
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movl %eax, %ebp
jmp 0x33e14
xorl %r14d, %r14d
xorl %ebp, %ebp
testb %r14b, %r14b
je 0x33e3a
leaq 0x1f0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33e3a
movq 0x1f0(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %bpl, %bpl
je 0x33e77
leaq 0x158(%rbx), %rdi
movq 0x160(%rbx), %rdx
leaq 0x6f47(%rip), %rcx      # 0x3ad9b
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x76f0
movb $0x1, 0x192(%rbx)
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, 0x190(%rbx)
jne 0x33fa3
leaq 0x8(%rbx), %r14
movq 0x198(%rbx), %rdx
leaq 0x9a58(%rip), %rsi      # 0x3d8ee
movl $0x4, %ecx
movq %r14, %rdi
callq 0x77f0
cmpq $-0x1, %rax
je 0x33f83
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
testb %al, %al
je 0x33e5e
movb 0x193(%rbx), %al
testb %al, %al
jne 0x33ed3
cmpq $0x0, 0x178(%rbx)
je 0x342bf
testb %al, %al
jne 0x33f1f
movq 0x198(%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x180(%rbx), %rdx
subq %rax, %rcx
cmpq %rdx, %rcx
jbe 0x33f1f
addq %rax, %rdx
leaq 0x99f5(%rip), %rsi      # 0x3d8f0
movl $0x2, %ecx
movq %r14, %rdi
callq 0x77f0
movq 0x180(%rbx), %rcx
addq 0x198(%rbx), %rcx
cmpq %rcx, %rax
jne 0x342fb
movq 0x10(%rbx), %rax
movq 0x198(%rbx), %r15
subq %r15, %rax
cmpq 0x178(%rbx), %rax
jne 0x34047
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq $-0x1, %rcx
callq 0x74d0
movq 0x8(%r12), %rcx
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x74d0
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %rbx, %rsi
callq 0x3563c
jmp 0x3408f
movq 0x178(%rbx), %rax
testq %rax, %rax
je 0x33fa3
movq 0x10(%rbx), %rcx
subq 0x198(%rbx), %rcx
cmpq %rax, %rcx
jae 0x33ead
cmpb $0x1, 0x190(%rbx)
jne 0x33e65
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
testb %al, %al
je 0x33e65
cmpb $0x0, 0x193(%rbx)
jne 0x33e65
movq 0x198(%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x180(%rbx), %rdx
subq %rax, %rcx
cmpq %rdx, %rcx
jbe 0x33e65
leaq 0x8(%rbx), %rdi
addq %rax, %rdx
leaq 0x98f5(%rip), %rsi      # 0x3d8f0
movl $0x2, %ecx
callq 0x77f0
movq 0x180(%rbx), %rcx
addq 0x198(%rbx), %rcx
cmpq %rcx, %rax
je 0x33e65
movl $0x19d, 0x188(%rbx)     # imm = 0x19D
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x96b5(%rip), %rsi      # 0x3d6ef
movq %rax, %rdi
callq 0x78c0
jmp 0x342e5
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq $-0x1, %rcx
callq 0x74d0
movq 0x8(%r12), %rcx
addq $-0x2, %rcx
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x74d0
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %rbx, %rsi
callq 0x3563c
leaq 0x158(%rbx), %rdi
leaq 0xa8(%rsp), %r14
movq %r14, %rsi
callq 0x7590
movq (%r14), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x340c6
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x340e1
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x340fc
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x74b0
cmpq $0x63, 0x160(%rbx)
ja 0x3415a
leaq 0x158(%rbx), %rdx
leaq 0x95f5(%rip), %rsi      # 0x3d709
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0xe31d
leaq 0x97b7(%rip), %rsi      # 0x3d8e2
movq %r14, %rdi
callq 0x78d0
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x341ad
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x341b4
leaq 0x98(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x95ae(%rip), %rsi      # 0x3d71b
leaq 0x95ca(%rip), %rdx      # 0x3d73e
leaq 0x88(%rsp), %r14
movq %r14, %rdi
callq 0x80f4
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xded8
movq 0x88(%rsp), %rdi
cmpq %r15, %rdi
je 0x33e5e
movq 0x98(%rsp), %rsi
jmp 0x3420f
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
leaq 0x68(%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 0xded8
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x341f2
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33e5e
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x33e5e
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x942e(%rip), %rsi      # 0x3d668
movq %rax, %rdi
callq 0x78c0
jmp 0x342e5
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x943a(%rip), %rsi      # 0x3d69f
movq %rax, %rdi
callq 0x78c0
jmp 0x342e5
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x93f8(%rip), %rsi      # 0x3d685
movq %rax, %rdi
callq 0x78c0
jmp 0x342e5
movl $0x190, 0x188(%rbx)     # imm = 0x190
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x940a(%rip), %rsi      # 0x3d6bf
movq %rax, %rdi
callq 0x78c0
jmp 0x342e5
movl $0x19b, 0x188(%rbx)     # imm = 0x19B
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x93fc(%rip), %rsi      # 0x3d6d9
movq %rax, %rdi
callq 0x78c0
movq 0x19cdc(%rip), %rsi     # 0x4dfc8
movq 0x19cbd(%rip), %rdx     # 0x4dfb0
movq %rbx, %rdi
callq 0x77d0
movl $0x19d, 0x188(%rbx)     # imm = 0x19D
movl $0x10, %edi
callq 0x7260
movq %rax, %rbx
leaq 0x93d6(%rip), %rsi      # 0x3d6ef
movq %rax, %rdi
callq 0x78c0
jmp 0x342e5
jmp 0x343e0
jmp 0x343e0
movq %rax, %r12
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x3436d
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x3436d
movq %rax, %r12
movq 0x88(%rsp), %rdi
cmpq %r15, %rdi
je 0x34427
movq 0x98(%rsp), %rsi
jmp 0x3441f
movq %rax, %r12
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x34412
jmp 0x34380
movq %rax, %r12
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x343a5
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x343a5
jmp 0x343a2
movq %rax, %r12
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x34427
movq 0x38(%rsp), %rsi
jmp 0x3441f
jmp 0x343e0
jmp 0x343e0
movq %rax, %r12
leaq 0x1f0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x34427
movq 0x1f0(%rsp), %rsi
jmp 0x3441f
jmp 0x343e0
jmp 0x343e0
movq %rax, %r12
movq %rbx, %rdi
callq 0x7390
jmp 0x34427
movq %rax, %r12
jmp 0x34402
movq %rax, %r12
leaq 0x1e0(%rsp), %rdi
callq 0x8eb0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x34427
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %r12, %rdi
callq 0x7810
nop