///////////////////////////////////////////////////////
| 383 | |
| 384 | //////////////////////////////////////////////////////////// |
| 385 | Ftp::Response Ftp::getResponse() |
| 386 | { |
| 387 | // We'll use a variable to keep track of the last valid code. |
| 388 | // It is useful in case of multi-lines responses, because the end of such a response |
| 389 | // will start by the same code |
| 390 | unsigned int lastCode = 0; |
| 391 | bool isInsideMultiline = false; |
| 392 | std::string message; |
| 393 | |
| 394 | for (;;) |
| 395 | { |
| 396 | // Receive the response from the server |
| 397 | std::array<char, 1024> buffer{}; |
| 398 | std::size_t length = 0; |
| 399 | |
| 400 | if (m_receiveBuffer.empty()) |
| 401 | { |
| 402 | if (m_commandSocket.receive(buffer.data(), buffer.size(), length) != Socket::Status::Done) |
| 403 | return Response(Response::Status::ConnectionClosed); |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | std::copy(m_receiveBuffer.begin(), m_receiveBuffer.end(), buffer.data()); |
| 408 | length = m_receiveBuffer.size(); |
| 409 | m_receiveBuffer.clear(); |
| 410 | } |
| 411 | |
| 412 | // There can be several lines inside the received buffer, extract them all |
| 413 | std::istringstream in(std::string(buffer.data(), length), std::ios_base::binary); |
| 414 | while (in) |
| 415 | { |
| 416 | // Try to extract the code |
| 417 | unsigned int code = 0; |
| 418 | if (in >> code) |
| 419 | { |
| 420 | // Extract the separator |
| 421 | char separator = 0; |
| 422 | in.get(separator); |
| 423 | |
| 424 | // The '-' character means a multiline response |
| 425 | if ((separator == '-') && !isInsideMultiline) |
| 426 | { |
| 427 | // Set the multiline flag |
| 428 | isInsideMultiline = true; |
| 429 | |
| 430 | // Keep track of the code |
| 431 | if (lastCode == 0) |
| 432 | lastCode = code; |
| 433 | |
| 434 | // Extract the line |
| 435 | std::getline(in, message); |
| 436 | |
| 437 | // Remove the ending '\r' (all lines are terminated by "\r\n") |
| 438 | message.erase(message.length() - 1); |
| 439 | message = separator + message + "\n"; |
| 440 | } |
| 441 | else |
| 442 | { |