Fix test mock server

This commit is contained in:
Federico Stazi 2020-09-10 22:34:41 +00:00
parent 9a4293a3cf
commit bbebeee1a6

View File

@ -14,6 +14,7 @@
#include "test_utils.h" #include "test_utils.h"
#include <fcntl.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -181,9 +182,17 @@ void CurlTestUtils::StartMockServer() {
accept(listening_socket, (sockaddr*)&remote_address, accept(listening_socket, (sockaddr*)&remote_address,
&remote_address_size); &remote_address_size);
if (accepted_socket == -1) { if (accepted_socket == -1) {
if (errno == ECONNABORTED)
continue;
else {
close(listening_socket); close(listening_socket);
return; return;
} }
}
// Set the socket's nonblocking flag to true
int flag = fcntl(accepted_socket, F_GETFL, nullptr);
fcntl(accepted_socket, F_SETFL, flag | O_NONBLOCK);
// Add the new socket to the fd_set and update max_fd // Add the new socket to the fd_set and update max_fd
FD_SET(accepted_socket, &master_fd_set); FD_SET(accepted_socket, &master_fd_set);
@ -194,18 +203,38 @@ void CurlTestUtils::StartMockServer() {
constexpr int kMaxRequestSize = 4096; constexpr int kMaxRequestSize = 4096;
char buf[kMaxRequestSize] = {}; char buf[kMaxRequestSize] = {};
// Receive message from socket ssize_t num_bytes = 0;
int num_bytes = recv(i, buf, sizeof(buf), 0); size_t read_bytes = 0;
if (num_bytes == -1) { bool close_socket = false;
close(listening_socket);
return;
} else if (num_bytes == 0) { // Read from the nonblocking socket until recv returns -1 and
// errno is EAGAIN or EWOULDBLOCK (busy wait)
do {
num_bytes = recv(i, buf + read_bytes, sizeof(buf) - read_bytes, 0);
// An error happened, close the socket
if (num_bytes == -1 and errno != EAGAIN and errno != EWOULDBLOCK) {
close_socket = true;
break;
}
// The connection was closed, close the socket
if (num_bytes == 0) {
close_socket = true;
break;
}
read_bytes += num_bytes;
} while (num_bytes > 0);
if (close_socket) {
// Close the connection and remove it from fd_set // Close the connection and remove it from fd_set
close(i); close(i);
FD_CLR(i, &master_fd_set); FD_CLR(i, &master_fd_set);
continue;
}
} else {
// Prepare a response for the request // Prepare a response for the request
std::string http_response = std::string http_response =
"HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: ";
@ -221,16 +250,18 @@ void CurlTestUtils::StartMockServer() {
std::string(post_fields); std::string(post_fields);
} else { } else {
close(listening_socket); // Close the connection and remove it from fd_set
return; close(i);
FD_CLR(i, &master_fd_set);
continue;
} }
// Write the response to the request // Ignore any errors, the connection will be closed anyway
if (write(i, http_response.c_str(), http_response.size()) == -1) { write(i, http_response.c_str(), http_response.size());
close(listening_socket);
return; // Close the connection and remove it from fd_set
} close(i);
} FD_CLR(i, &master_fd_set);
} }
} }
} }