// File: /workspace/github_cpp_makefile_star2_O3/alibakhtiar[P]ExampleHTTPServer/./HttpServer.hh
// Function: SimpleHttpServer::httpRequestParse(SimpleHttpServer::Request*, char const*, unsigned long)
bool httpRequestParse(Request *req, const char *buffer, size_t length)
{
	std::string tmp = "";
	int state = PARSER_STATE_METHOD;
	bool isKey = true;

	size_t i;
	for (i=0; i<length; ++i)
	{
		switch (state) {
			case PARSER_STATE_METHOD:
				if (buffer[i] == ' ') {
					req->method = tmp;
					state = PARSER_STATE_URL;
					tmp = "";
					break;
				}
				tmp += buffer[i];
			break;

			case PARSER_STATE_URL:
				if (buffer[i] == ' ' || buffer[i] == '?') {
					req->url = tmp;
					state = buffer[i] == '?' ? PARSER_STATE_URL_QS : PARSER_STATE_PROTOCOL;
					tmp = "";
					continue;
				}

				// Simple XSS filter
				// ascii 34 == "
				// ascii 39 == '
				if (buffer[i] == '<' || buffer[i] == '>' || buffer[i] == 34 || buffer[i] == 39)
					continue;

				tmp += buffer[i];
			break;

			case PARSER_STATE_URL_QS:
				if (buffer[i] == ' ') {
					req->queryString = tmp;
					state = PARSER_STATE_PROTOCOL;
					tmp = "";
					continue;
				}

				// Simple XSS filter
				// ascii 34 == "
				// ascii 39 == '
				if (buffer[i] == '<' || buffer[i] == '>' || buffer[i] == 34 || buffer[i] == 39)
					continue;

				tmp += buffer[i];
			break;

			case PARSER_STATE_PROTOCOL:
				if (buffer[i] == '/') {
					if (tmp != "HTTP")
						req->hasError = true;

					state = PARSER_STATE_MAJOR;
					tmp = "";
					continue;
				}
				else {
					tmp += buffer[i];
				}
			break;

			case PARSER_STATE_MAJOR:
				if (isdigit(buffer[i]) != 0) {
					req->httpMajorVersion = (int)buffer[i] - '0';
				}
				else if (buffer[i] == '.') {
					state = PARSER_STATE_MINOR;
				}
			break;

			case PARSER_STATE_MINOR:
				if (isdigit(buffer[i]) != 0) {
					req->httpMinorVersion = (int)buffer[i] - '0';
				}

				if (buffer[i] == '\n')
					state = PARSER_STATE_HEADERS;
			break;

			case PARSER_STATE_HEADERS:
				if (buffer[i] == '\n') {
					isKey = true;
					tmp = "";
				}
				else if (isKey == true && buffer[i] == ':') {
					strtolower(tmp);
					req->headers[tmp] = "";
					isKey = false;
				}
				else if (isKey == true) {
					tmp += buffer[i];
				}
				else if (isKey == false) {
					if (buffer[i] == '\r' || (req->headers[tmp].size() == 0 && buffer[i] == ' '))
						continue;

					req->headers[tmp] += buffer[i];
				}
			break;
		}
	}

	tmp.clear();

	if (req->httpMajorVersion != 1) {
		req->hasError = true;
	}

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x30(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x2d11(%rip), %rdx      # 0x52e6
leaq 0x8(%rsp), %rdi
movq %rdx, %rsi
callq 0x483c
testq %r14, %r14
je 0x298b
movq 0x30(%rsp), %rax
leaq 0x88(%rax), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x60(%rax), %rcx
movq %rcx, 0x40(%rsp)
leaq 0x40(%rax), %rcx
movq %rcx, 0x48(%rsp)
addq $0x20, %rax
movq %rax, 0x50(%rsp)
movl $0x1, %ebx
movb $0x1, %al
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
leaq 0x29d7(%rip), %r12      # 0x5004
leal -0x1(%rbx), %eax
cmpl $0x6, %eax
ja 0x28ec
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movb (%r15,%r13), %al
cmpb $0x20, %al
jne 0x285b
movq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2130
movl $0x2, %ebx
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2c71(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
jmp 0x28ec
movsbl (%r15,%r13), %eax
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x2835
movq 0x30(%rsp), %rax
movl %ecx, 0x80(%rax)
movl $0x5, %ebx
jmp 0x28ec
movzbl (%r15,%r13), %eax
movl $0x3, %ebx
cmpq $0x3e, %rax
ja 0x2915
movabsq $0x5000008400000000, %rcx # imm = 0x5000008400000000
btq %rax, %rcx
jb 0x28ec
cmpq $0x20, %rax
jne 0x2915
movq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2130
movl $0x4, %ebx
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2be5(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
jmp 0x28ec
movb (%r15,%r13), %al
cmpb $0x2f, %al
jne 0x2844
leaq 0x8(%rsp), %rdi
leaq 0x2906(%rip), %rsi      # 0x502c
callq 0x20a0
testl %eax, %eax
je 0x2738
movq 0x30(%rsp), %rax
movb $0x1, 0x1c(%rax)
movl $0x5, %ebx
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2b96(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
jmp 0x28ec
movzbl (%r15,%r13), %eax
movl $0x2, %ebx
cmpq $0x3f, %rax
ja 0x2924
movabsq $0x5000008400000000, %rcx # imm = 0x5000008400000000
btq %rax, %rcx
jb 0x28ec
movabsq $-0x7fffffff00000000, %rcx # imm = 0x8000000100000000
btq %rax, %rcx
jae 0x2924
movq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2130
xorl %ebx, %ebx
cmpb $0x3f, (%r15,%r13)
setne %bpl
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2b20(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
movb %bpl, %bl
addl $0x3, %ebx
jmp 0x28ec
movsbl (%r15,%r13), %eax
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0x27f7
movq 0x30(%rsp), %rax
movl %ecx, 0x84(%rax)
movb (%r15,%r13), %al
xorl %ebx, %ebx
cmpb $0xa, %al
sete %bl
orl $0x6, %ebx
jmp 0x28ec
movb (%r15,%r13), %al
cmpb $0xa, %al
jne 0x286f
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2ac5(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x28e7
xorl %ebx, %ebx
cmpb $0x2e, %al
sete %bl
addl $0x5, %ebx
jmp 0x28ec
movl $0x4, %ebx
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28ec
movl $0x1, %ebx
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28ec
cmpb $0x3a, %al
setne %cl
movq 0x28(%rsp), %rsi
movl %esi, %edx
notb %dl
orb %cl, %dl
testb $0x1, %dl
jne 0x28fd
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x28b7
xorl %ecx, %ecx
movq 0x8(%rsp), %rdx
movb (%rdx,%rcx), %sil
leal -0x41(%rsi), %edi
cmpb $0x19, %dil
ja 0x28af
orb $0x20, %sil
movb %sil, (%rdx,%rcx)
movq 0x10(%rsp), %rax
incq %rcx
cmpq %rax, %rcx
jb 0x2890
movq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35c8
movq 0x8(%rax), %rdx
movq $0x0, 0x28(%rsp)
movq %rax, %rdi
xorl %esi, %esi
leaq 0x2a07(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
movl $0x7, %ebx
incq %r13
cmpq %r14, %r13
jne 0x262d
jmp 0x298b
testb $0x1, %sil
je 0x2933
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x2829
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28ec
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28ec
movl $0x7, %ebx
cmpb $0xd, %al
je 0x297d
movq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35c8
cmpq $0x0, 0x8(%rax)
sete %al
movb (%r15,%r13), %bpl
cmpb $0x20, %bpl
sete %cl
testb %cl, %al
jne 0x297d
movq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35c8
movsbl %bpl, %esi
movq %rax, %rdi
callq 0x2180
movq $0x0, 0x28(%rsp)
jmp 0x28ec
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
movb $0x0, (%rax)
movq 0x30(%rsp), %rax
cmpl $0x1, 0x80(%rax)
je 0x29ae
movb $0x1, 0x1c(%rax)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x29ca
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21d0
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x29fa
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x22c0