// File: /workspace/github_cpp_makefile_star2_O2/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
leaq 0x70(%rsp), %rax
movq %rdi, %rbx
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0xc8(%rdi), %edi
leaq 0x230(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0x30870
movq %rax, %r14
testq %rax, %rax
jle 0x34386
leaq 0x10(%rsp), %rdi
leaq 0x230(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x30a10
leaq 0xb0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x33290
leaq 0x10(%rsp), %rdi
movq %r14, 0x30(%rsp)
callq 0x33b38
leaq 0xb0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x30360
leaq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0x33a30
leaq 0x28(%rbx), %rax
leaq 0x60(%rsp), %r13
leaq 0x1611b(%rip), %rbp     # 0x4a132
movq %rbx, 0x8(%rsp)
movq %rax, 0x38(%rsp)
pushq $-0x1
popq %r14
leaq 0x10(%rsp), %rbx
leaq 0x40(%rsp), %r12
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
callq 0x30360
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3420f
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x33610
leaq 0x1(%rax), %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x304c0
cmpq $0x0, 0x68(%rsp)
je 0x340ed
leaq 0x18a20(%rip), %rsi     # 0x4ca9d
movq %r13, %rdi
xorl %edx, %edx
callq 0x33aa0
movq %rax, %r15
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x31ea0
incq %r15
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x31ea0
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x30f80
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x304c0
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x305d0
movq %r12, %rdi
callq 0x33b38
movq %rbx, %rdi
callq 0x33b38
jmp 0x3402f
leaq 0x16043(%rip), %rsi     # 0x4a137
leaq 0x10(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x30a10
leaq 0x40(%rsp), %rdx
andl $0x0, (%rdx)
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x33ad0
leaq 0x10(%rsp), %rdi
movl %eax, %ebx
callq 0x33b38
cmpl $0x8000001, %ebx        # imm = 0x8000001
jae 0x343b4
testl %ebx, %ebx
je 0x3420f
movq 0x8(%rsp), %r15
movslq %ebx, %rbx
leaq 0x88(%r15), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x32ec0
movq 0x30(%rsp), %r12
leaq 0xb0(%rsp), %rdi
callq 0x33420
subq %rax, %r12
cmpq %rbx, %r12
cmovaeq %rbx, %r12
movq %r12, 0xa8(%r15)
leaq 0xb0(%rsp), %rdi
callq 0x33420
movq 0xa8(%r15), %rdx
leaq 0x230(%rsp,%rax), %rsi
movq %r14, %rdi
callq 0x30240
movq 0xa8(%r15), %r12
cmpq %rbx, %r12
jae 0x3420f
movq %rbx, %rdi
callq 0x30060
movq 0x8(%rsp), %r13
movq %rax, %r15
cmpq %rbx, %r12
jae 0x34207
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x30ec0
movl 0xc8(%r13), %edi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x30870
movq %rax, %r12
testq %rax, %rax
jle 0x34355
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x30240
addq 0xa8(%r13), %r12
movq %r12, 0xa8(%r13)
jmp 0x341b7
movq %r15, %rdi
callq 0x324f0
movq 0x8(%rsp), %r14
cmpq $0x0, 0x90(%r14)
je 0x3432c
leaq 0x15f1d(%rip), %rsi     # 0x4a146
leaq 0x40(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x30a10
leaq 0x163e7(%rip), %rcx     # 0x4a626
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %r14, %rsi
callq 0x335f0
leaq 0x40(%rsp), %rdi
callq 0x33b38
cmpq $0x0, 0x18(%rsp)
je 0x34322
leaq 0x15ee5(%rip), %rsi     # 0x4a153
leaq 0x10(%rsp), %rdi
leaq 0x88(%r14), %rbx
callq 0x33ae0
testb %al, %al
je 0x34293
movq %r14, %rdi
movq %rbx, %rsi
callq 0x328b0
jmp 0x34322
leaq 0x15edb(%rip), %rsi     # 0x4a175
leaq 0x10(%rsp), %rdi
callq 0x33ae0
testb %al, %al
jne 0x342bd
leaq 0x15ed7(%rip), %rsi     # 0x4a186
leaq 0x10(%rsp), %rdi
callq 0x33ae0
testb %al, %al
je 0x34322
leaq 0x90(%rsp), %rdx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rdx)
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
vzeroupper
callq 0x30cb0
leaq 0x80(%rsp), %rdi
movb 0xb0(%r14), %al
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 0x310a0
leaq 0x90(%rsp), %rdi
callq 0x31390
leaq 0x10(%rsp), %rdi
callq 0x33b38
leaq 0xb0(%rsp), %rdi
callq 0x30be0
leaq 0x60(%rsp), %rdi
callq 0x33b38
addq $0x1238, %rsp           # imm = 0x1238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x30c90
movq 0x28c14(%rip), %rcx     # 0x5cf78
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x28bbe(%rip), %rsi     # 0x5cf30
movq 0x28c37(%rip), %rdx     # 0x5cfb0
movq %rax, %rdi
callq 0x333b0
jmp 0x34426
pushq $0x8
popq %rdi
callq 0x30c90
movq 0x28be3(%rip), %rcx     # 0x5cf78
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x28b8d(%rip), %rsi     # 0x5cf30
movq 0x28c06(%rip), %rdx     # 0x5cfb0
movq %rax, %rdi
callq 0x333b0
jmp 0x34426
pushq $0x8
popq %rdi
callq 0x30c90
movq 0x28c2d(%rip), %rcx     # 0x5cff0
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x2896f(%rip), %rsi     # 0x5cd40
movq 0x28bd8(%rip), %rdx     # 0x5cfb0
movq %rax, %rdi
callq 0x333b0
jmp 0x34426
leaq 0x90(%rsp), %rdi
movq %rax, %rbx
callq 0x31390
movq %rbx, %rdi
callq 0x30a60
pushq $0x8
popq %rdi
callq 0x30c90
movq 0x2885f(%rip), %rcx     # 0x5cc68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x289c9(%rip), %rsi     # 0x5cde0
movq 0x28b92(%rip), %rdx     # 0x5cfb0
movq %rax, %rdi
callq 0x333b0
movq %rax, %rbx
callq 0x33100
jmp 0x34482
movq %rax, %rdi
callq 0x34aa2
jmp 0x3448e
jmp 0x34470
jmp 0x3448e
jmp 0x34470
jmp 0x3448e
jmp 0x3448e
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
jmp 0x34487
jmp 0x3448e
jmp 0x3448e
leaq 0x10(%rsp), %rdi
movq %rax, %rbx
callq 0x33b38
jmp 0x3449e
jmp 0x34465
jmp 0x3448e
movq %rax, %rbx
jmp 0x3449e
jmp 0x3448e
jmp 0x3448e
jmp 0x3448e
movq %rax, %rbx
jmp 0x34482
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
callq 0x33b38
leaq 0x10(%rsp), %rdi
callq 0x33b38
jmp 0x34491
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x30be0
leaq 0x60(%rsp), %rdi
callq 0x33b38
movq %rbx, %rdi
callq 0x33510