// File: solaldunckel[P]webserv/srcs/HTTP/Request.cpp
// Function: Request::headers()
int Request::headers() {
  size_t end, last;
  std::string header;
  std::string value;

  while ((end = buffer_.find("\r\n")) != std::string::npos) {
    if (buffer_.find("\r\n") == 0) {
      buffer_.erase(0, end + 2);
      status_ = PREBODY;
      break;
    }
    if ((last = buffer_.find(':', 0)) != std::string::npos) {
      if (last == 0 || buffer_[last - 1] == ' ')
        return 400;
      header = buffer_.substr(0, last);
      value = buffer_.substr(last + 1, end - last - 1);
      if (header == "Host" && headers_.count(header))
        return 400;
      if (header.length() > 1000 || value.length() > 4000)
        return 400;
      headers_[header] = ft::trim_left(ft::trim_right(value, ' '), ' ');
      if (headers_[header].empty())
        headers_.erase(header);
    }
    else
      return 400;
    buffer_.erase(0, end + 2);
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x57b0
leaq -0x60(%rbp), %rdi
callq 0x57b0
movq -0x118(%rbp), %rdi
leaq 0x211f1(%rip), %rsi     # 0x437bb
xorl %eax, %eax
movl %eax, %edx
callq 0x5a70
movq %rax, -0x18(%rbp)
cmpq $-0x1, %rax
je 0x2293c
movq -0x118(%rbp), %rdi
leaq 0x211cc(%rip), %rsi     # 0x437bb
xorl %eax, %eax
movl %eax, %edx
callq 0x5a70
cmpq $0x0, %rax
jne 0x2263f
movq -0x118(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x2, %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x50d0
jmp 0x22618
movq -0x118(%rbp), %rax
movl $0x2, 0x120(%rax)
jmp 0x2293c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x22968
movq -0x118(%rbp), %rdi
movl $0x3a, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x5730
movq %rax, -0x20(%rbp)
cmpq $-0x1, %rax
je 0x2290d
cmpq $0x0, -0x20(%rbp)
je 0x22694
movq -0x118(%rbp), %rdi
movq -0x20(%rbp), %rsi
decq %rsi
callq 0x5a80
movq %rax, -0x120(%rbp)
jmp 0x22685
movq -0x120(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x226a7
movl $0x190, -0x4(%rbp)      # imm = 0x190
movl $0x1, -0x70(%rbp)
jmp 0x2294a
movq -0x118(%rbp), %rsi
movq -0x20(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x90(%rbp), %rdi
callq 0x55a0
jmp 0x226c4
leaq -0x40(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movq %rsi, -0x128(%rbp)
callq 0x56d0
movq -0x128(%rbp), %rdi
callq 0x5aa8
movq -0x118(%rbp), %rsi
movq -0x20(%rbp), %rcx
movq %rcx, %rdx
incq %rdx
movq -0x18(%rbp), %rax
notq %rcx
addq %rax, %rcx
leaq -0xb0(%rbp), %rdi
callq 0x55a0
jmp 0x22710
leaq -0x60(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
movq %rsi, -0x138(%rbp)
callq 0x56d0
movq -0x138(%rbp), %rdi
callq 0x5aa8
leaq 0x20c0a(%rip), %rsi     # 0x43344
leaq -0x40(%rbp), %rdi
callq 0x8ff0
movb %al, -0x129(%rbp)
jmp 0x2274b
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x22757
jmp 0x22797
movq -0x118(%rbp), %rdi
addq $0xc0, %rdi
leaq -0x40(%rbp), %rsi
callq 0x239f0
movq %rax, -0x140(%rbp)
jmp 0x22777
movq -0x140(%rbp), %rax
cmpq $0x0, %rax
je 0x22797
movl $0x190, -0x4(%rbp)      # imm = 0x190
movl $0x1, -0x70(%rbp)
jmp 0x2294a
leaq -0x40(%rbp), %rdi
callq 0x5830
cmpq $0x3e8, %rax            # imm = 0x3E8
ja 0x227b9
leaq -0x60(%rbp), %rdi
callq 0x5830
cmpq $0xfa0, %rax            # imm = 0xFA0
jbe 0x227cc
movl $0x190, -0x4(%rbp)      # imm = 0x190
movl $0x1, -0x70(%rbp)
jmp 0x2294a
leaq -0x110(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x5200
jmp 0x227de
leaq -0xf0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
movl $0x20, %edx
callq 0x41540
jmp 0x227f8
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl $0x20, %edx
callq 0x414d0
jmp 0x22812
movq -0x118(%rbp), %rdi
addq $0xc0, %rdi
leaq -0x40(%rbp), %rsi
callq 0x13cd0
movq %rax, -0x148(%rbp)
jmp 0x22832
movq -0x148(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movq %rsi, -0x158(%rbp)
callq 0x56d0
movq -0x158(%rbp), %rdi
callq 0x5aa8
leaq -0xf0(%rbp), %rdi
callq 0x5aa8
leaq -0x110(%rbp), %rdi
callq 0x5aa8
movq -0x118(%rbp), %rdi
addq $0xc0, %rdi
leaq -0x40(%rbp), %rsi
callq 0x13cd0
movq %rax, -0x150(%rbp)
jmp 0x22890
movq -0x150(%rbp), %rdi
callq 0x5900
testb $0x1, %al
jne 0x228a2
jmp 0x2290b
movq -0x118(%rbp), %rdi
addq $0xc0, %rdi
leaq -0x40(%rbp), %rsi
callq 0x23a50
jmp 0x228bb
jmp 0x2290b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x228fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x228f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x5aa8
leaq -0xf0(%rbp), %rdi
callq 0x5aa8
leaq -0x110(%rbp), %rdi
callq 0x5aa8
jmp 0x22968
jmp 0x2291d
movl $0x190, -0x4(%rbp)      # imm = 0x190
movl $0x1, -0x70(%rbp)
jmp 0x2294a
movq -0x118(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x2, %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x50d0
jmp 0x22937
jmp 0x225bc
movl $0x0, -0x4(%rbp)
movl $0x1, -0x70(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5aa8
leaq -0x40(%rbp), %rdi
callq 0x5aa8
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x5aa8
leaq -0x40(%rbp), %rdi
callq 0x5aa8
movq -0x68(%rbp), %rdi
callq 0x5950
nopw %cs:(%rax,%rax)