// File: /workspace/github_cpp_makefile_star2_O1/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, 0x28(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x2d11(%rip), %rdx      # 0x52e6
leaq 0x8(%rsp), %rdi
movq %rdx, %rsi
callq 0x4848
testq %r14, %r14
je 0x297f
movq 0x28(%rsp), %rax
leaq 0x88(%rax), %rcx
movq %rcx, 0x30(%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, %r12d
movb $0x1, %al
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
leaq 0x29d7(%rip), %r13      # 0x5004
leal -0x1(%r12), %eax
cmpl $0x6, %eax
ja 0x28f6
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movb (%r15,%rbp), %al
cmpb $0x20, %al
jne 0x286a
movq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2130
movl $0x2, %r12d
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2c6d(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
jmp 0x28f6
movsbl (%r15,%rbp), %eax
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x2840
movq 0x28(%rsp), %rax
movl %ecx, 0x80(%rax)
movl $0x5, %r12d
jmp 0x28f6
movzbl (%r15,%rbp), %eax
movl $0x3, %r12d
cmpq $0x3e, %rax
ja 0x2910
movabsq $0x5000008400000000, %rcx # imm = 0x5000008400000000
btq %rax, %rcx
jb 0x28f6
cmpq $0x20, %rax
jne 0x2910
movq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2130
movl $0x4, %r12d
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2bde(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
jmp 0x28f6
movb (%r15,%rbp), %al
cmpb $0x2f, %al
jne 0x2852
leaq 0x8(%rsp), %rdi
leaq 0x28ff(%rip), %rsi      # 0x502c
callq 0x20a0
testl %eax, %eax
je 0x273f
movq 0x28(%rsp), %rax
movb $0x1, 0x1c(%rax)
movl $0x5, %r12d
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2b8e(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
jmp 0x28f6
movzbl (%r15,%rbp), %eax
movl $0x2, %r12d
cmpq $0x3f, %rax
ja 0x291f
movabsq $0x5000008400000000, %rcx # imm = 0x5000008400000000
btq %rax, %rcx
jb 0x28f6
movabsq $-0x7fffffff00000000, %rcx # imm = 0x8000000100000000
btq %rax, %rcx
jae 0x291f
movq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2130
xorl %r12d, %r12d
cmpb $0x3f, (%r15,%rbp)
setne %bl
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2b17(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
movb %bl, %r12b
addl $0x3, %r12d
jmp 0x28f6
movsbl (%r15,%rbp), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x27fb
movq 0x28(%rsp), %rcx
movl %eax, 0x84(%rcx)
xorl %r12d, %r12d
cmpb $0xa, (%r15,%rbp)
sete %r12b
orl $0x6, %r12d
jmp 0x28f6
movb (%r15,%rbp), %bl
cmpb $0xa, %bl
jne 0x287f
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x2aba(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
movb $0x1, %al
movq %rax, 0x38(%rsp)
jmp 0x28f0
xorl %r12d, %r12d
cmpb $0x2e, %al
sete %r12b
addl $0x5, %r12d
jmp 0x28f6
movl $0x4, %r12d
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28f6
movl $0x1, %r12d
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28f6
movq 0x38(%rsp), %rdx
movl %edx, %eax
notb %al
cmpb $0x3a, %bl
setne %cl
orb %al, %cl
testb $0x1, %cl
jne 0x2904
cmpq $0x0, 0x10(%rsp)
je 0x28c0
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movb (%rcx,%rax), %dl
leal -0x41(%rdx), %esi
cmpb $0x19, %sil
ja 0x28b6
orb $0x20, %dl
movb %dl, (%rcx,%rax)
incq %rax
cmpq 0x10(%rsp), %rax
jb 0x289f
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35b8
movq 0x8(%rax), %rdx
movq $0x0, 0x38(%rsp)
movq %rax, %rdi
xorl %esi, %esi
leaq 0x29fe(%rip), %rcx      # 0x52e6
xorl %r8d, %r8d
callq 0x2270
movl $0x7, %r12d
incq %rbp
cmpq %r14, %rbp
jne 0x262d
jmp 0x297f
testb $0x1, %dl
je 0x292e
leaq 0x8(%rsp), %rdi
jmp 0x296c
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28f6
movsbl %al, %esi
leaq 0x8(%rsp), %rdi
callq 0x2180
jmp 0x28f6
movl $0x7, %r12d
cmpb $0xd, %bl
je 0x28f6
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35b8
cmpq $0x0, 0x8(%rax)
jne 0x2956
cmpb $0x20, (%r15,%rbp)
je 0x28f6
movb (%r15,%rbp), %bl
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35b8
movq %rax, %rdi
movl $0x7, %r12d
movsbl %bl, %esi
callq 0x2180
jmp 0x28f6
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
movb $0x0, (%rax)
movq 0x28(%rsp), %rax
cmpl $0x1, 0x80(%rax)
je 0x29a2
movb $0x1, 0x1c(%rax)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x29be
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 0x29ee
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x22c0