Skip to content

Commit

Permalink
Change the message task to the new error interface
Browse files Browse the repository at this point in the history
  • Loading branch information
durner committed Mar 6, 2024
1 parent 3bd36f1 commit 412f63b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/network/http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ struct HttpResponse {
default: return "UNKNOWN";
}
}
/// Check for successful operation 2xx operations
static constexpr auto checkSuccess(const Code& code) {
return (code == Code::OK_200 || code == Code::CREATED_201 || code == Code::NO_CONTENT_204 || code == Code::PARTIAL_CONTENT_206);
}
/// Check if the result has no content
static constexpr auto withoutContent(const Code& code) {
return code == Code::NO_CONTENT_204;
}
/// Deserialize the response
[[nodiscard]] static HttpResponse deserialize(std::string_view data);
};
Expand Down
7 changes: 6 additions & 1 deletion src/network/http_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ HttpHelper::Info HttpHelper::detect(string_view header)
}
}

if (info.encoding == Encoding::Unknown)
if (info.encoding == Encoding::Unknown && !HttpResponse::withoutContent(info.response.code))
throw runtime_error("Unsupported HTTP encoding protocol");

if (HttpResponse::withoutContent(info.response.code))
info.headerLength = static_cast<unsigned>(header.length());

return info;
}
//---------------------------------------------------------------------------
Expand All @@ -69,6 +72,8 @@ bool HttpHelper::finished(const uint8_t* data, uint64_t length, unique_ptr<Info>
string_view sv(reinterpret_cast<const char*>(data), length);
info = make_unique<Info>(detect(sv));
}
if (info && HttpResponse::withoutContent(info->response.code))
return true;
switch (info->encoding) {
case Encoding::ContentLength:
return length >= info->headerLength + info->length;
Expand Down
10 changes: 8 additions & 2 deletions src/network/http_message.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "network/http_message.hpp"
#include "network/http_response.hpp"
#include "network/message_result.hpp"
#include "utils/data_vector.hpp"
#include "utils/timer.hpp"
Expand Down Expand Up @@ -98,8 +99,13 @@ MessageState HTTPMessage::execute(IOUringSocket& socket)
socket.disconnect(request->fd, originalMessage->hostname, originalMessage->port, &tcpSettings, static_cast<uint64_t>(sendBufferOffset + receiveBufferOffset));
originalMessage->result.size = info->length;
originalMessage->result.offset = info->headerLength;
state = MessageState::Finished;
return MessageState::Finished;
if (HttpResponse::checkSuccess(info->response.code)) {
state = MessageState::Finished;
} else {
originalMessage->result.failureCode |= static_cast<uint16_t>(MessageFailureCode::HTTP);
state = MessageState::Aborted;
}
return state;
}
} catch (exception&) {
originalMessage->result.failureCode |= static_cast<uint16_t>(MessageFailureCode::HTTP);
Expand Down
9 changes: 8 additions & 1 deletion src/network/https_message.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "network/https_message.hpp"
#include "network/http_response.hpp"
#include "network/message_result.hpp"
#include "utils/data_vector.hpp"
#include "utils/timer.hpp"
#include <cassert>
#include <memory>
#include <utility>
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <utility>
//---------------------------------------------------------------------------
// AnyBlob - Universal Cloud Object Storage Library
// Dominik Durner, 2023
Expand Down Expand Up @@ -104,6 +105,12 @@ MessageState HTTPSMessage::execute(IOUringSocket& socket)
if (HttpHelper::finished(receive.data(), static_cast<uint64_t>(receiveBufferOffset), info)) {
originalMessage->result.size = info->length;
originalMessage->result.offset = info->headerLength;
if (HttpResponse::checkSuccess(info->response.code)) {
state = MessageState::TLSShutdown;
} else {
originalMessage->result.failureCode |= static_cast<uint16_t>(MessageFailureCode::HTTP);
reset(socket, true);
}
state = MessageState::TLSShutdown;
return execute(socket);
} else {
Expand Down

0 comments on commit 412f63b

Please sign in to comment.