// File: tylerkaraszewski[P]Argyle/FilenameResolver.cpp
// Function: FilenameResolver::resolve(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Config const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, long*)
string FilenameResolver::resolve(const string& webPath, const Config& config, string& redirectPath, time_t* mtime)
{
  string basePath = config.getString(Config::S_KEY_BASE_PATH);

  if (mtime != NULL)
  {
    *mtime = 0; // We'll set this to something useful later if we can. Setting it to 0 implies "N/A".
  }

  bool needToRedirectIfDir = false;
  // Trim trailing slashes.
  string processedWebPath = webPath.substr(0, webPath.find_last_not_of('/') + 1);
  if (processedWebPath.size() == webPath.size())
  {
    // Nothing was stripped, indicating if a directory, it was requested like a file.
    needToRedirectIfDir = true;
  }
  string fullPath = basePath + processedWebPath;

  // Look our path to see if it exists, and if so, what we should do with it.
  struct stat fileStat;
  if(stat(fullPath.c_str(), &fileStat) == -1)
  {
    fullPath = "";
  }

  // Is it a directory?
  else if (fileStat.st_mode & S_IFDIR)
  {
    if (needToRedirectIfDir)
    {
      redirectPath = Utils::UriEscape(webPath + "/");
      fullPath = "";
    }
    else
    {
      fullPath = resolveDir(processedWebPath, config, mtime);
    }
  }

  // Is it something besides a normal file?
  else if (!(fileStat.st_mode & S_IFREG))
  {
    fullPath = "";
  }

  // Report our modified time if the caller gave us a place to put it (and if we're not operating on a directory)
  // If we are looking at a directory, resolveDir should have already set this if it resolved to a particular file.
  // If it's a 404, we want to leave it blank anyway.
  if ((mtime != NULL) && !(fileStat.st_mode & S_IFDIR))
  {
    *mtime = fileStat.st_mtime;
  }

  // Normal files need no special handling.
  return fullPath;
}

// Assembly:
subq $0x198, %rsp            # imm = 0x198
movq %rdi, 0x18(%rsp)
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x190(%rsp)
movq %rsi, 0x188(%rsp)
movq %rdx, 0x180(%rsp)
movq %rcx, 0x178(%rsp)
movq %r8, 0x170(%rsp)
movq 0x180(%rsp), %rsi
leaq 0x150(%rsp), %rdi
leaq 0xb5c5(%rip), %rdx      # 0x194f8
callq 0x11c60
cmpq $0x0, 0x170(%rsp)
je 0xdf52
movq 0x170(%rsp), %rax
movq $0x0, (%rax)
movb $0x0, 0x14f(%rsp)
movq 0x188(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x4270
movq 0x10(%rsp), %rsi
movq %rax, %rcx
incq %rcx
xorl %eax, %eax
movl %eax, %edx
leaq 0x128(%rsp), %rdi
callq 0x43d0
jmp 0xdf96
leaq 0x128(%rsp), %rdi
callq 0x42a0
movq %rax, 0x8(%rsp)
movq 0x188(%rsp), %rdi
callq 0x42a0
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jne 0xdfe5
movb $0x1, 0x14f(%rsp)
jmp 0xdfe5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
jmp 0xe214
movq 0x18(%rsp), %rdi
movb $0x0, 0x11b(%rsp)
leaq 0x150(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0xeb30
jmp 0xe009
movq 0x18(%rsp), %rdi
callq 0x4190
movq %rax, %rdi
leaq 0x88(%rsp), %rsi
callq 0x4370
cmpl $-0x1, %eax
jne 0xe072
movq 0x18(%rsp), %rdi
leaq 0x510c(%rip), %rsi      # 0x13140
callq 0x4540
jmp 0xe03b
jmp 0xe18b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
jmp 0xe207
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
jmp 0xe1fd
movl 0xa0(%rsp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0xe161
testb $0x1, 0x14f(%rsp)
je 0xe122
movq 0x188(%rsp), %rsi
leaq 0x521c(%rip), %rdx      # 0x132c0
leaq 0x48(%rsp), %rdi
callq 0xebc0
jmp 0xe0b0
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x11030
jmp 0xe0c1
movq 0x178(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movq %rsi, (%rsp)
callq 0x44d0
movq (%rsp), %rdi
callq 0x4788
leaq 0x48(%rsp), %rdi
callq 0x4788
movq 0x18(%rsp), %rdi
leaq 0x504a(%rip), %rsi      # 0x13140
callq 0x4540
jmp 0xe0fd
jmp 0xe15f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x4788
jmp 0xe1fd
movq 0x180(%rsp), %rdx
movq 0x170(%rsp), %rcx
leaq 0x28(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xe230
jmp 0xe146
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x44d0
leaq 0x28(%rsp), %rdi
callq 0x4788
jmp 0xe189
movl 0xa0(%rsp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0xe187
movq 0x18(%rsp), %rdi
leaq 0x4fc2(%rip), %rsi      # 0x13140
callq 0x4540
jmp 0xe185
jmp 0xe187
jmp 0xe189
jmp 0xe18b
cmpq $0x0, 0x170(%rsp)
je 0xe1ba
movl 0xa0(%rsp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
jne 0xe1ba
movq 0xe0(%rsp), %rcx
movq 0x170(%rsp), %rax
movq %rcx, (%rax)
movb $0x1, 0x11b(%rsp)
testb $0x1, 0x11b(%rsp)
jne 0xe1d6
movq 0x18(%rsp), %rdi
callq 0x4788
leaq 0x128(%rsp), %rdi
callq 0x4788
leaq 0x150(%rsp), %rdi
callq 0x4788
movq 0x20(%rsp), %rax
addq $0x198, %rsp            # imm = 0x198
retq
movq 0x18(%rsp), %rdi
callq 0x4788
leaq 0x128(%rsp), %rdi
callq 0x4788
leaq 0x150(%rsp), %rdi
callq 0x4788
movq 0x120(%rsp), %rdi
callq 0x46b0
nop