April 21th 2024 update

This commit is contained in:
Ned Wright
2024-04-21 12:38:24 +00:00
parent 189c9209c9
commit 66ed4a8d81
73 changed files with 994 additions and 1166 deletions

View File

@@ -513,7 +513,6 @@ private:
{
if (!isBioSocketReady()) return 0;
dbgTrace(D_MESSAGING) << "Sending request: " << printOut(request);
size_t offset = request.length() - data_left_to_send;
auto curr_data_to_send = request.c_str() + offset;
int data_sent_len = BIO_write(bio.get(), curr_data_to_send, data_left_to_send);
@@ -544,7 +543,6 @@ private:
int receive_len = BIO_read(bio.get(), buffer, sizeof(buffer) - 1);
if (receive_len > 0) {
dbgTrace(D_CONNECTION) << "Received " << receive_len << " bytes";
return string(buffer, receive_len);
}

View File

@@ -54,7 +54,7 @@ public:
bool force_buffering = true
);
Maybe<HTTPStatusCode, HTTPResponse> downloadFile(
Maybe<void, HTTPResponse> downloadFile(
HTTPMethod method,
const std::string &uri,
const std::string &download_file_path,
@@ -62,7 +62,7 @@ public:
const MessageMetadata &message_metadata = MessageMetadata()
);
Maybe<HTTPStatusCode, HTTPResponse> uploadFile(
Maybe<void, HTTPResponse> uploadFile(
const std::string &uri,
const std::string &upload_file_path,
MessageCategory category,

View File

@@ -62,7 +62,7 @@ public:
return messaging_comp.sendAsyncMessage(method, uri, body, category, message_metadata, force_buffering);
}
Maybe<HTTPStatusCode, HTTPResponse>
Maybe<void, HTTPResponse>
downloadFile(
const HTTPMethod method,
const std::string &uri,
@@ -74,7 +74,7 @@ public:
return messaging_comp.downloadFile(method, uri, download_file_path, category, message_metadata);
}
Maybe<HTTPStatusCode, HTTPResponse>
Maybe<void, HTTPResponse>
uploadFile(
const std::string &uri,
const std::string &upload_file_path,

View File

@@ -61,7 +61,9 @@ HTTPRequest::setConnectionHeaders(const Connection &conn)
}
}
insertHeader("Host", host);
if (headers.find("Host") == headers.end()) {
insertHeader("Host", host);
}
insertHeader("Content-Length", to_string(body.size()));
insertHeader("Content-type", "application/json");
insertHeader("Accept-Encoding", "identity");
@@ -82,10 +84,12 @@ HTTPRequest::prepareRequest(
{
HTTPRequest req(method, uri, headers, body);
if (!req.setConnectionHeaders(conn)) return genError("Failed to identify the HTTP method");
string agent_registration_query = R"("authenticationMethod": "token")";
bool dont_add_access_token = false;
if (headers.find("Host") != headers.end()) {
dont_add_access_token = true;
dbgTrace(D_MESSAGING) << "Request is not for FOG";
}
string agent_registration_query = R"("authenticationMethod": "token")";
if (method == HTTPMethod::CONNECT || body.find(agent_registration_query) != string::npos) {
dont_add_access_token = true;
dbgTrace(D_MESSAGING) << "Request is for agent authentication";
@@ -93,6 +97,8 @@ HTTPRequest::prepareRequest(
auto res = req.addAccessToken(conn, dont_add_access_token);
if (!res.ok()) return res.passErr();
if (!req.setConnectionHeaders(conn)) return genError("Failed to identify the HTTP method");
if (conn.isOverProxy()) {
auto res = req.addProxyAuthorization(conn);
if (!res.ok()) return res.passErr();

View File

@@ -145,7 +145,7 @@ MessagingComp::sendAsyncMessage(
i_messaging_buffer->pushNewBufferedMessage(body, method, uri, category, new_message_metadata, false);
}
Maybe<HTTPStatusCode, HTTPResponse>
Maybe<void, HTTPResponse>
MessagingComp::downloadFile(
HTTPMethod method,
const string &uri,
@@ -166,7 +166,9 @@ MessagingComp::downloadFile(
auto response = sendSyncMessage(method, uri, "", category, message_metadata);
if (!response.ok()) return response.passErr();
if (response.unpack().getHTTPStatusCode() != HTTPStatusCode::HTTP_OK) {
return genError(HTTPResponse(response.unpack().getHTTPStatusCode(), response.unpack().getBody()));
}
ofstream file_stream(download_file_path);
if (!file_stream.is_open()) {
string open_err = "Failed to open the destination file. Path: " + download_file_path;
@@ -177,10 +179,10 @@ MessagingComp::downloadFile(
file_stream.close();
dbgTrace(D_MESSAGING) << "Successfully downloaded and save file to: " << download_file_path;
return HTTPStatusCode::HTTP_OK;
return Maybe<void, HTTPResponse>();
}
Maybe<HTTPStatusCode, HTTPResponse>
Maybe<void, HTTPResponse>
MessagingComp::uploadFile(
const string &uri,
const string &upload_file_path,
@@ -205,9 +207,12 @@ MessagingComp::uploadFile(
sendSyncMessage(HTTPMethod::PUT, uri, buffer.str(), category, message_metadata);
if (!response.ok()) return response.passErr();
if (response.unpack().getHTTPStatusCode() != HTTPStatusCode::HTTP_OK) {
return genError(HTTPResponse(response.unpack().getHTTPStatusCode(), response.unpack().getBody()));
}
dbgTrace(D_MESSAGING) << "Successfully upload file from: " << upload_file_path;
return HTTPStatusCode::HTTP_OK;
return Maybe<void, HTTPResponse>();
}
bool

View File

@@ -183,7 +183,6 @@ TEST_F(TestMessagingComp, testUploadFile)
EXPECT_CALL(mock_messaging_connection, mockSendRequest(_, _, _)).WillOnce(Return(res));
auto upload_res = messaging_comp.uploadFile(uri, path, category, conn_metadata);
ASSERT_TRUE(upload_res.ok());
EXPECT_EQ(upload_res.unpack(), HTTPStatusCode::HTTP_OK);
}
TEST_F(TestMessagingComp, testDownloadFile)
@@ -207,7 +206,6 @@ TEST_F(TestMessagingComp, testDownloadFile)
EXPECT_CALL(mock_messaging_connection, mockSendRequest(_, _, _)).WillOnce(Return(res));
auto upload_res = messaging_comp.downloadFile(method, uri, "/tmp/test.txt", category, conn_metadata);
ASSERT_TRUE(upload_res.ok());
EXPECT_EQ(upload_res.unpack(), HTTPStatusCode::HTTP_OK);
}
bool