* Handle an http-connection * Returns false if the connection should be closed */
| 507 | * Returns false if the connection should be closed |
| 508 | */ |
| 509 | static bool HandleConnection(Server* server, Connection* connection) |
| 510 | { |
| 511 | int total_recv = 0; |
| 512 | |
| 513 | InternalRequest internal_req; |
| 514 | internal_req.m_Result = RESULT_OK; |
| 515 | internal_req.m_Socket = connection->m_Socket; |
| 516 | internal_req.m_Server = server; |
| 517 | |
| 518 | dmHttpServerPrivate::ParseResult parse_result; |
| 519 | bool first_recv = true; |
| 520 | do |
| 521 | { |
| 522 | // -1 in order to be able to null-terminate the string |
| 523 | int max_to_recv = (BUFFER_SIZE-1) - total_recv; |
| 524 | if (max_to_recv == 0) |
| 525 | break; |
| 526 | |
| 527 | int recv_bytes; |
| 528 | dmSocket::Result r = dmSocket::Receive(connection->m_Socket, server->m_Buffer + total_recv, max_to_recv, &recv_bytes); |
| 529 | if (r == dmSocket::RESULT_OK) |
| 530 | { |
| 531 | if (recv_bytes == 0) |
| 532 | { |
| 533 | // Interpret zero bytes, in first read, as connection closed by client. |
| 534 | if(!first_recv) |
| 535 | dmLogWarning("Client socket in http server was unexpectedly closed"); |
| 536 | |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | total_recv += recv_bytes; |
| 541 | internal_req.m_TotalReceived = (uint32_t) total_recv; |
| 542 | // null-terminate the string. |
| 543 | server->m_Buffer[dmMath::Min(total_recv, (int) BUFFER_SIZE-1)] = '\0'; |
| 544 | parse_result = dmHttpServerPrivate::ParseHeader(server->m_Buffer, &internal_req, &HandleRequest, &HandleHeader, &HandleReponse); |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | first_recv = false; |
| 552 | } |
| 553 | while (parse_result == dmHttpServerPrivate::PARSE_RESULT_NEED_MORE_DATA); |
| 554 | |
| 555 | switch (parse_result) |
| 556 | { |
| 557 | case dmHttpServerPrivate::PARSE_RESULT_NEED_MORE_DATA: |
| 558 | dmLogError("Buffer size in http-server too small"); |
| 559 | return false; |
| 560 | |
| 561 | case dmHttpServerPrivate::PARSE_RESULT_OK: |
| 562 | // Ok |
| 563 | break; |
| 564 | |
| 565 | case dmHttpServerPrivate::PARSE_RESULT_SYNTAX_ERROR: |
| 566 | dmLogWarning("Invalid http request"); |
no test coverage detected