// File: /workspace/github_cpp_makefile_star2_O1/alfanick[P]rest-cpp/src/rest/request.cpp
// Function: REST::Request::process()
void Request::process() {
  std::string line;
  bool is_header = true;
  char buffer[BUFFER_SIZE];
  ssize_t request_length;

  // receive data from client
  request_length = recv(handle, buffer, BUFFER_SIZE, 0);

  // handle timeout
  if (request_length <= 0)
    throw HTTP::RequestTimeout();

  // parse each line
  std::istringstream request_stream(buffer);

  // first line is HTTP header
  std::getline(request_stream, line);
  parse_header(line);

  // parse headers
  while (std::getline(request_stream, line)) {
    // next lines are headers, strip them
    line.erase(line.find_last_not_of(" \n\r\t")+1);

    // if line is empty, then content should follow
    if (line.size() == 0) {
      is_header = false;
      break;
    }

    // extract name and value from header
    size_t colon;
    std::string name = line.substr(0, colon = line.find(":"));
    std::string value = line.substr(colon+1);
    value.erase(0, value.find_first_not_of(" \n\r\t"));

    headers.emplace(name, value);
  }

  // TODO: handle long headers (longer than buffer)
  // if has content
  if (!is_header) {
    // assume proper content length
    size_t content_length = header("Content-Length", 0);

    // TODO: config
    if (content_length > 1024 * 1024 * 128)
      throw HTTP::PayloadTooLarge();

    if (content_length > 0) {
      raw.reserve(content_length);
      // read whats left in header
      length = std::min(content_length, (size_t)(request_length - request_stream.tellg()));
      raw.append(buffer + request_stream.tellg(), length);

      if (length < content_length) {
        char* large_buffer = new char[content_length];

        // receive some more
        while (length < content_length) {
          memset(large_buffer, 0, content_length);
          ssize_t buffer_length = recv(handle, large_buffer, content_length, 0);

          if (buffer_length <= 0)
            throw HTTP::RequestTimeout();

          raw.append(large_buffer, buffer_length);
          length += buffer_length;
        }

        delete[] large_buffer;
      }
    }
  }

  // if has some content
  if (!raw.empty()) {
    // try to parse it
    std::string ct = header("Content-Type", "");

    if (!ct.empty()) {
      if (ct == "application/x-www-form-urlencoded") {
        parse_query_string(raw);
      } else
      if (ct == "application/json" || ct == "text/json") {
        try {
          data = nlohmann::json::parse(raw);
        } catch (...) {
          throw HTTP::BadRequest();
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1238, %rsp           # imm = 0x1238
movq %rdi, %r14
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0xc8(%rdi), %edi
leaq 0x230(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0x21500
movq %rax, %r15
testq %rax, %rax
jle 0x240d1
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x230(%rsp), %rbx
movq %rbx, %rdi
callq 0x21710
leaq 0x230(%rsp,%rax), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x215e0
leaq 0xb0(%rsp), %rbx
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0x22fe0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x23c1a
callq 0x22130
movq %r15, 0x78(%rsp)
movq 0xb0(%rsp), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x22180
movsbl %al, %edx
leaq 0xb0(%rsp), %r15
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
callq 0x23250
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x23480
movq %r14, 0x28(%rsp)
leaq 0x28(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0x58(%rsp), %r13
leaq 0x1a4b4(%rip), %rbp     # 0x3e12b
leaq 0x8(%rsp), %rbx
leaq 0x38(%rsp), %r14
movq 0xb0(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x22180
movsbl %al, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x23250
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x23eed
movl $0x4, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq $-0x1, %rdx
callq 0x22e90
incq %rax
movq 0x60(%rsp), %rcx
cmpq %rax, %rcx
jb 0x24086
movq %rax, 0x60(%rsp)
movq 0x58(%rsp), %rcx
movb $0x0, (%rcx,%rax)
cmpq $0x0, 0x60(%rsp)
je 0x23d9d
movl $0x1, %ecx
movq %r13, %rdi
leaq 0x1ce62(%rip), %rsi     # 0x40b6d
xorl %edx, %edx
callq 0x23120
movq %rax, %r12
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x22330
incq %r12
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq $-0x1, %rcx
callq 0x22330
movl $0x4, %ecx
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x23030
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x212d0
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21370
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x23d80
callq 0x22130
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x23c81
callq 0x22130
jmp 0x23c81
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
movq %rax, (%rdi)
leaq 0x1a37f(%rip), %rsi     # 0x3e130
leaq 0x1a386(%rip), %rdx     # 0x3e13e
callq 0x215e0
leaq 0x8(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x219b0
movq 0x28(%rsp), %rcx
addq $0x30, %rcx
cmpq %rcx, %rax
je 0x23dea
addq $0x40, %rax
movq %rax, %rdi
callq 0x22dd0
movl %eax, %ebx
jmp 0x23dec
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x23e00
callq 0x22130
cmpl $0x8000001, %ebx        # imm = 0x8000001
jae 0x24104
testl %ebx, %ebx
je 0x23eed
movslq %ebx, %rbx
movq 0x28(%rsp), %r15
leaq 0x88(%r15), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22d30
movq 0x78(%rsp), %r12
leaq 0xb0(%rsp), %rdi
callq 0x230f0
subq %rax, %r12
cmpq %rbx, %r12
cmovaeq %rbx, %r12
movq %r12, 0xa8(%r15)
leaq 0xb0(%rsp), %rdi
callq 0x230f0
leaq 0x230(%rsp,%rax), %rsi
movq 0xa8(%r15), %rdx
movq %r14, %rdi
callq 0x21170
cmpq %rbx, 0xa8(%r15)
jae 0x23eed
movq %rbx, %rdi
callq 0x21050
movq %rax, %r15
movq 0x28(%rsp), %r13
cmpq %rbx, 0xa8(%r13)
jae 0x23ee5
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x21910
movl 0xc8(%r13), %edi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x21500
movq %rax, %r12
testq %rax, %rax
jle 0x2409e
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21170
addq 0xa8(%r13), %r12
movq %r12, 0xa8(%r13)
cmpq %rbx, %r12
jb 0x23e97
movq %r15, %rdi
callq 0x22710
movq 0x28(%rsp), %r14
cmpq $0x0, 0x90(%r14)
je 0x2403f
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
movq %rax, (%rdi)
leaq 0x1a22b(%rip), %rsi     # 0x3e13f
leaq 0x1a230(%rip), %rdx     # 0x3e14b
callq 0x215e0
leaq 0x1a72f(%rip), %rcx     # 0x3e656
leaq 0x8(%rsp), %rdi
leaq 0x38(%rsp), %rdx
movq %r14, %rsi
callq 0x231d0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x23f4d
callq 0x22130
cmpq $0x0, 0x10(%rsp)
je 0x2402b
leaq 0x88(%r14), %rbx
leaq 0x1a1e5(%rip), %rsi     # 0x3e14c
leaq 0x8(%rsp), %rdi
callq 0x213d0
testl %eax, %eax
je 0x24020
leaq 0x1a1ee(%rip), %rsi     # 0x3e16e
leaq 0x8(%rsp), %rdi
callq 0x213d0
testl %eax, %eax
je 0x23fa7
leaq 0x1a1ea(%rip), %rsi     # 0x3e17f
leaq 0x8(%rsp), %rdi
callq 0x213d0
testl %eax, %eax
jne 0x2402b
vxorps %xmm0, %xmm0, %xmm0
leaq 0x80(%rsp), %rdx
vmovups %ymm0, (%rdx)
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
vzeroupper
callq 0x217d0
movb 0xb0(%r14), %al
leaq 0xa0(%rsp), %rdi
movb (%rdi), %cl
movb %cl, 0xb0(%r14)
movb %al, (%rdi)
movq 0xb8(%r14), %rax
movq 0x8(%rdi), %rcx
movq %rcx, 0xb8(%r14)
movq %rax, 0x8(%rdi)
callq 0x21a10
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x2402b
leaq 0x80(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x2402b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22960
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2403f
callq 0x22130
movq 0x29dba(%rip), %rsi     # 0x4de00
leaq 0xb0(%rsp), %rdi
callq 0x22aa0
leaq 0x128(%rsp), %rdi
callq 0x21510
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x24074
callq 0x22130
addq $0x1238, %rsp           # imm = 0x1238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a4fa(%rip), %rdi     # 0x3e587
leaq 0x1acc9(%rip), %rsi     # 0x3ed5d
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c20
movl $0x8, %edi
callq 0x217b0
movq 0x29ed1(%rip), %rcx     # 0x4df80
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x29e83(%rip), %rsi     # 0x4df40
movq 0x29ef4(%rip), %rdx     # 0x4dfb8
movq %rax, %rdi
callq 0x230a0
jmp 0x24193
movl $0x8, %edi
callq 0x217b0
movq 0x29e9e(%rip), %rcx     # 0x4df80
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x29e50(%rip), %rsi     # 0x4df40
movq 0x29ec1(%rip), %rdx     # 0x4dfb8
movq %rax, %rdi
callq 0x230a0
jmp 0x24193
movl $0x8, %edi
callq 0x217b0
movq 0x29ee3(%rip), %rcx     # 0x4dff8
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x29c15(%rip), %rsi     # 0x4dd38
movq 0x29e8e(%rip), %rdx     # 0x4dfb8
movq %rax, %rdi
callq 0x230a0
jmp 0x24193
jmp 0x2419f
jmp 0x241e8
movq %rax, %rbx
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x2415d
leaq 0x80(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0x21630
movl $0x8, %edi
callq 0x217b0
movq 0x29aea(%rip), %rcx     # 0x4dc60
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x29c54(%rip), %rsi     # 0x4ddd8
movq 0x29e2d(%rip), %rdx     # 0x4dfb8
movq %rax, %rdi
callq 0x230a0
jmp 0x2419f
movq %rax, %rbx
callq 0x22ee0
jmp 0x24208
movq %rax, %rdi
callq 0x249b5
jmp 0x2421e
jmp 0x2421e
jmp 0x2421e
jmp 0x2421e
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
jmp 0x24212
jmp 0x2421e
jmp 0x2421e
jmp 0x241e8
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x24242
callq 0x22130
jmp 0x24242
jmp 0x241df
movq %rax, %rbx
jmp 0x24242
jmp 0x2421e
jmp 0x2421e
movq %rax, %rbx
jmp 0x24208
jmp 0x2421e
jmp 0x2421e
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x24208
callq 0x22130
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x24221
callq 0x22130
jmp 0x24221
movq %rax, %rbx
movq 0x29bd8(%rip), %rsi     # 0x4de00
leaq 0xb0(%rsp), %rdi
callq 0x22aa0
leaq 0x128(%rsp), %rdi
callq 0x21510
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x24256
callq 0x22130
movq %rbx, %rdi
callq 0x23170