// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
leaq 0x2e0c(%rip), %rsi      # 0x52e6
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x30ca
leaq 0x88(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x60(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x40(%rbx), %rax
movq %rax, 0x48(%rsp)
movq %rbx, 0x30(%rsp)
leaq 0x20(%rbx), %rax
movq %rax, 0x50(%rsp)
movb $0x1, %r12b
pushq $0x1
popq %rax
xorl %ebp, %ebp
movl %eax, %r13d
cmpq %rbp, %r14
je 0x282e
leal -0x1(%r13), %eax
cmpl $0x6, %eax
ja 0x2826
leaq 0x2ac7(%rip), %rcx      # 0x5004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb (%r15,%rbp), %al
cmpb $0x20, %al
jne 0x26bb
movq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2160
leaq 0x10(%rsp), %rdi
leaq 0x2d79(%rip), %rsi      # 0x52e6
callq 0x2360
pushq $0x2
popq %rax
movl %eax, %r13d
jmp 0x2826
movsbl (%r15,%rbp), %eax
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x2691
movq 0x30(%rsp), %rax
movl %ecx, 0x80(%rax)
pushq $0x5
jmp 0x2822
movzbl (%r15,%rbp), %eax
cmpq $0x3e, %rax
ja 0x27b0
movabsq $0x5000008400000000, %rcx # imm = 0x5000008400000000
btq %rax, %rcx
jae 0x2761
pushq $0x3
jmp 0x2822
movb (%r15,%rbp), %al
cmpb $0x2f, %al
jne 0x26a3
leaq 0x10(%rsp), %rdi
leaq 0x2a4a(%rip), %rsi      # 0x502c
callq 0x3105
testb %al, %al
je 0x25f4
movq 0x30(%rsp), %rax
movb $0x1, 0x1c(%rax)
leaq 0x10(%rsp), %rdi
leaq 0x2ce6(%rip), %rsi      # 0x52e6
callq 0x2360
pushq $0x5
popq %rax
movl %eax, %r13d
jmp 0x2826
movzbl (%r15,%rbp), %eax
cmpq $0x3f, %rax
ja 0x27c5
movabsq $0x5000008400000000, %rcx # imm = 0x5000008400000000
btq %rax, %rcx
jae 0x2716
pushq $0x2
jmp 0x2822
movsbl (%r15,%rbp), %eax
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0x2658
movq 0x30(%rsp), %rax
movl %ecx, 0x84(%rax)
movb (%r15,%rbp), %al
xorl %r13d, %r13d
cmpb $0xa, %al
sete %r13b
orl $0x6, %r13d
jmp 0x2826
movb (%r15,%rbp), %al
cmpb $0xa, %al
jne 0x26d3
movb $0x1, %r12b
leaq 0x10(%rsp), %rdi
leaq 0x2c65(%rip), %rsi      # 0x52e6
callq 0x2360
pushq $0x7
popq %rax
movl %eax, %r13d
jmp 0x2826
xorl %r13d, %r13d
cmpb $0x2e, %al
sete %r13b
addl $0x5, %r13d
jmp 0x2826
movsbl %al, %esi
leaq 0x10(%rsp), %rdi
callq 0x21c0
pushq $0x4
popq %rax
movl %eax, %r13d
jmp 0x2826
movsbl %al, %esi
leaq 0x10(%rsp), %rdi
callq 0x21c0
pushq $0x1
popq %rax
movl %eax, %r13d
jmp 0x2826
cmpb $0x3a, %al
setne %cl
movl %r12d, %edx
notb %dl
orb %cl, %dl
testb $0x1, %dl
jne 0x2792
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x287e
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x3110
movq %rax, %rdi
leaq 0x2bda(%rip), %rsi      # 0x52e6
callq 0x2360
jmp 0x281d
movabsq $-0x7fffffff00000000, %rcx # imm = 0x8000000100000000
btq %rax, %rcx
jae 0x27c5
movq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2160
xorl %r13d, %r13d
cmpb $0x3f, (%r15,%rbp)
setne %bl
leaq 0x10(%rsp), %rdi
leaq 0x2b96(%rip), %rsi      # 0x52e6
callq 0x2360
movb %bl, %r13b
addl $0x3, %r13d
jmp 0x2826
cmpq $0x20, %rax
jne 0x27b0
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2160
leaq 0x10(%rsp), %rdi
leaq 0x2b64(%rip), %rsi      # 0x52e6
callq 0x2360
pushq $0x4
popq %rax
movl %eax, %r13d
jmp 0x2826
testb $0x1, %r12b
je 0x27da
movb $0x1, %r12b
movsbl %al, %esi
leaq 0x10(%rsp), %rdi
callq 0x21c0
pushq $0x7
popq %rax
movl %eax, %r13d
jmp 0x2826
movsbl %al, %esi
leaq 0x10(%rsp), %rdi
callq 0x21c0
pushq $0x3
popq %rax
movl %eax, %r13d
jmp 0x2826
movsbl %al, %esi
leaq 0x10(%rsp), %rdi
callq 0x21c0
pushq $0x2
popq %rax
movl %eax, %r13d
jmp 0x2826
cmpb $0xd, %al
je 0x281d
movq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3110
cmpq $0x0, 0x8(%rax)
sete %al
movb (%r15,%rbp), %bl
cmpb $0x20, %bl
sete %cl
testb %cl, %al
jne 0x281d
movq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3110
movsbl %bl, %esi
movq %rax, %rdi
callq 0x21c0
xorl %r12d, %r12d
pushq $0x7
popq %rax
movl %eax, %r13d
incq %rbp
jmp 0x2520
andq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rax
movb $0x0, (%rax)
movq 0x30(%rsp), %rax
cmpl $0x1, 0x80(%rax)
je 0x284e
movb $0x1, 0x1c(%rax)
leaq 0x10(%rsp), %rdi
callq 0x2120
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x2320