// File: /workspace/github_cpp_makefile_star2_O3/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 0x40(%rsp), %rax
movq %rdi, %r14
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 0x240a8
leaq 0x18(%rsp), %rax
leaq 0x230(%rsp), %rbx
movq %rbx, %rdi
movq %rax, -0x10(%rax)
callq 0x21710
leaq 0x230(%rsp,%rax), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x215e0
leaq 0xb0(%rsp), %rbx
leaq 0x8(%rsp), %rsi
movl $0x8, %edx
movq %rbx, %rdi
callq 0x22fd0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x23bfa
callq 0x22130
movq 0xb0(%rsp), %rax
movq %r15, 0x78(%rsp)
addq -0x18(%rax), %rbx
movl $0xa, %esi
movq %rbx, %rdi
callq 0x22180
leaq 0xb0(%rsp), %r15
movsbl %al, %edx
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0x23240
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x23470
leaq 0x28(%r14), %rax
movq %r14, 0x28(%rsp)
leaq 0x30(%rsp), %r13
leaq 0x194d9(%rip), %rbp     # 0x3d12b
leaq 0x8(%rsp), %rbx
leaq 0x58(%rsp), %r14
movq %rax, 0x50(%rsp)
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 0x23240
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x23ec4
movl $0x4, %ecx
movq $-0x1, %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x22e90
movq 0x38(%rsp), %rcx
incq %rax
cmpq %rax, %rcx
jb 0x2405d
movq %rax, 0x38(%rsp)
movq 0x30(%rsp), %rcx
movb $0x0, (%rcx,%rax)
cmpq $0x0, 0x38(%rsp)
je 0x23d7d
movl $0x1, %ecx
leaq 0x1be85(%rip), %rsi     # 0x3fb6d
movq %r13, %rdi
xorl %edx, %edx
callq 0x23110
movq %rax, %r12
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x22330
incq %r12
movq $-0x1, %rcx
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x22330
movl $0x4, %ecx
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x23020
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x212d0
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21370
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x23d60
callq 0x22130
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x23c61
callq 0x22130
jmp 0x23c61
leaq 0x18(%rsp), %rax
leaq 0x8(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x1939f(%rip), %rsi     # 0x3d130
leaq 0x193a6(%rip), %rdx     # 0x3d13e
callq 0x215e0
movq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x219b0
movq 0x28(%rsp), %rcx
addq $0x30, %rcx
cmpq %rcx, %rax
je 0x23dca
addq $0x40, %rax
movq %rax, %rdi
callq 0x22dd0
movl %eax, %ebx
jmp 0x23dcc
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x23de0
callq 0x22130
cmpl $0x8000001, %ebx        # imm = 0x8000001
jae 0x240db
testl %ebx, %ebx
je 0x23ec4
movq 0x28(%rsp), %r15
movslq %ebx, %rbx
leaq 0x88(%r15), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22d30
movq 0x78(%rsp), %r12
leaq 0xb0(%rsp), %rdi
callq 0x230e0
subq %rax, %r12
cmpq %rbx, %r12
cmovaeq %rbx, %r12
movq %r12, 0xa8(%r15)
leaq 0xb0(%rsp), %rdi
callq 0x230e0
movq 0xa8(%r15), %rdx
leaq 0x230(%rsp,%rax), %rsi
movq %r14, %rdi
callq 0x21170
cmpq %rbx, 0xa8(%r15)
jae 0x23ec4
movq %rbx, %rdi
callq 0x21050
movq 0x28(%rsp), %r13
movq %rax, %r15
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 0x24075
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21170
addq 0xa8(%r13), %r12
movq %r12, 0xa8(%r13)
cmpq %rbx, %r12
jb 0x23e6e
movq %r15, %rdi
callq 0x22710
movq 0x28(%rsp), %r14
cmpq $0x0, 0x90(%r14)
je 0x24016
leaq 0x68(%rsp), %rax
leaq 0x58(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x19254(%rip), %rsi     # 0x3d13f
leaq 0x19259(%rip), %rdx     # 0x3d14b
callq 0x215e0
leaq 0x19758(%rip), %rcx     # 0x3d656
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
movq %r14, %rsi
callq 0x231c0
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x23f24
callq 0x22130
cmpq $0x0, 0x10(%rsp)
je 0x24002
leaq 0x19215(%rip), %rsi     # 0x3d14c
leaq 0x8(%rsp), %rdi
leaq 0x88(%r14), %rbx
callq 0x213d0
testl %eax, %eax
je 0x23ff7
leaq 0x19217(%rip), %rsi     # 0x3d16e
leaq 0x8(%rsp), %rdi
callq 0x213d0
testl %eax, %eax
je 0x23f7e
leaq 0x19213(%rip), %rsi     # 0x3d17f
leaq 0x8(%rsp), %rdi
callq 0x213d0
testl %eax, %eax
jne 0x24002
leaq 0x80(%rsp), %rdx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rdx)
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
vzeroupper
callq 0x217d0
leaq 0xa0(%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 0x21a10
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x24002
leaq 0x80(%rsp), %rdi
movl $0x3, %edx
movq %rdi, %rsi
callq *%rax
jmp 0x24002
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22960
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x24016
callq 0x22130
movq 0x28de3(%rip), %rsi     # 0x4ce00
leaq 0xb0(%rsp), %rdi
callq 0x22aa0
leaq 0x128(%rsp), %rdi
callq 0x21510
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2404b
callq 0x22130
addq $0x1238, %rsp           # imm = 0x1238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19523(%rip), %rdi     # 0x3d587
leaq 0x19cf2(%rip), %rsi     # 0x3dd5d
movq %rax, %rdx
xorl %eax, %eax
callq 0x22c20
movl $0x8, %edi
callq 0x217b0
movq 0x28efa(%rip), %rcx     # 0x4cf80
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x28eac(%rip), %rsi     # 0x4cf40
movq 0x28f1d(%rip), %rdx     # 0x4cfb8
movq %rax, %rdi
callq 0x23090
jmp 0x2416a
movl $0x8, %edi
callq 0x217b0
movq 0x28ec7(%rip), %rcx     # 0x4cf80
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x28e79(%rip), %rsi     # 0x4cf40
movq 0x28eea(%rip), %rdx     # 0x4cfb8
movq %rax, %rdi
callq 0x23090
jmp 0x2416a
movl $0x8, %edi
callq 0x217b0
movq 0x28f0c(%rip), %rcx     # 0x4cff8
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x28c3e(%rip), %rsi     # 0x4cd38
movq 0x28eb7(%rip), %rdx     # 0x4cfb8
movq %rax, %rdi
callq 0x23090
jmp 0x2416a
jmp 0x24176
jmp 0x241bf
movq %rax, %rbx
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x24134
leaq 0x80(%rsp), %rdi
movl $0x3, %edx
movq %rdi, %rsi
callq *%rax
movq %rbx, %rdi
callq 0x21630
movl $0x8, %edi
callq 0x217b0
movq 0x28b13(%rip), %rcx     # 0x4cc60
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x28c7d(%rip), %rsi     # 0x4cdd8
movq 0x28e56(%rip), %rdx     # 0x4cfb8
movq %rax, %rdi
callq 0x23090
jmp 0x24176
movq %rax, %rbx
callq 0x22ee0
jmp 0x241df
movq %rax, %rdi
callq 0x24985
jmp 0x241f5
jmp 0x241f5
jmp 0x241f5
jmp 0x241f5
movq 0x58(%rsp), %rdi
movq %rax, %rbx
leaq 0x68(%rsp), %rax
jmp 0x241e9
jmp 0x241f5
jmp 0x241f5
jmp 0x241bf
movq 0x8(%rsp), %rdi
movq %rax, %rbx
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x24219
callq 0x22130
jmp 0x24219
jmp 0x241b6
movq %rax, %rbx
jmp 0x24219
jmp 0x241f5
jmp 0x241f5
movq %rax, %rbx
jmp 0x241df
jmp 0x241f5
jmp 0x241f5
movq 0x58(%rsp), %rdi
movq %rax, %rbx
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x241df
callq 0x22130
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x241f8
callq 0x22130
jmp 0x241f8
movq %rax, %rbx
movq 0x28c01(%rip), %rsi     # 0x4ce00
leaq 0xb0(%rsp), %rdi
callq 0x22aa0
leaq 0x128(%rsp), %rdi
callq 0x21510
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2422d
callq 0x22130
movq %rbx, %rdi
callq 0x23160
nop