// File: /workspace/github_cpp_makefile_star2_O2/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 $0x358, %rsp            # imm = 0x358
cmpb $0x0, 0x192(%rdi)
jne 0x27d3c
movq %rdi, %rbx
cmpq $0x0, 0x28(%rdi)
jne 0x276b3
movq %rbx, %rdi
callq 0x27ee4
cmpq $0x0, 0x28(%rbx)
je 0x27d3c
movb 0x191(%rbx), %al
testb %al, %al
jne 0x276ed
movq 0x198(%rbx), %rax
incq %rax
cmpq 0x10(%rbx), %rax
jae 0x27d3c
movq 0x8(%rbx), %rcx
cmpb $0x20, (%rcx,%rax)
je 0x27d8d
movq %rbx, %rdi
callq 0x28074
movb 0x191(%rbx), %al
testb $0x1, %al
je 0x27d3c
cmpq $0x0, 0x128(%rbx)
je 0x27962
leaq 0x120(%rbx), %r14
movb 0x190(%rbx), %al
testb %al, %al
jne 0x27726
movq %rbx, %rdi
callq 0x2850a
testb %al, %al
jne 0x2770a
movb 0x190(%rbx), %al
testb $0x1, %al
movq %rbx, 0x8(%rsp)
je 0x279c2
movq %r14, 0x38(%rsp)
leaq 0x180(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0xb8(%rbx), %rax
movq %rax, 0x48(%rsp)
xorl %ebp, %ebp
leaq 0x98(%rsp), %r14
leaq 0x18(%rsp), %r12
movl $0xa8, %eax
addq 0x1b84d(%rip), %rax     # 0x42fb8
movq %rax, 0x50(%rsp)
xorl %r15d, %r15d
movq 0x8(%rsp), %rcx
movq 0x140(%rcx), %rax
movq 0x148(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r15
jae 0x2799f
movq (%rax,%r15,8), %rsi
movq (%rsi), %rax
leaq 0x220(%rsp), %rdi
callq *0x10(%rax)
movq %r14, %rdi
callq 0x896c
movq %r12, %rdi
movq %r14, %rsi
callq 0x89c4
movq %r14, %rbx
leaq 0x220(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0xc2ad
movl %eax, %r13d
movq %r12, %rdi
callq 0x79b8
movq %rbx, %rdi
callq 0x89a4
movq %r14, %rdi
callq 0x79b8
testb %r13b, %r13b
je 0x2794d
testq %rbp, %rbp
jne 0x27d4e
movq 0x8(%rsp), %rax
cmpl $0x0, 0x18c(%rax)
pushq $0x1
popq %rbp
leaq 0x98(%rsp), %r14
leaq 0x18(%rsp), %r12
je 0x2795a
movq 0x8(%rsp), %rax
cmpq $0x0, 0x180(%rax)
jne 0x2795a
movq 0x8(%rsp), %rbx
movq 0x140(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x8964
movl 0x18c(%rbx), %esi
movq %rax, %rdi
movq 0x50(%rsp), %rdx
callq 0x2b724
leaq 0x220(%rsp), %rdi
movq %rax, %rsi
callq 0x2b4ca
movq %r14, %rdi
callq 0x73a0
leaq 0x220(%rsp), %rdi
callq 0x2b716
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x21cc6
movq %rax, %r13
movq %r12, %rdi
leaq 0x74aa(%rip), %rsi      # 0x2ed44
leaq 0x17(%rsp), %rdx
callq 0x8032
movq %r13, %rdi
movq %r12, %rsi
callq 0x2167a
movq (%rax), %rsi
addq $0x10, %rsi
leaq 0xa8(%rsp), %rdi
callq 0x74a0
movq %r12, %rdi
callq 0x79b8
movq %r14, %rdi
movq 0x40(%rsp), %rsi
callq 0x7310
movq %r14, %rdi
callq 0x73f0
leaq 0x220(%rsp), %rdi
callq 0x2b6d6
pushq $0x1
popq %rbp
jmp 0x2795a
jmp 0x2790c
movq %rdx, %rbx
movq %rax, %r14
jmp 0x2791f
movq %rdx, %rbx
movq %rax, %r14
movq %r12, %rdi
callq 0x79b8
jmp 0x27912
movq %rdx, %rbx
movq %rax, %r14
leaq 0x98(%rsp), %rdi
callq 0x73f0
leaq 0x220(%rsp), %rdi
callq 0x2b6d6
jmp 0x27934
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x1, %ebx
jne 0x27edc
movq %r14, %rdi
callq 0x7220
callq 0x77f0
pushq $0x1
popq %rbp
leaq 0x98(%rsp), %r14
leaq 0x18(%rsp), %r12
incq %r15
jmp 0x27773
movq 0x198(%rbx), %rax
incq %rax
cmpq 0x10(%rbx), %rax
jae 0x27d3c
movq 0x8(%rbx), %rcx
cmpb $0x20, (%rcx,%rax)
je 0x27db3
movq %rbx, %rdi
callq 0x281c0
cmpq $0x0, 0x128(%rbx)
jne 0x27703
jmp 0x27d3c
testq %rbp, %rbp
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rbx
jne 0x279c2
leaq 0x7774(%rip), %rsi      # 0x2f129
callq 0x8f4f
testb %al, %al
jne 0x27dd9
cmpb $0x1, 0x190(%rbx)
jne 0x27a60
cmpb $0x0, 0x193(%rbx)
jne 0x27a60
cmpq $0x0, 0x178(%rbx)
jne 0x27a60
movq 0x28(%rbx), %rsi
movq (%rsi), %rax
leaq 0x98(%rsp), %r14
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x4e22(%rip), %rsi      # 0x2c824
movq %r14, %rdi
callq 0x8f4f
testb %al, %al
je 0x27a4e
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movl %eax, %ebp
leaq 0x98(%rsp), %rdi
callq 0x79b8
testb %bpl, %bpl
movq 0x8(%rsp), %rbx
je 0x27a60
leaq 0x158(%rbx), %rdi
leaq 0x530b(%rip), %rsi      # 0x2cd4f
callq 0x7930
jmp 0x27d35
leaq 0x98(%rsp), %rdi
callq 0x79b8
movq 0x8(%rsp), %rbx
cmpb $0x1, 0x190(%rbx)
jne 0x27b8a
leaq 0x8(%rbx), %r14
movq 0x198(%rbx), %rdx
leaq 0x7dff(%rip), %rsi      # 0x2f87e
movq %r14, %rdi
callq 0x7980
cmpq $-0x1, %rax
je 0x27b6a
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
testb %al, %al
je 0x27d35
movb 0x193(%rbx), %cl
movq 0x178(%rbx), %rax
testq %rax, %rax
setne %dl
orb %cl, %dl
je 0x27e02
movq 0x198(%rbx), %r15
testb %cl, %cl
jne 0x27b0f
movq 0x10(%rbx), %rcx
movq 0x180(%rbx), %rdx
subq %r15, %rcx
cmpq %rdx, %rcx
jbe 0x27b0f
addq %r15, %rdx
leaq 0x7d9a(%rip), %rsi      # 0x2f880
movq %r14, %rdi
callq 0x7980
movq 0x198(%rbx), %r15
movq 0x180(%rbx), %rcx
addq %r15, %rcx
cmpq %rcx, %rax
jne 0x27e2b
movq 0x178(%rbx), %rax
movq 0x10(%rbx), %rcx
subq %r15, %rcx
leaq 0x18(%rsp), %r12
cmpq %rax, %rcx
jne 0x27c27
pushq $-0x1
popq %rcx
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7540
movq 0x8(%r12), %rcx
leaq 0x220(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7540
leaq 0x98(%rsp), %rdi
leaq 0x220(%rsp), %rdx
movq %rbx, %rsi
callq 0x28bda
jmp 0x27c6c
movq 0x178(%rbx), %rax
testq %rax, %rax
je 0x27b8a
movq 0x10(%rbx), %rcx
subq 0x198(%rbx), %rcx
cmpq %rax, %rcx
jae 0x27a91
cmpb $0x1, 0x190(%rbx)
jne 0x27d3c
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
testb %al, %al
je 0x27d3c
cmpb $0x0, 0x193(%rbx)
jne 0x27d3c
movq 0x198(%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x180(%rbx), %rdx
subq %rax, %rcx
cmpq %rdx, %rcx
jbe 0x27d3c
leaq 0x8(%rbx), %rdi
addq %rax, %rdx
leaq 0x7c9e(%rip), %rsi      # 0x2f880
callq 0x7980
movq 0x180(%rbx), %rcx
addq 0x198(%rbx), %rcx
cmpq %rcx, %rax
je 0x27d3c
movl $0x19d, 0x188(%rbx)     # imm = 0x19D
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %rbx
leaq 0x7a65(%rip), %rsi      # 0x2f67f
movq %rax, %rdi
callq 0x7910
jmp 0x27d77
pushq $-0x1
popq %rcx
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7540
movq 0x8(%r12), %rcx
addq $-0x2, %rcx
leaq 0x220(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7540
leaq 0x98(%rsp), %rdi
leaq 0x220(%rsp), %rdx
movq %rbx, %rsi
callq 0x28bda
leaq 0x158(%rbx), %rdi
leaq 0x98(%rsp), %r14
movq %r14, %rsi
callq 0x75f0
movq %r14, %rdi
callq 0x79b8
leaq 0x220(%rsp), %rdi
callq 0x79b8
leaq 0x18(%rsp), %rdi
callq 0x79b8
cmpq $0x63, 0x160(%rbx)
ja 0x27d02
leaq 0x158(%rbx), %rdx
leaq 0x79df(%rip), %rsi      # 0x2f699
leaq 0x98(%rsp), %r14
movq %r14, %rdi
callq 0xc154
leaq 0x7ba1(%rip), %rdx      # 0x2f872
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0xbe83
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xbe10
leaq 0x78(%rsp), %rdi
callq 0x79b8
leaq 0x98(%rsp), %rdi
jmp 0x27d30
leaq 0x79a2(%rip), %rsi      # 0x2f6ab
leaq 0x58(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x8032
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xbe10
leaq 0x58(%rsp), %rdi
callq 0x79b8
movb $0x1, 0x192(%rbx)
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movl $0x190, 0x188(%rax)     # imm = 0x190
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %rbx
leaq 0x78c0(%rip), %rsi      # 0x2f62f
movq %rax, %rdi
callq 0x7910
movq 0x1b24a(%rip), %rsi     # 0x42fc8
movq 0x1b22b(%rip), %rdx     # 0x42fb0
movq %rbx, %rdi
callq 0x7830
movl $0x190, 0x188(%rbx)     # imm = 0x190
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %rbx
leaq 0x784f(%rip), %rsi      # 0x2f5f8
movq %rax, %rdi
callq 0x7910
jmp 0x27d77
movl $0x190, 0x188(%rbx)     # imm = 0x190
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %rbx
leaq 0x7846(%rip), %rsi      # 0x2f615
movq %rax, %rdi
callq 0x7910
jmp 0x27d77
movl $0x190, 0x188(%rbx)     # imm = 0x190
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %rbx
leaq 0x785a(%rip), %rsi      # 0x2f64f
movq %rax, %rdi
callq 0x7910
jmp 0x27d77
movl $0x19b, 0x188(%rbx)     # imm = 0x19B
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %rbx
leaq 0x784b(%rip), %rsi      # 0x2f669
movq %rax, %rdi
callq 0x7910
jmp 0x27d77
movl $0x19d, 0x188(%rbx)     # imm = 0x19D
pushq $0x10
popq %rdi
callq 0x7290
movq %rax, %rbx
leaq 0x7838(%rip), %rsi      # 0x2f67f
movq %rax, %rdi
callq 0x7910
jmp 0x27d77
jmp 0x27ead
jmp 0x27ead
movq %rax, %r14
leaq 0x58(%rsp), %rdi
jmp 0x27ed7
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x79b8
jmp 0x27ea1
movq %rax, %r14
jmp 0x27edc
jmp 0x27e9e
jmp 0x27e7a
movq %rax, %r14
leaq 0x220(%rsp), %rdi
callq 0x79b8
jmp 0x27e91
jmp 0x27e8e
movq %rax, %r14
leaq 0x18(%rsp), %rdi
jmp 0x27ed7
jmp 0x27ead
jmp 0x27ead
jmp 0x27ead
movq %rax, %r14
leaq 0x98(%rsp), %rdi
jmp 0x27ed7
jmp 0x27ead
movq %rax, %r14
movq %rbx, %rdi
callq 0x73d0
jmp 0x27edc
movq %rax, %r14
jmp 0x27ecf
movq %rax, %r14
leaq 0x98(%rsp), %rdi
callq 0x89a4
leaq 0x220(%rsp), %rdi
callq 0x79b8
movq %r14, %rdi
callq 0x7860