// File: /workspace/github_cpp_makefile_star2_O2/solaldunckel[P]webserv/srcs/HTTP/Response.cpp
// Function: Response::handleMethods()
int Response::handleMethods() {
  std::string &method = config_.getMethod();
  std::string path;

  if (method == "GET" || method == "HEAD") {
    if (file_.is_directory()) {
      std::string index = file_.find_index(config_.getIndexes());
      if (index.length()) {
        redirect_ = true;
        redirect_target_ = ft::unique_char("/" + config_.getRequestTarget() + "/" + index);
        return 200;
      }
      else if (!config_.getAutoindex())
        return 404;
    }

    path = file_.getPath();
    if (!file_.is_directory()) {
      if (!file_.exists())
        return 404;
      file_.parse_match();
      std::vector<std::string> &matches = file_.getMatches();

      if (!config_.getHeader("Accept-Language").empty()) {
        if (localization(matches))
          file_.set_path(path.substr(0, path.find_last_of("/") + 1) + matches.front(), true);
      }
      if (!config_.getHeader("Accept-Charset").empty()) {
        charset_ = accept_charset(matches);
        file_.set_path(path.substr(0, path.find_last_of("/") + 1) + matches.front(), true);
      }

      if (!file_.open())
        return 403;

      headers_["Last-Modified"] = file_.last_modified();
    }
  }

  if (isCGI(file_.getMimeExtension())) {
    CGI cgi(file_, config_, config_.getHeaders(), config_.getBody());
    cgi.init(worker_id_);
    if ((status_code_ = cgi.execute()) > 400)
      return status_code_;
    cgi.parseHeaders(headers_);
    body_ = cgi.getBody();
    headers_["Content-Length"] = ft::to_string(body_.length());
    return status_code_;
  }

  if (method == "PUT" || method == "POST") {
    std::string path = config_.getUri() + "/" + config_.getTarget();

    if (!config_.getUpload().empty()) {
      File dir(config_.getRoot() + "/" + config_.getUpload());

      path = config_.getUri() + "/" + config_.getUpload() + "/" + config_.getTarget();
      if (dir.exists() && !dir.is_directory()) {
        dir.unlink();
      }

      if (!dir.exists()) {
        if (mkdir(dir.getPath().c_str(), 0755) == -1)
          Log.print(DEBUG, "mkdir : " + std::string(strerror(errno)), RED, true);
      }
      file_.set_path(dir.getPath() + "/" + config_.getTarget());
    }
    headers_["Location"] = ft::unique_char(path);
  }

  return (this->*(Response::methods_[method]))();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x8e4e
movq %rax, %r14
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xbb37(%rip), %rsi      # 0x1c73a
movq %r14, %rdi
callq 0x610b
testb %al, %al
jne 0x10c26
leaq 0xbb28(%rip), %rsi      # 0x1c73e
movq %r14, %rdi
callq 0x610b
testb %al, %al
je 0x10d55
leaq 0x8(%rbx), %r15
movq %r15, %rdi
callq 0x17768
testb %al, %al
je 0x10d30
movq (%rbx), %rdi
callq 0x8e26
leaq 0x220(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x17828
cmpq $0x0, 0x228(%rsp)
je 0x10d0a
movb $0x1, 0x100(%rbx)
movq (%rbx), %rdi
callq 0x8da4
leaq 0xb904(%rip), %rsi      # 0x1c57e
leaq 0x40(%rsp), %rdi
movq %rax, %rdx
callq 0x52b1
leaq 0xb8f0(%rip), %rdx      # 0x1c57e
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x5292
leaq 0x200(%rsp), %rdi
movq %rsp, %rsi
leaq 0x220(%rsp), %rdx
callq 0x5273
leaq 0x20(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x1b4d0
leaq 0x108(%rbx), %rdi
leaq 0x20(%rsp), %r12
movq %r12, %rsi
callq 0x4560
movq %r12, %rdi
callq 0x4898
leaq 0x200(%rsp), %rdi
callq 0x4898
movq %rsp, %rdi
callq 0x4898
leaq 0x40(%rsp), %rdi
callq 0x4898
movl $0xc8, %ebp
xorl %r12d, %r12d
jmp 0x10d1a
movq (%rbx), %rdi
callq 0x8e10
movl %eax, %r12d
movl $0x194, %ebp            # imm = 0x194
leaq 0x220(%rsp), %rdi
callq 0x4898
testb %r12b, %r12b
je 0x11496
movq %r15, %rdi
callq 0x17e90
leaq 0x60(%rsp), %rdi
movq %rax, %rsi
callq 0x4240
movq %r15, %rdi
callq 0x17768
testb %al, %al
je 0x11243
leaq 0x8(%rbx), %r15
movq %r15, %rdi
callq 0x17be8
leaq 0x160(%rsp), %rdi
movq %rax, %rsi
callq 0x45e0
leaq 0x160(%rsp), %rsi
movq %rbx, %rdi
callq 0x10148
movl %eax, %ebp
leaq 0x160(%rsp), %rdi
callq 0x4898
testb %bpl, %bpl
je 0x10e96
movq (%rbx), %r14
movq %r14, %rdi
callq 0x8e66
movq %rax, %r12
movq (%rbx), %rdi
callq 0x8e44
leaq 0x220(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0xca0c
movl 0xac(%rbx), %esi
leaq 0x220(%rsp), %rdi
callq 0xcbfc
leaq 0x220(%rsp), %rdi
callq 0xd0be
movl %eax, %ebp
movl %eax, 0xb8(%rbx)
cmpl $0x190, %eax            # imm = 0x190
jg 0x10e84
leaq 0x190(%rbx), %r14
leaq 0x220(%rsp), %rdi
movq %r14, %rsi
callq 0xdb2a
leaq 0x220(%rsp), %rdi
callq 0xdd34
leaq 0xe0(%rbx), %rdi
movq %rax, %rsi
callq 0x4240
movq 0xe8(%rbx), %rsi
leaq 0x20(%rsp), %rdi
callq 0xddbc
leaq 0xb775(%rip), %rsi      # 0x1c5bb
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
callq 0x5328
movq %rsp, %rsi
movq %r14, %rdi
callq 0xdd56
leaq 0x20(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x4560
movq %rsp, %rdi
callq 0x4898
movq %r14, %rdi
callq 0x4898
movl 0xb8(%rbx), %ebp
leaq 0x220(%rsp), %rdi
callq 0xd008
jmp 0x11496
leaq 0xb899(%rip), %rsi      # 0x1c736
movq %r14, %rdi
callq 0x610b
testb %al, %al
jne 0x10ec0
leaq 0xb6c9(%rip), %rsi      # 0x1c579
movq %r14, %rdi
callq 0x610b
testb %al, %al
je 0x11214
movq (%rbx), %rdi
callq 0x8de8
leaq 0xb6af(%rip), %rdx      # 0x1c57e
leaq 0x220(%rsp), %rdi
movq %rax, %rsi
callq 0x60d3
movq (%rbx), %rdi
callq 0x8d9e
leaq 0x20(%rsp), %rdi
leaq 0x220(%rsp), %rsi
movq %rax, %rdx
callq 0x5273
leaq 0x220(%rsp), %rdi
callq 0x4898
movq (%rbx), %rdi
callq 0x8e1c
cmpq $0x0, 0x8(%rax)
je 0x11190
movq (%rbx), %rdi
callq 0x8dd4
leaq 0xb653(%rip), %rdx      # 0x1c57e
movq %rsp, %rdi
movq %rax, %rsi
callq 0x60d3
movq (%rbx), %rdi
callq 0x8e1c
leaq 0x140(%rsp), %rdi
movq %rsp, %rsi
movq %rax, %rdx
callq 0x5273
leaq 0x220(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x1651a
leaq 0x140(%rsp), %rdi
callq 0x4898
movq %rsp, %rdi
callq 0x4898
movq (%rbx), %rdi
callq 0x8de8
leaq 0xb5f4(%rip), %rdx      # 0x1c57e
leaq 0x120(%rsp), %rdi
movq %rax, %rsi
callq 0x60d3
movq (%rbx), %rdi
callq 0x8e1c
leaq 0x80(%rsp), %rdi
leaq 0x120(%rsp), %rsi
movq %rax, %rdx
callq 0x5273
leaq 0xb5bd(%rip), %rdx      # 0x1c57e
leaq 0x40(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x5292
movq (%rbx), %rdi
callq 0x8d9e
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
movq %rax, %rdx
callq 0x5273
leaq 0x20(%rsp), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0x4560
movq %r12, %rdi
callq 0x4898
leaq 0x40(%rsp), %rdi
callq 0x4898
leaq 0x80(%rsp), %rdi
callq 0x4898
leaq 0x120(%rsp), %rdi
callq 0x4898
leaq 0x220(%rsp), %rdi
callq 0x16fcc
testb %al, %al
je 0x11056
leaq 0x220(%rsp), %rdi
callq 0x17768
testb %al, %al
jne 0x11056
leaq 0x220(%rsp), %rdi
callq 0x16e72
leaq 0x220(%rsp), %rdi
callq 0x16fcc
testb %al, %al
jne 0x11122
leaq 0x220(%rsp), %rdi
callq 0x17e90
movq (%rax), %rdi
movl $0x1ed, %esi            # imm = 0x1ED
callq 0x41b0
cmpl $-0x1, %eax
jne 0x11122
callq 0x4040
movl (%rax), %edi
callq 0x4130
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
movq %rax, %rsi
callq 0x5328
leaq 0xb7aa(%rip), %rsi      # 0x1c85b
leaq 0x100(%rsp), %rdi
movq %rsp, %rdx
callq 0x8234
leaq 0xaf58(%rip), %rsi      # 0x1c020
leaq 0xe0(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x5328
leaq 0x1a404(%rip), %rdi     # 0x2b4e8
pushq $0x2
popq %rsi
leaq 0x100(%rsp), %rdx
leaq 0xe0(%rsp), %rcx
pushq $0x1
popq %r8
callq 0x18e1e
leaq 0xe0(%rsp), %rdi
callq 0x4898
leaq 0x100(%rsp), %rdi
callq 0x4898
movq %rsp, %rdi
callq 0x4898
leaq 0x220(%rsp), %rdi
callq 0x17e90
leaq 0xb448(%rip), %rdx      # 0x1c57e
movq %rsp, %rdi
movq %rax, %rsi
callq 0x60d3
movq (%rbx), %rdi
callq 0x8d9e
leaq 0xc0(%rsp), %rdi
movq %rsp, %rsi
movq %rax, %rdx
callq 0x5273
leaq 0xc0(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x16610
leaq 0xc0(%rsp), %rdi
callq 0x4898
movq %rsp, %rdi
callq 0x4898
leaq 0x220(%rsp), %rdi
callq 0x1668a
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x45e0
leaq 0x220(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b4d0
leaq 0xb6a6(%rip), %rsi      # 0x1c864
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
callq 0x5328
leaq 0x190(%rbx), %rdi
movq %rsp, %rsi
callq 0xdd56
leaq 0x220(%rsp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x4560
movq %rsp, %rdi
callq 0x4898
movq %r15, %rdi
callq 0x4898
leaq 0xa0(%rsp), %rdi
callq 0x4898
leaq 0x20(%rsp), %rdi
callq 0x4898
leaq 0x160(%rbx), %rdi
movq %r14, %rsi
callq 0x12904
movq (%rax), %rcx
addq 0x8(%rax), %rbx
testb $0x1, %cl
je 0x11237
movq (%rbx), %rax
movq -0x1(%rax,%rcx), %rcx
movq %rbx, %rdi
callq *%rcx
movl %eax, %ebp
jmp 0x11496
movq %r15, %rdi
callq 0x16fcc
movl $0x194, %ebp            # imm = 0x194
testb %al, %al
je 0x11496
movq %r15, %rdi
callq 0x17bee
movq %r15, %rdi
callq 0x17e8a
movq %rax, %r12
movq (%rbx), %r13
leaq 0xb5b9(%rip), %rsi      # 0x1c82e
leaq 0x1e0(%rsp), %rdi
leaq 0x220(%rsp), %rdx
callq 0x5328
leaq 0x1e0(%rsp), %rsi
movq %r13, %rdi
callq 0x8e56
movq 0x8(%rax), %r13
leaq 0x1e0(%rsp), %rdi
callq 0x4898
testq %r13, %r13
je 0x11336
movq %rbx, %rdi
movq %r12, %rsi
callq 0x11b2c
testb %al, %al
je 0x11336
leaq 0xb2b4(%rip), %rsi      # 0x1c57e
leaq 0x60(%rsp), %r13
pushq $-0x1
popq %rdx
movq %r13, %rdi
callq 0x4430
leaq 0x1(%rax), %rcx
leaq 0x220(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x44a0
movq (%r12), %rdx
leaq 0x1c0(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x5273
leaq 0x1c0(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x16610
leaq 0x1c0(%rsp), %rdi
callq 0x4898
leaq 0x220(%rsp), %rdi
callq 0x4898
movq (%rbx), %r13
leaq 0xb4fe(%rip), %rsi      # 0x1c83e
leaq 0x1a0(%rsp), %rdi
leaq 0x220(%rsp), %rdx
callq 0x5328
leaq 0x1a0(%rsp), %rsi
movq %r13, %rdi
callq 0x8e56
movq 0x8(%rax), %r13
leaq 0x1a0(%rsp), %rdi
callq 0x4898
testq %r13, %r13
je 0x11424
leaq 0x220(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x11f82
leaq 0x140(%rbx), %rdi
leaq 0x220(%rsp), %r13
movq %r13, %rsi
callq 0x4560
movq %r13, %rdi
callq 0x4898
leaq 0xb1c6(%rip), %rsi      # 0x1c57e
leaq 0x60(%rsp), %r13
pushq $-0x1
popq %rdx
movq %r13, %rdi
callq 0x4430
leaq 0x1(%rax), %rcx
leaq 0x220(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x44a0
movq (%r12), %rdx
leaq 0x180(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x5273
leaq 0x180(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x16610
leaq 0x180(%rsp), %rdi
callq 0x4898
leaq 0x220(%rsp), %rdi
callq 0x4898
movq %r15, %rdi
xorl %esi, %esi
callq 0x16a60
movl $0x193, %ebp            # imm = 0x193
testb %al, %al
je 0x11496
leaq 0x220(%rsp), %rdi
movq %r15, %rsi
callq 0x177be
leaq 0xb3ff(%rip), %rsi      # 0x1c84d
leaq 0x20(%rsp), %rdi
movq %rsp, %rdx
callq 0x5328
leaq 0x190(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0xdd56
leaq 0x220(%rsp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x4560
leaq 0x20(%rsp), %rdi
callq 0x4898
movq %r15, %rdi
callq 0x4898
jmp 0x10d55
leaq 0x60(%rsp), %rdi
callq 0x4898
movl %ebp, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x4898
jmp 0x114c9
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
jmp 0x1156a
jmp 0x11613
jmp 0x11665
movq %rax, %rbx
leaq 0x1c0(%rsp), %rdi
jmp 0x1160c
jmp 0x1167c
jmp 0x116b1
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x1160c
jmp 0x1167c
jmp 0x116b1
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
jmp 0x1160c
jmp 0x1167c
jmp 0x116b1
jmp 0x116b1
movq %rax, %rbx
leaq 0x1a0(%rsp), %rdi
jmp 0x11694
jmp 0x116b1
movq %rax, %rbx
leaq 0x1e0(%rsp), %rdi
jmp 0x11694
jmp 0x116b1
jmp 0x1167c
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x4898
jmp 0x11616
movq %rax, %rbx
jmp 0x1162a
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x4898
jmp 0x11650
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4898
jmp 0x115a1
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4898
jmp 0x116a2
jmp 0x1169f
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4898
jmp 0x115c5
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
callq 0x4898
jmp 0x115d7
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x11653
jmp 0x1165a
jmp 0x1165a
movq %rax, %rbx
leaq 0x200(%rsp), %rdi
callq 0x4898
jmp 0x115fa
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4898
jmp 0x11607
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4898
jmp 0x1167f
movq %rax, %rbx
movq %rsp, %rdi
jmp 0x11644
jmp 0x11665
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4898
leaq 0x80(%rsp), %rdi
callq 0x4898
jmp 0x1163c
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x4898
jmp 0x11668
jmp 0x11665
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4898
jmp 0x11675
movq %rax, %rbx
jmp 0x11675
jmp 0x116b1
jmp 0x1167c
jmp 0x116b1
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
callq 0x1668a
leaq 0x20(%rsp), %rdi
jmp 0x11694
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
jmp 0x11694
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x4898
jmp 0x116b4
jmp 0x116b1
jmp 0x116b1
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
callq 0xd008
jmp 0x116b4
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4898
movq %rbx, %rdi
callq 0x4750